You are here

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

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

Implementation of hook_form_alter().

File

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

Code

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

  // Node add/edit forms.
  if (isset($form['type']) && isset($form['#node'])) {
    $type = $form['type']['#value'];
    if ("{$type}_node_form" == $form_id && $form_id != 'merci_reservation_node_form') {
      $merci_settings = merci_load_content_type_settings($type);

      // Only active MERCI node types get processed.
      if ($merci_settings->type_setting != 'disabled') {

        // Cast to object here, as it can come in both ways.
        $node = (object) $form['#node'];

        // Make the data representation consistent.
        if (isset($node->merci_sub_type)) {
          $sub_type = $node->merci_sub_type;
          $default_availability = $node->merci_default_availability;
          $late_fee_per_hour = $node->merci_late_fee_per_hour;
          $rate_per_hour = $node->merci_rate_per_hour;
          $fee_free_hours = $node->merci_fee_free_hours;
          $min_cancel_hours = $node->merci_min_cancel_hours;
          $autocheckout = $node->merci_autocheckout;
          $autocheckin = $node->merci_autocheckin;
          $selfcheckout = $node->merci_selfcheckout;
        }
        elseif (isset($node->merci['sub_type'])) {
          $sub_type = $node->merci['sub_type'];
          $default_availability = $node->merci['default_availability'];
          $late_fee_per_hour = $node->merci['late_fee_per_hour'];
          $rate_per_hour = $node->merci['rate_per_hour'];
          $fee_free_hours = $node->merci['fee_free_hours'];
          $min_cancel_hours = $node->merci['min_cancel_hours'];
          $autocheckout = $node->merci['autocheckout'];
          $autocheckin = $node->merci['autocheckin'];
          $selfcheckout = $node->merci['selfcheckout'];
        }
        else {
          $sub_type = MERCI_SUB_TYPE_ITEM;
          $default_availability = MERCI_AVA_F;

          // Only resource types have individual pricing data.
          if ($merci_settings->type_setting == 'bucket') {
            $rate_per_hour = 0;
            $late_fee_per_hour = 0;
            $fee_free_hours = 0;
            $min_cancel_hours = 0;
            $autocheckout = 0;
            $autocheckin = 0;
            $selfcheckout = 0;
          }
          else {
            $rate_per_hour = $merci_settings->rate_per_hour;
            $late_fee_per_hour = $merci_settings->late_fee_per_hour;
            $fee_free_hours = $merci_settings->fee_free_hours;
            $min_cancel_hours = $merci_settings->min_cancel_hours;
            $autocheckout = $node->merci_autocheckout;
            $autocheckin = $node->merci_autocheckin;
            $selfcheckout = $node->merci_selfcheckout;
          }
        }

        // New nodes are always sub type item.
        $form['merci_sub_type'] = array(
          '#type' => 'value',
          '#value' => $sub_type,
        );
        if (user_access('administer MERCI')) {
          $form['merci'] = array(
            '#type' => 'fieldset',
            '#title' => t('MERCI settings'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
          );
          $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,
          );

          // Bucket item nodes have no individual pricing, so just zero these values out.
          if ($merci_settings->type_setting == 'bucket' && $sub_type == MERCI_SUB_TYPE_ITEM) {
            $form['merci_rate_per_hour'] = array(
              '#type' => 'value',
              '#value' => $rate_per_hour,
            );
            $form['merci_late_fee_per_hour'] = array(
              '#type' => 'value',
              '#value' => $late_fee_per_hour,
            );
            $form['merci_fee_free_hours'] = array(
              '#type' => 'value',
              '#value' => $fee_free_hours,
            );
            $form['merci_min_cancel_hours'] = array(
              '#type' => 'value',
              '#value' => $min_cancel_hours,
            );
            $form['merci_autocheckout'] = array(
              '#type' => 'value',
              '#value' => $autocheckout,
            );
            $form['merci_autocheckin'] = array(
              '#type' => 'value',
              '#value' => $autocheckin,
            );
            $form['merci_selfcheckout'] = array(
              '#type' => 'value',
              '#value' => $selfcheckout,
            );
          }
          else {
            $form['merci']['merci_rate_per_hour'] = array(
              '#type' => 'textfield',
              '#title' => t('Rate per hour'),
              '#size' => 10,
              '#default_value' => $rate_per_hour,
              '#element_validate' => array(
                'merci_is_numeric_validate',
              ),
              '#description' => t('The per hour rental fee for the item.'),
            );
            $form['merci']['merci_late_fee_per_hour'] = array(
              '#type' => 'textfield',
              '#title' => t('Late fee per hour'),
              '#size' => 10,
              '#default_value' => $late_fee_per_hour,
              '#element_validate' => array(
                'merci_is_numeric_validate',
              ),
              '#description' => t('The per hour fee for returning the item late.'),
            );
            $form['merci']['merci_fee_free_hours'] = array(
              '#type' => 'textfield',
              '#title' => t('Fee free hours'),
              '#size' => 10,
              '#default_value' => $fee_free_hours,
              '#element_validate' => array(
                'merci_is_numeric_validate',
              ),
              '#description' => t('The number of hours the item can be used before fees are charged.'),
            );
            $form['merci']['merci_autocheckout'] = array(
              '#type' => 'checkbox',
              '#title' => t('Auto checkout'),
              '#default_value' => $autocheckout,
              '#description' => t('Automatically check this item out when the Reservation starts.'),
            );
            $form['merci']['merci_autocheckin'] = array(
              '#type' => 'checkbox',
              '#title' => t('Auto checkin'),
              '#default_value' => $autocheckout,
              '#description' => t('Automatically check this item in when the Reservation ends.'),
            );
            $form['merci']['merci_selfcheckout'] = array(
              '#type' => 'checkbox',
              '#title' => t('Self checkout'),
              '#default_value' => $autocheckout,
              '#description' => t('Manage checkout with additional code.'),
            );
          }
        }
        else {
          $form['merci_default_availability'] = array(
            '#type' => 'value',
            '#value' => $default_availability,
          );
          $form['merci_rate_per_hour'] = array(
            '#type' => 'value',
            '#value' => $rate_per_hour,
          );
          $form['merci_late_fee_per_hour'] = array(
            '#type' => 'value',
            '#value' => $late_fee_per_hour,
          );
          $form['merci_fee_free_hours'] = array(
            '#type' => 'value',
            '#value' => $fee_free_hours,
          );
          $form['merci_min_cancel_hours'] = array(
            '#type' => 'value',
            '#value' => $min_cancel_hours,
          );
          $form['merci_autocheckout'] = array(
            '#type' => 'value',
            '#value' => $autocheckout,
          );
          $form['merci_autocheckin'] = array(
            '#type' => 'value',
            '#value' => $autocheckin,
          );
          $form['merci_selfcheckout'] = array(
            '#type' => 'value',
            '#value' => $selfcheckout,
          );
        }
      }
    }
  }
  switch ($form_id) {

    // Add check availability button inside date selector.
    case 'merci_reservation_node_form':

      //Hide member cost and commerical cost.  These are only here to make Views easier.
      unset($form['field_merci_member_cost']);
      unset($form['field_merci_commercial_cost']);

      //Users without administer MERCI permission can only alter Unconfirmed Reservations.
      if (!user_access('administer MERCI') && $form['merci']['merci_status']['#default_value']) {
        unset($form['buttons']['preview']);
        unset($form['buttons']['submit']);
      }
      if (user_access('suspend MERCI access') && !user_access('administer MERCI')) {
        form_set_error('merci_status', t('Your access to make new Reservations or edit existing Reservations has been suspended.'));
      }
      else {
        $form['field_merci_date'][0]['merci_date_filter'] = array(
          '#type' => 'submit',
          '#value' => t('Check availability'),
          '#weight' => 10,
          '#submit' => array(
            'merci_date_filter',
          ),
        );

        // Since hook_validate is broken in 6.x, we add our own
        // custom validation here.
        $form['#validate'][] = 'merci_node_validate';
      }
      break;

    // Node settings form.
    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;
      }
      $warning = '<div>' . t('<strong> WARNING:</strong> changing this setting has no effect on existing reserved items.') . '</div>';
      $type = $form['old_type']['#value'];
      $settings = db_fetch_object(db_query("SELECT * FROM {merci_node_type} WHERE type = '%s'", $type));
      $options = array(
        'disabled' => t('Disabled'),
        'bucket' => t('Bucket'),
        'resource' => t('Resource'),
      );
      $form['#validate'][] = 'merci_node_type_save_validate';
      $form['#submit'][] = 'merci_node_type_save_submit';
      $form['merci'] = array(
        '#type' => 'fieldset',
        '#title' => t('MERCI settings'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );

      // If any nodes have already been created, lock the type setting.
      if (merci_check_existing_bucket_items($type)) {
        $form['merci']['merci_type_setting'] = array(
          '#type' => 'value',
          '#value' => $settings->type_setting,
        );
        $form['merci']['merci_type_setting_display'] = array(
          '#type' => 'item',
          '#title' => t('Reservable item type'),
          '#value' => $options[$settings->type_setting],
          '#description' => t('The setting can not be changed because content already exists for this type.'),
        );
      }
      else {
        $description_items = array(
          t('Resource:  Use this content type to create unique items that can be reserved.'),
          t('Bucket:  Use this content type to create interchangable items that can be reserved (ex. Camera). Buckets reference interchangable 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' => $settings ? $settings->type_setting : 'disabled',
          '#description' => theme('item_list', $description_items),
        );
      }
      $status = array(
        MERCI_STATUS_ACTIVE => t('Active'),
        MERCI_STATUS_INACTIVE => t('Inactive'),
      );
      $form['merci']['merci_status'] = array(
        '#type' => 'radios',
        '#title' => t('Status'),
        '#options' => $status,
        '#default_value' => isset($settings->status) && $settings->status ? $settings->status : MERCI_STATUS_ACTIVE,
        '#description' => t('Set to active to allow this type to be reserved.'),
      );

      // This setting is only valid for buckets.
      if (!$settings || $settings->type_setting == 'bucket') {
        $form['merci']['merci_spare_items'] = array(
          '#type' => 'textfield',
          '#title' => t('Spare items'),
          '#size' => 10,
          '#default_value' => $settings ? $settings->spare_items : 0,
          '#element_validate' => array(
            'merci_is_numeric_validate',
          ),
          '#description' => t("Set this to the number of items of this type that should always be available even when it's fully reserved.") . $warning,
        );
      }
      else {
        $form['merci']['merci_spare_items'] = array(
          '#type' => 'value',
          '#value' => 0,
        );
      }

      //Grouping
      $items = array();
      $terms = taxonomy_get_tree(variable_get('merci_equipment_grouping_vid', 37));
      $groupings[0] = 'None';
      foreach ($terms as $term) {
        $groupings[$term->tid] = $term->name;
      }
      $form['merci']['merci_grouping'] = array(
        '#type' => 'select',
        '#title' => t('Grouping'),
        '#default_value' => $settings ? $settings->grouping : 0,
        '#options' => $groupings,
        '#description' => 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.'),
      );
      $form['merci']['merci_max_hours_per_reservation'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum hours per reservation'),
        '#size' => 10,
        '#default_value' => $settings ? $settings->max_hours_per_reservation : 0,
        '#element_validate' => array(
          'merci_is_numeric_validate',
        ),
        '#description' => 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' => $settings ? $settings->allow_overnight : 0,
        '#description' => t('Allow a reservation to continue over multiple days.') . $warning,
      );
      $form['merci']['merci_allow_weekends'] = array(
        '#type' => 'checkbox',
        '#title' => t('Allow weekend reservation'),
        '#default_value' => $settings ? $settings->allow_weekends : 0,
        '#description' => t('Allow a reservation to be made over days defined as weekend.') . $warning,
      );
      $form['merci']['merci_rate_per_hour'] = array(
        '#type' => 'textfield',
        '#title' => t('Rate per hour'),
        '#size' => 10,
        '#default_value' => $settings ? $settings->rate_per_hour : 0,
        '#element_validate' => array(
          'merci_is_numeric_validate',
        ),
        '#description' => t('The per hour rental fee for the item.'),
      );
      $form['merci']['merci_late_fee_per_hour'] = array(
        '#type' => 'textfield',
        '#title' => t('Late fee per hour'),
        '#size' => 10,
        '#default_value' => $settings ? $settings->late_fee_per_hour : 0,
        '#element_validate' => array(
          'merci_is_numeric_validate',
        ),
        '#description' => t('The per hour fee for returning the item late.'),
      );
      $form['merci']['merci_fee_free_hours'] = array(
        '#type' => 'textfield',
        '#title' => t('Fee free hours'),
        '#size' => 10,
        '#default_value' => $settings ? $settings->fee_free_hours : 0,
        '#element_validate' => array(
          'merci_is_numeric_validate',
        ),
        '#description' => t('The number of hours the item can be used before fees are charged.'),
      );
      $form['merci']['merci_min_cancel_hours'] = array(
        '#type' => 'textfield',
        '#title' => t('Minimum hours for cancelation without No Show'),
        '#size' => 10,
        '#default_value' => $settings ? $settings->min_cancel_hours : 0,
        '#element_validate' => array(
          'merci_is_numeric_validate',
        ),
        '#description' => t('Minimum number of hours a user can cancel a reservation for the item.'),
      );
      $form['merci']['merci_autocheckout'] = array(
        '#type' => 'checkbox',
        '#title' => t('Automatic Checkout'),
        '#default_value' => $settings ? $settings->autocheckout : 0,
        '#description' => t('Automatically check item out when reservation starts.  Use for resources like meetings rooms that a user does not actually take.') . $warning,
      );
      $form['merci']['merci_autocheckin'] = array(
        '#type' => 'checkbox',
        '#title' => t('Automatic Checkin'),
        '#default_value' => $settings ? $settings->autocheckin : 0,
        '#description' => t('Automatically check item in when reservation ends.  Use for resources like meetings rooms that a user does not actually return.') . $warning,
      );
      $form['merci']['merci_selfcheckout'] = array(
        '#type' => 'checkbox',
        '#title' => t('Self Checkout'),
        '#default_value' => $settings ? $settings->selfcheckout : 0,
        '#description' => t('The checkout and checkin process for this resource is managed with additional software (ie. lab computer where login process can by linked to checkout status).') . $warning,
      );
      break;
    case 'node_delete_confirm':
      $node = node_load((int) arg(1));
      merci_delete_item_validate($node);
      break;
    case 'node_type_delete_confirm':
      $type = str_replace('-', '_', arg(3));
      merci_delete_node_type_validate($type);
      break;
    case 'node_admin_content':
      if (!isset($form['#validate'])) {
        $form['#validate'] = array();
      }
      $form['#validate'][] = 'merci_node_admin_delete_validate';
      break;
  }
}