PluginProbe
ShiftController Employee Shift Scheduling / 4.9.85
ShiftController Employee Shift Scheduling v4.9.85
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.85, at sh4/shifts/query.php

294 lines 6.8 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 keep( SH4_Shifts_Model $model )
59 {
60 $id = $model->getId();
61 if( $id ){
62 $this->_storage[$id] = $model;
63 }
64
65 return $this;
66 }
67
68 public function findById( $id )
69 {
70 $return = NULL;
71
72 if( is_array($id) && isset($id['id']) ){
73 $id = $id['id'];
74 }
75
76 if( array_key_exists($id, $this->_storage) ){
77 $return = $this->_storage[$id];
78 return $return;
79 }
80
81 $args = array();
82 $args[] = array('id', '=', $id);
83 $args[] = array('limit', 1);
84
85 if( $array = $this->self->read( $args ) ){
86 $return = array_shift( $array );
87 $this->_storage[$id] = $return;
88 }
89
90 return $return;
91 }
92
93 public function findManyById( array $ids )
94 {
95 $return = array();
96 $notOnCache = FALSE;
97 reset( $ids );
98 foreach( $ids as $id ){
99 if( ! array_key_exists($id, $this->_storage) ){
100 $return = NULL;
101 break;
102 }
103 $return[ $id ] = $this->_storage[$id];
104 }
105
106 if( $return ){
107 return $return;
108 }
109
110 $args = array();
111 $args[] = array('id', 'IN', $ids);
112
113 $return = $this->self->read( $args );
114 return $return;
115 }
116
117 public function find()
118 {
119 $this->_load();
120
121 $return = array();
122 foreach( $this->_storage as $shift ){
123 if( $this->end && $shift->getStart() >= $this->end ){
124 continue;
125 }
126
127 if( $this->start && $shift->getEnd() <= $this->start ){
128 continue;
129 }
130
131 $return[ $shift->getId() ] = $shift;
132 }
133
134 return $return;
135 }
136
137 protected function _load()
138 {
139 // if we need to reload
140 if(
141 ( $this->_loadedStart && $this->_loadedStart <= $this->start )
142 &&
143 ( $this->_loadedEnd && $this->_loadedEnd >= $this->end )
144 ){
145 // echo "NO2 NEED TO LOAD!<br>";
146 return;
147 }
148
149 // adjust borders to include day before and after
150 $this->start = $this->t->setDateTimeDb( $this->start )
151 ->setStartDay()
152 ->modify('-1 day')
153 ->formatDateTimeDb()
154 ;
155 $this->end = $this->t->setDateTimeDb( $this->end )
156 ->setStartDay()
157 ->modify('+2 days')
158 ->formatDateTimeDb()
159 ;
160
161 // echo 'RELOAD: ' . $this->start . ' - ' . $this->end . '<br>';
162 $args = array();
163 if( $this->end ){
164 $args[] = array( 'starts_at', '<', $this->end );
165 }
166 if( $this->start ){
167 $args[] = array( 'ends_at', '>', $this->start );
168 }
169
170 // if( $this->calendars ){
171 // $args[] = array( 'calendar_id', 'IN', array_keys($this->calendars) );
172 // }
173 // if( $this->employees ){
174 // $args[] = array( 'employee_id', 'IN', array_keys($this->employees) );
175 // }
176
177 $args[] = array('sort', 'starts_at', 'asc');
178 $args[] = array('sort', 'ends_at', 'asc');
179 $args[] = array('sort', 'calendar_id', 'asc');
180 // _print_r( $args );
181 // exit;
182 $results = $this->crud->read( $args );
183
184 $this->_storage = array();
185 foreach( $results as $array ){
186 $shift = $this->arrayToModel( $array );
187 if( ! $shift ){
188 continue;
189 }
190
191 $shiftId = $shift->getId();
192 $this->_storage[ $shiftId ] = $shift;
193 }
194
195 $this->_loadedEnd = $this->end ? $this->end : 230001010000;
196 $this->_loadedStart = $this->start ? $this->start : 197001010000;
197 }
198
199 public function findAllByEmployee( SH4_Employees_Model $employee )
200 {
201 $args = array();
202
203 $employeeId = $employee->getId();
204 $args[] = array( 'employee_id', '=', $employeeId );
205
206 $return = $this->self->read( $args );
207 return $return;
208 }
209
210 public function findAllByCalendar( SH4_Calendars_Model $calendar )
211 {
212 $args = array();
213
214 $calendarId = $calendar->getId();
215 $args[] = array( 'calendar_id', '=', $calendarId );
216
217 $return = $this->self->read( $args );
218 return $return;
219 }
220
221 public function read( array $args = array() )
222 {
223 $hasSort = false;
224 foreach( $args as $a ){
225 if( isset($a[0]) && ('sort' == $a[0]) ){
226 $hasSort = true;
227 break;
228 }
229 }
230
231 if( ! $hasSort ){
232 $args[] = array('sort', 'starts_at', 'asc');
233 $args[] = array('sort', 'ends_at', 'asc');
234 }
235 $args[] = array('sort', 'calendar_id', 'asc');
236
237 $return = $this->crud->read( $args );
238
239 $ids = array_keys($return);
240 foreach( $ids as $id ){
241 $model = $this->arrayToModel( $return[$id] );
242 if( ! $model ){
243 continue;
244 }
245 $return[$id] = $model;
246 }
247
248 return $return;
249 }
250
251 public function arrayToModel( array $array )
252 {
253 if( array_key_exists($array['calendar_id'], $this->calendars) ){
254 $calendar = $this->calendars[ $array['calendar_id'] ];
255 }
256 else {
257 $calendar = $this->calendarsQuery->findById( $array['calendar_id'] );
258 }
259
260 if( ! $calendar ){
261 return;
262 }
263
264 $this->calendars[ $calendar->getId() ] = $calendar;
265
266 $employee = NULL;
267 if( isset($array['employee_id']) ){
268 if( array_key_exists($array['employee_id'], $this->employees) ){
269 $employee = $this->employees[ $array['employee_id'] ];
270 }
271 else {
272 $employee = $this->employeesQuery->findById( $array['employee_id'] );
273 }
274 if( ! $employee ){
275 return;
276 }
277 $this->employees[ $employee->getId() ] = $employee;
278 }
279
280 $start = $array['starts_at'];
281 $end = $array['ends_at'];
282
283 $breakStart = array_key_exists('break_starts_at', $array) ? $array['break_starts_at'] : NULL;
284 $breakEnd = array_key_exists('break_ends_at', $array) ? $array['break_ends_at'] : NULL;
285
286 $status = array_key_exists('status', $array) ? $array['status'] : NULL;
287 $id = array_key_exists('id', $array) ? $array['id'] : NULL;
288
289 $return = new SH4_Shifts_Model( $id, $calendar, $start, $end, $employee, $breakStart, $breakEnd, $status );
290 $return->setRawData( $array );
291
292 return $return;
293 }
294 }