You are here

bat_event_ui.module in Booking and Availability Management Tools for Drupal 7

Same filename and directory in other branches
  1. 8 modules/bat_event_ui/bat_event_ui.module

Manages events for Units and displaying dates on the jquery FullCalendar plugin.

File

modules/bat_event_ui/bat_event_ui.module
View source
<?php

/**
 * @file
 * Manages events for Units and displaying dates on the jquery FullCalendar plugin.
 */
define('BAT_EVENT_CALENDAR_ADMIN_STYLE', 1);
define('BAT_EVENT_CALENDAR_GENERIC_STYLE', 2);

/**
 * Implements hook_menu().
 */
function bat_event_ui_menu() {
  $items = array();
  $items['admin/bat/calendar'] = array(
    'title' => 'Calendar',
    'page callback' => 'bat_event_ui_calendar_page',
    'page arguments' => array(
      3,
      4,
    ),
    'access arguments' => array(
      'administer calendar events',
    ),
    'type' => MENU_CALLBACK,
    'weight' => 20,
  );
  return $items;
}

/**
 * Callback for admin/bat/calendar.
 */
function bat_event_ui_calendar_page($unit_type, $event_type) {
  module_load_include('inc', 'composer_manager', 'composer_manager.admin');
  $composer_manager_installed_packages = composer_manager_installed_packages();
  if (!isset($composer_manager_installed_packages['roomify/bat'])) {
    drupal_set_message(t('Please install the BAT PHP Library. For instructions click <a target="_blank" href="@link">here</a>.', array(
      '@link' => 'http://docs.roomify.us/bat/drupal/installation.html#install-bat-php-library',
    )), 'error');
    return '';
  }
  $ev_type = bat_event_type_load($event_type);

  // Check if current type support this event type.
  if ($type = bat_type_load($unit_type)) {
    $type_bundle = bat_type_bundle_load($type->type);
    if (is_array($type_bundle->default_event_value_field_ids)) {
      if (!(isset($type_bundle->default_event_value_field_ids[$event_type]) && !empty($type_bundle->default_event_value_field_ids[$event_type]))) {
        return drupal_not_found();
      }
    }
    else {
      return drupal_not_found();
    }
  }
  elseif ($unit_type != 'all') {
    return drupal_not_found();
  }

  // Check if user has permission to view calendar data for this event type.
  if (!user_access('view calendar data for any ' . $ev_type->type . ' event')) {
    return drupal_access_denied();
  }

  // Set the page title.
  drupal_set_title(t('View Event Data'));

  // Pick what modal style to use.
  $calendar_settings['modal_style'] = 'default';
  if ($type = bat_event_type_load($event_type)) {
    $event_granularity = $type->event_granularity;
  }
  else {
    $event_granularity = 'both';
  }

  // All Drupal JS settings inside the batCalendar object.
  $fc_user_settings = array(
    'batCalendar' => array(
      array(
        'unitType' => $unit_type,
        'eventType' => $event_type,
        'eventGranularity' => $event_granularity,
      ),
    ),
  );
  $calendar_settings['user_settings'] = $fc_user_settings;
  $calendar_settings['calendar_id'] = 'fullcalendar-scheduler';
  if ($ev_type->fixed_event_states) {
    $calendar_settings['class'] = array(
      'fixed_event_states',
    );
  }
  else {
    $calendar_settings['class'] = array(
      'open_event_states',
    );
  }
  drupal_add_css(drupal_get_path('module', 'bat_event_ui') . '/css/bat_event_ui.css');
  $render_array = array(
    'event_type_form' => drupal_get_form('bat_event_ui_event_type_form', $unit_type, $event_type),
    'bulk_update_form' => array(),
    'calendar' => array(
      '#theme' => 'bat_fullcalendar',
      '#calendar_settings' => $calendar_settings,
    ),
  );
  if ($ev_type->fixed_event_states) {
    $render_array['bulk_update_form'] = drupal_get_form('bat_event_ui_bulk_update_form', $unit_type, $event_type);
  }
  $jquery_version = variable_get('jquery_update_jquery_version', '1.10');
  $admin_theme = variable_get('admin_theme', FALSE);
  if (theme_get_setting('jquery_update_jquery_version', $admin_theme) != '') {
    $jquery_version = theme_get_setting('jquery_update_jquery_version', $admin_theme);
  }
  if (function_exists('jquery_update_get_versions')) {
    $jquery_versions = array_keys(jquery_update_get_versions());
  }
  else {
    $jquery_versions = array(
      '1.5',
      '1.6',
      '1.7',
      '1.8',
      '1.9',
      '1.10',
    );
  }
  if ($jquery_version == 'default' || array_search($jquery_version, $jquery_versions) < array_search('1.8', $jquery_versions)) {
    drupal_set_message(t('Please ensure that you are using version 1.8 or greater of the jQuery library (<a href="@link">configure</a>)', array(
      '@link' => url('admin/config/development/jquery_update'),
    )), 'error');
  }
  return $render_array;
}

/**
 * Bulk update form.
 */
