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