You are here

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

Same name and namespace in other branches
  1. 6.2 includes/api.inc \merci_check_content_type_restrictions()
  2. 6 merci.module \merci_check_content_type_restrictions()

Checks for reservation restrictions for a content type.

These include maximum hours per reservation, and if the bucket/resource is reservable overnight and/or on weekends.

Parameters

$content_type: The content type to be checked.

$start: The start date of the reservation in DATETIME format and UTC timezone.

$end: The end date of the reservation in DATETIME format and UTC timezone.

Return value

An array of warning messages for any restrictions found.

2 calls to merci_check_content_type_restrictions()
merci_build_reservable_items in includes/api.inc
Builds the list of all currently reservable items, filtered by date.
merci_validate_merci_selected_items in includes/api.inc
@file MERCI - Managed Equipment Reservation Checkout and Inventory

File

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

Code

function merci_check_content_type_restrictions($content_type, $start, $end, $title = '') {
  if (!user_access("manage reservations")) {

    //TODO I don't like this.
    $type_settings = merci_load_item_settings($content_type);
    $return = array();

    // Convert start/end dates to local time.
    // TODO clean this up.
    $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->merci_max_hours_per_reservation && $reserved_hours > $type_settings->merci_max_hours_per_reservation) {

      // Override max_hours_per_reservation if we can reserve this over the weekend
      // Validate allow_weekend.
      if (user_access('override max hours over closed days') || $type_settings->merci_allow_weekends) {
        $closed_days = array();

        // Do we allow extending this reservation over days checked as a weekend in addition to days we are closed?
        if ($type_settings->merci_allow_weekends) {
          $i = 0;
          foreach (array(
            'sunday',
            'monday',
            'tuesday',
            'wednesday',
            'thursday',
            'friday',
            'saturday',
          ) as $day) {
            if (variable_get('merci_' . $day . '_is_weekend', 0)) {
              $closed_days[$i] = TRUE;
            }
            $i++;
          }
        }

        // Do we allow extending a reservtion over days we are closed?
        if (user_access('override max hours over closed days')) {
          $hours_of_operation = merci_load_hours_of_operation($content_type);
          for ($i = 1; $i <= 6; $i++) {
            if (empty($hours_of_operation[$i])) {
              $closed_days[$i] = TRUE;
            }
          }
        }

        // Only extend if the following day is closed/weekend.
        // TODO check that the end time is not the same day and within the hours of being open.
        if (!array_key_exists(date('w', $start_timestamp + 86400), $closed_days)) {
          $return[] = t('%name cannot be reserved for more than %hours hours.', array(
            '%name' => $title,
            '%hours' => $type_settings->merci_max_hours_per_reservation,
          ));
        }

        // Only extend the max time if the default max time falls on a weekend.
        if (array_key_exists(date('w', $start_timestamp + $type_settings->merci_max_hours_per_reservation * 60 * 60), $closed_days)) {

          //Find the next day we are open.

          //  Keep adding 24 hours to start_time until we find our next opened day.
          for ($i = 1; $i <= 6; $i++) {

            // Are we at a day which is not closed.
            if (!array_key_exists(date('w', $start_timestamp + $i * 86400), $closed_days)) {

              // Does the end_day fall here?
              // TODO force time to be exactly when open.
              if ($end_day_of_week != date('w', $start_timestamp + $i * 86400)) {
                $return[] = t('%name cannot be reserved more then one day after a weekend.', array(
                  '%name' => $title,
                  '%hours' => $type_settings->merci_max_hours_per_reservation,
                ));
              }
              break;
            }
          }
        }
      }
      else {
        $return[] = t('%name cannot be reserved for more than %hours hours.', array(
          '%name' => $title,
          '%hours' => $type_settings->merci_max_hours_per_reservation,
        ));
      }
    }

    // Validate allow_overnight.
    if (!$type_settings->merci_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.', array(
          '%name' => $title,
        ));
      }
    }
  }
  return isset($return) ? $return : NULL;
}