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