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 / upgrade3 / command.php

command.php in ShiftController Employee Shift Scheduling 4.9.24, at sh4/upgrade3/command.php

305 lines 8.2 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_Command
3 {
4 public function __construct(
5 HC3_Hooks $hooks,
6 HC3_Dic $dic,
7
8 HC3_Time $t,
9
10 SH4_App_Command $appCommand,
11
12 HC3_Users_Query $usersQuery,
13
14 SH4_ShiftTypes_Presenter $shiftTypesPresenter,
15 SH4_ShiftTypes_Query $shiftTypes,
16 SH4_ShiftTypes_Command $shiftTypesCommand,
17
18 SH4_Calendars_Query $calendars,
19 SH4_Calendars_Command $calendarsCommand,
20
21 SH4_Employees_Query $employees,
22 SH4_Employees_Command $employeesCommand,
23
24 SH4_Shifts_Query $shifts,
25 SH4_Shifts_Command $shiftsCommand
26 )
27 {
28 $this->t = $t;
29 $this->dic = $dic;
30
31 $this->appCommand = $hooks->wrap( $appCommand );
32 $this->usersQuery = $hooks->wrap( $usersQuery );
33
34 $this->shiftTypesPresenter = $hooks->wrap($shiftTypesPresenter);
35 $this->shiftTypes = $hooks->wrap($shiftTypes);
36 $this->shiftTypesCommand = $hooks->wrap($shiftTypesCommand);
37
38 $this->calendars = $hooks->wrap($calendars);
39 $this->calendarsCommand = $hooks->wrap($calendarsCommand);
40
41 $this->employees = $hooks->wrap($employees);
42 $this->employeesCommand = $hooks->wrap($employeesCommand);
43
44 $this->shifts = $hooks->wrap($shifts);
45 $this->shiftsCommand = $hooks->wrap($shiftsCommand);
46
47 $this->self = $hooks->wrap($this);
48 }
49
50 public function upgrade( $oldLocations, $oldEmployees, $oldShifts, $importUsers = FALSE )
51 {
52 ini_set( 'memory_limit', '600M' );
53 set_time_limit( 3000 );
54
55 $onlyCalendars = [
56 ];
57
58 $out = array();
59
60 $this->shiftsCommand->deleteAll();
61
62 // shift types
63 $this->shiftTypesCommand->deleteAll();
64 $newShiftTypesIds = array();
65
66 $newShiftTypesIds = array();
67 try {
68 $newShiftTypesIds[] = $this->shiftTypesCommand->createHours( 'Full Time', 9*60*60, 18*60*60, 13*60*60, 14*60*60 );
69 $newShiftTypesIds[] = $this->shiftTypesCommand->createHours( 'Morning', 9*60*60, 13*60*60 );
70 $newShiftTypesIds[] = $this->shiftTypesCommand->createHours( 'Evening', 17*60*60, 21*60*60 );
71 $holidaysId = $this->shiftTypesCommand->createDays( 'Holidays', 2, 30 );
72 }
73 catch( HC3_ExceptionArray $e ){
74 echo "ERROR!";
75 _print_r( $e->getErrors() );
76 exit;
77 }
78
79 $newShiftTypes = $this->shiftTypes->findManyById( $newShiftTypesIds );
80 $holidaysShiftType = $this->shiftTypes->findById( $holidaysId );
81
82 // locations
83 $this->calendarsCommand->deleteAll();
84
85 $newCalendarsIds = array();
86 $oldToNew_Calendars = array();
87 foreach( $oldLocations as $e ){
88 if( $onlyCalendars ){
89 if( ! in_array($e['name'], $onlyCalendars) ) continue;
90 }
91
92 try {
93 $newId = $this->calendarsCommand->create( $e['name'], $e['color'], $e['description'] );
94 }
95 catch( HC3_ExceptionArray $e ){
96 echo "ERROR!";
97 _print_r( $e->getErrors() );
98 exit;
99 }
100
101 $oldToNew_Calendars[ $e['id'] ] = $newId;
102 $newCalendarsIds[] = $newId;
103 }
104
105 $newCalendars = $this->calendars->findManyActiveById( $newCalendarsIds );
106 reset( $newCalendars );
107 foreach( $newCalendars as $calendar ){
108 reset( $newShiftTypes );
109 foreach( $newShiftTypes as $shiftType ){
110 $this->appCommand->addShiftTypeToCalendar( $shiftType, $calendar );
111 }
112 }
113
114 // timeoff
115 try {
116 $newTimeoffId = $this->calendarsCommand->create( 'Time Off', '#a9a9a9' );
117 }
118 catch( HC3_ExceptionArray $e ){
119 echo "ERROR!";
120 _print_r( $e->getErrors() );
121 exit;
122 }
123
124 $newTimeoffCalendar = $this->calendars->findById( $newTimeoffId );
125 $this->appCommand->addShiftTypeToCalendar( $holidaysShiftType, $newTimeoffCalendar );
126
127 $newCalendars[ $newTimeoffId ] = $newTimeoffCalendar;
128
129 // employees
130 $this->employeesCommand->deleteAll();
131
132 $newEmployeesIds = array();
133 $oldToNew_Employees = array();
134 reset( $oldEmployees );
135
136 foreach( $oldEmployees as $e ){
137 $title = array();
138 if( strlen($e['first_name']) ){
139 $title[] = $e['first_name'];
140 }
141 if( strlen($e['last_name']) ){
142 $title[] = $e['last_name'];
143 }
144 $title = join(' ', $title);
145
146 $try = 1;
147 $newId = 0;
148 $srcTitle = $title;
149
150 while( ! $newId ){
151 try {
152 $newId = $this->employeesCommand->create( $title );
153 if( ! $e['active'] ){
154 $newEmpl = $this->employees->findById( $newId );
155 $this->employeesCommand->archive( $newEmpl );
156 }
157 }
158 catch( HC3_ExceptionArray $ex ){
159 $errors = $ex->getErrors();
160 if( isset($errors['title']) ){
161 $try++;
162 $title = $srcTitle . ' (' . $try . ')';
163 }
164 else {
165 echo "ERROR IN EMPLOYEES!";
166 _print_r( $errors );
167 exit;
168 }
169 }
170 }
171
172 $oldToNew_Employees[ $e['id'] ] = $newId;
173 $newEmployeesIds[] = $newId;
174 }
175
176 if( $importUsers ){
177 $usersCommand = $this->dic->make('HC3_Users_Command');
178 $usersCommand->deleteAll();
179
180 reset( $oldEmployees );
181 foreach( $oldEmployees as $e ){
182 $title = array();
183 if( strlen($e['first_name']) ){
184 $title[] = $e['first_name'];
185 }
186 if( strlen($e['last_name']) ){
187 $title[] = $e['last_name'];
188 }
189 $title = join(' ', $title);
190
191 if( ! $e['username'] ){
192 $e['username'] = $e['email'];
193 }
194
195 $array = array();
196 $array['display_name'] = $title;
197 $array['username'] = $e['username'];
198 $array['email'] = $e['email'];
199 $array['token'] = $e['token'];
200 $array['status'] = $e['active'] ? 'active' : NULL;
201 $array['role'] = ($e['level'] == 3) ? 'admin' : NULL;
202 $array['id'] = $e['id'];
203 $array['hashed_password'] = $e['password'];
204
205 try {
206 $newId = $usersCommand->create( $array['display_name'], $array['username'], $array['email'], $array['role'], $array );
207 }
208 catch( HC3_ExceptionArray $e ){
209 echo "ERROR!";
210 _print_r( $e->getErrors() );
211 exit;
212 continue;
213 }
214
215 $newUsersIds[] = $newId;
216 }
217 }
218 else {
219 $newUsersIds = array_keys( $oldToNew_Employees );
220 }
221
222 $newUsers = $this->usersQuery->findManyById( $newUsersIds );
223
224 $newEmployees = $this->employees->findManyActiveById( $newEmployeesIds );
225 $openShiftEmployee = $this->employees->findById( 0 );
226
227 reset( $newUsers );
228 foreach( $newUsers as $user ){
229 $userId = $user->getId();
230 $employeeId = $oldToNew_Employees[ $userId ];
231 if( ! isset($newEmployees[ $employeeId ]) ){
232 continue;
233 }
234 $employee = $newEmployees[ $employeeId ];
235 $this->appCommand->linkEmployeeToUser( $employee, $user );
236 }
237
238 reset( $newCalendars );
239 foreach( $newCalendars as $calendar ){
240 reset( $newEmployees );
241 foreach( $newEmployees as $employee ){
242 if( ! $employee->isActive() ){
243 continue;
244 }
245 $this->appCommand->addEmployeeToCalendar( $employee, $calendar );
246 }
247 $this->appCommand->addEmployeeToCalendar( $openShiftEmployee, $calendar );
248 }
249 reset( $newEmployees );
250 foreach( $newEmployees as $employee ){
251 if( ! $employee->isActive() ){
252 continue;
253 }
254 $this->appCommand->addEmployeeToCalendar( $employee, $newTimeoffCalendar );
255 }
256
257 // echo count( $oldShifts );
258 // exit;
259 // $oldShifts = [];
260 // $oldShifts = array_slice( $oldShifts, 0, 20000 );
261
262 // shifts
263 foreach( $oldShifts as $e ){
264 // timeoff
265 if( 2 == $e['type'] ){
266 $newCalendarId = $newTimeoffId;
267 }
268 else {
269 if( ! isset($oldToNew_Calendars[$e['location_id']]) ){
270 continue;
271 }
272 $newCalendarId = $oldToNew_Calendars[$e['location_id']];
273 }
274
275 if( ! array_key_exists($newCalendarId, $newCalendars) ){
276 continue;
277 }
278 $calendar = $newCalendars[ $newCalendarId ];
279
280 if( ! isset($oldToNew_Employees[$e['user_id']]) ){
281 continue;
282 }
283 $newEmployeeId = $oldToNew_Employees[$e['user_id']];
284
285 if( ! array_key_exists($newEmployeeId, $newEmployees) ){
286 continue;
287 }
288 $employee = $newEmployees[ $newEmployeeId ];
289
290 $start = $this->t->setDateDb( $e['date'] )->modify('+' . $e['start'] . ' seconds')->formatDateTimeDb();
291 $end = $this->t->setDateDb( $e['date_end'] )->modify('+' . $e['end'] . ' seconds')->formatDateTimeDb();
292
293 $status = ( $e['status'] == 1 ) ? SH4_Shifts_Model::STATUS_PUBLISH : SH4_Shifts_Model::STATUS_DRAFT;
294
295 try {
296 $this->shiftsCommand->create( $calendar, $start, $end, $employee, NULL, NULL, $status );
297 }
298 catch( HC3_ExceptionArray $e ){
299 echo "ERROR!";
300 _print_r( $e->getErrors() );
301 exit;
302 }
303 }
304 }
305 }