PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / calendar / wrapper.php
vikappointments / site / helpers / libraries / calendar Last commit date
index.html 2 days ago rect.php 2 days ago wrapper.php 2 days ago
wrapper.php
206 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 VAPLoader::import('libraries.calendar.rect');
15
16 /**
17 * Class used to wrap a list of calendar appointments.
18 *
19 * @since 1.6
20 * @see CalendarRect
21 */
22 class CalendarWrapper
23 {
24 /**
25 * A list of appointments.
26 *
27 * @var CalendarRect[]
28 */
29 protected $list;
30
31 /**
32 * Class constructor.
33 */
34 public function __construct()
35 {
36 $this->list = array();
37 }
38
39 /**
40 * Pushes a new appointment within the internal list.
41 *
42 * @param CalendarRect $app The appointment to push.
43 *
44 * @return self This object to support chaining.
45 */
46 public function push(CalendarRect $app)
47 {
48 $this->list[] = $app;
49
50 return $this;
51 }
52
53 /**
54 * Checks if there are reservations within the calendar.
55 *
56 * @return boolean True if any, false otherwise.
57 */
58 public function has()
59 {
60 return count($this->list);
61 }
62
63 /**
64 * Returns the full events list.
65 *
66 * @return array
67 */
68 public function getEventsList()
69 {
70 $events = array();
71
72 foreach ($this->list as $app)
73 {
74 $events = array_merge($events, $app->events());
75 }
76
77 return $events;
78 }
79
80 /**
81 * Returns the minimum hour found. In case there are no appointments
82 * the default value will be returned.
83 *
84 * @param integer $def The default opening hour.
85 *
86 * @return integer The minimum hour.
87 */
88 public function getMinimumHour($def)
89 {
90 if (!$this->has())
91 {
92 // no appointments found
93 return $def;
94 }
95
96 $min = null;
97
98 foreach ($this->list as $rect)
99 {
100 if (!$rect->isSameDay())
101 {
102 // we can stop immediately as we have an
103 // appointment that started in the previous day
104 return 0;
105 }
106 else if (is_null($min))
107 {
108 $min = $rect->startH();
109 }
110 else
111 {
112 $min = min(array($rect->startH(), $min));
113 }
114 }
115
116 return $min;
117 }
118
119 /**
120 * Returns the maximum hour found. In case there are no appointments
121 * the default value will be returned.
122 *
123 * @param integer $def The default closing hour.
124 *
125 * @return integer The maximum hour.
126 *
127 * @since 1.7
128 */
129 public function getMaximumHour($def)
130 {
131 if (!$this->has())
132 {
133 // no appointments found
134 return $def;
135 }
136
137 $max = null;
138
139 foreach ($this->list as $rect)
140 {
141 if (!$rect->isSameDay())
142 {
143 // we can stop immediately as we have an
144 // appointment that started in the previous day
145 return 23;
146 }
147 else if (is_null($max))
148 {
149 $max = $rect->endH();
150 }
151 else
152 {
153 $max = max(array($rect->endH(), $max));
154 }
155 }
156
157 return $max;
158 }
159
160 /**
161 * Returns the appointment that intersects the given bounds.
162 *
163 * @param integer $start The start delimiter.
164 * @param integer $end The end delimiter.
165 *
166 * @return mixed The appointment in case there is an intersection, false otherwise.
167 *
168 * @uses getIntersections()
169 */
170 public function getIntersection($start, $end)
171 {
172 $tmp = $this->getIntersections($start, $end);
173
174 if ($tmp)
175 {
176 return $tmp[0];
177 }
178
179 return false;
180 }
181
182 /**
183 * Returns a list of appointments that intersect the given bounds.
184 *
185 * @param integer $start The start delimiter.
186 * @param integer $end The end delimiter.
187 *
188 * @return array The appointments that intersect the bounds.
189 */
190 public function getIntersections($start, $end)
191 {
192 $tmp = array();
193
194 foreach ($this->list as $i => $app)
195 {
196 if ($app->intersects($start, $end))
197 {
198 $tmp[] = $this->list[$i];
199 }
200 }
201
202 // return all intersections found
203 return $tmp;
204 }
205 }
206