You are here

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

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

Pulls an array of items that are reservable for the content type and date range.

Parameters

$merci_type: The MERCI type. bucket|resource

$content_type: The content type name of the bucket/resource.

$start: Start time in DATETIME format UTC timezone.

$end: End time in DATETIME format UTC timezone.

$reservation_nid: (Optional) A reservation nid to exclude from the reserved items.

Return value

An array of reservable items, in select option format.

2 calls to merci_get_reservable_items()
merci_build_reservable_items in ./merci.module
Builds the list of all currently reservable items, filtered by date.
merci_get_available_bucket_items in ./merci.module
Pulls items available to assign to a bucket for a reservation.

File

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

Code

function merci_get_reservable_items($merci_type, $content_type, $start, $end, $reservation_nid = NULL) {

  // Determine CCK table and columns the date data is stored in.
  $field = content_fields('field_merci_date');
  $db_info = content_database_info($field);
  $table = $db_info['table'];
  $column_start_date = $db_info['columns']['value']['column'];
  $column_end_date = $db_info['columns']['value2']['column'];
  $args = array(
    MERCI_AVA_F,
    MERCI_AVA_S,
    $content_type,
    MERCI_SUB_TYPE_ITEM,
    $start,
    $end,
    $start,
    $end,
    $start,
    $end,
    MERCI_ITEM_STATUS_AVAILABLE,
  );

  // If there's an already selected bucket item, then we need to make sure we
  // include it in the list of available items.
  $where = '';
  $inner_where = '';
  if ($reservation_nid) {
    $inner_where = ' AND md2.nid <> %d';
    $args[] = $reservation_nid;
  }

  // Pull reservable items.  This query takes the following into consideration:
  //   1. Pulls all all item nodes of the content type that are in an available state,
  //   2. Excludes all item nodes that have associated reservations in the date range
  //      of the this reservation where the item is in an already reserved state.
  //   3. Allows a reservation to be excluded from the exclusions if necessary (this
  //      is usually used to allow an already assigned item to not conflict with itself.
  $items = db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {merci_{$merci_type}_node} m ON n.vid = m.vid WHERE (m.default_availability IN (%d, %d) AND n.type = '%s' AND m.sub_type = %d AND n.nid NOT IN (SELECT md2.item_nid FROM {" . $table . "} ct INNER JOIN {merci_reservation_detail} md2 ON ct.vid = md2.vid INNER JOIN {merci_{$merci_type}_node} m2 ON md2.item_nid = m2.nid INNER JOIN {node} ctn ON ctn.vid = ct.vid INNER JOIN {node} m2n ON m2.vid = m2n.vid WHERE (({$column_start_date} >= '%s' AND {$column_start_date} <= '%s') OR ({$column_end_date} >= '%s' AND {$column_end_date} <= '%s') OR ({$column_start_date} <= '%s' AND {$column_end_date} >= '%s')) AND NOT (md2.item_status <= %d){$inner_where})) ORDER BY n.title", $args);
  $options = array();
  while ($item = db_fetch_object($items)) {
    $options[$item->nid] = $item->title;
  }
  return $options;
}