PluginProbe
ShiftController Employee Shift Scheduling / 4.9.24
ShiftController Employee Shift Scheduling v4.9.24
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.24, at sh4/shifts/query.php

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