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 / cart / utils.php
vikappointments / site / helpers / libraries / cart Last commit date
cart.php 2 days ago discount.php 2 days ago index.html 2 days ago item.php 2 days ago option.php 2 days ago utils.php 2 days ago
utils.php
242 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 /**
15 * Helper class used to manipulate the items within the cart.
16 *
17 * @since 1.6
18 */
19 abstract class VAPCartUtils
20 {
21 /**
22 * Sorts the items by service and checkin date.
23 *
24 * @param array $items The items to sort.
25 *
26 * @return array The sorted items.
27 *
28 * @uses quicksort()
29 */
30 public static function sortItemsByServiceDate($items)
31 {
32 return self::quicksort($items);
33 }
34
35 /**
36 * Returns the total cost of all the appointments for the given service.
37 *
38 * @param array $items The items list.
39 * @param integer $id The service ID.
40 *
41 * @return float The resulting total cost.
42 */
43 public static function getServiceTotalCost($items, $id)
44 {
45 $price = 0;
46
47 for ($i = 0; $i < count($items); $i++)
48 {
49 if ($items[$i]->getServiceID() == $id)
50 {
51 $price += $items[$i]->getTotalCost();
52 }
53 }
54
55 return $price;
56 }
57
58 /**
59 * Categorizes the items array by service.
60 *
61 * @param array $items The items list.
62 *
63 * @return array The grouped items.
64 *
65 * @since 1.6
66 */
67 public static function groupItemsByService(array $items)
68 {
69 $groups = array();
70
71 foreach ($items as $item)
72 {
73 $id_service = $item->getServiceID();
74
75 if (!isset($groups[$id_service]))
76 {
77 $groups[$id_service] = array(
78 'id' => $id_service,
79 'name' => $item->getName(),
80 'total' => self::getServiceTotalCost($items, $id_service),
81 'items' => array(),
82 );
83 }
84
85 $groups[$id_service]['items'][] = $item;
86 }
87
88 return $groups;
89 }
90
91 /**
92 * Scans the cart items to check whether the booked appointments
93 * have been assigned to the same employee.
94 *
95 * @param array $items A list of items.
96 *
97 * @return boolean True if the same employee, false otherwise.
98 *
99 * @since 1.7
100 */
101 public static function isSameEmployee($items)
102 {
103 if (!$items)
104 {
105 // no specified items
106 return false;
107 }
108
109 // make sure the employee has been selected
110 $same = $items[0]->getEmployeeID() > 0;
111
112 // iterate for all the items length and automatically break when
113 // we find an employee not equals to the first one
114 for ($i = 1; $i < count($items) && $same; $i++)
115 {
116 $same = $items[$i]->getEmployeeID() == $items[0]->getEmployeeID();
117 }
118
119 return $same;
120 }
121
122 /**
123 * Extracts all the employees IDs that have been booked.
124 *
125 * @param array $items A list of items.
126 *
127 * @return array A list of employees.
128 *
129 * @since 1.7
130 */
131 public static function getEmployees($items)
132 {
133 $arr = array();
134
135 foreach ($items as $item)
136 {
137 $id_emp = $item->getEmployeeID();
138
139 // include employee ID only if not yet specified and not empty
140 if ($id_emp > 0 && !in_array($id_emp, $arr))
141 {
142 $arr[] = $id_emp;
143 }
144 }
145
146 return $arr;
147 }
148
149 /**
150 * Extracts all the services IDs that have been booked.
151 *
152 * @param array $items A list of items.
153 *
154 * @return array A list of services.
155 *
156 * @since 1.7
157 */
158 public static function getServices($items)
159 {
160 $arr = array();
161
162 foreach ($items as $item)
163 {
164 // include service ID only if not yet specified
165 if (!in_array($item->getServiceID(), $arr))
166 {
167 $arr[] = $item->getServiceID();
168 }
169 }
170
171 return $arr;
172 }
173
174 /**
175 * Returns the total number of participants. The correct number
176 * will be returned only in case all the appointments within the
177 * cart contains the same number of attendees.
178 *
179 * @param array $items A list of items.
180 *
181 * @return integer The attendees count.
182 *
183 * @since 1.7
184 */
185 public static function getAttendees($items)
186 {
187 if (!$items)
188 {
189 return 1;
190 }
191
192 for ($i = 1; $i < count($items); $i++)
193 {
194 if ($items[$i]->getPeople() != $items[$i - 1]->getPeople())
195 {
196 // at least 2 appointments have different attendees
197 return 1;
198 }
199 }
200
201 // all the appointments share the same number of attendees
202 return $items[0]->getPeople();
203 }
204
205 /**
206 * Sorts the cart items using QuickSort method.
207 *
208 * @param array $items The items to sort.
209 *
210 * @return array The sorted items.
211 *
212 * @since 1.6
213 */
214 private static function quicksort($items)
215 {
216 usort($items, function($a, $b)
217 {
218 if ($a->getServiceID() > $b->getServiceID())
219 {
220 return 1;
221 }
222 else if ($a->getServiceID() < $b->getServiceID())
223 {
224 return -1;
225 }
226
227 if ($a->getCheckinTimestamp() > $b->getCheckinTimestamp())
228 {
229 return 1;
230 }
231 else if ($a->getCheckinTimestamp() < $b->getCheckinTimestamp())
232 {
233 return -1;
234 }
235
236 return 0;
237 });
238
239 return $items;
240 }
241 }
242