PluginProbe
ShiftController Employee Shift Scheduling / 4.9.95
ShiftController Employee Shift Scheduling v4.9.95
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / sh4 / shifts / query.php

query.php in ShiftController Employee Shift Scheduling 4.9.95, at sh4/shifts/query.php

333 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 interface SH4_Shifts_IQuery
3 {
4 public function setStart( $dateTime );
5 public function setEnd( $dateTime );
6 public function find();
7 public function findById( $id );
8 public function findManyById( array $ids );
9 public function findAllByEmployee( SH4_Employees_Model $employee );
10 public function findAllByCalendar( SH4_Calendars_Model $calendar );
11 }
12
13 class SH4_Shifts_Query implements SH4_Shifts_IQuery
14 {
15 protected $start = 197001010000;
16 protected $end = 230001010000;
17 protected $calendars = array();
18 protected $employees = array();
19
20 protected $_loadedStart = NULL;
21 protected $_loadedEnd = NULL;
22 protected $_storage = array();
23
24 public $self, $t, $crud, $calendarsQuery, $employeesQuery;
25
26 public function __construct(
27 HC3_Time $t,
28 HC3_CrudFactory $crudFactory,
29 SH4_Calendars_Query $calendarsQuery,
30 SH4_Employees_Query $employeesQuery,
31 HC3_Hooks $hooks
32 )
33 {
34 // echo "CONTSTRUCTING!";
35 $this->t = $t;
36 $this->self = $hooks->wrap( $this );
37
38 $this->crud = $crudFactory->make('shift')
39 ->withMeta()
40 ;
41
42 $this->calendarsQuery = $calendarsQuery;
43 $this->employeesQuery = $employeesQuery;
44 }
45
46 public function setStart( $dateTime )
47 {
48 $this->start = $dateTime;
49 return $this;
50 }
51
52 public function setEnd( $dateTime )
53 {
54 $this->end = $dateTime;
55 return $this;
56 }
57
58 public function getStart()
59 {
60 return $this->start;
61 }
62
63 public function getEnd()
64 {
65 return $this->end;
66 }
67
68 public function keep( SH4_Shifts_Model $model )
69 {
70 $id = $model->getId();
71 if( $id ){
72 $this->_storage[$id] = $model;
73 }
74
75 return $this;
76 }
77
78 public function findById( $id )
79 {
80 $return = NULL;
81
82 if( is_array($id) && isset($id['id']) ){
83 $id = $id['id'];
84 }
85
86 if( array_key_exists($id, $this->_storage) ){
87 $return = $this->_storage[$id];
88 return $return;
89 }
90
91 $args = array();
92 $args[] = array('id', '=', $id);
93 $args[] = array('limit', 1);
94
95 if( $array = $this->self->read( $args ) ){
96 $return = array_shift( $array );
97 $this->_storage[$id] = $return;
98 }
99
100 return $return;
101 }
102
103 public function findManyById( array $ids )
104 {
105 $return = array();
106 $notOnCache = FALSE;
107 reset( $ids );
108 foreach( $ids as $id ){
109 if( ! array_key_exists($id, $this->_storage) ){
110 $return = NULL;
111 break;
112 }
113 $return[ $id ] = $this->_storage[$id];
114 }
115
116 if( $return ){
117 return $return;
118 }
119
120 $args = array();
121 $args[] = array('id', 'IN', $ids);
122
123 $return = $this->self->read( $args );
124 return $return;
125 }
126
127 public function find()
128 {
129 $this->_load();
130
131 $return = array();
132 foreach( $this->_storage as $shift ){
133 if( $this->end && $shift->getStart() >= $this->end ){
134 continue;
135 }
136
137 if( $this->start && $shift->getEnd() <= $this->start ){
138 continue;
139 }
140
141 $return[ $shift->getId() ] = $shift;
142 }
143
144 return $return;
145 }
146
147 protected function _load()
148 {
149 // if we need to reload
150 if(
151 ( $this->_loadedStart && $this->_loadedStart <= $this->start )
152 &&
153 ( $this->_loadedEnd && $this->_loadedEnd >= $this->end )
154 ){
155 // echo "NO2 NEED TO LOAD!<br>";
156 return;
157 }
158
159 // adjust borders to include day before and after
160 $this->start = $this->t->setDateTimeDb( $this->start )
161 ->setStartDay()
162 ->modify('-1 day')
163 ->formatDateTimeDb()
164 ;
165 $this->end = $this->t->setDateTimeDb( $this->end )
166 ->setStartDay()
167 ->modify('+2 days')
168 ->formatDateTimeDb()
169 ;
170
171 // echo 'RELOAD: ' . $this->start . ' - ' . $this->end . '<br>';
172 $args = array();
173 if( $this->end ){
174 $args[] = array( 'starts_at', '<', $this->end );
175 }
176 if( $this->start ){
177 $args[] = array( 'ends_at', '>', $this->start );
178 }
179
180 // if( $this->calendars ){
181 // $args[] = array( 'calendar_id', 'IN', array_keys($this->calendars) );
182 // }
183 // if( $this->employees ){
184 // $args[] = array( 'employee_id', 'IN', array_keys($this->employees) );
185 // }
186
187 /*
188 2025-04-29: commented out as it produced a bug, resort later
189 $args[] = array('sort', 'starts_at', 'asc');
190 $args[] = array('sort', 'ends_at', 'asc');
191 $args[] = array('sort', 'calendar_id', 'asc');
192 */
193
194
195 // $args = array();
196 $results = $this->crud->read( $args );
197 // _print_r($results);
198 // exit;
199
200
201 $this->_storage = array();
202 foreach( $results as $array ){
203 $shift = $this->arrayToModel( $array );
204 if( ! $shift ){
205 continue;
206 }
207
208 $shiftId = $shift->getId();
209 $this->_storage[ $shiftId ] = $shift;
210 }
211
212 $this->_loadedEnd = $this->end ? $this->end : 230001010000;
213 $this->_loadedStart = $this->start ? $this->start : 197001010000;
214
215 /*
216 2025-04-29: resort
217 $args[] = array('sort', 'starts_at', 'asc');
218 $args[] = array('sort', 'ends_at', 'asc');
219 $args[] = array('sort', 'calendar_id', 'asc');
220 */
221
222 // uasort($this->_storage, function($a, $b) {
223 // if ($a->getStart() > $b->getStart()) {
224 // return 1;
225 // } elseif ($a->getStart() < $b->getStart()) {
226 // return -1;
227 // }
228
229 // if ($a->getEnd() > $b->getEnd()) {
230 // return 1;
231 // } elseif ($a->getEnd() < $b->getEnd()) {
232 // return -1;
233 // }
234 // });
235
236 }
237
238 public function findAllByEmployee( SH4_Employees_Model $employee )
239 {
240 $args = array();
241
242 $employeeId = $employee->getId();
243 $args[] = array( 'employee_id', '=', $employeeId );
244
245 $return = $this->self->read( $args );
246 return $return;
247 }
248
249 public function findAllByCalendar( SH4_Calendars_Model $calendar )
250 {
251 $args = array();
252
253 $calendarId = $calendar->getId();
254 $args[] = array( 'calendar_id', '=', $calendarId );
255
256 $return = $this->self->read( $args );
257 return $return;
258 }
259
260 public function read( array $args = array() )
261 {
262 $hasSort = false;
263 foreach( $args as $a ){
264 if( isset($a[0]) && ('sort' == $a[0]) ){
265 $hasSort = true;
266 break;
267 }
268 }
269
270 if( ! $hasSort ){
271 $args[] = array('sort', 'starts_at', 'asc');
272 $args[] = array('sort', 'ends_at', 'asc');
273 }
274 $args[] = array('sort', 'calendar_id', 'asc');
275
276 $return = $this->crud->read( $args );
277
278 $ids = array_keys($return);
279 foreach( $ids as $id ){
280 $model = $this->arrayToModel( $return[$id] );
281 if( ! $model ){
282 continue;
283 }
284 $return[$id] = $model;
285 }
286
287 return $return;
288 }
289
290 public function arrayToModel( array $array )
291 {
292 if( array_key_exists($array['calendar_id'], $this->calendars) ){
293 $calendar = $this->calendars[ $array['calendar_id'] ];
294 }
295 else {
296 $calendar = $this->calendarsQuery->findById( $array['calendar_id'] );
297 }
298
299 if( ! $calendar ){
300 return;
301 }
302
303 $this->calendars[ $calendar->getId() ] = $calendar;
304
305 $employee = NULL;
306 if( isset($array['employee_id']) ){
307 if( array_key_exists($array['employee_id'], $this->employees) ){
308 $employee = $this->employees[ $array['employee_id'] ];
309 }
310 else {
311 $employee = $this->employeesQuery->findById( $array['employee_id'] );
312 }
313 if( ! $employee ){
314 return;
315 }
316 $this->employees[ $employee->getId() ] = $employee;
317 }
318
319 $start = $array['starts_at'];
320 $end = $array['ends_at'];
321
322 $breakStart = array_key_exists('break_starts_at', $array) ? $array['break_starts_at'] : NULL;
323 $breakEnd = array_key_exists('break_ends_at', $array) ? $array['break_ends_at'] : NULL;
324
325 $status = array_key_exists('status', $array) ? $array['status'] : NULL;
326 $id = array_key_exists('id', $array) ? $array['id'] : NULL;
327
328 $return = new SH4_Shifts_Model( $id, $calendar, $start, $end, $employee, $breakStart, $breakEnd, $status );
329 $return->setRawData( $array );
330
331 return $return;
332 }
333 }