OfficeHoursItemList.php in Office Hours 8
File
src/Plugin/Field/FieldType/OfficeHoursItemList.php
View source
<?php
namespace Drupal\office_hours\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldItemList;
use Drupal\office_hours\Element\OfficeHoursDatetime;
use Drupal\office_hours\OfficeHoursFormatterTrait;
class OfficeHoursItemList extends FieldItemList implements OfficeHoursItemListInterface {
use OfficeHoursFormatterTrait {
getRows as getFieldRows;
}
public function getRows(array $settings, array $field_settings, $time = NULL) {
return $this
->getFieldRows($this
->getValue(), $settings, $field_settings, $time);
}
public function isOpen($time = NULL) {
$time = $time === NULL ? \Drupal::time()
->getRequestTime() : $time;
$today = (int) idate('w', $time);
$now = date('Hi', $time);
$is_open = FALSE;
foreach ($this
->getValue() as $key => $item) {
$day = (int) $item['day'];
$start = OfficeHoursDatetime::get($item['starthours'], 'Hi');
$end = OfficeHoursDatetime::get($item['endhours'], 'Hi');
if ($day - $today == -1 || $day - $today == 6 || $day == strtotime('yesterday midnight')) {
if ($start >= $end && $end > $now) {
$is_open = TRUE;
}
}
elseif ($day == $today || $day == strtotime('today midnight')) {
if ($start <= $now) {
if ($start > $end || $start == $end && !is_null($start) || $start < $end && $end > $now) {
$is_open = TRUE;
}
}
}
}
return $is_open;
}
}