| | |
| | | <?php |
| | | namespace JVBase\managers\SEO\render\Traits\_Properties; |
| | | |
| | | use JVBase\base\SchemaHelper; |
| | | use JVBase\managers\SEO\render\Thing\Intangible\StructuredValue\OpeningHoursSpecification; |
| | | use JVBase\managers\SEO\render\Traits\_Helpers\arrayHelper; |
| | | use JVBase\meta\Meta; |
| | | use JVBase\registrar\Fields; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; |
| | | } |
| | | trait openingHoursSpecificationTrait { |
| | | use arrayHelper; |
| | | /** |
| | | * @var OpeningHoursSpecification The opening hours of a certain place. |
| | | * @var OpeningHoursSpecification|array The opening hours of a certain place. |
| | | */ |
| | | protected OpeningHoursSpecification $openingHoursSpecification; |
| | | protected OpeningHoursSpecification|array $openingHoursSpecification; |
| | | |
| | | public function getOpeningHoursSpecification():?OpeningHoursSpecification |
| | | public function getOpeningHoursSpecification():OpeningHoursSpecification|array|null |
| | | { |
| | | return $this->openingHoursSpecification??null; |
| | | } |
| | | public function setOpeningHoursSpecification(OpeningHoursSpecification $openingHoursSpecification):void |
| | | public function setOpeningHoursSpecification(OpeningHoursSpecification|array $openingHoursSpecification):void |
| | | { |
| | | if (is_array($openingHoursSpecification)) { |
| | | if (array_key_exists('dayOfWeek', $openingHoursSpecification)) { |
| | | if (!array_key_exists('type', $openingHoursSpecification)) { |
| | | $openingHoursSpecification['type'] = 'JVBase\managers\SEO\render\Thing\Intangible\StructuredValue\OpeningHoursSpecification'; |
| | | } |
| | | $openingHoursSpecification = SchemaHelper::classFromConfig($openingHoursSpecification); |
| | | } else { |
| | | $out = []; |
| | | foreach ($openingHoursSpecification as $hours) { |
| | | if (!array_key_exists('type', $hours)){ |
| | | $hours['type'] = 'JVBase\managers\SEO\render\Thing\Intangible\StructuredValue\OpeningHoursSpecification'; |
| | | } |
| | | $out[] = SchemaHelper::classFromConfig($hours); |
| | | } |
| | | $openingHoursSpecification = $out; |
| | | } |
| | | } |
| | | $this->openingHoursSpecification = $openingHoursSpecification; |
| | | } |
| | | |
| | | public function getOpeningHoursSpecificationFieldConfig():array |
| | | public function setOpeningHoursSpecificationField(Fields $fields):void |
| | | { |
| | | return [ |
| | | $fields->addField('openingHours', [ |
| | | 'type' => 'repeater', |
| | | 'label' => 'Opening Hours', |
| | | 'label' => __('Opening Hours', 'jvb'), |
| | | 'fields' => [ |
| | | 'dayOfWeek' => [ |
| | | 'type' => 'radio', |
| | | 'label' => 'Day(s) of Week', |
| | | 'type' => 'set', |
| | | 'label' => __('Day(s) of Week', 'jvb'), |
| | | 'options' => [ |
| | | 'Mo' => 'Monday', |
| | | 'Tu' => 'Tuesday', |
| | |
| | | 'Fr' => 'Friday', |
| | | 'Sa' => 'Saturday', |
| | | 'Su' => 'Sunday' |
| | | ] |
| | | ], |
| | | 'required' => true |
| | | ], |
| | | 'opens' => [ |
| | | 'type' => 'time', |
| | | 'label' => 'Opens At', |
| | | 'label' => __('Opens at', 'jvb'), |
| | | 'required' => true |
| | | ], |
| | | 'closes' => [ |
| | | 'type' => 'time', |
| | | 'label' => 'Closes At', |
| | | 'label' => __('Closes at', 'jvb'), |
| | | 'required' => true |
| | | ] |
| | | ] |
| | | ]; |
| | | ]); |
| | | $fields->addField('by_appointment', [ |
| | | 'type' => 'true_false', |
| | | 'label' => __('By Appointment Only', 'jvb'), |
| | | ]); |
| | | $fields->addField('allow_walkins', [ |
| | | 'type' => 'true_false', |
| | | 'label' => __('Walk Ins Welcome', 'jvb') |
| | | ]); |
| | | } |
| | | public function formatOpeningHoursSpecificationField(Meta $meta):void |
| | | { |
| | | $openingHours = $meta->get('openingHours'); |
| | | |
| | | if (!empty($openingHours)) { |
| | | $used = [ |
| | | 'Mo' => false, |
| | | 'Tu' => false, |
| | | 'We' => false, |
| | | 'Th' => false, |
| | | 'Fr' => false, |
| | | 'Sa' => false, |
| | | 'Su' => false, |
| | | ]; |
| | | $hours = []; |
| | | foreach ($openingHours as $row) { |
| | | $days = array_filter(explode(',', $row['dayOfWeek']), |
| | | function ($d) use ($used) { |
| | | if ($used[$d] === false) { |
| | | $used[$d] = true; |
| | | return true; |
| | | } |
| | | return false; |
| | | }); |
| | | if (empty($days)) { |
| | | continue; |
| | | } |
| | | $opens = new OpeningHoursSpecification(); |
| | | $opens->setDayOfWeek($days); |
| | | $opens->setOpens($row['opens']); |
| | | $opens->setCloses($row['closes']); |
| | | } |
| | | if (!empty($hours)){ |
| | | $this->setOpeningHoursSpecification($hours); |
| | | } |
| | | } |
| | | } |
| | | } |