You are here

function merci_validate_merci_reservation_date 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_merci_reservation_date()
1 call to merci_validate_merci_reservation_date()
merci_reservation_node_validate in ./merci.module
Implementation of hook_validate().

File

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

Code

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

  // ****
  // Build date objects we'll need for our different validations.
  // ****
  $start = $node->field_merci_date[LANGUAGE_NONE][0]['value'];
  $end = $node->field_merci_date[LANGUAGE_NONE][0]['value2'];
  $start_object = merci_create_local_date_object($start);
  $end_object = merci_create_local_date_object($end);
  $hours_of_operation = merci_load_hours_of_operation();
  $start_day_of_week = (int) date_format($start_object, 'w');
  $end_day_of_week = (int) date_format($end_object, 'w');
  $start_month_day = date_format($start_object, 'm-d');
  $end_month_day = date_format($end_object, 'm-d');
  $start_hours = $hours_of_operation[$start_day_of_week];
  $end_hours = $hours_of_operation[$end_day_of_week];
  $start_date = date_format($start_object, 'm-d-Y');
  $max_days = variable_get("merci_max_days_advance_reservation", '0');

  // Hours of operation restrictions, max days, and closed dates checks
  // Users in role with Administer MERCI permssion or outside hours of operation skip these checks
  if (user_access('create reservations outside hours of operation')) {
    merci_verbose_logging('SKIP Hours of Operation Check, Max Days Check, and Closed Dates Check because user has create reservations outside hours of operation permission');

    //check to see if warning should be displayed
    if (strtotime(date('G:i', strtotime($start . ' UTC'))) < strtotime($start_hours['open']) || strtotime($start_hours['close']) < strtotime(date('G:i', strtotime($end . ' UTC')))) {
      drupal_set_message('<b>' . t('You are making a Reservation outside the normal hours of operation.  This may impact access to the items you are reserving.') . '</b>');
    }
  }
  else {

    // Reservation start date cannot exceed the max advance
    merci_verbose_logging('CHECKING Max Days');
    if ($max_days) {
      $max_date = new DateTime("+{$max_days} day");

      //$max_date = date('m-d-Y', mktime(0, 0, 0, date("m"), date("d")+$max_days, date("Y")));
      if ($start_object > $max_date) {
        form_set_error('field_merci_date][0][value][date', t('You cannot make a Reservation more than %days days in advance. Start the Reservation before %date.', array(
          '%days' => $max_days,
          '%date' => date_format($max_date, 'm-d-Y'),
        )));
      }
    }

    // Can't start or end a reservation on days that are
    // closed dates.
    merci_verbose_logging('CHECKING Closed Dates');
    if (in_array($start_month_day, $hours_of_operation['closed_days'])) {
      $name = date_format($start_object, 'F jS');
      form_set_error('field_merci_date][0][value][date', t('Sorry, but we are closed on %day for a holiday or special event.', array(
        '%day' => $name,
      )));
    }
    if (in_array($end_month_day, $hours_of_operation['closed_days'])) {
      $name = date_format($end_object, 'F jS');
      form_set_error('field_merci_date][0][value2][date', t('Sorry, but we are closed on %day for a holiday or special event.', array(
        '%day' => $name,
      )));
    }

    // Can't start or end a reservation on a day the facility
    // has no hours of operation, or outside hours of operation.
    merci_verbose_logging('CHECKING Hours of Operation');
    $start_name = date_format($start_object, 'l');
    if (!$hours_of_operation[$start_day_of_week]) {
      form_set_error('field_merci_date][0][value][date', t('Reservations cannot start on a %day.', array(
        '%day' => $start_name,
      )));
    }
    else {
      $start_time = date_format($start_object, 'H:i');
      if ($start_time < $start_hours['open']) {
        form_set_error('field_merci_date][0][value][time', t('Reservations cannot start at a time before %start.', array(
          '%start' => merci_format_time($start_hours['open']),
        )));
      }
      elseif ($start_time > $start_hours['close']) {
        form_set_error('field_merci_date][0][value][time', t('Reservations cannot start at a time after %end.', array(
          '%end' => merci_format_time($start_hours['close']),
        )));
      }
    }
    $end_name = date_format($end_object, 'l');
    if (!$hours_of_operation[$end_day_of_week]) {
      form_set_error('field_merci_date][0][value2][date', t('Reservations cannot end on a %day.', array(
        '%day' => $end_name,
      )));
    }
    else {
      $end_time = date_format($end_object, 'H:i');
      if ($end_time < $end_hours['open']) {
        form_set_error('field_merci_date][0][value2][time', t('Reservations cannot end at a time before %start.', array(
          '%start' => merci_format_time($end_hours['open']),
        )));
      }
      elseif ($end_time > $end_hours['close']) {
        form_set_error('field_merci_date][0][value2][time', t('Reservations cannot end at a time after %end.', array(
          '%end' => merci_format_time($end_hours['close']),
        )));
      }
    }
  }

  // Hours of operation restrictions, max days, and closed dates checks
}