You are here

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

Same name and namespace in other branches
  1. 6.2 includes/api.inc \merci_validate_status()
1 call to merci_validate_status()
merci_reservation_node_validate in ./merci.module
Implementation of hook_validate().

File

includes/api.inc, line 257
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_validate_status($form, &$form_state) {
  $node = (object) $form_state['values'];
  $langcode = $form_state['node']->language;

  // Reservations with a checked out status.
  if ($node->merci_reservation_status == MERCI_STATUS_CHECKED_OUT) {

    // Make sure all existing bucket reservations have an item assigned.
    if (empty($node->merci_reservation_items)) {
      form_set_error('merci_reservation_status', t('You can not finalize a reservation that has no reserved items.'));
    }
    else {
      foreach ($node->merci_reservation_items as $did => $item) {
        if ($item['merci_item_nid'] == "0") {
          form_set_error("merci_reservation_items][{$did}][merci_item_nid", t("The reservation for %title must have an item associated with it for finalized reservations.", array(
            '%title' => $item['name'],
          )));
        }

        // Can't add a bucket item and finalize at the same time.
        if (!is_numeric($item['merci_item_nid']) and strlen($item['merci_item_nid'])) {
          form_set_error("merci_reservation_items][{$did}][merci_item_nid", t("You cannot finalize a reservation while adding a bucket item."));
        }
        if (!empty($item['merci_item_nid'])) {
          $bad_reservations = merci_checked_out_reservations_for_item_nid($item['merci_item_nid'], $node->nid);
          if (!empty($bad_reservations)) {
            $output = '<ul>';
            foreach ($bad_reservations as $reservation) {
              $output .= '<li>' . $reservation . '</li>';
            }
            $output .= '</ul>';
            form_set_error("merci_reservation_items][{$did}][merci_item_nid", t("Cannot check out this item as it is currently checked out by:") . $output);
          }
        }
      }
    }
  }

  // Prevent status changes on reservations that have past.
  $current_status = $node->merci_original_reservation_status;
  if ($current_status && $current_status != $node->merci_reservation_status && time() > strtotime($node->field_merci_date[LANGUAGE_NONE][0]['value2'] . ' UTC') && !in_array((int) $node->merci_reservation_status, array(
    MERCI_STATUS_CANCELLED,
    MERCI_STATUS_CHECKED_IN,
    MERCI_STATUS_DENIED,
    MERCI_STATUS_NO_SHOW,
  ))) {
    $statuses = merci_record_status();
    form_set_error('merci_reservation_status', t('You cannot change the status to %status for a reservation that has past.', array(
      '%status' => $statuses[$node->merci_reservation_status],
    )));
  }
}