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 / models / calendarweek.php
vikappointments / site / models Last commit date
allorders.php 1 day ago calendarweek.php 1 day ago cart.php 1 day ago confirmapp.php 1 day ago empaccountstat.php 1 day ago empattachser.php 1 day ago empcoupons.php 1 day ago empcustfields.php 1 day ago empeditcoupon.php 1 day ago empeditcustfield.php 1 day ago empeditlocation.php 1 day ago empeditpay.php 1 day ago empeditprofile.php 1 day ago empeditservice.php 1 day ago empeditwdays.php 1 day ago emplocations.php 1 day ago emplocwdays.php 1 day ago emplogin.php 1 day ago employeesearch.php 1 day ago employeeslist.php 1 day ago empmanres.php 1 day ago emppaylist.php 1 day ago empserviceslist.php 1 day ago empsettingsman.php 1 day ago empsubscrcart.php 1 day ago empsubscrhistory.php 1 day ago empsubscrorder.php 1 day ago empwdays.php 1 day ago index.html 1 day ago packages.php 1 day ago packagescart.php 1 day ago packagesconfirm.php 1 day ago packorders.php 1 day ago servicesearch.php 1 day ago serviceslist.php 1 day ago subscrcart.php 1 day ago subscrhistory.php 1 day ago subscrpayment.php 1 day ago
calendarweek.php
220 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.mvc.model');
15 VAPLoader::import('libraries.availability.manager');
16 VAPLoader::import('libraries.availability.timeline.factory');
17
18 /**
19 * VikAppointments weekly calendar generator model.
20 *
21 * @since 1.7
22 */
23 class VikAppointmentsModelCalendarweek extends JModelVAP
24 {
25 /**
26 * Returns the structure of the calendar to display
27 * as tables of months.
28 *
29 * @param array $options An array of options.
30 *
31 * @return object The resulting calendar.
32 */
33 public function getCalendar(array $options = array())
34 {
35 $config = VAPFactory::getConfig();
36
37 if (empty($options['numdays']))
38 {
39 // use default amount specified from the configuration
40 $options['numdays'] = $config->getUint('calendarweekdays', 7);
41 }
42
43 $now = JFactory::getDate()->format('Y-m-d');
44
45 if (empty($options['start']) || $options['start'] < $now)
46 {
47 // start from current date
48 $options['start'] = $now;
49 }
50
51 // get service details
52 $assocModel = JModelVAP::getInstance('serempassoc');
53 $service = $assocModel->getOverrides($options['id_ser'], $options['id_emp']);
54
55 // fetch employee/system timezone
56 $emp_tz = JModelVAP::getInstance('employee')->getTimezone($options['id_emp']);
57
58 $prevThreshold = $now;
59
60 if (!VAPDateHelper::isNull($service->start_publishing))
61 {
62 // adjust start publishing to given timezone
63 $startPub = JFactory::getDate($service->start_publishing);
64 $startPub->setTimezone(new DateTimeZone($emp_tz));
65
66 $start_publishing = $startPub->format('Y-m-d', $local = true);
67
68 // compare service start publishing with start date
69 if ($start_publishing > $options['start'])
70 {
71 // use service start publishing
72 $options['start'] = $start_publishing;
73 }
74
75 if ($start_publishing > $now)
76 {
77 // use the start publishing as minimum date
78 $prevThreshold = $start_publishing;
79 }
80 }
81
82 if (!isset($options['admin']))
83 {
84 // flag as customer when not specified
85 $options['admin'] = false;
86 }
87
88 // set initial date at midnight
89 $date = JFactory::getDate($options['start']);
90 $date->modify('00:00:00');
91
92 // create availability search object
93 $search = VAPAvailabilityManager::getInstance($options['id_ser'], $options['id_emp'], $options);
94
95 $calendar = new stdClass;
96
97 // fetch head
98 $calendar->days = array();
99
100 $calendar->prev = clone $date;
101 $calendar->prev->modify('-' . $options['numdays'] . ' days');
102 $calendar->prev = $calendar->prev->format('Y-m-d');
103
104 if ($calendar->prev < $prevThreshold)
105 {
106 if ($date->format('Y-m-d') <= $prevThreshold)
107 {
108 // date in the past, cannot go behind
109 $calendar->prev = null;
110 }
111 else
112 {
113 $calendar->prev = $prevThreshold;
114 }
115 }
116
117 // get employee search model to generate the timeline
118 $empSearchModel = JModelVAP::getInstance('employeesearch');
119
120 for ($i = 0; $i < $options['numdays']; $i++)
121 {
122 $day = new stdClass;
123 // get the day of the week that should stay at this position
124 $day->wday = (int) $date->format('w');
125 $day->day = (int) $date->format('d');
126 $day->mon = (int) $date->format('m');
127 $day->year = (int) $date->format('Y');
128 $day->date = $date->format('Y-m-d');
129
130 $day->week = new stdClass;
131 $day->week->name = new stdClass;
132 $day->week->name->long = $date->dayToString($day->wday, false);
133 $day->week->name->short = $date->dayToString($day->wday, true);
134
135 $day->month = new stdClass;
136 $day->month->name = new stdClass;
137 $day->month->name->long = $date->monthToString($day->mon, $abbr = false);
138 $day->month->name->short = $date->monthToString($day->mon, $abbr = true);
139
140 // calculate the availability of this day
141 $avail = $this->checkDayStatus($day->date, $search);
142
143 // set open/closed status
144 $day->closed = !$avail;
145 // register availability flag
146 $day->open = (bool) $avail;
147
148 if ($day->open)
149 {
150 // prepare timeline options
151 $timelineOptions = $options;
152 $timelineOptions['date'] = $day->date;
153
154 // get timeline details
155 $day->timeline = $empSearchModel->getTimeline($timelineOptions);
156
157 if (!$day->timeline)
158 {
159 // register fetched error
160 $day->timelineError = $empSearchModel->getError($last = null, $string = true);
161 }
162 }
163 else
164 {
165 // day closed or expired, use an empty timeline
166 $day->timeline = array();
167 }
168
169 $calendar->days[] = $day;
170
171 $date->modify('+1 day');
172 }
173
174 $calendar->next = $date->format('Y-m-d');
175
176 if (!VAPDateHelper::isNull($service->end_publishing))
177 {
178 // adjust end publishing to given timezone
179 $endPub = JFactory::getDate($service->end_publishing);
180 $endPub->setTimezone(new DateTimeZone($emp_tz));
181
182 $end_publishing = $endPub->format('Y-m-d', $local = true);
183
184 if ($calendar->next > $end_publishing)
185 {
186 // already reached the maximum threshold
187 $calendar->next = null;
188 }
189 }
190
191 return $calendar;
192 }
193
194 /**
195 * Checks whether the selected date is closed or open.
196 *
197 * @param string $date The UTC date string (military format).
198 * @param mixed $search An availability search instance.
199 *
200 * @return boolean False if closed, true if open.
201 */
202 public function checkDayStatus($date, $search)
203 {
204 if (!$search->isAdmin())
205 {
206 // make sure the date is not in the past if we
207 // are checking the availability as customers
208 if ($search->isPastTime($date))
209 {
210 // date in the past
211 return false;
212 }
213 }
214
215 // check whether there's at least an employee offering
216 // the given service on the specified day
217 return (bool) $search->isDayOpen($date);
218 }
219 }
220