You are here

function merci_form_alter in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.2

Same name and namespace in other branches
  1. 6.2 merci.module \merci_form_alter()
  2. 6 merci.module \merci_form_alter()

Implementation of hook_form_alter().

File

./merci.module, line 462
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_form_alter(&$form, $form_state, $form_id) {

  // Node add/edit forms.
  $type = isset($form['#bundle']) ? $form['#bundle'] : NULL;
  switch ($form_id) {

    // Node settings form.
    case 'merci_reservation_node_form':
      $form['field_merci_date']['field_merci_date_button'] = array(
        '#type' => 'submit',
        '#value' => t('Limit Lists to Available Items'),
        //'#weight' => $weight,
        '#submit' => array(
          'merci_date_filter',
        ),
        '#limit_validation_errors' => array(
          array(
            'merci_reservation_items',
          ),
          array(
            'choice_count',
          ),
          array(
            'field_merci_date',
          ),
        ),
        // No need to validate when submitting this.
        '#validate' => array(),
        '#ajax' => array(
          //'path' => 'merci/js',
          'callback' => 'merci_choice_js',
          'wrapper' => 'merci-choice-wrapper',
          'method' => 'replace',
          'effect' => 'fade',
        ),
      );
      return;
    case $type . '_node_form':
      if (merci_is_merci_type($type)) {
        $node = (object) $form['#node'];
        $sub_type = isset($node->merci_sub_type) ? $node->merci_sub_type : MERCI_SUB_TYPE_ITEM;
        $default_availability = isset($node->merci_default_availability) ? $node->merci_default_availability : MERCI_AVA_F;
        if ($sub_type == MERCI_SUB_TYPE_ITEM) {
          if (empty($form['merci'])) {
            $form['merci'] = array(
              '#type' => 'fieldset',
              '#title' => t('MERCI settings'),
              '#collapsible' => TRUE,
              '#collapsed' => TRUE,
              '#group' => 'additional_settings',
            );
          }
          $form['merci']['merci_default_availability'] = array(
            '#title' => t('Default booking availability'),
            '#type' => 'radios',
            '#options' => merci_item_status(),
            '#description' => t('If no availability information is defined for a given time, the resource falls back onto this setting.'),
            '#default_value' => $default_availability,
            '#element_validate' => array(
              'merci_validate_default_availability',
            ),
          );
        }
        $form['merci_sub_type'] = array(
          '#type' => 'value',
          '#value' => $sub_type,
        );
        merci_add_settings_form($form, $form_state);
      }
      break;
    case 'node_type_form':

      // Reservation content type can't used for other MERCI functionality.
      if (isset($form['#node_type']->type) && $form['#node_type']->type == 'merci_reservation') {
        return;
      }
      $type = $form['old_type']['#value'];

      // If any nodes have already been created, lock the type setting.
      if ($type) {
        $nodes = merci_nodes_for_type_count($type);
        $settings = merci_load_item_settings($type);
      }
      if (empty($settings) and isset($nodes)) {
        return;
      }
      $warning = '';
      if (!empty($settings->merci_type_setting) and $settings->merci_type_setting == 'resource') {
        $warning = '<div>' . t('<strong> WARNING:</strong> changing this setting has no effect on existing reserved items.') . '</div>';
      }
      $options = array(
        'disabled' => t('Disabled'),
        'bucket' => t('Bucket'),
        'resource' => t('Resource'),
      );
      $form['merci'] = array(
        '#type' => 'fieldset',
        '#title' => t('MERCI settings'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#group' => 'additional_settings',
        '#attached' => array(
          'js' => array(
            drupal_get_path('module', 'menu') . '/menu.admin.js',
          ),
        ),
      );

      // If any nodes have already been created, lock the type setting.
      if (isset($nodes) and $nodes) {
        $form['merci']['merci_type_setting'] = array(
          '#type' => 'value',
          '#value' => isset($settings->merci_type_setting) ? $settings->merci_type_setting : 'disabled',
        );
        $form['merci']['merci_type_setting_display'] = array(
          '#type' => 'item',
          '#title' => t('Reservable item type'),
          '#value' => isset($settings->merci_type_setting) ? $options[$settings->merci_type_setting] : '',
          '#description' => t('The setting can not be changed because content already exists for this type.'),
        );
      }
      else {
        $description_items = array(
          'disabled' => t('This content type cannot be reserved'),
          'resource' => t('Use this content type to create unique items that can be reserved.'),
          'bucket' => t('Use this content type to create interchangeable items that can be reserved (ex. Camera). Buckets reference interchangeable items. The actual item does not have to be chosen until the reservation is checked out.'),
        );
        $form['merci']['merci_type_setting'] = array(
          '#type' => 'radios',
          '#title' => t('Reservable item type'),
          '#options' => $options,
          '#default_value' => isset($settings->merci_type_setting) ? $settings->merci_type_setting : 'disabled',
          '#description_items' => $description_items,
          '#after_build' => array(
            'merci_type_setting_after_build',
          ),
        );
      }
      $status = array(
        MERCI_STATUS_ACTIVE => t('Active'),
        MERCI_STATUS_INACTIVE => t('Inactive'),
      );
      $form['merci']['merci_active_status'] = array(
        '#type' => 'radios',
        '#title' => t('Status'),
        '#options' => $status,
        '#default_value' => isset($settings->merci_active_status) ? intval($settings->merci_active_status) : MERCI_STATUS_ACTIVE,
        '#description' => t('Set to active to allow this type to be reserved.'),
      );

      // This setting is only valid for buckets.
      if (!isset($settings->merci_type_setting) || $settings->merci_type_setting == 'bucket') {
        $form['merci']['merci_spare_items'] = array(
          '#type' => 'textfield',
          '#title' => t('Spare items'),
          '#size' => 10,
          '#default_value' => isset($settings->merci_spare_items) ? $settings->merci_spare_items : 0,
          '#element_validate' => array(
            'merci_is_numeric_validate',
          ),
          '#description' => filter_xss(t("Set this to the number of items of this type that should always be unavailable and thus unreservable.  This way you'll still have enough items for future reservations in case something breaks.") . $warning),
        );
        $form['merci']['merci_auto_assign_bucket_item'] = array(
          '#type' => 'checkbox',
          '#title' => t('Automatically assign a bucket item'),
          '#default_value' => isset($settings->merci_auto_assign_bucket_item) ? $settings->merci_auto_assign_bucket_item : 0,
          '#description' => t('Automatically assign the best fit bucket item when reserving a new bucket item.'),
        );
      }
      $vid = variable_get('merci_equipment_grouping_vid', 0);
      $form['merci']['merci_grouping'] = merci_taxonomy_term_select(t('Grouping'), NULL, variable_get('merci_grouping_' . $type, 0), $vid, t('This will alter order the content types are displayed to users reserving items from buckets.  Terms added to the MERCI Equipment Groupings taxonomy will appear here.'), 0, t('<Select>'));
      $form['merci']['merci_max_hours_per_reservation'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum hours per reservation'),
        '#size' => 10,
        '#default_value' => isset($settings->merci_max_hours_per_reservation) ? $settings->merci_max_hours_per_reservation : 0,
        '#element_validate' => array(
          'merci_is_numeric_validate',
        ),
        '#description' => filter_xss(t('The maximum hours the item can be reserved for in one reservation. Set to zero for no limit.') . $warning),
      );
      $form['merci']['merci_allow_overnight'] = array(
        '#type' => 'checkbox',
        '#title' => t('Allow overnight reservation'),
        '#default_value' => isset($settings->merci_allow_overnight) ? $settings->merci_allow_overnight : 0,
        '#description' => filter_xss(t('Allow a reservation to continue over multiple days.  If this is not checked, items in this content type must be returned before the checkout closes.') . $warning),
      );
      $form['merci']['merci_allow_weekends'] = array(
        '#type' => 'checkbox',
        '#title' => t('Allow weekend reservation'),
        '#default_value' => isset($settings->merci_allow_weekends) ? $settings->merci_allow_weekends : 0,
        '#description' => filter_xss(t('Allow a reservation to be made over days defined as weekend.') . $warning),
      );
      merci_add_settings_form($form, $form_state);
      $form['#validate'][] = 'merci_node_type_save_validate';

      //$form['#submit'][]   = 'merci_node_type_save_submit';
      break;
    case 'node_delete_confirm':
      $node = node_load((int) arg(1));
      if (!merci_delete_item_validate($node)) {
        unset($form['actions']['submit']);
      }
      break;
    case 'node_type_delete_confirm':
      $type = str_replace('-', '_', arg(3));
      merci_delete_node_type_validate($form);
      break;
    case 'node_admin_content':
      if (!isset($form['#validate'])) {
        $form['#validate'] = array();
      }
      $form['#validate'][] = 'merci_node_admin_delete_validate';
      break;
  }
}