View source
<?php
include_once 'commons_events.features.inc';
function commons_events_forms($form_id, $args) {
foreach (array(
'commons_events_attend_event_form',
'commons_events_cancel_event_form',
) as $form) {
if (substr($form_id, 0, strlen($form)) == $form) {
return array(
$form_id => array(
'callback' => $form,
),
);
}
}
}
function commons_events_menu() {
$items['node/%node/attendees'] = array(
'title' => 'Attendees',
'page callback' => 'commons_events_event_attendees_page',
'page arguments' => array(
1,
),
'access callback' => 'commons_events_attendee_access',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function commons_events_menu_alter(&$items) {
$items['node/%/register']['access callback'] = FALSE;
}
function commons_events_attendee_access($event_node) {
if ($event_node->type == 'event' && user_access('view event registration')) {
return TRUE;
}
return FALSE;
}
function commons_events_event_attendees_page($event_node) {
return theme('commons_events_event_attendees', array(
'event_nid' => $event_node->nid,
'display' => 'full',
));
}
function commons_events_theme($existing, $type, $theme, $path) {
return array(
'commons_events_attending_event' => array(
'variables' => array(
'event' => NULL,
'attendee_count' => NULL,
),
'file' => '/includes/commons_events.theme.inc',
),
'commons_events_event_attendees' => array(
'variables' => array(
'display' => NULL,
'event_nid' => NULL,
),
'file' => '/includes/commons_events.theme.inc',
),
'commons_events_date_display_range_advanced' => array(
'variables' => array(
'date1' => NULL,
'date2' => NULL,
'timezone' => NULL,
'dates' => NULL,
'attributes' => array(),
'attributes_start' => array(),
'attributes_end' => array(),
'rdf_mapping' => NULL,
'add_rdf' => NULL,
),
),
'commons_events_date_display_range_simple' => array(
'variables' => array(
'date1' => NULL,
'date2' => NULL,
'timezone' => NULL,
'dates' => NULL,
'attributes' => array(),
'attributes_start' => array(),
'attributes_end' => array(),
'rdf_mapping' => NULL,
'add_rdf' => NULL,
),
),
);
}
function commons_events_theme_registry_alter(&$theme_registry) {
$theme_registry['date_display_range']['theme path'] = drupal_get_path('module', 'commons_events');
$theme_registry['date_display_range']['function'] = 'theme_commons_events_date_display_range_advanced';
$theme_registry['date_display_single']['theme path'] = drupal_get_path('module', 'commons_events');
$theme_registry['date_display_single']['function'] = 'theme_commons_events_date_display_single';
}
function commons_events_element_info_alter(&$type) {
if (isset($type['date_combo'])) {
$type['date_combo']['#process'][] = 'commons_events_date_combo_element_process';
}
if (isset($type['date_popup'])) {
$type['date_popup']['#process'][] = 'commons_events_date_popup_element_process';
}
}
function commons_events_date_combo_element_process($element, &$form_state, $form) {
$field = field_widget_field($element, $form_state);
if (!empty($field['settings']['todate'])) {
$element['value']['#title'] = t('Start date');
$element['value2']['#title'] = t('End date');
}
return $element;
}
function commons_events_date_popup_element_process($element, &$form_state, $form) {
if (!empty($element['#field']['settings']['todate'])) {
foreach (array(
'date',
'time',
) as $type) {
if (!empty($element[$type])) {
unset($element[$type]['#description']);
$element[$type]['#title'] = '';
}
}
}
return $element;
}
function commons_events_entity_view_alter(&$build, $type) {
if ($type == 'node' && isset($build['#node'])) {
$node = $build['#node'];
list(, , $bundle) = entity_extract_ids('node', $node);
if ($bundle == 'event') {
$build['attending'] = array(
0 => theme('commons_events_attending_event', array(
'event' => $node,
'attendee_count' => commons_events_get_raw_attendee_count($node),
)),
'#weight' => -10,
);
$items = field_get_items('node', $node, 'field_location');
if (is_array($items)) {
foreach ($items as $item) {
if ($item['value'] == 'online') {
$build['field_address']['#access'] = FALSE;
}
}
}
}
}
}
function commons_events_form_node_form_alter(&$form, &$form_state, $form_id) {
$node = $form_state['node'];
list(, , $bundle) = entity_extract_ids('node', $node);
if ($bundle == 'event' && empty($node->nid)) {
drupal_set_title(t('Create an event'));
}
if ($bundle == 'event') {
$form['#attached']['css'][] = drupal_get_path('module', 'commons_events') . '/css/commons_events.css';
$form['#attached']['js'][] = drupal_get_path('module', 'commons_events') . '/js/commons_events.js';
unset($form['field_location'][$form['field_location']['#language']]['#options']['_none']);
unset($form['field_registration_type'][$form['field_registration_type']['#language']]['#options']['_none']);
unset($form['field_registration'][$form['field_registration']['#language']]['0']['registration_type']['#description']);
if (empty($form['field_organizers'][$form['field_organizers']['#language']]['#default_value'])) {
global $user;
$form['field_organizers'][$form['field_organizers']['#language']]['#default_value'] = format_username($user) . " (" . $user->uid . ")";
}
$form['field_registration']['#disabled'] = commons_events_event_type_disabled($node);
$form['field_number_of_attendees'][$form['field_number_of_attendees']['#language']]['0']['value']['#required'] = FALSE;
$form['field_address']['#states'] = array(
'invisible' => array(
':input[name^="field_location"]' => array(
'value' => 'online',
),
),
);
$node_registration_status = 1;
if (isset($node->nid)) {
$settings = registration_entity_settings('node', $node->nid);
$node_registration_status = $settings['status'];
}
$form['field_status'] = array(
'#type' => 'select',
'#title' => t('Status'),
'#options' => array(
1 => t('Open to new attendees'),
0 => t('Closed'),
),
'#default_value' => $node_registration_status,
'#states' => array(
'visible' => array(
':input[name^="field_registration_type"]' => array(
'value' => 'onsite',
),
),
),
'#weight' => 10,
);
$form['field_registration']['#states'] = $form['field_status']['#states'];
$form['field_number_of_attendees']['#states'] = $form['field_status']['#states'];
$form['field_number_of_attendees']['#states']['visible'][':input[name="field_status"]'] = array(
'value' => 1,
);
$form['field_number_of_attendees']['#weight'] = 11;
$form['event_registration_settings'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#title' => t('Registration settings'),
'#group' => 'additional_settings',
'#weight' => -10,
);
$form['event_registration_settings']['field_organizers'] = $form['field_organizers'];
$form['event_registration_settings']['field_registration_type'] = $form['field_registration_type'];
$form['event_registration_settings']['field_status'] = $form['field_status'];
$form['event_registration_settings']['field_number_of_attendees'] = $form['field_number_of_attendees'];
$form['event_registration_settings']['field_registration'] = $form['field_registration'];
hide($form['field_organizers']);
hide($form['field_registration_type']);
hide($form['field_status']);
hide($form['field_number_of_attendees']);
hide($form['field_registration']);
$form['#validate'][] = 'commons_events_node_form_validate';
$form['#submit'][] = 'commons_events_node_form_submit';
}
}
function commons_events_node_form_validate($form, &$form_state) {
$language = isset($form_state['language']) ? $form_state['language'] : LANGUAGE_NONE;
switch ($form_state['values']['field_registration_type'][$language]['0']['value']) {
case 'onsite':
if ((int) $form_state['values']['field_status'] == 1 && !is_numeric($form_state['values']['field_number_of_attendees'][$language]['0']['value'])) {
form_set_error('field_number_of_attendees', t('Maximum number of attendees must be set for registrations to be accepted.'));
}
break;
}
}
function commons_events_node_form_submit($form, $form_state) {
$node = $form_state['node'];
$language = isset($form_state['language']) ? $form_state['language'] : LANGUAGE_NONE;
if (isset($node->nid)) {
$settings = registration_entity_settings('node', $node->nid);
$settings['status'] = (int) $form_state['values']['field_status'];
if ($form_state['values']['field_registration_type'][$language]['0']['value'] == 'external') {
$settings['status'] = 0;
}
if (!empty($form_state['values']['field_number_of_attendees'][$language]['0']['value']) && is_numeric($form_state['values']['field_number_of_attendees'][$language]['0']['value'])) {
$settings['capacity'] = (int) $form_state['values']['field_number_of_attendees'][$language]['0']['value'];
}
$settings['settings'] = serialize($settings['settings']);
registration_update_entity_settings('node', $node->nid, $settings);
$registration_type = registration_get_entity_registration_type('node', $node);
$values = array(
'entity_type' => 'node',
'entity_id' => $node->nid,
'type' => $registration_type,
);
$registration = entity_create('registration', $values);
if (!isset($node->uid)) {
global $user;
$uid = $user->uid;
}
else {
$uid = $node->uid;
}
if ($settings['status'] == 1 && !registration_is_registered($registration, NULL, $uid)) {
$registration->user_uid = $uid;
$registration->author_uid = $uid;
$registration->state = NULL;
$registration->count = 1;
registration_save($registration);
}
}
}
function commons_events_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'node' && !empty($data['node'])) {
$event = $data['node'];
foreach ($tokens as $name => $original) {
if ($name == 'attend-form') {
$replacements[$original] = theme('commons_events_attending_event', array(
'event' => $event,
'attendee_count' => commons_events_get_raw_attendee_count($event),
));
}
}
}
return $replacements;
}
function commons_events_token_info_alter(&$tokens) {
$tokens['tokens']['node']['attend-form'] = array(
'name' => t('Attending event form'),
'description' => t('Displays the appropriate form for attending or cancelling event attendance'),
);
}
function commons_events_upcoming_more_link($group_id) {
if (module_exists('commons_events_solr')) {
$options = array(
'query' => array(
'f[1]' => 'sm_og_group_ref:node:' . $group_id,
),
);
return l(t('more'), 'events', $options);
}
$options = array(
'query' => array(
'gid' => $group_id,
),
);
return l(t('more'), 'events', $options);
}
function commons_events_views_post_execute(&$view) {
if ($view->name == 'commons_events_upcoming' && $view->current_display == 'panel_pane_1') {
$view->attachment_after = '<div class="more-link">' . commons_events_upcoming_more_link($view->args[0]) . '</div>';
}
if ($view->name == 'commons_events_upcoming' && $view->current_display == 'panel_pane_2') {
$view->attachment_before = '<div class="commons-event-count">' . format_plural(count($view->result), '1 event', '@count events') . '</div>';
}
}
function commons_events_block_info() {
$blocks['commons_events_create_event_link'] = array(
'info' => t('"Create an event" call to action'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
$blocks['commons_events_anonymous_login'] = array(
'info' => t('"Login to create an event" call to action'),
'cache' => DRUPAL_CACHE_GLOBAL,
);
return $blocks;
}
function commons_events_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'commons_events_create_event_link':
if (node_access('create', 'event')) {
$block['subject'] = NULL;
$block['content'] = array(
'#type' => 'link',
'#title' => t('List an event'),
'#href' => 'node/add/event',
'#options' => array(
'query' => array(
'destination' => 'events',
),
),
);
}
break;
case 'commons_events_anonymous_login':
if (user_is_anonymous()) {
$block['subject'] = NULL;
$block['content'] = t('You must !login in order to list events.', array(
'!login' => l(t('log in'), 'user'),
));
}
break;
}
return $block;
}
function theme_commons_events_date_display_range_simple($variables) {
$date1 = $variables['dates']['value']['formatted_date'];
$date2 = $variables['dates']['value2']['formatted_date'];
$time1 = $variables['dates']['value']['formatted_time'];
$time2 = $variables['dates']['value2']['formatted_time'];
$timezone = $variables['timezone'];
$attributes_start = $variables['attributes_start'];
$attributes_end = $variables['attributes_end'];
if ($date1 == $date2) {
return t('!start-time - !end-time', array(
'!start-time' => '<span class="date-display-start"' . drupal_attributes($attributes_start) . '>' . $time1 . '</span>',
'!end-time' => '<span class="date-display-end"' . drupal_attributes($attributes_end) . '>' . $time2 . $timezone . '</span>',
));
}
return t('!start-date to !end-date, !start-time - !end-time', array(
'!start-date' => '<span class="date-display-start"' . drupal_attributes($attributes_start) . '>' . $date1 . '</span>',
'!end-date' => '<span class="date-display-end"' . drupal_attributes($attributes_end) . '>' . $date2 . '</span>',
'!start-time' => '<span class="date-display-start"' . drupal_attributes($attributes_start) . '>' . $time1 . '</span>',
'!end-time' => '<span class="date-display-end"' . drupal_attributes($attributes_end) . '>' . $time2 . $timezone . '</span>',
));
}
function theme_commons_events_date_display_single($variables) {
return $variables['date'];
}
function theme_commons_events_date_display_range_advanced($variables) {
$timezone = $variables['timezone'];
$attributes_start = $variables['attributes_start'];
$attributes_end = $variables['attributes_end'];
$time1 = $variables['dates']['value']['formatted_time'];
$time2 = $variables['dates']['value2']['formatted_time'];
if ($variables['dates']['value']['formatted_date'] === $variables['dates']['value2']['formatted_date']) {
return theme('commons_events_date_display_range_simple', $variables);
}
if ($variables['dates']['value']['db']['object']
->difference($variables['dates']['value2']['db']['object'], 'years') > 0) {
return theme('commons_events_date_display_range_simple', $variables);
}
else {
if ($variables['dates']['value']['db']['object']
->difference($variables['dates']['value2']['db']['object'], 'months') > 0) {
$variables['dates']['value']['db']['object']
->limitGranularity(array(
'day',
'month',
));
$variables['dates']['value2']['db']['object']
->limitGranularity(array(
'day',
'month',
'year',
));
$date1 = $variables['dates']['value']['db']['object']
->format($variables['dates']['format'], FALSE);
$date2 = $variables['dates']['value2']['db']['object']
->format($variables['dates']['format'], FALSE);
}
else {
if ($variables['dates']['value']['db']['object']
->difference($variables['dates']['value2']['db']['object'], 'days') > 0) {
if (strpos($variables['dates']['format'], 'M') != 0 || strpos($variables['dates']['format'], 'm') != 0 || strpos($variables['dates']['format'], 'F') != 0 || strpos($variables['dates']['format'], 'n') != 0) {
$variables['dates']['value']['db']['object']
->limitGranularity(array(
'day',
));
$variables['dates']['value2']['db']['object']
->limitGranularity(array(
'day',
'month',
'year',
));
}
else {
$variables['dates']['value']['db']['object']
->limitGranularity(array(
'month',
'day',
));
$variables['dates']['value2']['db']['object']
->limitGranularity(array(
'day',
'year',
));
}
$date1 = $variables['dates']['value']['db']['object']
->format($variables['dates']['format'], FALSE);
$date2 = $variables['dates']['value2']['db']['object']
->format($variables['dates']['format'], FALSE);
if (strpbrk($variables['dates']['format'], 'Dl')) {
return theme('commons_events_date_display_range_simple', $variables);
}
}
else {
return theme('commons_events_date_display_range_simple', $variables);
}
}
}
return t('!start-date to !end-date, !start-time - !end-time', array(
'!start-date' => '<span class="date-display-start"' . drupal_attributes($attributes_start) . '>' . $date1 . '</span>',
'!end-date' => '<span class="date-display-end"' . drupal_attributes($attributes_end) . '>' . $date2 . '</span>',
'!start-time' => '<span class="date-display-start"' . drupal_attributes($attributes_start) . '>' . $time1 . '</span>',
'!end-time' => '<span class="date-display-end"' . drupal_attributes($attributes_end) . '>' . $time2 . $timezone . '</span>',
));
}
function commons_events_commons_notify_message_selection_alter(&$message_type, $hook, $node) {
if ($hook == 'node_insert' && $node->type == 'event') {
$message_type = 'commons_events_event_node_created';
}
}
function commons_events_event_type_disabled($node) {
if (!isset($node->nid) || empty($node->nid)) {
return FALSE;
}
$registrations = db_select('registration', 'r')
->fields('r')
->condition('entity_type', 'node')
->condition('entity_id', $node->nid)
->countQuery()
->execute()
->fetchCol();
if ($registrations[0] > 0) {
return TRUE;
}
return FALSE;
}
function commons_events_get_raw_attendee_count($node) {
$attendee_count = 0;
$query = db_select('registration', 'r')
->fields('r', array(
'count',
))
->condition('entity_id', $node->nid)
->condition('entity_type', 'node')
->execute();
foreach ($query as $record) {
$attendee_count += $record->count;
}
return $attendee_count;
}