You are here

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

Same name and namespace in other branches
  1. 6.2 includes/api.inc \merci_build_reservable_items()
  2. 7.2 includes/api.inc \merci_build_reservable_items()

Builds the list of all currently reservable items, filtered by date.

Parameters

$node: The reservation node object.

$form_state: Current form state array.

$reservation_nid: (Optional) The nid of a reservation to ignore in the options exclusions.

Return value

An associative array with the following key/value pairs: 'options' => An array of available items, in the format used for the item selector. 'flat_options' => An array of available items, whose values are the nids of the items.

2 calls to merci_build_reservable_items()
merci_node_validate in ./merci.module
Implementation of hook_validate().
_merci_choice_form in ./merci.module
Builds an individual item selector.

File

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

Code

function merci_build_reservable_items($node, $form_state, $reservation_nid = NULL) {

  // Newly set dates take precedence.
  if (isset($form_state['values']['field_merci_date'])) {
    $start = $form_state['values']['field_merci_date'][0]['value'];
    $end = $form_state['values']['field_merci_date'][0]['value2'];
  }
  elseif (isset($node->nid)) {
    $date_info = $node->field_merci_date[0];
    $start = $date_info['value'];
    $end = $date_info['value2'];
  }
  else {
    $is_new = TRUE;
  }
  $options = array();
  $options['options'] = array(
    '' => t('<Select>'),
  );

  // Buckets.
  $buckets = merci_load_merci_type_settings('bucket');
  $bucket_options = array();

  // Loop through each bucket type.
  while ($bucket = db_fetch_object($buckets)) {

    // Only include active buckets, and content types the user
    // can reserve.
    if ($bucket->status == MERCI_STATUS_ACTIVE && merci_check_content_type_user_permissions($bucket->type)) {

      // No filtering for new reservations.
      if (isset($is_new)) {

        // Only add the bucket if there is at least one published item.
        if (merci_check_existing_bucket_items($bucket->type, TRUE)) {
          $bucket_options[$bucket->type] = $bucket->name;
        }

        // if
      }
      else {

        // Check bucket restrictions.
        // DIFF??
        $restrictions = merci_check_content_type_restrictions($bucket->type, $start, $end);
        if (empty($restrictions)) {

          // Check for available items in the bucket.
          $available_bucket_items = merci_get_available_bucket_count($bucket->type, $start, $end, $reservation_nid) > $bucket->spare_items;
          if ($available_bucket_items) {
            $bucket_options[$bucket->type] = $bucket->name;
          }

          // if
        }

        // if
      }

      // else
    }

    // if
  }

  // while
  // FIX - NEED TO CHANGE FORMATTING HERE
  $options['options'][t('Buckets')] = $bucket_options;
  $options['flat_options'] = array_keys($bucket_options);

  // Resources.
  $resources = merci_load_merci_type_settings('resource');

  // Loop through each resource type.
  while ($resource = db_fetch_object($resources)) {
    $item_options = array();

    // Only include active resources, and content types the user can reserve.
    if ($resource->status == MERCI_STATUS_ACTIVE && merci_check_content_type_user_permissions($resource->type)) {

      // No filtering for new reservations.
      if (isset($is_new)) {

        // Filter out any placeholder nodes for this resource.
        $items = db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {merci_resource_node} m ON n.vid = m.vid WHERE n.type = '%s' AND n.status = %d AND m.sub_type = %d ORDER BY n.title", $resource->type, 1, MERCI_SUB_TYPE_ITEM);
        while ($item = db_fetch_object($items)) {
          $item_options[$item->nid] = $item->title;
        }

        // while
      }
      else {

        // Check resource restrictions.

        //DIFF?
        $restrictions = merci_check_content_type_restrictions($resource->type, $start, $end);
        if (empty($restrictions)) {
          $item_options = merci_get_reservable_items('resource', $resource->type, $start, $end, $reservation_nid);
        }

        // if
      }

      // else
      $options['options'][$resource->name] = $item_options;
      $options['flat_options'] = array_merge($options['flat_options'], array_keys($item_options));
    }

    // if
  }

  // while
  return $options;
}