function merci_reserved_bucket_items in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.2
Same name and namespace in other branches
- 6.2 includes/database.inc \merci_reserved_bucket_items()
3 calls to merci_reserved_bucket_items()
- merci_get_available_bucket_count in includes/
database.inc - Calculates the total number of available bucket items for a reservation.
- merci_get_suggested_bucket_item in includes/
api.inc - merci_load_reservations_for_node_in_timespan in includes/
database.inc - Builds an array representing reservations for a Resource within a given timespan
File
- includes/
database.inc, line 517 - MERCI - Managed Equipment Reservation Checkout and Inventory
Code
function merci_reserved_bucket_items($content_type, $start = NULL, $end = NULL, $exclude_nid = NULL, $overdue = TRUE) {
// 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'];
// Get all assignable nodes for this bucket item.
$total_items_nodes = db_query("SELECT n.nid FROM {node} n INNER JOIN {merci_reservation_item_node} m ON n.vid = m.vid WHERE n.type = :type AND m.merci_sub_type = :merci_sub_type AND m.merci_default_availability IN (:avail1, :avail2)", array(
':type' => $content_type,
':merci_sub_type' => MERCI_SUB_TYPE_ITEM,
':avail1' => MERCI_AVA_F,
':avail2' => MERCI_AVA_T,
));
$total_items_array = array();
foreach ($total_items_nodes as $ctnodes) {
$total_items_array[$ctnodes->nid] = array();
}
$args = array(
':start1' => $start,
':start2' => $end,
':start3' => $start,
':start4' => $end,
':start5' => $start,
':start6' => $end,
':type' => $content_type,
':merci_item_status' => MERCI_ITEM_STATUS_AVAILABLE,
);
// If we're checking an existing reservation, exclude it from the
// reserved items.
if (isset($exclude_nid)) {
$where = ' AND ct.entity_id <> :exclude_nid';
$args[':exclude_nid'] = $exclude_nid;
}
else {
$where = '';
}
// pull reservations with assigned nodes and status of MERCI_ITEM_STATUS_RESERVED or MERCI_ITEM_STATUS_CHECKED_OUT
// TODO Please convert this statement to the D7 database API syntax.
$reserved_nodes = db_query("\n SELECT ct.entity_id as nid,{$column_start_date} AS field_merci_date_value, {$column_end_date} AS field_merci_date_value2 ,md.merci_item_nid FROM {" . $table . "} ct\n INNER JOIN {merci_reservation_detail} md on ct.revision_id = md.vid\n INNER JOIN {merci_bucket_node} m on md.merci_placeholder_nid = m.nid\n INNER JOIN {node} ctn on ct.revision_id = ctn.vid\n INNER JOIN {node} mn on m.vid = mn.vid\n WHERE (\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 )\n AND mn.type = :type\n AND md.merci_item_nid !=0\n AND NOT md.merci_item_status <= :merci_item_status\n {$where}", $args);
// Use up items for assigned nodes.
foreach ($reserved_nodes as $node) {
// If item is assigned then item is in use by this node.
$total_items_array[$node->merci_item_nid][$node->nid] = $node;
}
if ($overdue) {
$overdue_items_array = merci_overdue_items($content_type, $start, $exclude_nid);
if (!empty($overdue_items_array)) {
foreach ($overdue_items_array as $merci_item_nid => $nodes) {
foreach ($nodes as $nid => $node) {
$total_items_array[$node->merci_item_nid][$node->nid] = $node;
}
}
}
}
// pull reservations without assigned nodes and not status of MERCI_ITEM_STATUS_CHECKED_IN
// TODO Please convert this statement to the D7 database API syntax.
$reserved_nodes = db_query("\n SELECT ct.entity_id AS nid,{$column_start_date} AS field_merci_date_value, {$column_end_date} AS field_merci_date_value2 ,md.merci_item_nid FROM {" . $table . "} ct\n INNER JOIN {merci_reservation_detail} md ON ct.revision_id = md.vid\n INNER JOIN {merci_bucket_node} m ON md.merci_placeholder_nid = m.nid\n INNER JOIN {node} ctn on ct.revision_id = ctn.vid\n INNER JOIN {node} mn ON m.vid = mn.vid\n WHERE (\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 )\n AND mn.type = :type\n AND md.merci_item_nid = 0\n AND NOT md.merci_item_status < :merci_item_status\n {$where}", $args);
uasort($total_items_array, '_merci_sort_array');
// Temporarily assign an item for these nodes.
foreach ($reserved_nodes as $node) {
// Eat up a bucket item for this node.
// If item is not assigned then temporarily add one.
foreach ($total_items_array as $item_nid => $reservations) {
$willitfit = TRUE;
foreach ($reservations as $oldnode) {
// Does the start date overlap this reservation.
if (date_create($node->field_merci_date_value) > date_create($oldnode->field_merci_date_value) and date_create($node->field_merci_date_value) < date_create($oldnode->field_merci_date_value2) or date_create($node->field_merci_date_value2) > date_create($oldnode->field_merci_date_value) and date_create($node->field_merci_date_value2) < date_create($oldnode->field_merci_date_value2) or date_create($node->field_merci_date_value) <= date_create($oldnode->field_merci_date_value) and date_create($node->field_merci_date_value2) >= date_create($oldnode->field_merci_date_value2)) {
// Can't use this item for this reservation. So try another.
$willitfit = FALSE;
break;
}
}
if ($willitfit) {
$total_items_array[$item_nid][$node->nid] = $node;
break;
}
}
}
return $total_items_array;
}