View source
<?php
module_load_include('inc', 'registration', 'includes/registration.field');
module_load_include('inc', 'registration', 'includes/registration.forms');
define('REGISTRATION_STATE_ONE', 1);
define('REGISTRATION_INSUFFICIENT_SPACES_MESSAGE', 'Insufficient spaces remaining.');
define('REGISTRATION_REGISTRANT_TYPE_ME', 'registration_registrant_type_me');
define('REGISTRATION_REGISTRANT_TYPE_USER', 'registration_registrant_type_user');
define('REGISTRATION_REGISTRANT_TYPE_ANON', 'registration_registrant_type_anon');
function registration_entity_info() {
$entities = array(
'registration' => array(
'label' => t('Registration'),
'plural label' => t('Registrations'),
'controller class' => 'EntityAPIController',
'entity class' => 'Registration',
'metadata controller class' => 'RegistrationMetadataController',
'base table' => 'registration',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'registration_id',
'bundle' => 'type',
),
'access callback' => 'registration_access',
'bundle keys' => array(
'bundle' => 'name',
),
'bundles' => array(),
'view modes' => array(
'full' => array(
'label' => t('Full Registration'),
'custom settings' => FALSE,
),
),
'uri callback' => 'entity_class_uri',
'token type' => 'registration',
'module' => 'registration',
'label callback' => 'entity_class_label',
'entity cache' => module_exists('entitycache'),
),
'registration_type' => array(
'label' => t('Registration type'),
'entity class' => 'RegistrationType',
'controller class' => 'RegistrationTypeController',
'base table' => 'registration_type',
'fieldable' => FALSE,
'bundle of' => 'registration',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'id',
'name' => 'name',
'label' => 'label',
),
'access callback' => 'registration_type_access',
'module' => 'registration',
'admin ui' => array(
'path' => 'admin/structure/registration/registration_types',
'file' => 'registration_type.admin.inc',
'file path' => drupal_get_path('module', 'registration') . '/includes',
'controller class' => 'RegistrationTypeUIController',
),
'entity cache' => module_exists('entitycache'),
),
'registration_state' => array(
'label' => t('Registration State'),
'plural label' => t('Registration states'),
'controller class' => 'RegistrationStateController',
'entity class' => 'RegistrationState',
'base table' => 'registration_state',
'fieldable' => FALSE,
'entity keys' => array(
'id' => 'registration_state_id',
'label' => 'label',
'name' => 'name',
),
'bundles' => array(
'registration_state' => array(
'label' => 'Registration States',
),
),
'admin ui' => array(
'path' => 'admin/structure/registration/registration_states',
'file' => 'registration.forms.inc',
'file path' => drupal_get_path('module', 'registration') . '/includes',
'controller class' => 'RegistrationStatesUIController',
),
'token type' => 'registration_state',
'module' => 'registration',
'access callback' => 'registration_state_access',
'exportable' => TRUE,
'entity cache' => module_exists('entitycache'),
),
);
return $entities;
}
function registration_entity_info_alter(&$entity_info) {
if (drupal_get_schema('registration')) {
if (class_exists('RegistrationTypeController')) {
foreach (registration_get_types() as $type => $info) {
$entity_info['registration']['bundles'][$type] = array(
'label' => $info->label,
'admin' => array(
'path' => 'admin/structure/registration/registration_types/manage/%registration_type',
'real path' => 'admin/structure/registration/registration_types/manage/' . $type,
'bundle argument' => 5,
'access arguments' => array(
'administer registration types',
),
),
);
}
}
}
}
function registration_menu() {
$items['admin/structure/registration'] = array(
'title' => 'Registration',
'description' => 'Administer Registration items, such as types, states, etc.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array(
'administer registration',
),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['registration/%registration'] = array(
'title callback' => 'registration_page_title',
'title arguments' => array(
1,
),
'page callback' => 'registration_view',
'page arguments' => array(
1,
),
'access callback' => 'registration_own_access',
'access arguments' => array(
'view',
1,
),
);
$items['registration/%registration/view'] = array(
'title' => 'View',
'page callback' => 'registration_view',
'page arguments' => array(
1,
),
'access callback' => 'registration_own_access',
'access arguments' => array(
'view',
1,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['registration/%registration/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'registration_form',
1,
),
'access callback' => 'registration_own_access',
'access arguments' => array(
'update',
1,
),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
$items['registration/%registration/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'registration_delete_confirm',
1,
),
'access callback' => 'registration_own_access',
'access arguments' => array(
'delete',
1,
),
'type' => MENU_CALLBACK,
);
$items['registration/%registration/%/%'] = array(
'title' => 'Registration Access Check',
'page callback' => 'registration_access_check_redirect',
'page arguments' => array(
1,
2,
),
'access callback' => 'registration_own_access',
'access arguments' => array(
2,
1,
3,
),
);
foreach (registration_get_registration_instances() as $instance) {
$type = $instance['entity_type'];
if (!in_array($type, array(
'registration',
'registration_type',
))) {
$hide_register_tab = isset($instance['settings']['hide_register_tab']) && $instance['settings']['hide_register_tab'];
$items[$type . '/%entity_object/register'] = array(
'load arguments' => array(
$type,
),
'title' => 'Register',
'page callback' => 'registration_register_page',
'page arguments' => array(
0,
1,
),
'access callback' => 'registration_register_page_access',
'access arguments' => array(
0,
1,
),
'type' => $hide_register_tab ? MENU_CALLBACK : MENU_LOCAL_TASK,
);
$items[$type . '/%entity_object/registrations'] = array(
'load arguments' => array(
$type,
),
'title' => 'Manage Registrations',
'page callback' => 'registration_registrations_page',
'page arguments' => array(
0,
1,
),
'access callback' => 'registration_administer_registrations_access',
'access arguments' => array(
0,
1,
),
'type' => MENU_LOCAL_TASK,
);
$items[$type . '/%entity_object/registrations/list'] = array(
'load arguments' => array(
$type,
),
'title' => 'Registrations',
'page callback' => 'registration_registrations_page',
'page arguments' => array(
0,
1,
),
'access callback' => 'registration_administer_registrations_access',
'access arguments' => array(
0,
1,
),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[$type . '/%entity_object/registrations/settings'] = array(
'load arguments' => array(
$type,
),
'title' => 'Settings',
'page callback' => 'registration_entity_settings_page',
'page arguments' => array(
0,
1,
),
'access callback' => 'registration_administer_registrations_access',
'access arguments' => array(
0,
1,
),
'weight' => 9,
'type' => MENU_LOCAL_TASK,
);
$items[$type . '/%entity_object/registrations/broadcast'] = array(
'load arguments' => array(
$type,
),
'title' => 'Email Registrants',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'registration_registrations_broadcast_form',
0,
1,
),
'access callback' => 'registration_administer_registrations_access',
'access arguments' => array(
0,
1,
),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
);
}
}
if (module_exists('devel')) {
$items['registration/%registration/devel'] = array(
'title' => 'Devel',
'page callback' => 'devel_load_object',
'page arguments' => array(
'node',
1,
),
'access arguments' => array(
'access devel information',
),
'type' => MENU_LOCAL_TASK,
'file path' => drupal_get_path('module', 'devel'),
'file' => 'devel.pages.inc',
'weight' => 100,
);
$items['registration/%registration/devel/load'] = array(
'title' => 'Load',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
}
return $items;
}
function registration_anonymous_access_hash($registration, $options = array(), $name = "", $type = "") {
return drupal_hmac_base64($registration->created . $registration
->identifier(), drupal_get_hash_salt() . $registration->anon_mail);
}
function registration_anonymous_link_get($registration, $options = array(), $name = "", $type = "") {
$path = entity_uri('registration', $registration);
$url = url($path['path'] . '/view/' . registration_anonymous_access_hash($registration), array(
'absolute' => TRUE,
));
return $url;
}
function registration_access_check_redirect($registration, $function) {
drupal_goto('registration/' . $registration
->identifier() . '/' . $function);
}
function registration_own_access($action, $registration, $hash = NULL) {
if (entity_access($action, 'registration', $registration)) {
if (!user_is_anonymous()) {
return TRUE;
}
$in_session = FALSE;
if (!$hash) {
if (isset($_SESSION['registration_ids'][$registration->registration_id])) {
$in_session = TRUE;
$hash = $_SESSION['registration_ids'][$registration->registration_id];
}
else {
global $_SESSION;
$params = drupal_get_query_parameters();
if (isset($params['registration_hash'])) {
$hash = $params['registration_hash'];
}
}
}
if ($hash === registration_anonymous_access_hash($registration)) {
if (!$in_session) {
$_SESSION['registration_ids'][$registration->registration_id] = $hash;
}
return TRUE;
}
}
return FALSE;
}
function registration_admin_paths() {
$paths = array();
if (variable_get('registration_paths_administrative', FALSE)) {
foreach (registration_get_registration_instances() as $instance) {
$type = $instance['entity_type'];
if (!in_array($type, array(
'registration',
'registration_type',
))) {
$paths[$type . '/*/registrations'] = TRUE;
$paths[$type . '/*/registrations/list'] = TRUE;
$paths[$type . '/*/registrations/settings'] = TRUE;
$paths[$type . '/*/registrations/broadcast'] = TRUE;
}
}
}
return $paths;
}
function registration_permission() {
$permissions = array(
'administer registration types' => array(
'title' => t('Administer registration types'),
'description' => t('Manage registration types, fields, and display settings.'),
'restrict access' => TRUE,
),
'administer registration states' => array(
'title' => t('Administer registration states'),
'description' => t('Manage registration states, fields, and display settings.'),
'restrict access' => TRUE,
),
'administer registration' => array(
'title' => t('Administer registration'),
'description' => t('View, edit, delete, and manage all registrations, regardless of type.'),
'restrict access' => TRUE,
),
);
foreach (registration_get_types() as $type_info) {
$permissions += registration_permission_list($type_info);
}
return $permissions;
}
function registration_permission_list($info) {
$type = $info->name;
$label = $info->label;
return array(
"administer {$type} registration" => array(
'title' => t('%type_name: Administer settings', array(
'%type_name' => $label,
)),
'description' => t('Allow changing registration settings for all entities of this type.'),
),
"administer own {$type} registration" => array(
'title' => t('%type_name: Administer own settings', array(
'%type_name' => $label,
)),
'description' => t('Allow changing registration settings for entities which a user has edit access.'),
),
"view {$type} registration" => array(
'title' => t('%type_name: View all registrations', array(
'%type_name' => $label,
)),
),
"view own {$type} registration" => array(
'title' => t('%type_name: View own registrations', array(
'%type_name' => $label,
)),
),
"create {$type} registration" => array(
'title' => t('%type_name: Create new registration', array(
'%type_name' => $label,
)),
),
"update own {$type} registration" => array(
'title' => t('%type_name: Edit own registrations', array(
'%type_name' => $label,
)),
),
"update any {$type} registration" => array(
'title' => t('%type_name: Edit any registrations', array(
'%type_name' => $label,
)),
),
"delete own {$type} registration" => array(
'title' => t('%type_name: Delete own registrations', array(
'%type_name' => $label,
)),
),
"delete any {$type} registration" => array(
'title' => t('%type_name: Delete any registrations', array(
'%type_name' => $label,
)),
),
"create {$type} registration self" => array(
'title' => t('%type_name: Register self', array(
'%type_name' => $label,
)),
),
"create {$type} registration other users" => array(
'title' => t('%type_name: Register other accounts', array(
'%type_name' => $label,
)),
'description' => t("Register other users by username. Note that giving this permission to Anonymous is not recommended, as it can expose a user's registration status to a site visitor who knows their username."),
),
"create {$type} registration other anonymous" => array(
'title' => t('%type_name: Register other people', array(
'%type_name' => $label,
)),
),
"edit {$type} registration state" => array(
'title' => t('%type_name: Edit registration state', array(
'%type_name' => $label,
)),
),
);
}
function registration_user_cancel($edit, $account, $method) {
if (isset($account->uid)) {
if ($method == 'user_cancel_reassign') {
db_update('registration')
->fields(array(
'author_uid' => 0,
))
->condition('author_uid', $account->uid)
->execute();
db_update('registration')
->fields(array(
'user_uid' => NULL,
))
->condition('user_uid', $account->uid)
->execute();
}
}
}
function registration_user_delete($account) {
if (isset($account->uid)) {
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'registration')
->propertyCondition('author_uid', $account->uid)
->execute();
if ($result) {
registration_delete_multiple(array_keys($result['registration']));
}
db_update('registration')
->fields(array(
'user_uid' => NULL,
))
->condition('user_uid', $account->uid)
->execute();
cache_clear_all('*', 'cache_entity_registration', TRUE);
}
}
function registration_view(Registration $registration, $view_mode = 'full') {
return $registration
->view($view_mode);
}
function registration_page_title(Registration $registration) {
return $registration
->label();
}
function registration_register_page_access($entity_type, $entity) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
if ($type = registration_get_entity_registration_type($entity_type, $entity)) {
$registration = entity_get_controller('registration')
->create(array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'type' => $type,
));
if (entity_access('create', 'registration', $registration)) {
$settings = registration_entity_settings($entity_type, $entity_id);
if ($settings['status']) {
return TRUE;
}
}
}
return FALSE;
}
function registration_administer_registrations_access($entity_type, $entity, $account = NULL) {
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
if ($registration_type) {
if (user_access("administer {$registration_type} registration", $account)) {
return TRUE;
}
elseif (user_access("update own {$registration_type} registration", $account) && entity_access('update', $entity_type, $entity, $account)) {
return TRUE;
}
}
return FALSE;
}
function registration_register_page($entity_type, $entity) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
if (registration_status($entity_type, $entity_id)) {
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
$registration = entity_get_controller('registration')
->create(array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'type' => $registration_type,
));
return drupal_get_form('registration_form', $registration);
}
else {
return t('Sorry, registrations are no longer available for %name', array(
'%name' => entity_label($entity_type, $entity),
));
}
}
function registration_registrations_page($entity_type, $entity) {
$header = array(
array(
'data' => t('id'),
'field' => 'registration_id',
'type' => 'property',
'specifier' => 'registration_id',
),
array(
'data' => t('Email'),
),
array(
'data' => t('User'),
'field' => 'user_uid',
'type' => 'property',
'specifier' => 'user_uid',
),
array(
'data' => t('Created By'),
'field' => 'author_uid',
'type' => 'property',
'specifier' => 'author_uid',
),
array(
'data' => t('Count'),
'field' => 'count',
'type' => 'property',
'specifier' => 'count',
),
array(
'data' => t('Created'),
'field' => 'created',
'sort' => 'desc',
'type' => 'property',
'specifier' => 'created',
),
array(
'data' => t('State'),
'field' => 'state',
'type' => 'property',
'specifier' => 'state',
),
array(
'data' => t('Actions'),
),
);
list($entity_id) = entity_extract_ids($entity_type, $entity);
$label = entity_label($entity_type, $entity);
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', 'registration')
->propertyCondition('entity_id', $entity_id)
->propertyCondition('entity_type', $entity_type)
->pager(20)
->tableSort($header)
->execute();
if (!empty($result['registration'])) {
$registrations = registration_load_multiple(array_keys($result['registration']));
$rows = array();
foreach ($registrations as $registration) {
$wrapper = entity_metadata_wrapper('registration', $registration);
$author = $wrapper->author
->value();
$user = $wrapper->user
->value();
$state = $wrapper->state
->value();
$author_col = '';
if ($registration->author_uid) {
$author_col = theme('username', array(
'account' => $author,
));
}
$user_col = '';
if ($registration->user_uid) {
$user_col = theme('username', array(
'account' => $user,
));
}
$actions = array();
if (entity_access('view', 'registration', $registration)) {
$actions[] = l(t('View'), 'registration/' . $registration->registration_id);
}
if (entity_access('update', 'registration', $registration)) {
$actions[] = l(t('Edit'), 'registration/' . $registration->registration_id . '/edit', array(
'query' => drupal_get_destination(),
));
}
if (entity_access('delete', 'registration', $registration)) {
$actions[] = l(t('Delete'), 'registration/' . $registration->registration_id . '/delete', array(
'query' => drupal_get_destination(),
));
}
$rows[] = array(
l($registration->registration_id, 'registration/' . $registration->registration_id),
l($wrapper->mail
->value(), 'mailto:' . $wrapper->mail
->value()),
$user_col,
$author_col,
$registration->count,
format_date($registration->created),
$state ? filter_xss_admin(entity_label('registration_state', $state)) : '',
implode(' | ', $actions),
);
}
$settings = registration_entity_settings($entity_type, $entity_id);
$table = array(
'header' => $header,
'rows' => $rows,
);
if ($settings['capacity'] != 0) {
$table['caption'] = t('List of registrations for %title. !count of !capacity spaces are filled.', array(
'%title' => $label,
'!count' => '<strong>' . registration_event_count($entity_type, $entity_id) . '</strong>',
'!capacity' => '<strong>' . $settings['capacity'] . '</strong>',
));
}
else {
$table['caption'] = t('List of registrations for %title. !count spaces are filled.', array(
'%title' => $label,
'!count' => '<strong>' . registration_event_count($entity_type, $entity_id) . '</strong>',
));
}
$out = theme('table', $table) . theme('pager');
}
else {
$out = t('There are no registrants for %name', array(
'%name' => $label,
));
}
return $out;
}
function registration_entity_settings_page($entity_type, $entity) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
$settings = registration_entity_settings($entity_type, $entity_id);
return drupal_get_form('registration_entity_settings_form', $settings, $entity_type, $entity_id);
}
function registration_has_room($entity_type, $entity_id, $spaces = 1, $registration_id = NULL, $reset = FALSE) {
$settings = registration_entity_settings($entity_type, $entity_id, $reset);
$capacity = $settings['capacity'];
if ($capacity) {
$count = registration_event_count($entity_type, $entity_id, $registration_id, $reset) + $spaces;
if ($capacity - $count < 0) {
return FALSE;
}
}
return TRUE;
}
function registration_event_count($entity_type, $entity_id, $registration_id = NULL, $reset = FALSE) {
$count =& drupal_static(__FUNCTION__ . '_' . $entity_type . '_' . $entity_id . '_' . $registration_id, FALSE);
if (!$count || $reset) {
$query = db_select('registration', 'r');
$query
->addExpression('sum(count)', 'count');
$query
->condition('entity_id', $entity_id);
$query
->condition('entity_type', $entity_type);
if ($registration_id != NULL) {
$query
->condition('registration_id', $registration_id, '<>');
}
$active_held_states = array_merge(registration_get_active_states(), registration_get_held_states());
if (!empty($active_held_states)) {
$query
->condition('state', $active_held_states, 'IN');
}
$result = $query
->execute();
$count = $result
->fetchField();
$count = $count == '' ? 0 : $count;
}
$settings = registration_entity_settings($entity_type, $entity_id);
$context = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'registration_id' => $registration_id,
'settings' => $settings,
);
drupal_alter('registration_event_count', $count, $context);
return $count;
}
function registration_entity_insert($entity, $entity_type) {
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
if ($registration_type !== FALSE) {
registration_entity_set_default_settings($entity_type, $entity);
}
}
function registration_entity_update($entity, $entity_type) {
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
if ($registration_type !== FALSE) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
$settings = registration_entity_settings($entity_type, $entity_id);
if (!$settings) {
registration_entity_set_default_settings($entity_type, $entity);
}
}
}
function registration_entity_set_default_settings($entity_type, $entity) {
list($entity_id, , $bundle) = entity_extract_ids($entity_type, $entity);
$registration_instances = registration_get_registration_instances(array(
'entity_type' => $entity_type,
'bundle' => $bundle,
));
foreach ($registration_instances as $instance) {
if (isset($instance['settings']['default_registration_settings'])) {
$settings = registration_convert_form_settings($instance['settings']['default_registration_settings']);
registration_update_entity_settings($entity_type, $entity_id, $settings);
}
}
}
function registration_entity_delete($entity, $entity_type) {
list($entity_id) = entity_extract_ids($entity_type, $entity);
db_delete('registration')
->condition('entity_id', $entity_id)
->condition('entity_type', $entity_type)
->execute();
db_delete('registration_entity')
->condition('entity_id', $entity_id)
->condition('entity_type', $entity_type)
->execute();
if ($entity_type == 'registration_type') {
$registration_fields = field_read_fields(array(
'type' => 'registration',
));
if (!empty($registration_fields)) {
foreach (array_keys($registration_fields) as $field_name) {
$query = new EntityFieldQuery();
$result = $query
->fieldCondition($field_name, 'registration_type', $entity->name)
->execute();
foreach ($result as $host_entity_type => $entities) {
$entities = entity_load($host_entity_type, array_keys($entities));
foreach ($entities as $entity_id => $host_entity) {
$host_entity->{$field_name}[LANGUAGE_NONE][0] = NULL;
entity_save($host_entity_type, $host_entity);
}
}
}
}
}
}
function registration_entity_settings($entity_type, $entity_id, $reset = FALSE) {
$result =& drupal_static(__FUNCTION__ . $entity_type . $entity_id);
if (!$result || $reset) {
$result = db_select('registration_entity', 're')
->fields('re')
->condition('entity_id', $entity_id, '=')
->condition('entity_type', $entity_type, '=')
->execute()
->fetchAssoc();
if ($result) {
$result['settings'] = unserialize($result['settings']);
}
}
return $result;
}
function registration_theme() {
return array(
'registration_link' => array(
'variables' => array(
'label' => NULL,
'path' => NULL,
),
),
'registration_state_overview_form' => array(
'file' => 'includes/registration.forms.inc',
'render element' => 'form',
),
'registration_property_field' => array(
'variables' => array(
'label_hidden' => FALSE,
'title_attributes' => NULL,
'label' => '',
'content_attributes' => NULL,
'items' => array(),
'item_attributes' => array(
0 => '',
),
'classes' => '',
'attributes' => '',
),
),
);
}
function theme_registration_property_field($variables) {
return theme_field($variables);
}
function theme_registration_link($variables) {
$output = '';
$registration_label = $variables['label'];
$registration_path = $variables['path'];
$output .= l($registration_label, $registration_path);
return $output;
}
function registration_mail($key, &$message, $params) {
$subject = $params['subject'];
$body = $params['message'];
$message['subject'] .= str_replace(array(
"\r",
"\n",
), '', $subject);
$message['body'][] = drupal_html_to_text($body);
}
function registration_send_broadcast($entity_type, $entity_id, $subject, $message) {
global $language;
$active_states = registration_get_active_states();
if (empty($active_states)) {
$error_message = t('There are no active registration states configured. For email to be sent to registrants, an active registration state must be specified at "admin/structure/registration/registration_states".');
drupal_set_message($error_message, 'error');
watchdog('registration', $error_message, WATCHDOG_ERROR);
return;
}
$settings = registration_entity_settings($entity_type, $entity_id);
$from = $settings['settings']['from_address'];
$query = new EntityFieldQuery();
$entities = $query
->entityCondition('entity_type', 'registration')
->propertyCondition('entity_id', $entity_id)
->propertyCondition('entity_type', $entity_type)
->propertyCondition('state', $active_states, 'IN')
->execute();
if (!empty($entities)) {
$recipients = array();
$message_template = $message;
$params = array(
'subject' => $subject,
'message' => $message,
);
$registrations = registration_load_multiple(array_keys($entities['registration']));
$hooks = array(
'registration_send_broadcast',
);
$hooks[] = 'registration_send_broadcast_' . $entity_type;
$hooks[] = 'registration_send_broadcast_' . $entity_type . '_' . $entity_id;
$context = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
);
drupal_alter($hooks, $registrations, $context);
$success_count = 0;
foreach ($registrations as $registration) {
$wrapper = entity_metadata_wrapper('registration', $registration);
$recipients[] = $wrapper->mail
->value();
$entity = entity_load_single($entity_type, $entity_id);
if (module_exists('token')) {
$message = token_replace($message_template, array(
$entity_type => $entity,
'registration' => $registration,
), array(
'clear' => TRUE,
));
}
$params['message'] = $message;
$result = drupal_mail('registration', 'broadcast', $wrapper->mail
->value(), $language, $params, $from);
if ($result['result'] !== FALSE) {
$success_count++;
}
else {
watchdog('registration', 'Failed to send registration broadcast email to %email.', array(
'%email' => $wrapper->mail
->value(),
), WATCHDOG_ERROR);
}
}
if ($success_count) {
drupal_set_message(t('Registration broadcast sent to @count registrants.', array(
'@count' => $success_count,
)));
watchdog('registration', 'Registration broadcast sent to @count registrants.', array(
'@count' => $success_count,
));
}
}
else {
drupal_set_message(t('There are no participants registered for this %type.', array(
'%type' => $entity_type,
)), 'warning');
}
}
function registration_update_entity_settings($entity_type, $entity_id, $settings) {
db_merge('registration_entity')
->key(array(
'entity_id' => $entity_id,
'entity_type' => $entity_type,
))
->fields($settings)
->execute();
drupal_set_message(t('Registration settings have been saved.'));
}
function registration_cron() {
$results = db_select('registration_entity', 're')
->fields('re')
->condition('send_reminder', 1)
->condition('reminder_date', date('Y-m-d G:i:s'), '<=')
->condition('reminder_date', date('Y-m-d G:i:s', strtotime('-2 days')), '>')
->range(0, 10)
->addTag('registration_cron_select')
->execute()
->fetchAllAssoc('entity_id');
foreach ($results as $result) {
$entity = entity_load_single($result->entity_type, $result->entity_id);
$message = $result->reminder_template;
$subject = t('Reminder for !label', array(
'!label' => entity_label($result->entity_type, $entity),
));
registration_send_broadcast($result->entity_type, $result->entity_id, $subject, $message);
db_update('registration_entity')
->fields(array(
'send_reminder' => 0,
))
->condition('entity_id', $result->entity_id)
->condition('entity_type', $result->entity_type)
->execute();
}
$held_states = registration_get_held_states();
if (!empty($held_states)) {
$results = db_select('registration', 'r')
->fields('r')
->condition('state', $held_states, 'IN')
->execute()
->fetchAllAssoc('registration_id');
$registrations = registration_load_multiple(array_keys($results));
foreach ($registrations as $registration) {
$registration_wrapper = entity_metadata_wrapper('registration', $registration);
$registration_type = registration_type_load($registration_wrapper
->getBundle());
if ($registration_type->held_expire !== '0' && time() - $registration_type->held_expire * 60 * 60 > $registration->updated) {
$registration_wrapper->state
->set($registration_type->held_expire_state);
$registration_wrapper
->save();
}
}
}
}
function registration_status($entity_type, $entity_id, $reset = FALSE, $spaces = 1, $registration_id = NULL, &$errors = array()) {
$checked =& drupal_static(__FUNCTION__, array());
if (!$reset && isset($checked[$entity_type][$entity_id])) {
if (!is_array($checked[$entity_type][$entity_id]['errors'])) {
$checked[$entity_type][$entity_id]['errors'] = array();
}
$errors = is_array($errors) ? array_merge($errors, $checked[$entity_type][$entity_id]['errors']) : $checked[$entity_type][$entity_id]['errors'];
return $checked[$entity_type][$entity_id]['status'];
}
$entity = entity_load_single($entity_type, $entity_id);
$registration_type = registration_get_entity_registration_type($entity_type, $entity);
if (!$registration_type) {
return FALSE;
}
$settings = registration_entity_settings($entity_type, $entity_id, $reset);
$status = $settings['status'];
$open = isset($settings['open']) ? strtotime($settings['open']) : NULL;
$close = isset($settings['close']) ? strtotime($settings['close']) : NULL;
$now = REQUEST_TIME;
if ($status) {
if (isset($settings['settings']['maximum_spaces']) && $settings['settings']['maximum_spaces'] && $spaces > $settings['settings']['maximum_spaces']) {
$status = FALSE;
$errors[] = t('You may not register for more than @count spaces.', array(
'@count' => $settings['settings']['maximum_spaces'],
));
}
if (!registration_has_room($entity_type, $entity_id, $spaces, $registration_id, $reset)) {
$status = FALSE;
$errors[] = t(REGISTRATION_INSUFFICIENT_SPACES_MESSAGE);
}
if (isset($open) && $now < $open) {
$status = FALSE;
$errors[] = t('Registration is not yet open.');
}
if (isset($close) && $now >= $close) {
$status = FALSE;
$errors[] = t('Registration is closed.');
}
}
else {
$errors[] = t('Registration is disabled.');
}
$context = array(
'entity_type' => $entity_type,
'entity_id' => $entity_id,
'errors' => &$errors,
);
drupal_alter('registration_status', $status, $context);
$checked[$entity_type][$entity_id] = array(
'status' => $status,
'errors' => $errors,
);
return $status;
}
function registration_get_entity_registration_type($entity_type, $entity) {
$fields = field_read_fields(array(
'type' => 'registration',
));
foreach ($fields as $field) {
if (isset($entity->{$field['field_name']})) {
$items = field_get_items($entity_type, $entity, $field['field_name']);
if (!empty($items) && count($items) == 1 && !empty($items[0]['registration_type'])) {
return $items[0]['registration_type'];
}
}
}
return FALSE;
}
function registration_get_registration_instances($params = array()) {
$registration_fields = field_read_fields(array(
'type' => 'registration',
));
$registration_instances = array();
if (!empty($registration_fields)) {
$field_name = array(
'field_name' => array_keys($registration_fields),
);
$params = array_merge($field_name, $params);
$registration_instances = field_read_instances($params);
}
return $registration_instances;
}
function registration_token_info() {
$type = array(
'name' => t('Registration'),
'description' => t('Tokens related to individual Registrations.'),
'needs-data' => 'registration',
);
$registration['entity'] = array(
'name' => t("Registration Host Entity"),
'description' => t("The host entity for the registration."),
);
$registration['user'] = array(
'name' => t("Registration User"),
'description' => t("The user the registration is for."),
'type' => 'user',
);
return array(
'types' => array(
'registration' => $type,
),
'tokens' => array(
'registration' => $registration,
),
);
}
function registration_tokens($type, $tokens, array $data = array(), array $options = array()) {
if ($type == 'registration' && !empty($data['registration'])) {
$registration = $data['registration'];
$wrapper = entity_metadata_wrapper('registration', $data['registration']);
$replacements = array();
if ($entity_tokens = token_find_with_prefix($tokens, 'entity')) {
$entity = $wrapper->entity
->value();
$replacements += token_generate($registration->entity_type, $entity_tokens, array(
$registration->entity_type => $entity,
), $options);
}
if ($entity_tokens = token_find_with_prefix($tokens, 'user')) {
$entity = $wrapper->user
->value();
$replacements += token_generate('user', $entity_tokens, array(
'user' => $entity,
), $options);
}
return $replacements;
}
}
function registration_is_registered(Registration $registration, $anon_mail = NULL, $uid = NULL, $states = array()) {
if (!$anon_mail && !$uid) {
return FALSE;
}
if (empty($states)) {
$states = registration_get_active_states();
}
if (empty($states)) {
return FALSE;
}
$query = db_select('registration', 'r')
->condition('entity_id', $registration->entity_id)
->condition('entity_type', $registration->entity_type)
->condition('state', $states, 'IN');
if ($anon_mail) {
if ($user = user_load_by_mail($anon_mail)) {
$query
->condition(db_or()
->condition('anon_mail', $anon_mail)
->condition('user_uid', $user->uid));
}
else {
$query
->condition('anon_mail', $anon_mail);
}
}
elseif ($uid) {
$query
->condition('user_uid', $uid);
}
if (isset($registration->registration_id)) {
$query
->condition('registration_id', $registration->registration_id, '<>');
}
$count = $query
->countQuery()
->execute()
->fetchField();
return $count > 0;
}
function registration_access_people(Registration $registration, $account = NULL) {
$account = isset($account) ? $account : $GLOBALS['user'];
$people = array();
$settings = registration_entity_settings($registration->entity_type, $registration->entity_id);
$allow_multiple = !empty($settings['settings']['multiple_registrations']) && $settings['settings']['multiple_registrations'];
if ($account->uid && user_access("create {$registration->type} registration self", $account) && ($account->uid === $registration->user_uid || (!registration_is_registered($registration, NULL, $account->uid) || $allow_multiple))) {
$people[REGISTRATION_REGISTRANT_TYPE_ME] = t('Myself');
}
if (user_access("create {$registration->type} registration other users", $account)) {
$people[REGISTRATION_REGISTRANT_TYPE_USER] = t('Other account');
}
$user_is_anonymous = empty($account->uid) || $account->uid == 0;
if (user_access("create {$registration->type} registration other anonymous", $account) && !$user_is_anonymous) {
$people[REGISTRATION_REGISTRANT_TYPE_ANON] = t('Other person');
}
if ($user_is_anonymous && user_access("create {$registration->type} registration")) {
$people[REGISTRATION_REGISTRANT_TYPE_ANON] = t('Myself');
}
return $people;
}
function registration_field_extra_fields() {
$extra = array();
foreach (registration_get_types() as $type => $reg_type) {
$extra['registration'][$type] = array(
'form' => array(
'who_is_registering' => array(
'label' => t('Registrant'),
'description' => t('Select who is registering for this event.'),
'weight' => 0,
),
'anon_mail' => array(
'label' => t('Email'),
'description' => t('Registrant\'s email address.'),
'weight' => 0,
),
),
'display' => array(
'mail' => array(
'label' => t('Email'),
'description' => t('Registrant\'s email address.'),
'weight' => 0,
),
'host_entity_link' => array(
'label' => t('Entity Link'),
'description' => t('Link to host entity.'),
'weight' => 0,
),
'created' => array(
'label' => t('Created'),
'description' => t('When the registration was created.'),
'weight' => 0,
),
'updated' => array(
'label' => t('Updated'),
'description' => t('When the registration was updated.'),
'weight' => 0,
),
'spaces' => array(
'label' => t('Spaces Used'),
'description' => t('How many spaces were used in this registration.'),
'weight' => 0,
),
'author' => array(
'label' => t('Author'),
'description' => t('User who created the registration.'),
'weight' => 0,
),
'user' => array(
'label' => t('User'),
'description' => t('User associated with this registration.'),
'weight' => 0,
),
'state' => array(
'label' => t('State'),
'description' => t('State of the registration.'),
'weight' => 0,
),
),
);
}
return $extra;
}
function registration_states($conditions = array()) {
$states =& drupal_static(__FUNCTION__ . serialize($conditions), array());
if (!empty($states)) {
return $states;
}
$entity_type = 'registration_state';
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', $entity_type)
->propertyOrderBy('weight', 'ASC');
foreach ($conditions as $col => $val) {
$query
->propertyCondition($col, $val);
}
if ($results = $query
->execute()) {
$states = entity_load($entity_type, array_keys($results[$entity_type]));
}
return $states;
}
function registration_get_active_states() {
$active = array();
$states = registration_states(array(
'active' => TRUE,
));
foreach ($states as $state) {
$active[] = $state
->identifier();
}
return $active;
}
function registration_get_held_states() {
$held = array();
$states = registration_states(array(
'held' => TRUE,
));
foreach ($states as $state) {
$held[] = $state
->identifier();
}
return $held;
}
function registration_get_default_state($type = NULL) {
$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;
}
function registration_get_states($name = NULL) {
$types = entity_load_multiple_by_name('registration_state', isset($name) ? array(
$name,
) : FALSE);
return isset($name) ? reset($types) : $types;
}
function registration_get_states_options($conditions = array()) {
$options = array();
if (!is_array($conditions)) {
$conditions = array();
}
$states = registration_states($conditions);
foreach ($states as $state) {
$options[$state
->identifier()] = t('@state', array(
'@state' => entity_label('registration_state', $state),
));
}
return $options;
}
function registration_property_host_get(Registration $registration, array $options, $property_name, $entity_type) {
$entity = entity_load_single($registration->entity_type, $registration->entity_id);
return entity_metadata_wrapper($registration->entity_type, $entity);
}
function registration_property_host_set(Registration $registration, $name, $value, $lang, $type, $info) {
$registration->entity_type = $value
->type();
$registration->entity_id = $value
->getIdentifier();
}
function registration_property_author_get(Registration $registration, array $options, $property_name, $entity_type) {
if (is_numeric($registration->author_uid)) {
$entity = entity_load_single('user', $registration->author_uid);
return entity_metadata_wrapper('user', $entity);
}
}
function registration_property_user_get(Registration $registration, array $options, $property_name, $entity_type) {
if (is_numeric($registration->user_uid)) {
$entity = entity_load_single('user', $registration->user_uid);
return entity_metadata_wrapper('user', $entity);
}
}
function registration_property_user_set(Registration $registration, $name, $value, $lang, $type, $info) {
if (is_object($value) && ($account = $value
->value())) {
$registration->{$info['schema field']} = $account->uid;
}
else {
$registration->{$info['schema field']} = NULL;
}
}
function registration_property_email_set(Registration $registration, $name, $value, $lang, $type, $info) {
if (!empty($value)) {
$registration->{$info['schema field']} = $value;
}
else {
$registration->{$info['schema field']} = NULL;
}
}
function registration_get_properties($entity, array $options, $name, $type) {
switch ($name) {
case 'anon_mail':
return $entity->anon_mail;
break;
case 'anon_access_hash':
return registration_anonymous_access_hash($entity);
break;
case 'anon_link':
$path = entity_uri('registration', $entity);
$url = url($path['path'] . '/view/' . registration_anonymous_access_hash($entity), array(
'absolute' => TRUE,
));
return $url;
break;
case 'entity':
return entity_metadata_wrapper($entity->entity_type, $entity->entity);
break;
case 'mail':
$user = entity_metadata_wrapper('registration', $entity)->user;
return $user
->value() ? $user->mail
->value() : $entity->anon_mail;
break;
}
}
function registration_load($registration_id) {
if (empty($registration_id)) {
return FALSE;
}
$registrations = registration_load_multiple(array(
$registration_id,
), array());
return $registrations ? reset($registrations) : FALSE;
}
function registration_load_multiple($registration_ids = array(), $conditions = array(), $reset = FALSE) {
if (empty($registration_ids) && empty($conditions)) {
return array();
}
return entity_load('registration', $registration_ids, $conditions, $reset);
}
function registration_delete_multiple($registration_ids) {
return entity_get_controller('registration')
->delete($registration_ids);
}
function registration_save(Registration $registration) {
return $registration
->save();
}
function registration_access($op, Registration $registration = NULL, $account = NULL) {
$account = isset($account) ? $account : $GLOBALS['user'];
$admin = user_access('administer registration', $account);
if (!isset($registration)) {
return $admin;
}
$type = $registration
->bundle();
if ($admin || user_access("administer {$type} registration", $account)) {
return TRUE;
}
switch ($op) {
case 'update':
$people = registration_access_people($registration);
$registrant_type = $registration
->registrant_type($account);
if (!isset($registrant_type) && !isset($people[$registrant_type])) {
return FALSE;
}
break;
case 'create':
if (!count(registration_access_people($registration))) {
return FALSE;
}
break;
}
$access_results = module_invoke_all('registration_access', $op, $registration, $account);
if (in_array(FALSE, $access_results, TRUE)) {
return FALSE;
}
elseif (in_array(TRUE, $access_results, TRUE)) {
return TRUE;
}
$wrapper = entity_metadata_wrapper('registration', $registration);
$author = $wrapper->author
->value();
$account_own = $author && $author->uid == $account->uid;
switch ($op) {
case 'view':
return $account_own && user_access("view own {$type} registration", $account) || user_access("view {$type} registration", $account);
case 'update':
return $account_own && user_access("update own {$type} registration", $account) || user_access("update any {$type} registration", $account);
case 'create':
return user_access("create {$type} registration", $account);
case 'delete':
return $account_own && user_access("delete own {$type} registration", $account) || user_access("delete any {$type} registration", $account);
}
}
function registration_get_types($name = NULL) {
$types = entity_load_multiple_by_name('registration_type', isset($name) ? array(
$name,
) : FALSE);
return isset($name) ? reset($types) : $types;
}
function registration_type_get_names($name = NULL) {
$types = registration_get_types();
$data = array();
foreach (array_keys($types) as $name) {
$data[$name] = $name;
}
return $data;
}
function registration_type_load($type) {
return registration_get_types($type);
}
function registration_type_access($op, $entity = NULL, $account = NULL, $entity_type = NULL) {
return user_access('administer registration types', $account);
}
function registration_type_save(RegistrationType $type) {
$type
->save();
}
function registration_type_delete(RegistrationType $type) {
$type
->delete();
}
function registration_state_access($op, $entity = NULL, $account = NULL, $entity_type = NULL) {
return user_access('administer registration states', $account);
}
function registration_i18n_string_info() {
$groups = array();
$groups['registration'] = array(
'title' => t('Entity registration'),
'description' => t('Translatable registration settings.'),
'format' => FALSE,
'list' => TRUE,
);
return $groups;
}
function _registration_translate($name, $string, $langcode = NULL) {
return function_exists('i18n_string') ? i18n_string('registration:' . $name, $string, array(
'langcode' => $langcode,
)) : $string;
}
function _registration_translate_update($name, $string, $langcode = NULL) {
return function_exists('i18n_string') ? i18n_string('registration:' . $name, $string, array(
'update' => TRUE,
'langcode' => $langcode,
)) : FALSE;
}