View source
<?php
class MerciHours extends Entity {
const SUNDAY = 1;
const MONDAY = 2;
const TUESDAY = 4;
const WEDNESDAY = 8;
const THURSDAY = 16;
const FRIDAY = 32;
const SATURDAY = 64;
public $name;
public $label;
public $description;
public $week = 0;
public $week_days;
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;
}
}
}
function isLocked() {
return isset($this->status) && empty($this->is_new) && ($this->status & ENTITY_IN_CODE || $this->status & ENTITY_FIXED);
}
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;
}
private static function dayCodes() {
static $codes;
if (empty($codes)) {
foreach (date_week_days_untranslated() as $name) {
$codes[$name] = constant('self::' . strtoupper($name));
}
}
return $codes;
}
private static function weekDayName($date) {
$week_day = date_day_of_week($date);
$names = date_week_days_untranslated();
return $names[$week_day];
}
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,
);
}
private static function weekDaysDates($start, $end, $week_days) {
$byday = array();
$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));
}
$rrule = 'RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=' . implode(',', $byday) . ';';
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);
foreach ($dates as $key => $date) {
$dates[$key] = substr($date, 0, 10);
}
$week_day = self::weekDayName($dates[0]);
if (!in_array($week_day, $week_days)) {
array_shift($dates);
}
return $dates;
}
function getOpenDaysInPeriod($start, $end) {
$week_days = $this->week_days;
$dates = self::weekDaysDates($start, $end, $week_days);
return $dates;
}
function getOpenDays($year, $month = NULL) {
list($start, $end) = self::periodDates($year, $month);
return $this
->getOpenDaysInPeriod($start, $end);
}
function getClosedDaysInPeriod($start, $end) {
$week_days = array_diff(date_week_days_untranslated(), $this->week_days);
$dates = self::weekDaysDates($start, $end, $week_days);
return $dates;
}
function getClosedDays($year, $month = NULL) {
list($start, $end) = self::periodDates($year, $month);
return $this
->getClosedDaysInPeriod($start, $end);
}
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);
}
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);
}
}