function bat_event_ui_bulk_update_form($form, &$form_state, $unit_type, $event_type) {
  $form['bulk_update'] = array(
    '#type' => 'fieldset',
    '#title' => t('Update event state'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['bulk_update']['event_type'] = array(
    '#type' => 'hidden',
    '#value' => $event_type,
  );
  if ($unit_type == 'all') {
    $types = bat_unit_get_types(NULL, TRUE);
    $types_options = array();
    foreach ($types as $type) {
      $type_bundle = bat_type_bundle_load($type->type);
      if (is_array($type_bundle->default_event_value_field_ids)) {
        if (isset($type_bundle->default_event_value_field_ids[$event_type]) && !empty($type_bundle->default_event_value_field_ids[$event_type])) {
          $types_options[$type->type_id] = $type->name;
        }
      }
    }
    $form['bulk_update']['type'] = array(
      '#type' => 'select',
      '#title' => t('Type'),
      '#options' => $types_options,
      '#required' => TRUE,
    );
  }
  else {
    $form['bulk_update']['type'] = array(
      '#type' => 'hidden',
      '#value' => $unit_type,
    );
  }
  $form['bulk_update'] += bat_date_range_fields();
  $form['bulk_update']['state'] = array(
    '#type' => 'select',
    '#title' => t('State'),
    '#options' => bat_unit_state_options($event_type, array(
      'blocking' => 0,
    )),
    '#required' => TRUE,
  );
  $form['bulk_update']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );
  return $form;
}

/**
 * Submit callback for bat_event_ui_bulk_update_form form.
 */
function bat_event_ui_bulk_update_form_submit($form, &$form_state) {
  $values = $form_state['values'];
  $start_date = new DateTime($values['bat_start_date']);
  $end_date = new DateTime($values['bat_end_date']);
  $end_date
    ->sub(new DateInterval('PT1M'));
  $event_type = $values['event_type'];
  $event_state = $values['state'];
  $type = bat_type_load($values['type']);
  $units = bat_unit_load_multiple(FALSE, array(
    'type_id' => $type->type_id,
  ));
  foreach ($units as $unit) {
    $event = bat_event_create(array(
      'type' => $event_type,
      'start_date' => $start_date
        ->format('Y-m-d H:i:s'),
      'end_date' => $end_date
        ->format('Y-m-d H:i:s'),
      'uid' => $type->uid,
      'created' => REQUEST_TIME,
    ));
    $event->event_bat_unit_reference[LANGUAGE_NONE][0]['target_id'] = $unit->unit_id;
    $event->event_state_reference[LANGUAGE_NONE][0]['state_id'] = $event_state;
    $event
      ->save();
  }
}

/**
 * Form - "Event type" and "Unit type" to shows in the calendar view.
 */
function bat_event_ui_event_type_form($form, &$form_state, $unit_type, $event_type) {
  if (isset($form_state['values']['event_types'])) {
    $event_type = $form_state['values']['event_types'];
  }
  $event_types = bat_event_get_types();
  foreach ($event_types as $ev_type) {
    if (user_access('view calendar data for any ' . $ev_type->type . ' event')) {
      $event_types_options[$ev_type->type] = $ev_type->label;
    }
  }
  $form['event_types'] = array(
    '#type' => 'select',
    '#title' => 'Event type',
    '#options' => $event_types_options,
    '#default_value' => $event_type,
    '#ajax' => array(
      'callback' => 'bat_event_ui_event_type_form_callback',
      'wrapper' => 'unit-type-wrapper',
    ),
  );
  $types = bat_unit_get_types();
  if (!empty($types)) {
    $types_options = array(
      'all' => t('All'),
    );
    foreach ($types as $type) {
      $type_bundle = bat_type_bundle_load($type->type);
      if (is_array($type_bundle->default_event_value_field_ids)) {
        if (isset($type_bundle->default_event_value_field_ids[$event_type]) && !empty($type_bundle->default_event_value_field_ids[$event_type])) {
          $types_options[$type->type_id] = $type->name;
        }
      }
    }
    $form['unit_type'] = array(
      '#type' => 'select',
      '#title' => 'Unit type',
      '#options' => $types_options,
      '#default_value' => $unit_type,
      '#prefix' => '<div id="unit-type-wrapper">',
      '#suffix' => '</div>',
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Change',
  );
  return $form;
}

/**
 * Ajax callback for bat_event_ui_event_type_form form.
 */
function bat_event_ui_event_type_form_callback($form, &$form_state) {
  return $form['unit_type'];
}

/**
 * Submit callback for bat_event_ui_event_type_form form.
 */
function bat_event_ui_event_type_form_submit($form, &$form_state) {
  $type = $form_state['values']['unit_type'];
  $event_type = $form_state['values']['event_types'];
  $form_state['redirect'] = 'admin/bat/calendar/' . $type . '/' . $event_type;
}

Functions

Namesort descending Description
bat_event_ui_bulk_update_form Bulk update form.
bat_event_ui_bulk_update_form_submit Submit callback for bat_event_ui_bulk_update_form form.
bat_event_ui_calendar_page Callback for admin/bat/calendar.
bat_event_ui_event_type_form Form - "Event type" and "Unit type" to shows in the calendar view.
bat_event_ui_event_type_form_callback Ajax callback for bat_event_ui_event_type_form form.
bat_event_ui_event_type_form_submit Submit callback for bat_event_ui_event_type_form form.
bat_event_ui_menu Implements hook_menu().

Constants

Namesort descending Description
BAT_EVENT_CALENDAR_ADMIN_STYLE @file Manages events for Units and displaying dates on the jquery FullCalendar plugin.
BAT_EVENT_CALENDAR_GENERIC_STYLE