PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
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 / upgrade3 / controller.php

controller.php in ShiftController Employee Shift Scheduling 4.9.26, at sh4/upgrade3/controller.php

434 lines 11.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 class SH4_Upgrade3_Controller
3 {
4 public function __construct(
5 HC3_Hooks $hooks,
6 SH4_Upgrade3_Command $upgradeCommand,
7 HC3_Time $t,
8
9 SH4_App_Command $appCommand,
10 HC3_Users_Query $usersQuery,
11
12 SH4_ShiftTypes_Query $shiftTypes,
13 SH4_ShiftTypes_Command $shiftTypesCommand,
14
15 SH4_Calendars_Query $calendars,
16 SH4_Calendars_Command $calendarsCommand,
17
18 SH4_Employees_Query $employees,
19 SH4_Employees_Command $employeesCommand,
20
21 SH4_Shifts_Query $shifts,
22 SH4_Shifts_Command $shiftsCommand
23
24 )
25 {
26 ini_set( 'memory_limit', '600M' );
27 set_time_limit( 3000 );
28
29 $this->t = $t;
30 $this->upgradeCommand = $hooks->wrap( $upgradeCommand );
31 $this->self = $hooks->wrap($this);
32
33 $this->appCommand = $hooks->wrap( $appCommand );
34 $this->usersQuery = $hooks->wrap( $usersQuery );
35
36 $this->shiftTypes = $hooks->wrap($shiftTypes);
37 $this->shiftTypesCommand = $hooks->wrap($shiftTypesCommand);
38
39 $this->calendars = $hooks->wrap($calendars);
40 $this->calendarsCommand = $hooks->wrap($calendarsCommand);
41
42 $this->employees = $hooks->wrap($employees);
43 $this->employeesCommand = $hooks->wrap($employeesCommand);
44
45 $this->shifts = $hooks->wrap($shifts);
46 $this->shiftsCommand = $hooks->wrap($shiftsCommand);
47 }
48
49 public function execute()
50 {
51 global $wpdb;
52 $prfx = $wpdb->prefix . 'shiftcontroller_v3_';
53
54 $sql = "SELECT * FROM {$prfx}locations";
55 $oldLocations = $wpdb->get_results( $sql, ARRAY_A );
56
57 $level = 1;
58 $sql = "SELECT * FROM {$prfx}users WHERE level & $level";
59 $oldEmployees = $wpdb->get_results( $sql, ARRAY_A );
60
61 $sql = "SELECT * FROM {$prfx}shifts";
62 $oldShifts = $wpdb->get_results( $sql, ARRAY_A );
63
64 $this->upgradeCommand->upgrade( $oldLocations, $oldEmployees, $oldShifts );
65
66 return array('schedule', '__Upgraded__');
67 }
68
69 public function get()
70 {
71 global $wpdb;
72 $prfx = $wpdb->prefix . 'shiftcontroller_v3_';
73
74 $step = isset( $_GET['step'] ) ? $_GET['step'] : '';
75
76 $sql = "SELECT * FROM {$prfx}locations";
77 $oldLocations = $wpdb->get_results( $sql, ARRAY_A );
78
79 $level = 1;
80 $sql = "SELECT * FROM {$prfx}users WHERE level & $level";
81 $oldEmployees = $wpdb->get_results( $sql, ARRAY_A );
82
83 $sql = "SELECT * FROM {$prfx}shifts";
84 $oldShifts = $wpdb->get_results( $sql, ARRAY_A );
85
86 $this->upgradeCommand->upgrade( $oldLocations, $oldEmployees, $oldShifts );
87
88 echo 'Locations: ' . count( $oldLocations ) . '<br>';
89 echo 'Employees: ' . count( $oldEmployees ) . '<br>';
90 echo 'Employees: ' . count( $oldShifts ) . '<br>';
91
92 // echo "__Upgraded__";
93 exit;
94
95 return array('schedule', '__Upgraded__');
96 }
97
98 public function old()
99 {
100 global $wpdb;
101 $prfx = $wpdb->prefix . 'shiftcontroller_v3_';
102
103 $sql = "SELECT * FROM {$prfx}locations";
104 $oldLocations = $wpdb->get_results( $sql, ARRAY_A );
105
106 $level = 1;
107 $sql = "SELECT * FROM {$prfx}users WHERE level & $level";
108 $oldEmployees = $wpdb->get_results( $sql, ARRAY_A );
109
110 $sql = "SELECT * FROM {$prfx}shifts";
111 $oldShifts = $wpdb->get_results( $sql, ARRAY_A );
112
113 $ret = array( $oldLocations, $oldEmployees, $oldShifts );
114 return $ret;
115 }
116
117 public function get1()
118 {
119 list( $oldLocations, $oldEmployees, $oldShifts ) = $this->old();
120
121 $onlyCalendars = [
122 ];
123
124 // shift types
125 $this->shiftTypesCommand->deleteAll();
126 $newShiftTypesIds = array();
127
128 $newShiftTypesIds = array();
129 try {
130 $newShiftTypesIds[] = $this->shiftTypesCommand->createHours( 'Full Time', 9*60*60, 18*60*60, 13*60*60, 14*60*60 );
131 $newShiftTypesIds[] = $this->shiftTypesCommand->createHours( 'Morning', 9*60*60, 13*60*60 );
132 $newShiftTypesIds[] = $this->shiftTypesCommand->createHours( 'Evening', 17*60*60, 21*60*60 );
133 $holidaysId = $this->shiftTypesCommand->createDays( 'Holidays', 2, 30 );
134 }
135 catch( HC3_ExceptionArray $e ){
136 echo "ERROR!";
137 _print_r( $e->getErrors() );
138 exit;
139 }
140
141 $newShiftTypes = $this->shiftTypes->findManyById( $newShiftTypesIds );
142 $holidaysShiftType = $this->shiftTypes->findById( $holidaysId );
143
144 // locations
145 $this->calendarsCommand->deleteAll();
146
147 $newCalendarsIds = array();
148 $oldToNew_Calendars = array();
149 foreach( $oldLocations as $e ){
150 if( $onlyCalendars ){
151 if( ! in_array($e['name'], $onlyCalendars) ) continue;
152 }
153
154 try {
155 $newId = $this->calendarsCommand->create( $e['name'], $e['color'], $e['description'] );
156 }
157 catch( HC3_ExceptionArray $e ){
158 echo "ERROR!";
159 _print_r( $e->getErrors() );
160 exit;
161 }
162
163 $oldToNew_Calendars[ $e['id'] ] = $newId;
164 $newCalendarsIds[] = $newId;
165 }
166
167 $newCalendars = $this->calendars->findManyActiveById( $newCalendarsIds );
168 reset( $newCalendars );
169 foreach( $newCalendars as $calendar ){
170 reset( $newShiftTypes );
171 foreach( $newShiftTypes as $shiftType ){
172 $this->appCommand->addShiftTypeToCalendar( $shiftType, $calendar );
173 }
174 }
175
176 // timeoff
177 try {
178 $newTimeoffId = $this->calendarsCommand->create( 'Time Off', '#a9a9a9' );
179 }
180 catch( HC3_ExceptionArray $e ){
181 echo "ERROR!";
182 _print_r( $e->getErrors() );
183 exit;
184 }
185
186 $newTimeoffCalendar = $this->calendars->findById( $newTimeoffId );
187 $this->appCommand->addShiftTypeToCalendar( $holidaysShiftType, $newTimeoffCalendar );
188
189 $newCalendars[ $newTimeoffId ] = $newTimeoffCalendar;
190
191 echo "NEW CALENDARS: " . count( $newCalendars ) . '<br>';
192 exit;
193 }
194
195 public function get2()
196 {
197 list( $oldLocations, $oldEmployees, $oldShifts ) = $this->old();
198
199 $newEmployeesIds = array();
200 $oldToNew_Employees = array();
201
202 $nextOffset = NULL;
203 $perStep = 100;
204 $totalCount = count($oldEmployees);
205
206 if( $totalCount > $perStep ){
207 $offset = isset( $_GET['offset'] ) ? $_GET['offset'] : 0;
208
209 if( ! $offset ){
210 // employees
211 $this->employeesCommand->deleteAll();
212 }
213
214 $nextOffset = $offset + $perStep;
215 if( $nextOffset > $totalCount ) $nextOffset = NULL;
216
217 $oldEmployees = array_slice( $oldEmployees, $offset, $perStep );
218 }
219 else {
220 // employees
221 $this->employeesCommand->deleteAll();
222 }
223
224 reset( $oldEmployees );
225 foreach( $oldEmployees as $e ){
226 $title = array();
227 if( strlen($e['first_name']) ){
228 $title[] = $e['first_name'];
229 }
230 if( strlen($e['last_name']) ){
231 $title[] = $e['last_name'];
232 }
233 $title = join(' ', $title);
234
235 $try = 1;
236 $newId = 0;
237 $srcTitle = $title;
238
239 while( ! $newId ){
240 try {
241 $newId = $this->employeesCommand->create( $title );
242 if( ! $e['active'] ){
243 $newEmpl = $this->employees->findById( $newId );
244 $this->employeesCommand->archive( $newEmpl );
245 }
246 }
247 catch( HC3_ExceptionArray $ex ){
248 $errors = $ex->getErrors();
249 if( isset($errors['title']) ){
250 $try++;
251 $title = $srcTitle . ' (' . $try . ')';
252 }
253 else {
254 echo "ERROR IN EMPLOYEES!";
255 _print_r( $errors );
256 exit;
257 }
258 }
259 }
260
261 $oldToNew_Employees[ $e['id'] ] = $newId;
262 $newEmployeesIds[] = $newId;
263 }
264
265 $newUsersIds = array_keys( $oldToNew_Employees );
266
267 $newUsers = $this->usersQuery->findManyById( $newUsersIds );
268
269 $newEmployees = $this->employees->findManyActiveById( $newEmployeesIds );
270 $openShiftEmployee = $this->employees->findById( 0 );
271
272 reset( $newUsers );
273 foreach( $newUsers as $user ){
274 $userId = $user->getId();
275 $employeeId = $oldToNew_Employees[ $userId ];
276 if( ! isset($newEmployees[ $employeeId ]) ){
277 continue;
278 }
279 $employee = $newEmployees[ $employeeId ];
280 $this->appCommand->linkEmployeeToUser( $employee, $user );
281 }
282
283 echo "NEW EMPLOYEES: " . $offset . '-' . ($offset + count($newEmployeesIds) ) . '/' . $totalCount . '<br>';
284
285 if( $nextOffset ){
286 echo '<a href="admin.php?page=shiftcontroller4&hca=upgrade3-2&offset=' . $nextOffset . '">next</a>' . '<br>';
287 }
288 exit;
289 }
290
291 public function get3()
292 {
293 $newCalendars = $this->calendars->findAll();
294 $newEmployees = $this->employees->findAll();
295 $openShiftEmployee = $this->employees->findById( 0 );
296
297 reset( $newCalendars );
298 foreach( $newCalendars as $calendar ){
299 reset( $newEmployees );
300 foreach( $newEmployees as $employee ){
301 if( ! $employee->isActive() ){
302 continue;
303 }
304 $this->appCommand->addEmployeeToCalendar( $employee, $calendar );
305 }
306 $this->appCommand->addEmployeeToCalendar( $openShiftEmployee, $calendar );
307 }
308
309 echo "NEW EMPLOYEES TO CALENDARS: " . count( $newEmployees ) . ' - ' . count( $newCalendars ) . '<br>';
310 exit;
311 }
312
313 public function get4()
314 {
315 list( $oldLocations, $oldEmployees, $oldShifts ) = $this->old();
316
317 $nextOffset = NULL;
318 $perStep = 100;
319 $totalCount = count($oldShifts);
320
321 if( $totalCount > $perStep ){
322 $offset = isset( $_GET['offset'] ) ? $_GET['offset'] : 0;
323
324 if( ! $offset ){
325 $this->shiftsCommand->deleteAll();
326 }
327
328 $nextOffset = $offset + $perStep;
329 if( $nextOffset > $totalCount ) $nextOffset = NULL;
330
331 $oldShifts = array_slice( $oldShifts, $offset, $perStep );
332 }
333 else {
334 $this->shiftsCommand->deleteAll();
335 }
336
337 $newCalendars = $this->calendars->findAll();
338 $newEmployees = $this->employees->findAll();
339
340 $newTimeoffId = NULL;
341 foreach( $newCalendars as $e ){
342 if( $e->isTimeoff() ){
343 $newTimeoffId = $e->getId();
344 break;
345 }
346 }
347
348 $oldToNew_Calendars = array();
349 foreach( $oldLocations as $e ){
350 reset( $newCalendars );
351 foreach( $newCalendars as $e2 ){
352 if( $e2->getTitle() == $e['name'] ){
353 $oldToNew_Calendars[ $e['id'] ] = $e2->getId();
354 break;
355 }
356 }
357 }
358
359 $oldToNew_Employees = array();
360 foreach( $oldEmployees as $e ){
361 $title = array();
362 if( strlen($e['first_name']) ){
363 $title[] = $e['first_name'];
364 }
365 if( strlen($e['last_name']) ){
366 $title[] = $e['last_name'];
367 }
368 $title = join(' ', $title);
369
370 reset( $newEmployees );
371 foreach( $newEmployees as $e2 ){
372 if( $e2->getTitle() == $title ){
373 $oldToNew_Employees[ $e['id'] ] = $e2->getId();
374 break;
375 }
376 }
377 }
378
379 // shifts
380 $newShiftIds = array();
381 foreach( $oldShifts as $e ){
382 // timeoff
383 if( 2 == $e['type'] ){
384 $newCalendarId = $newTimeoffId;
385 }
386 else {
387 if( ! isset($oldToNew_Calendars[$e['location_id']]) ){
388 continue;
389 }
390 $newCalendarId = $oldToNew_Calendars[$e['location_id']];
391 }
392
393 if( ! $newCalendarId ) continue;
394 $calendar = $newCalendars[ $newCalendarId ];
395
396 if( ! isset($oldToNew_Employees[$e['user_id']]) ){
397 continue;
398 }
399 $newEmployeeId = $oldToNew_Employees[$e['user_id']];
400
401 if( ! array_key_exists($newEmployeeId, $newEmployees) ){
402 continue;
403 }
404 $employee = $newEmployees[ $newEmployeeId ];
405
406 $start = $this->t->setDateDb( $e['date'] )->modify('+' . $e['start'] . ' seconds')->formatDateTimeDb();
407 $end = $this->t->setDateDb( $e['date_end'] )->modify('+' . $e['end'] . ' seconds')->formatDateTimeDb();
408
409 $status = ( $e['status'] == 1 ) ? SH4_Shifts_Model::STATUS_PUBLISH : SH4_Shifts_Model::STATUS_DRAFT;
410
411 try {
412 $newShiftIds[] = $this->shiftsCommand->create( $calendar, $start, $end, $employee, NULL, NULL, $status );
413 }
414 catch( HC3_ExceptionArray $e ){
415 echo "ERROR!";
416 _print_r( $e->getErrors() );
417 exit;
418 }
419 }
420
421 echo "NEW SHIFTS: " . $offset . '-' . ($offset + count( $newShiftIds )) . '/' . $totalCount . '<br>';
422
423 // $nextOffset = $offset + count($newShiftIds);
424 // if( $nextOffset > $totalCount ) $nextOffset = NULL;
425
426 if( $nextOffset ){
427 $to = 'admin.php?page=shiftcontroller4&hca=upgrade3-4&offset=' . $nextOffset;
428 $html = "<META http-equiv=\"refresh\" content=\"0;URL=$to\">";
429 echo '<a href="' . $to . '">next</a>' . '<br>';
430 echo $html;
431 }
432 exit;
433 }
434 }