You are here

class MerciHours in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

Define a WorkCalendar object with its operations.

Hierarchy

Expanded class hierarchy of MerciHours

1 string reference to 'MerciHours'
merci_hours_entity_info in merci_hours/merci_hours.module
Implements hook_entity_info().

File

merci_hours/includes/entity.inc, line 6

View source
class MerciHours extends Entity {

  /**
   * @defgroup wc_week_days_constants Constants to represent week days
   * @{
   * Constants that represent bit numbers to compound the week byte.
   *
   * The week byte stores which week days are working days in the calendar.
   */
  const SUNDAY = 1;

  // 00000001
  const MONDAY = 2;

  // 00000010
  const TUESDAY = 4;

  // 00000100
  const WEDNESDAY = 8;

  // 00001000
  const THURSDAY = 16;

  // 00010000
  const FRIDAY = 32;

  // 00100000
  const SATURDAY = 64;

  // 01000000

  /**
   * @} End of "defgroup wc_week_days_constants".
   */
  public $name;
  public $label;
  public $description;
  public $week = 0;
  public $week_days;

  /**
   * Initialize object attributes and decode `week`.
   */
  function __construct(array $values = array(), $entityType = 'merci_hours') {
    parent::__construct($values, $entityType);
    $this->week_days = array();
    foreach ($this
      ->dayCodes() as $name => $code) {
      if ($this->week & $code) {
        $this->week_days[$name] = $name;
      }
    }
  }

  /**
   * Returns whether the entity is locked, thus may not be deleted or renamed.
   *
   * Entities provided in code are automatically treated as locked, as well
   * as any fixed profile type.
   */
  function isLocked() {
    return isset($this->status) && empty($this->is_new) && ($this->status & ENTITY_IN_CODE || $this->status & ENTITY_FIXED);
  }

  /**
   * Encodes given week days into week byte.
   */
  function updateWeek($week_days) {
    $codes = $this
      ->dayCodes();
    $this->week = 0;
    foreach ($week_days as $name) {
      if (!empty($name)) {
        $this->week = $this->week | $codes[$name];
      }
    }
    $this->week_days = $week_days;
  }

  /**
   * Map week day names to our internal bits.
   *
   */
  private static function dayCodes() {
    static $codes;
    if (empty($codes)) {
      foreach (date_week_days_untranslated() as $name) {
        $codes[$name] = constant('self::' . strtoupper($name));
      }
    }
    return $codes;
  }

  /**
   * Returns the untranslated week day name for the requested date.
   *
   * @param $date DateObject or date string.
   */
  private static function weekDayName($date) {
    $week_day = date_day_of_week($date);
    $names = date_week_days_untranslated();
    return $names[$week_day];
  }

  /**
   * Returns start and end date for a year or month period.
   */
  private static function periodDates($year, $month = NULL) {
    if (is_null($month)) {
      $start = "{$year}-01-01 00:00:00";
      $end = "{$year}-12-31 00:00:00";
    }
    else {
      $start = "{$year}-{$month}-01 00:00:00";
      $days = date_days_in_month($year, $month);
      $end = "{$year}-{$month}-{$days} 00:00:00";
    }
    return array(
      $start,
      $end,
    );
  }

  /**
   * Returns dates in a period corresponding to requested week days.
   *
   * Internally it builds a rrule to do the job.
   */
  private static function weekDaysDates($start, $end, $week_days) {

    // Transform $week_days to a rrule "byday" string. Example: SU,MO,TU,WE.
    // date_repeat api wants the byday array to start by the week day of $start.
    $byday = array();

    // Convert $week_days to a key based array and find the index of the week
    // day of start in order to assign bydays starting from this index.
    $week_days = array_values($week_days);
    $index = array_search(self::weekDayName($start), $week_days);
    $count = count($week_days);
    for ($offset = 0; $offset < $count; $offset++) {
      $wday = $week_days[($index + $offset) % $count];
      $byday[] = strtoupper(substr($wday, 0, 2));
    }

    // Request date_repeat to run the rrule.
    $rrule = 'RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=' . implode(',', $byday) . ';';

    // Don't require date_repeat module to be enabled. Just use its api.
    module_load_include('module', 'date_repeat', 'date_repeat');
    module_load_include('inc', 'date_repeat', 'date_repeat_calc');
    $dates = date_repeat_calc($rrule, (string) $start, (string) $end);

    // Strip hours from dates.
    foreach ($dates as $key => $date) {
      $dates[$key] = substr($date, 0, 10);
    }

    // Special case. First day is always included in despite of the rrule.
    // We need to exclude it by hand if neccesary.
    $week_day = self::weekDayName($dates[0]);
    if (!in_array($week_day, $week_days)) {
      array_shift($dates);
    }
    return $dates;
  }

