Skip to content

Commit

Permalink
Issue #2055145 by ratface, caxy4: adding ability to set default regis…
Browse files Browse the repository at this point in the history
…tration state per registration type. this setting overrides the global default
  • Loading branch information
Alex Ellison committed Mar 25, 2015
1 parent 791c97d commit 3f76f2e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 18 deletions.
8 changes: 7 additions & 1 deletion includes/registration.forms.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function registration_form($form, &$form_state, Registration $registration) {
$user = $wrapper->user->value();
$host = $wrapper->entity->value();
$state = $wrapper->state->value();
$type = $wrapper->type->value();

$form_state['registration'] = $registration;

Expand Down Expand Up @@ -115,9 +116,14 @@ function registration_form($form, &$form_state, Registration $registration) {
'#element_validate' => array('element_validate_integer_positive', '_registration_validate_space_request'),
);

$default_state = registration_get_default_state();
$default_state = registration_get_default_state($type);
$states = registration_get_states_options(array('show_on_form' => TRUE));

// Ensure default state is in options or it won't be set.
if (!in_array($default_state->label, $states)) {
$states[$default_state->name] = $default_state->label;
}

$form['state'] = array(
'#type' => 'select',
'#title' => t('State'),
Expand Down
21 changes: 16 additions & 5 deletions includes/registration_type.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ function registration_type_form($form, &$form_state, $registration_type, $op = '
),
'#description' => t('A unique machine-readable name for this registration type. It must only contain lowercase letters, numbers, and underscores.'),
);
// Setting for default state for regsitrations of this type.
// Overrides global default state setting.
$state_options = registration_get_states_options();
$form['default_state'] = array(
'#title' => 'Default state',
'#type' => 'select',
'#description' => t('The default state for this registration type. Overrides the global default state set at /admin/structure/registration/registration_states.'),
'#default_value' => isset($registration_type->default_state) ? $registration_type->default_state : 0,
'#options' => array('none' => 'None') + $state_options,
);
// Settings for registration type.
$form['data'] = array(
'#type' => 'fieldset',
Expand All @@ -53,14 +63,10 @@ function registration_type_form($form, &$form_state, $registration_type, $op = '
'#default_value' => isset($registration_type->held_expire) ? $registration_type->held_expire : 1,
);
// Setting for which state a registration will be put in if its hold expires.
$states = registration_get_states();
foreach ($states as $key => $value) {
$states[$key] = $value->label;
}
$form['data']['held_expire_state'] = array(
'#type' => 'select',
'#title' => t('Hold expiration state'),
'#options' => $states,
'#options' => $state_options,
'#required' => FALSE,
'#description' => t('The state a registration will be put into when its hold expires (default is "canceled").'),
'#default_value' => isset($registration_type->held_expire_state) ? $registration_type->held_expire_state : 'canceled',
Expand Down Expand Up @@ -90,6 +96,11 @@ function registration_type_form($form, &$form_state, $registration_type, $op = '
* Form API submit callback for the type form.
*/
function registration_type_form_submit(&$form, &$form_state) {
// Set default state to NULL if 'none' is selected for database insert.
if ($form_state['values']['default_state'] == 'none') {
$form_state['values']['default_state'] = NULL;
}

$registration_type = entity_ui_form_submit_build_entity($form, $form_state);
$registration_type->save();
$form_state['redirect'] = 'admin/structure/registration/registration_types';
Expand Down
2 changes: 1 addition & 1 deletion lib/registration.entity.inc
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class Registration extends Entity {
$wrapper = entity_metadata_wrapper('registration', $this);
$state = $wrapper->state->value();
if (!$state) {
$default_state = registration_get_default_state();
$default_state = registration_get_default_state($wrapper->type->value());
if ($default_state) {
$this->state = $default_state->identifier();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/registration_type.entity.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ class RegistrationType extends Entity {

public $name;
public $label;
public $data;
public $locked;
public $default_state;
public $data;

public function __construct($values = array()) {
parent::__construct($values, 'registration_type');
Expand Down
41 changes: 33 additions & 8 deletions registration.install
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ function registration_schema() {
'default' => 0,
'size' => 'tiny',
),
'default_state' => array(
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
'description' => 'The machine name of the default registration state.',
),
'data' => array(
'type' => 'text',
'not null' => FALSE,
Expand Down Expand Up @@ -567,13 +573,32 @@ function registration_update_7104(&$sandbox) {
* Adds column to registration_state table for held property.
*/
function registration_update_7105(&$sandbox) {
$spec = array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'A flag showing held registration states.',
);
if (!db_field_exists('registration_state', 'held')) {
$spec = array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'size' => 'tiny',
'description' => 'A flag showing held registration states.',
);

db_add_field('registration_state', 'held', $spec);
}
}

db_add_field( 'registration_state', 'held', $spec);
/**
* Add the new default_state field to the registration_type table
*/

function registration_update_7106(&$sandbox) {
if (!db_field_exists('registration_type', 'default_state')) {
db_add_field('registration_type', 'default_state',
array(
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
'description' => 'The machine name of the default registration state.',
)
);
}
}
12 changes: 10 additions & 2 deletions registration.module
Original file line number Diff line number Diff line change
Expand Up @@ -1555,8 +1555,16 @@ function registration_get_held_states() {
*
* @return array
*/
function registration_get_default_state() {
$states = registration_states(array('default_state' => 1));
function registration_get_default_state($type = NULL) {
// If a type of registration is specified, look for its default registration
// state before using the global default.
$states_query = array('default_state' => 1);
if (isset($type)) {
$reg_type = registration_get_types($type);
$states_query = isset($reg_type->default_state) ? array('name' => $reg_type->default_state) : $states_query;
}
$states = registration_states($states_query);

return !empty($states) ? reset($states) : NULL;
}

Expand Down

0 comments on commit 3f76f2e

Please sign in to comment.