You are here

function bat_event_edit_form in Booking and Availability Management Tools for Drupal 7

Form callback: create or edit a event.

Parameters

BatEvent $event: The BatEvent object to edit, or for a new event form, an empty event object with only an event type defined.

3 string references to 'bat_event_edit_form'
bat_event_create_form_wrapper in modules/bat_event/bat_event.admin.inc
Form callback wrapper: create a Event.
bat_event_form_wrapper in modules/bat_event/bat_event.admin.inc
Form callback wrapper: edit a Event.
bat_fullcalendar_bat_fullcalendar_modal_content in modules/bat_fullcalendar/bat_fullcalendar.module
Implements hook_bat_fullcalendar_modal_content().

File

modules/bat_event/bat_event.admin.inc, line 197
Unit event editing UI.

Code

function bat_event_edit_form($form, &$form_state, BatEvent $event) {
  $event->date = format_date($event->created, 'custom', 'Y-m-d H:i:s O');
  $account = user_load($event->uid);
  $event->owner_name = $account->name;
  if ($event->end_date_object) {

    // We are about to display an event, so we to add a minute to then end date.
    $event->end_date_object
      ->add(new DateInterval('PT1M'));

    // Re-set the end date string to reflect the change.
    $event->end_date = $event->end_date_object
      ->format('Y-m-d H:i:s');
  }
  $form['#attributes']['class'][] = 'bat-management-form bat-event-form';
  $form['#attached'] = array(
    'js' => array(
      drupal_get_path('module', 'bat') . '/js/bat_date_popup.js',
    ),
  );
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $event->type,
  );
  $form['data']['#tree'] = TRUE;

  // A fieldset to hold the date range fields.
  $form['bat_date_range'] = array(
    '#type' => 'fieldset',
    '#title' => t('Event dates'),
    '#attributes' => array(
      'class' => array(
        'bat-event-date-range-wrapper',
      ),
    ),
    '#weight' => -1,
  );
  $event_type = bat_event_type_load($event->type);
  $form['bat_date_range'] += bat_date_range_fields(NULL, NULL, $event_type->event_granularity);

  // Unset the default for max days away for events since we are on the admin.
  drupal_add_js(array(
    'bat' => array(
      'batEventStartDay' => 0,
    ),
  ), 'setting');

  // Set the default values for the dates.
  $form['bat_date_range']['bat_start_date']['#default_value'] = isset($event->start_date) ? $event->start_date : '';
  $form['bat_date_range']['bat_end_date']['#default_value'] = isset($event->end_date) ? $event->end_date : '';

  // Check the start and end dates to avoid damage from dirty input.
  $startdate = '';
  if (isset($_GET['startdate'])) {
    $startdate = is_numeric(check_plain($_GET['startdate'])) ? check_plain($_GET['startdate']) : '';
    if ($startdate != '') {
      $form['bat_date_range']['bat_start_date']['#default_value'] = gmdate('Y-m-d', $startdate);
    }
  }
  $enddate = '';
  if (isset($_GET['enddate'])) {
    $enddate = is_numeric(check_plain($_GET['enddate'])) ? check_plain($_GET['enddate']) : '';
    if ($enddate != '') {
      $form['bat_date_range']['bat_end_date']['#default_value'] = gmdate('Y-m-d', $enddate);
    }
  }

  // Add the field related form elements.
  $form_state['bat_event'] = $event;
  field_attach_form('bat_event', $event, $form, $form_state);

  // Construct target entity reference field name using this event type's target entity type.
  $target_field_name = 'event_' . $event_type->target_entity_type . '_reference';
  isset($form[$target_field_name]['#language']) ? $language = $form[$target_field_name]['#language'] : ($language = LANGUAGE_NONE);
  $targetid = '';
  if (isset($_GET['targetid'])) {
    $targetid = check_plain($_GET['targetid']);
    if ($target_entity = entity_load_single($event_type->target_entity_type, $targetid)) {
      $form[$target_field_name][$language][0]['target_id']['#default_value'] = $target_entity->name . ' (' . $targetid . ')';
    }
  }

  // The unit reference value should be mandatory.
  $form[$target_field_name][$language][0]['target_id']['#required'] = TRUE;
  if ($event_type->fixed_event_states && isset($form['event_state_reference'][$language][0]['state_id'])) {
    $form['event_state_reference'][$language][0]['state_id']['#required'] = TRUE;
  }
  else {
    $field_name = $event_type->default_event_value_field_ids[$event_type->type];
    if (isset($form[$field_name][$language][0]['amount']['#required'])) {
      $form[$field_name][$language][0]['amount']['#required'] = TRUE;
    }
    elseif (isset($form[$field_name][$language][0]['value']['#required'])) {
      $form[$field_name][$language][0]['value']['#required'] = TRUE;
    }
  }

  // Management vertical tabs.
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  );

  // Add the user account and e-mail fields.
  $form['user'] = array(
    '#type' => 'fieldset',
    '#title' => t('User information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#access' => user_access('bypass bat_event entities access'),
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'bat_event') . '/js/bat_event.js',
        array(
          'type' => 'setting',
          'data' => array(
            'anonymous' => variable_get('anonymous', t('Anonymous')),
          ),
        ),
      ),
    ),
    '#weight' => 30,
  );
  $form['user']['owner_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Owned by'),
    '#description' => t('Leave blank for %anonymous.', array(
      '%anonymous' => variable_get('anonymous', t('Anonymous')),
    )),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => !empty($event->owner_name) ? $event->owner_name : '',
    '#weight' => -1,
  );

  // Add a log checkbox and timestamp field to a history tab.
  $form['event_history'] = array(
    '#type' => 'fieldset',
    '#title' => t('Event history'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'bat_event') . '/js/bat_event.js',
      ),
    ),
    '#weight' => 40,
  );
  $form['event_history']['date'] = array(
    '#type' => 'textfield',
    '#title' => t('Created on'),
    '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array(
      '%time' => !empty($event->date) ? date_format(date_create($event->date), 'Y-m-d H:i:s O') : format_date($event->created, 'custom', 'Y-m-d H:i:s O'),
      '%timezone' => !empty($event->date) ? date_format(date_create($event->date), 'O') : format_date($event->created, 'custom', 'O'),
    )),
    '#maxlength' => 25,
    '#default_value' => !empty($event->created) ? format_date($event->created, 'custom', 'Y-m-d H:i:s O') : '',
  );
  $form['event_history']['created'] = array(
    '#type' => 'hidden',
    '#value' => !empty($event->created) ? format_date($event->created, 'short') : '',
    '#attributes' => array(
      'id' => 'edit-created',
    ),
  );
  $form['event_history']['changed'] = array(
    '#type' => 'hidden',
    '#value' => !empty($event->changed) ? format_date($event->changed, 'short') : '',
    '#attributes' => array(
      'id' => 'edit-changed',
    ),
  );
  $form['actions'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'form-actions',
      ),
    ),
    '#weight' => 400,
  );

  // We add the form's #submit array to this button along with the actual submit
  // handler to preserve any submit handlers added by a form callback_wrapper.
  $submit = array();
  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Event'),
    '#submit' => $submit + array(
      'bat_event_edit_form_submit',
    ),
  );
  if (!isset($event->is_new) && bat_event_access('delete', $event)) {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete Event'),
      '#submit' => $submit + array(
        'bat_event_form_submit_delete',
      ),
      '#weight' => 45,
    );
  }

  // Depending on whether the form is in a popup or a normal page we need to change
  // the behavior of the cancel button.
  if (isset($form_state['ajax']) && $form_state['ajax'] == TRUE) {
    unset($form['actions']['cancel']);
  }
  else {
    $form['actions']['cancel'] = array(
      '#markup' => l(t('Cancel'), 'admin/bat/events'),
      '#weight' => 50,
    );
  }
  $event_type = bat_event_type_load($event->type);
  if ($event_type->event_granularity == 'bat_hourly') {
    $form['bat_date_range']['bat_start_date']['#date_format'] = variable_get('bat_date_format', 'Y-m-d H:i');
    $form['bat_date_range']['bat_end_date']['#date_format'] = variable_get('bat_date_format', 'Y-m-d H:i');
  }

  // We append the validate handler to #validate in case a form callback_wrapper
  // is used to add validate handlers earlier.
  $form['#validate'][] = 'bat_form_start_end_dates_validate';
  $form['#validate'][] = 'bat_event_edit_form_validate';
  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');
    $form['actions']['submit']['#disabled'] = TRUE;
    if (isset($form['actions']['delete'])) {
      $form['actions']['delete']['#disabled'] = TRUE;
    }
  }
  return $form;
}