  /**
   * Returns a list of opening days for the requested period.
   */
  function getOpenDaysInPeriod($start, $end) {
    $week_days = $this->week_days;
    $dates = self::weekDaysDates($start, $end, $week_days);
    return $dates;
  }

  /**
   * Returns a list of opening days for the requested year or month.
   */
  function getOpenDays($year, $month = NULL) {
    list($start, $end) = self::periodDates($year, $month);
    return $this
      ->getOpenDaysInPeriod($start, $end);
  }

  /**
   * Returns a list of closed days for the requested period.
   */
  function getClosedDaysInPeriod($start, $end) {
    $week_days = array_diff(date_week_days_untranslated(), $this->week_days);
    $dates = self::weekDaysDates($start, $end, $week_days);
    return $dates;
  }

  /**
   * Returns a list of closed days for the requested year or month.
   */
  function getClosedDays($year, $month = NULL) {
    list($start, $end) = self::periodDates($year, $month);
    return $this
      ->getClosedDaysInPeriod($start, $end);
  }

  /**
   * Returns boolean indicating wether the requested day the business is open.
   */
  function isOpenDay($year, $month, $day) {
    $date = new DateObject("{$year}-{$month}-{$day} 00:00:00");
    $week_day = self::weekDayName($date);
    return in_array($week_day, $this->week_days);
  }

  /**
   * Returns boolean indicating wether the requested day the business is closed.
   */
  function isClosedDay($year, $month, $day) {
    $date = new DateObject("{$year}-{$month}-{$day} 00:00:00");
    $week_day = self::weekDayName($date);
    return !in_array($week_day, $this->week_days);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::$wrapper protected property
Entity::buildContent public function Builds a structured array representing the entity's content. Overrides EntityInterface::buildContent 1
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. 1
Entity::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info().
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::save public function Permanently saves the entity. Overrides EntityInterface::save
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.
MerciHours::$description public property
MerciHours::$label public property
MerciHours::$name public property End of "defgroup wc_week_days_constants".
MerciHours::$week public property
MerciHours::$week_days public property
MerciHours::dayCodes private static function Map week day names to our internal bits.
MerciHours::FRIDAY constant
MerciHours::getClosedDays function Returns a list of closed days for the requested year or month.
MerciHours::getClosedDaysInPeriod function Returns a list of closed days for the requested period.
MerciHours::getOpenDays function Returns a list of opening days for the requested year or month.
MerciHours::getOpenDaysInPeriod function Returns a list of opening days for the requested period.
MerciHours::isClosedDay function Returns boolean indicating wether the requested day the business is closed.
MerciHours::isLocked function Returns whether the entity is locked, thus may not be deleted or renamed.
MerciHours::isOpenDay function Returns boolean indicating wether the requested day the business is open.
MerciHours::MONDAY constant
MerciHours::periodDates private static function Returns start and end date for a year or month period.
MerciHours::SATURDAY constant
MerciHours::SUNDAY constant
MerciHours::THURSDAY constant
MerciHours::TUESDAY constant
MerciHours::updateWeek function Encodes given week days into week byte.
MerciHours::WEDNESDAY constant
MerciHours::weekDayName private static function Returns the untranslated week day name for the requested date.
MerciHours::weekDaysDates private static function Returns dates in a period corresponding to requested week days.
MerciHours::__construct function Initialize object attributes and decode `week`. Overrides Entity::__construct