You are here

function merci_check_content_type_restrictions in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6

Same name and namespace in other branches
  1. 6.2 includes/api.inc \merci_check_content_type_restrictions()
  2. 7.2 includes/api.inc \merci_check_content_type_restrictions()
2 calls to merci_check_content_type_restrictions()
merci_build_reservable_items in ./merci.module
Builds the list of all currently reservable items, filtered by date.
merci_node_validate in ./merci.module
Implementation of hook_validate().

File

./merci.module, line 1125
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_check_content_type_restrictions($content_type, $start, $end) {

  // Users in role with administer MERCI or create reservations outside hours of operation permssion
  // are exempt from content type restriction for max hours and the checkout's hours of operation
  if (user_access('administer MERCI') || user_access('create reservations outside hours of operation')) {

    //drupal_set_message(t('You are making a Reservation outside the normal hours of operation.  This may impact access to the items you are reserving.'));
  }
  else {

    //DIFF?
    $type_settings = merci_content_type_rules($content_type);
    $hours_of_operation = merci_load_hours_of_operation($content_type);
    $return = array();

    // Convert start/end dates to local time.
    $start_object = merci_create_local_date_object($start);
    $end_object = merci_create_local_date_object($end);

    // Convert start/end dates to local time.
    $start_object = merci_create_local_date_object($start);
    $end_object = merci_create_local_date_object($end);

    // We want these timestamps generated in UTC.
    $old_timezone = date_default_timezone_get();
    date_default_timezone_set('UTC');
    $start_timestamp = strtotime($start);
    $end_timestamp = strtotime($end);
    date_default_timezone_set($old_timezone);
    $reserved_hours = ($end_timestamp - $start_timestamp) / (60 * 60);
    $start_day_of_week = date_format($start_object, 'w');
    $end_day_of_week = date_format($end_object, 'w');

    // Make sure max hours aren't exceeded.
    if ($type_settings->max_hours_per_reservation && $reserved_hours > $type_settings->max_hours_per_reservation) {
      $return[] = t('%name cannot be reserved for more than %hours hours.', array(
        '%hours' => $type_settings->max_hours_per_reservation,
      ));
    }

    // Validate allow_overnight.
    if (!$type_settings->allow_overnight) {

      // Need the 48 hour check in case somebody starts and ends their
      // reservation on the same day.
      if ($start_day_of_week != $end_day_of_week || $reserved_hours > 48) {
        $return[] = t('%name cannot be reserved overnight.');
      }
    }

    // Validate allow_weekend.
    if (!$type_settings->allow_weekends) {
      $on_weekend = FALSE;

      // Check the start and end dates for the reservation first.
      if (in_array($start_day_of_week, array(
        '6',
        '0',
      )) || in_array($end_day_of_week, array(
        '6',
        '0',
      ))) {
        $on_weekend = TRUE;
      }

      // Check all dates between the start and end dates for the reservation next.
      if (!$on_weekend) {
        $day = 60 * 60 * 24;
        $counter = $start_timestamp + $day;
        while ($counter <= $end_timestamp) {
          $utc_datetime = gmdate('Y-m-d H:i:s', $counter);
          $local_date = merci_create_local_date_object($utc_datetime);
          $day_of_week = date_format($local_date, 'w');
          if (in_array($day_of_week, array(
            '6',
            '0',
          ))) {
            $on_weekend = TRUE;
            break;
          }
          $counter += $day;
        }
      }
      if ($on_weekend) {
        $return[] = t('%name cannot be reserved on weekends.');
      }
    }
  }
  return $return;
}