View source
<?php
define('REGISTRATION_WAITLIST_STATE', 'waitlist');
function registration_waitlist_registration_entity_settings($settings) {
return array(
'registration_waitlist_enable' => array(
'#type' => 'checkbox',
'#title' => t('Enable wait list'),
'#description' => t('When wait list is enabled, registrations made after capacity has been reached will be set to the wait list state.'),
'#default_value' => isset($settings['settings']['registration_waitlist_enable']) ? $settings['settings']['registration_waitlist_enable'] : 0,
),
'registration_waitlist_capacity' => array(
'#type' => 'textfield',
'#title' => t('Wait list capacity'),
'#size' => 5,
'#maxsize' => 10,
'#required' => TRUE,
'#description' => t('The maximum number of registrants on the wait list. Leave at 0 for no limit.'),
'#default_value' => isset($settings['settings']['registration_waitlist_capacity']) ? $settings['settings']['registration_waitlist_capacity'] : 0,
'#states' => array(
'visible' => array(
':input[name="settings[registration_waitlist_enable]"]' => array(
'checked' => TRUE,
),
),
),
),
'registration_waitlist_message_enable' => array(
'#type' => 'checkbox',
'#title' => t('Enable wait list message'),
'#description' => t('Enable to allow display of a message above the registration form when capacity has been reached.'),
'#default_value' => isset($settings['settings']['registration_waitlist_message_enable']) ? $settings['settings']['registration_waitlist_message_enable'] : 1,
'#states' => array(
'visible' => array(
':input[name="settings[registration_waitlist_enable]"]' => array(
'checked' => TRUE,
),
),
),
),
'registration_waitlist_message' => array(
'#type' => 'textarea',
'#title' => t('Wait list message'),
'#description' => t('Enter a message to display above the registration form to notify users that they will be placed on a waitlist. Message only appears if capacity is reached.'),
'#default_value' => isset($settings['settings']['registration_waitlist_message']) ? $settings['settings']['registration_waitlist_message'] : 'Please note: completing this registration form will place you on a waitlist as there are currently no places left.',
'#states' => array(
'visible' => array(
':input[name="settings[registration_waitlist_message_enable]"]' => array(
'checked' => TRUE,
),
':input[name="settings[registration_waitlist_enable]"]' => array(
'checked' => TRUE,
),
),
),
),
);
}
function registration_waitlist_default_registration_state() {
$defaults = array();
$defaults[REGISTRATION_WAITLIST_STATE] = \Drupal::entityManager()
->getStorage('registration_state')
->create(array(
'name' => REGISTRATION_WAITLIST_STATE,
'label' => t('Wait list'),
'description' => t('Special state for registrations after capacity is reached.'),
'default_state' => FALSE,
'active' => FALSE,
'show_on_form' => TRUE,
'weight' => 0,
));
return $defaults;
}
function registration_waitlist_registration_status_alter(&$status, &$context) {
$entity_type = $context['entity_type'];
$entity_id = $context['entity_id'];
$settings = registration_entity_settings($entity_type, $entity_id);
$capacity = $settings['capacity'];
if ($capacity) {
$capacity_error = FALSE;
if (count($context['errors']) == 1) {
$error = reset($context['errors']);
$capacity_error = $error == t(REGISTRATION_INSUFFICIENT_SPACES_MESSAGE) ? TRUE : FALSE;
}
if ($capacity_error && registration_waitlist_is_active($entity_type, $entity_id, $context['errors'])) {
$status = TRUE;
}
}
}
function registration_waitlist_entity_presave($entity, $type) {
if ($type == 'registration') {
$errors = array();
$status = registration_status($entity->entity_type, $entity->entity_id, TRUE, $entity->count, $entity->registration_id, $errors);
if (registration_waitlist_is_active($entity->entity_type, $entity->entity_id, $errors)) {
$active_states = registration_get_active_states();
if (in_array($entity->state, $active_states)) {
if (isset($entity->original) && !in_array($entity->original->state, $active_states) || !isset($entity->original)) {
drupal_set_message(t('Registration placed on the wait list.'), 'status');
$waitlist_state = registration_get_states(REGISTRATION_WAITLIST_STATE);
$entity->state = $waitlist_state
->identifier();
}
}
}
}
}
function registration_waitlist_is_active($entity_type, $entity_id, &$errors = array()) {
$settings = registration_entity_settings($entity_type, $entity_id);
$active = FALSE;
$capacity = $settings['capacity'];
if ($capacity && isset($settings['settings']['registration_waitlist_enable']) && $settings['settings']['registration_waitlist_enable']) {
if (in_array(t(REGISTRATION_INSUFFICIENT_SPACES_MESSAGE), $errors)) {
$active = TRUE;
}
$waitlist_capacity = isset($settings['settings']['registration_waitlist_capacity']) ? $settings['settings']['registration_waitlist_capacity'] : 0;
if ($active && $waitlist_capacity) {
$waitlist_state = registration_get_states(REGISTRATION_WAITLIST_STATE);
$query = db_select('registration', 'r');
$query
->addExpression('sum(count)', 'count');
$query
->condition('entity_id', $entity_id);
$query
->condition('entity_type', $entity_type);
$query
->condition('state', $waitlist_state
->identifier());
$result = $query
->execute();
$waitlist_count = $result
->fetchField();
$waitlist_count = $waitlist_count == '' ? 0 : $waitlist_count;
if ($waitlist_capacity - $waitlist_count < 1) {
$active = FALSE;
$errors[] = t('Waitlist is full.');
}
}
}
return $active;
}