You are here

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

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

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

Parameters

$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.

3 calls to merci_get_reservable_items()
merci_build_reservable_items in includes/api.inc
Builds the list of all currently reservable items, filtered by date.
merci_get_available_bucket_items in includes/api.inc
Pulls items available to assign to a bucket for a reservation.
merci_views_handler_filter_reserved_item_nid::allowed_values in handlers/merci_views_handler_filter_reserved_item_nid.inc

File

includes/database.inc, line 369
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_get_reservable_items($content_type, $start = NULL, $end = NULL, $reservation_nid = NULL, $overdue = TRUE) {
  $merci_type = merci_type_setting($content_type);

  // 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 or checked in 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 or checked out 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.
  //   4. Exclude items from past reservations where the item is in a checked out state.
  $query = "SELECT n.nid, n.title FROM {node} n\n    INNER JOIN {merci_reservation_item_node} m ON n.vid = m.vid\n      WHERE m.merci_default_availability IN (:avail, :avail2)\n        AND n.type = :type\n        AND m.merci_sub_type = :merci_sub_type\n        ";
  $args = array(
    ':avail' => MERCI_AVA_F,
    ':avail2' => MERCI_AVA_T,
    ':type' => $content_type,
    ':merci_sub_type' => MERCI_SUB_TYPE_ITEM,
  );
  if ($start) {

    // Determine CCK table and columns the date data is stored in.
    $field = field_info_field('field_merci_date');
    $table = key($field['storage']['details']['sql']['FIELD_LOAD_CURRENT']);
    $column_start_date = $field['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$table]['value'];
    $column_end_date = $field['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$table]['value2'];
    $args += array(
      ':start1' => $start,
      ':start2' => $end,
      ':start3' => $start,
      ':start4' => $end,
      ':start5' => $start,
      ':start6' => $end,
      ':merci_item_status' => 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.
    $query .= "\n      AND n.nid NOT IN\n      (SELECT md2.merci_item_nid FROM {" . $table . "} ct\n      INNER JOIN {merci_reservation_detail} md2 ON ct.revision_id = md2.vid\n      INNER JOIN {merci_reservation_item_node} m2 ON md2.merci_item_nid = m2.nid\n      INNER JOIN {node} ctn ON ctn.vid = ct.revision_id\n      INNER JOIN {node} m2n ON m2.vid = m2n.vid\n      WHERE (\n        (\n          (({$column_start_date} >= :start1 AND {$column_start_date} <= :start2)\n          OR ({$column_end_date} >= :start3 AND {$column_end_date} <= :start4)\n          OR ({$column_start_date} <= :start5 AND {$column_end_date} >= :start6))\n          AND NOT md2.merci_item_status <= :merci_item_status\n        ) ";
    if ($reservation_nid) {
      $where = ' AND md2.nid <> :reservation_nid';
      $args[':reservation_nid'] = $reservation_nid;
    }
    $query .= "\n      )\n      " . (isset($where) ? $where : "") . "\n    )\n    ";
  }
  $query .= " ORDER BY n.title";
  $items = db_query($query, $args);
  $options = array();
  foreach ($items as $item) {
    $options[$item->nid] = $item->title;
  }
  if ($overdue) {
    $overdue_items_array = merci_overdue_items($content_type, $start, $reservation_nid);
    if (!empty($overdue_items_array)) {
      foreach ($options as $item_nid => $title) {
        if (array_key_exists($item_nid, $overdue_items_array)) {
          unset($options[$item_nid]);
        }
      }
    }
  }
  return $options;
}