You are here

function resource_conflict_form_alter in Resource Conflict 5.2

Same name and namespace in other branches
  1. 5 resource_conflict.module \resource_conflict_form_alter()
  2. 6.2 resource_conflict.module \resource_conflict_form_alter()
  3. 7.3 resource_conflict.module \resource_conflict_form_alter()
  4. 7.2 resource_conflict.module \resource_conflict_form_alter()

Implementation of hook_form_alter

File

./resource_conflict.module, line 118

Code

function resource_conflict_form_alter($form_id, &$form) {
  if ($form_id == 'node_type_form') {
    $requirements = array();
    $type = isset($form['old_type']) && isset($form['old_type']['#value']) ? $form['old_type']['#value'] : NULL;
    $form['resource_conflict_set'] = array(
      '#type' => 'fieldset',
      '#title' => t('Resource Conflict'),
      '#collapsible' => TRUE,
    );

    // The user is adding a new content type
    if ($type == NULL) {
      $form['resource_conflict_set']['rc_info'] = array(
        '#prefix' => '<p>',
        '#suffix' => '</p>',
        '#value' => t('To set up this content type for conflict checking, first event-enable the type, or add a Date CCK field with required start and end dates. Then, add at least one Node Reference field linked to the content type of items you would like to be able to book. When all of the conditions have been met, this section will be enabled for configuration.'),
      );
      return;
    }
    $date_fields = array();
    if ($type != NULL) {
      $type_info = content_types($type);
      $fields = $type_info['fields'];
      foreach ($fields as $field) {
        if ($field['type'] == 'nodereference') {
          $nodereference_fields[$field['field_name']] = $field['widget']['label'];
        }
        elseif ($field['type'] == 'date' && $field['todate'] == 'required' && $field['required']) {
          $date_fields[$field['field_name']] = $field['widget']['label'];
        }
      }
    }
    if (empty($nodereference_fields)) {
      $requirements['nodereference'] = t('At least one Node Reference field must be added to this content type.');
    }
    if (module_exists('event')) {
      $event_enabled = event_enabled_state($type) == "all" || event_enabled_state($type) == "solo";
    }
    else {
      $event_enabled = FALSE;
    }

    // If we are event enabled, allow the selection of the Event field.
    if ($event_enabled) {
      $date_fields += array(
        'event' => t('Use Event Field from the Event Module'),
      );
    }
    if (empty($date_fields) && !$event_enabled) {
      if (module_exists('event')) {
        $requirements['event'] = t('This content type is not event enabled. Please event-enable this content type if you wish to use Resource Conflict with the Event module.');
      }
      else {
        $requirements['event'] = t('This content type is not event enabled. Please install the Event module if you wish to use Resource Conflict with the Event module.');
      }
      $requirements['date_api'] = t('This content type does not contain any suitable Date fields. Please add at least one Date field with required start and end dates if you wish to use Resource Conflict with the Date module.');
    }
    if (!empty($requirements)) {
      _resource_conflict_disable($type, TRUE);
      $form['resource_conflict_set']['requirements'] = array(
        '#prefix' => '<p>' . t('The following requirements for Resource Conflict have not been met:') . '</p><ol>',
        '#suffix' => '</ol>',
        '#weight' => -10,
      );
      foreach ($requirements as $component => $error) {
        $form['resource_conflict_set']['requirements'][$component] = array(
          '#prefix' => '<li>',
          '#suffix' => '</li>',
          '#value' => $error,
        );
      }
    }
    else {
      $form['resource_conflict_set']['rc_type'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable resource conflict checking for this content type'),
        '#default_value' => variable_get('rc_type_' . $type, 0),
        '#weight' => -8,
      );
      $form['resource_conflict_set']['rc_date_field'] = array(
        '#type' => 'select',
        '#title' => t('Field to use as the date for conflict checks'),
        '#options' => $date_fields,
        '#multiple' => FALSE,
        '#default_value' => variable_get('rc_date_field_' . $type, FALSE),
        '#description' => t("Select the date field to use to check for resource conflicts."),
      );
      $form['resource_conflict_set']['rc_reference_fields'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Fields to check for conflicts'),
        '#options' => $nodereference_fields,
        '#default_value' => variable_get('rc_reference_fields_' . $type, FALSE),
        '#description' => t("Select the resources which can't be booked at the same time."),
      );
    }

    //set custom validation and submit callbacks
    $form['#validate']['resource_conflict_form_validate'] = array();
    $form['#submit']['resource_conflict_form_submit'] = array();
  }
}