function merci_validate_merci_selected_items in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6.2
Same name and namespace in other branches
- 7.2 includes/api.inc \merci_validate_merci_selected_items()
@file MERCI - Managed Equipment Reservation Checkout and Inventory
1 call to merci_validate_merci_selected_items()
- merci_node_validate in ./
merci.module - Implementation of hook_validate().
File
- includes/
api.inc, line 9 - MERCI - Managed Equipment Reservation Checkout and Inventory
Code
function merci_validate_merci_selected_items($form, &$form_state) {
$node = (object) $form_state['values'];
// ****
// Build date objects we'll need for our different validations.
// ****
$start = $node->field_merci_date[0]['value'];
$end = $node->field_merci_date[0]['value2'];
$messages = array();
foreach ($node->merci_reservation_items as $did => $item) {
if (empty($item['merci_item_nid'])) {
continue;
}
// Bucket choice?
if (!is_numeric($item['merci_item_nid'])) {
$item['type'] = $item['merci_item_nid'];
$item['merci_item_nid'] = 0;
}
// Get the title of the item.
// Also get the content type for new resource items.
if (!isset($item['item_title']) or !isset($item['type'])) {
$new_item = node_load($item['merci_item_nid']);
if ($new_item) {
$item['item_title'] = $new_item->title;
$item['type'] = $new_item->type;
}
else {
//TODO: should not be doing theming here.
$content_settings = merci_load_item_settings($item['type']);
$item['item_title'] = $content_settings->type_name;
}
}
$messages[$did] = '';
if (isset($item['type'])) {
$type = $item['type'];
$item_nid = $item['merci_item_nid'];
$title = $item['item_title'];
// Is this content type active?
$content_settings = merci_load_item_settings($type);
if ($content_settings->merci_active_status != MERCI_STATUS_ACTIVE) {
$messages[$did] = '<div> ' . t("%name is not active.", array(
'%name' => $title,
)) . '</div>';
form_set_error("merci_reservation_items][{$did}][merci_item_nid", $messages[$did]);
continue;
}
// Does the user have access to manage reservations or this content type?
if (!user_access('manage reservations') && !merci_check_content_type_user_permissions($type)) {
$messages[$did] = '<div> ' . t("You do not have permission to reserve %name.", array(
'%name' => $title,
)) . '</div>';
form_set_error("merci_reservation_items][{$did}][merci_item_nid", $messages[$did]);
continue;
}
// Did the user select too many of the same bucket item?
if (merci_type_setting($type) == 'bucket') {
$selected_count[$type] = isset($selected_count[$type]) ? $selected_count[$type] + 1 : 1;
if (!isset($inventory_count[$type])) {
$inventory_count[$type] = merci_get_available_bucket_count($type);
}
if ($selected_count[$type] > $inventory_count[$type]) {
$messages[$did] = '<div> ' . t("You've selected too many %name's. We only have %amount in the current inventory.", array(
'%name' => $title,
'%amount' => $inventory_count[$type],
)) . '</div>';
form_set_error("merci_reservation_items][{$did}][merci_item_nid", $messages[$did]);
continue;
}
}
// Did the user select too many of the same item?
if ($item_nid) {
if (isset($selected_count[$item_nid])) {
$inventory_count = 1;
$messages[$did] = '<div> ' . t("You've selected too many %name's. We only have %amount in the current inventory.", array(
'%name' => $title,
'%amount' => $inventory_count,
)) . '</div>';
form_set_error("merci_reservation_items][{$did}][merci_item_nid", $messages[$did]);
continue;
}
else {
$selected_count[$item_nid] = 1;
}
}
// Is it available?
// Are we checking an existing item?
$nid = isset($item['did']) ? $node->nid : NULL;
// Is the item available at this time?
$count = merci_is_item_reservable($item_nid, $type, $start, $end, $nid);
if (!$count or empty($item_nid) and merci_type_setting($type) == 'bucket' and $count - $selected_count[$type] < 0) {
$messages[$did] = merci_theme_conflict_grid($type, $title, $start, $end, $item_nid, $node->nid);
form_set_error("merci_reservation_items][{$did}][merci_item_nid", $messages[$did]);
continue;
}
// How many items are overdue and thus unavailable at this time?
$overdue_items_array = merci_overdue_items($type, $start, $node->nid);
$overdue_count = 0;
$overdue_message = '';
if (empty($item_nid) and !empty($overdue_items_array) or !empty($item_nid) and array_key_exists($item_nid, $overdue_items_array)) {
$overdue_message = '<div> ' . t("%name is not available because it is still checked out by:", array(
'%name' => $title,
)) . '</div>';
foreach ($overdue_items_array as $reservations) {
foreach (array_keys($reservations) as $nid) {
$overdue = node_load($nid);
$overdue_message .= '<div> ' . l($overdue->title, 'node/' . $overdue->nid) . '</div>';
// Item is overdue so decriment the number of items available.
$overdue_count++;
}
}
}
// Show the error if conflict due to overdue.
if ($overdue_count and $count - $overdue_count <= 0) {
// But also show conflict grid if bucket items not available due to other reservations at the same time.
// Only done for buckets because resources always only have a count of 1 (if available) or 0 (not available).
if (merci_type_setting($type) == 'bucket') {
if ($inventory_count[$type] - $count > $overdue_count) {
$overdue_message .= merci_theme_conflict_grid($type, $title, $start, $end, $item_nid, $node->vid);
form_set_error("merci_reservation_items][{$did}][merci_item_nid", $messages[$did]);
}
}
form_set_error("merci_reservation_items][{$did}][merci_item_nid", $overdue_message);
continue;
}
// Check item restrictions. max hours, etc.
$restrictions = merci_check_content_type_restrictions($type, $start, $end);
if (!empty($restrictions)) {
foreach ($restrictions as $restriction) {
$messages[$did] .= '<div>' . t($restriction, array(
'%name' => $title,
)) . '</div>';
}
form_set_error("merci_reservation_items][{$did}][merci_item_nid", $messages[$did]);
continue;
}
}
}
return $messages;
}