| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_App_IQuery |
| 3 |
{ |
| 4 |
public function findUserByEmployee( SH4_Employees_Model $employee ); |
| 5 |
public function findEmployeeByUser( HC3_Users_Model $user ); |
| 6 |
public function findAllUsersWithEmployee(); |
| 7 |
|
| 8 |
public function findManagersForCalendar( SH4_Calendars_Model $calendar ); |
| 9 |
public function findViewersForCalendar( SH4_Calendars_Model $calendar ); |
| 10 |
|
| 11 |
public function findCalendarsManagedByUser( HC3_Users_Model $user ); |
| 12 |
public function findCalendarsViewedByUser( HC3_Users_Model $user ); |
| 13 |
|
| 14 |
public function filterEmployeesForCalendar( array $employeeList, SH4_Calendars_Model $calendar ); |
| 15 |
public function findEmployeesForCalendar( SH4_Calendars_Model $calendar ); |
| 16 |
public function findCalendarsForEmployee( SH4_Employees_Model $employee ); |
| 17 |
|
| 18 |
public function findShiftTypesForCalendar( SH4_Calendars_Model $calendar ); |
| 19 |
|
| 20 |
public function filterShiftsForUser( HC3_Users_Model $user, array $shifts ); |
| 21 |
public function findCalendarsForChange( SH4_Shifts_Model $model ); |
| 22 |
} |
| 23 |
|
| 24 |
class SH4_App_Query implements SH4_App_IQuery |
| 25 |
{ |
| 26 |
public $self, $settings, $permission, $shiftTypesQuery, $calendarsQuery, $employeesQuery, $cp, $t, $usersQuery, $crudFactory; |
| 27 |
|
| 28 |
public function __construct( |
| 29 |
HC3_Settings $settings, |
| 30 |
HC3_IPermission $permission, |
| 31 |
|
| 32 |
SH4_ShiftTypes_Query $shiftTypesQuery, |
| 33 |
SH4_Calendars_Query $calendarsQuery, |
| 34 |
SH4_Employees_Query $employeesQuery, |
| 35 |
SH4_Calendars_Permissions $cp, |
| 36 |
|
| 37 |
HC3_Time $t, |
| 38 |
HC3_Users_Query $usersQuery, |
| 39 |
HC3_CrudFactory $crudFactory, |
| 40 |
HC3_Hooks $hooks |
| 41 |
) |
| 42 |
{ |
| 43 |
$this->settings = $hooks->wrap( $settings ); |
| 44 |
$this->permission = $hooks->wrap( $permission ); |
| 45 |
$this->crudFactory = $hooks->wrap( $crudFactory ); |
| 46 |
$this->t = $t; |
| 47 |
|
| 48 |
$this->shiftTypesQuery = $hooks->wrap( $shiftTypesQuery ); |
| 49 |
$this->employeesQuery = $hooks->wrap( $employeesQuery ); |
| 50 |
$this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 51 |
$this->usersQuery = $hooks->wrap( $usersQuery ); |
| 52 |
$this->cp = $hooks->wrap( $cp ); |
| 53 |
|
| 54 |
$this->self = $hooks->wrap( $this ); |
| 55 |
} |
| 56 |
|
| 57 |
public function findCalendarsForChange( SH4_Shifts_Model $shift ) |
| 58 |
{ |
| 59 |
$employee = $shift->getEmployee(); |
| 60 |
|
| 61 |
// find current shift type |
| 62 |
$startInDay = $shift->getStartInDay(); |
| 63 |
$endInDay = $shift->getEndInDay(); |
| 64 |
$shiftKey = $startInDay . '-' . $endInDay; |
| 65 |
|
| 66 |
$calendar = $shift->getCalendar(); |
| 67 |
$calendarId = $calendar->getId(); |
| 68 |
|
| 69 |
$needMultiDay = $shift->isMultiDay(); |
| 70 |
|
| 71 |
$ret = $this->self->findCalendarsForEmployee( $employee ); |
| 72 |
unset( $ret[$calendarId] ); |
| 73 |
|
| 74 |
$ids = array_keys( $ret ); |
| 75 |
foreach( $ids as $id ){ |
| 76 |
if( $calendar->isTimeoff() && (! $ret[$id]->isTimeoff() ) ){ |
| 77 |
unset( $ret[$id] ); |
| 78 |
continue; |
| 79 |
} |
| 80 |
if( $calendar->isShift() && (! $ret[$id]->isShift() ) ){ |
| 81 |
unset( $ret[$id] ); |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
$gotShiftTypes = FALSE; |
| 86 |
$thisShiftTypes = $this->self->findShiftTypesForCalendar( $ret[$id] ); |
| 87 |
foreach( $thisShiftTypes as $shiftType ){ |
| 88 |
$range = $shiftType->getRange(); |
| 89 |
|
| 90 |
// multiday? |
| 91 |
if( $needMultiDay ){ |
| 92 |
if( SH4_ShiftTypes_Model::RANGE_HOURS == $range ){ |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
// if this allowed days cover this duration |
| 97 |
$shiftEnd = $shift->getEnd(); |
| 98 |
$this->t->setDateTimeDb( $shift->getStart() ); |
| 99 |
$minEnd = $this->t->setDateTimeDb( $shift->getStart() )->modify( '+' . $shiftType->getStart() . ' days' )->formatDateTimeDb(); |
| 100 |
if( $minEnd > $shiftEnd ){ |
| 101 |
continue; |
| 102 |
} |
| 103 |
$maxEnd = $this->t->setDateTimeDb( $shift->getStart() )->modify( '+' . ($shiftType->getEnd() + 1) . ' days' )->formatDateTimeDb(); |
| 104 |
if( $maxEnd < $shiftEnd ){ |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
$gotShiftTypes = TRUE; |
| 109 |
break; |
| 110 |
} |
| 111 |
else { |
| 112 |
if( SH4_ShiftTypes_Model::RANGE_DAYS == $range ){ |
| 113 |
continue; |
| 114 |
} |
| 115 |
|
| 116 |
$thisStart = $shiftType->getStart(); |
| 117 |
$thisEnd = $shiftType->getEnd(); |
| 118 |
|
| 119 |
// custom time |
| 120 |
if( (NULL === $thisStart) && (NULL === $thisEnd) ){ |
| 121 |
$gotShiftTypes = TRUE; |
| 122 |
break; |
| 123 |
} |
| 124 |
|
| 125 |
$thisKey = $thisStart . '-' . $thisEnd; |
| 126 |
if( $thisKey == $shiftKey ){ |
| 127 |
$gotShiftTypes = TRUE; |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if( ! $gotShiftTypes ){ |
| 134 |
unset( $ret[$id] ); |
| 135 |
continue; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
return $ret; |
| 140 |
} |
| 141 |
|
| 142 |
public function filterShiftsForUser( HC3_Users_Model $user, array $return ) |
| 143 |
{ |
| 144 |
$currentUserId = $user->getId(); |
| 145 |
|
| 146 |
if( ! $currentUserId ){ |
| 147 |
$return = array(); |
| 148 |
return $return; |
| 149 |
} |
| 150 |
|
| 151 |
$calendarsAsManager = $this->self->findCalendarsManagedByUser( $user ); |
| 152 |
$calendarsAsViewer = $this->self->findCalendarsViewedByUser( $user ); |
| 153 |
|
| 154 |
$calendarsAsEmployee = array(); |
| 155 |
$meEmployee = $this->self->findEmployeeByUser( $user ); |
| 156 |
if( $meEmployee ){ |
| 157 |
$meEmployeeId = $meEmployee->getId(); |
| 158 |
$calendarsAsEmployee = $this->self->findCalendarsForEmployee( $meEmployee ); |
| 159 |
} |
| 160 |
|
| 161 |
$ids = array_keys( $return ); |
| 162 |
|
| 163 |
foreach( $ids as $id ){ |
| 164 |
$shift = $return[$id]; |
| 165 |
|
| 166 |
$shiftCalendar = $shift->getCalendar(); |
| 167 |
$shiftCalendarId = $shiftCalendar->getId(); |
| 168 |
$shiftEmployee = $shift->getEmployee(); |
| 169 |
$shiftEmployeeId = $shiftEmployee->getId(); |
| 170 |
|
| 171 |
if( isset($calendarsAsManager[$shiftCalendarId]) ){ |
| 172 |
continue; |
| 173 |
} |
| 174 |
|
| 175 |
if( isset($calendarsAsViewer[$shiftCalendarId]) ){ |
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
if( isset($calendarsAsEmployee[$shiftCalendarId]) ){ |
| 180 |
if( $shiftEmployeeId == $meEmployeeId ){ |
| 181 |
if( $shift->isPublished() ){ |
| 182 |
$perm = 'employee_view_own_publish'; |
| 183 |
} |
| 184 |
else { |
| 185 |
$perm = 'employee_view_own_draft'; |
| 186 |
} |
| 187 |
} |
| 188 |
else { |
| 189 |
if( $shift->isOpen() ){ |
| 190 |
if( $shift->isPublished() ){ |
| 191 |
$perm = 'employee_view_open_publish'; |
| 192 |
} |
| 193 |
else { |
| 194 |
$perm = 'employee_view_open_draft'; |
| 195 |
} |
| 196 |
} |
| 197 |
else { |
| 198 |
if( $shift->isPublished() ){ |
| 199 |
$perm = 'employee_view_others_publish'; |
| 200 |
} |
| 201 |
else { |
| 202 |
$perm = 'employee_view_others_draft'; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
if( $this->cp->get($shiftCalendar, $perm) ){ |
| 208 |
continue; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
if( $shift->isOpen() ){ |
| 213 |
if( $shift->isPublished() ){ |
| 214 |
$perm = 'visitor_view_open_publish'; |
| 215 |
} |
| 216 |
else { |
| 217 |
$perm = 'visitor_view_open_draft'; |
| 218 |
} |
| 219 |
} |
| 220 |
else { |
| 221 |
if( $shift->isPublished() ){ |
| 222 |
$perm = 'visitor_view_others_publish'; |
| 223 |
} |
| 224 |
else { |
| 225 |
$perm = 'visitor_view_others_draft'; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
if( $this->cp->get($shiftCalendar, $perm) ){ |
| 230 |
continue; |
| 231 |
} |
| 232 |
|
| 233 |
unset( $return[$id] ); |
| 234 |
} |
| 235 |
|
| 236 |
return $return; |
| 237 |
} |
| 238 |
|
| 239 |
public function filterCalendarsManagedByUser( array $ret, HC3_Users_Model $user ) |
| 240 |
{ |
| 241 |
$isAdmin = $this->permission->isAdmin( $user ); |
| 242 |
if( $isAdmin ){ |
| 243 |
return $ret; |
| 244 |
} |
| 245 |
|
| 246 |
$userId = $user->getId(); |
| 247 |
foreach( $ret as $calendar ){ |
| 248 |
$calendarId = $calendar->getId(); |
| 249 |
$managers = $this->self->findManagersForCalendar( $calendar ); |
| 250 |
if( ! isset($managers[$userId])){ |
| 251 |
unset($ret[$calendarId]); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return $ret; |
| 256 |
} |
| 257 |
|
| 258 |
public function findCalendarsManagedByUser( HC3_Users_Model $user ) |
| 259 |
{ |
| 260 |
$ret = $this->calendarsQuery->findActive(); |
| 261 |
$ret = $this->self->filterCalendarsManagedByUser( $ret, $user ); |
| 262 |
return $ret; |
| 263 |
} |
| 264 |
|
| 265 |
public function filterCalendarsViewedByUser( array $ret, HC3_Users_Model $user ) |
| 266 |
{ |
| 267 |
$isAdmin = $this->permission->isAdmin( $user ); |
| 268 |
if( $isAdmin ){ |
| 269 |
return $ret; |
| 270 |
} |
| 271 |
|
| 272 |
$userId = $user->getId(); |
| 273 |
foreach( $ret as $calendar ){ |
| 274 |
$calendarId = $calendar->getId(); |
| 275 |
$viewers = $this->self->findViewersForCalendar( $calendar ); |
| 276 |
if( ! isset($viewers[$userId])){ |
| 277 |
unset($ret[$calendarId]); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
return $ret; |
| 282 |
} |
| 283 |
|
| 284 |
public function findCalendarsViewedByUser( HC3_Users_Model $user ) |
| 285 |
{ |
| 286 |
$ret = $this->calendarsQuery->findActive(); |
| 287 |
$ret = $this->self->filterCalendarsViewedByUser( $ret, $user ); |
| 288 |
return $ret; |
| 289 |
} |
| 290 |
|
| 291 |
public function findManagersForCalendar( SH4_Calendars_Model $calendar ) |
| 292 |
{ |
| 293 |
$return = $this->permission->findAdmins(); |
| 294 |
|
| 295 |
$calendarId = $calendar->getId(); |
| 296 |
$settingName = 'calendar_' . $calendarId . '_manager'; |
| 297 |
|
| 298 |
$usersIds = $this->settings->get( $settingName, TRUE ); |
| 299 |
if( $usersIds ){ |
| 300 |
$moreReturn = $this->usersQuery->findManyById( $usersIds ); |
| 301 |
foreach( $moreReturn as $id => $user ){ |
| 302 |
if( ! array_key_exists($id, $return) ){ |
| 303 |
$return[ $id ] = $user; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
return $return; |
| 309 |
} |
| 310 |
|
| 311 |
public function findViewersForCalendar( SH4_Calendars_Model $calendar ) |
| 312 |
{ |
| 313 |
// $return = $this->permission->findAdmins(); |
| 314 |
$return = array(); |
| 315 |
|
| 316 |
$calendarId = $calendar->getId(); |
| 317 |
$settingName = 'calendar_' . $calendarId . '_viewer'; |
| 318 |
|
| 319 |
$usersIds = $this->settings->get( $settingName, TRUE ); |
| 320 |
if( $usersIds ){ |
| 321 |
$moreReturn = $this->usersQuery->findManyById( $usersIds ); |
| 322 |
foreach( $moreReturn as $id => $user ){ |
| 323 |
if( ! array_key_exists($id, $return) ){ |
| 324 |
$return[ $id ] = $user; |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
return $return; |
| 330 |
} |
| 331 |
|
| 332 |
public function filterEmployeesForCalendar( array $employeeList, SH4_Calendars_Model $calendar ) |
| 333 |
{ |
| 334 |
$ret = array(); |
| 335 |
|
| 336 |
$calendarId = $calendar->getId(); |
| 337 |
$settingName = 'calendar_' . $calendarId . '_employee'; |
| 338 |
$employeeIds = $this->settings->get( $settingName, true ); |
| 339 |
|
| 340 |
if( ! $employeeIds ){ |
| 341 |
return $ret; |
| 342 |
} |
| 343 |
|
| 344 |
$ret = $employeeList; |
| 345 |
|
| 346 |
$ids = array_keys( $ret ); |
| 347 |
foreach( $ids as $id ){ |
| 348 |
if( ! in_array($id, $employeeIds) ){ |
| 349 |
unset( $ret[$id] ); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
return $ret; |
| 354 |
} |
| 355 |
|
| 356 |
public function findEmployeesForCalendar( SH4_Calendars_Model $calendar ) |
| 357 |
{ |
| 358 |
$return = array(); |
| 359 |
|
| 360 |
$calendarId = $calendar->getId(); |
| 361 |
$settingName = 'calendar_' . $calendarId . '_employee'; |
| 362 |
|
| 363 |
$employeesIds = $this->settings->get( $settingName, TRUE ); |
| 364 |
if( $employeesIds ){ |
| 365 |
$return = $this->employeesQuery->findManyActiveById( $employeesIds ); |
| 366 |
} |
| 367 |
|
| 368 |
return $return; |
| 369 |
} |
| 370 |
|
| 371 |
public function findShiftTypesForCalendar( SH4_Calendars_Model $calendar ) |
| 372 |
{ |
| 373 |
$return = array(); |
| 374 |
|
| 375 |
$calendarId = $calendar->getId(); |
| 376 |
$settingName = 'calendar_' . $calendarId . '_shifttype'; |
| 377 |
|
| 378 |
$shiftTypesIds = $this->settings->get( $settingName, TRUE ); |
| 379 |
if( $shiftTypesIds ){ |
| 380 |
$return = $this->shiftTypesQuery->findManyById( $shiftTypesIds ); |
| 381 |
} |
| 382 |
|
| 383 |
return $return; |
| 384 |
} |
| 385 |
|
| 386 |
public function filterCalendarsForEmployee( array $ret, SH4_Employees_Model $employee ) |
| 387 |
{ |
| 388 |
$employeeId = $employee->getId(); |
| 389 |
|
| 390 |
$calendarIds = array_keys($ret); |
| 391 |
foreach( $calendarIds as $calendarId ){ |
| 392 |
$settingName = 'calendar_' . $calendarId . '_employee'; |
| 393 |
$employeesIds = $this->settings->get( $settingName, TRUE ); |
| 394 |
if( ! in_array($employeeId, $employeesIds) ){ |
| 395 |
unset( $ret[$calendarId] ); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
return $ret; |
| 400 |
} |
| 401 |
|
| 402 |
public function findCalendarsForEmployee( SH4_Employees_Model $employee ) |
| 403 |
{ |
| 404 |
$ret = $this->calendarsQuery->findActive(); |
| 405 |
$ret = $this->self->filterCalendarsForEmployee( $ret, $employee ); |
| 406 |
return $ret; |
| 407 |
} |
| 408 |
|
| 409 |
public function findAllUsersWithEmployee() |
| 410 |
{ |
| 411 |
$return = array(); |
| 412 |
|
| 413 |
$crud = $this->crudFactory->make('employee'); |
| 414 |
|
| 415 |
$args = array(); |
| 416 |
$results = $crud->read( $args ); |
| 417 |
if( ! $results ){ |
| 418 |
return $return; |
| 419 |
} |
| 420 |
|
| 421 |
$usersIds = array(); |
| 422 |
foreach( $results as $r ){ |
| 423 |
$userId = array_key_exists('user_id', $r) ? $r['user_id'] : NULL; |
| 424 |
if( ! $userId ){ |
| 425 |
continue; |
| 426 |
} |
| 427 |
$usersIds[ $userId ] = $userId; |
| 428 |
} |
| 429 |
|
| 430 |
if( ! $usersIds ){ |
| 431 |
return $return; |
| 432 |
} |
| 433 |
|
| 434 |
$return = $this->usersQuery->findManyById( $usersIds ); |
| 435 |
return $return; |
| 436 |
} |
| 437 |
|
| 438 |
public function findUserByEmployee( SH4_Employees_Model $employee ) |
| 439 |
{ |
| 440 |
$return = NULL; |
| 441 |
|
| 442 |
$employeeId = $employee->getId(); |
| 443 |
if( ! $employeeId ){ |
| 444 |
return $return; |
| 445 |
} |
| 446 |
|
| 447 |
$crud = $this->crudFactory->make('employee'); |
| 448 |
$args = array(); |
| 449 |
$args[] = array('id', '=', $employeeId ); |
| 450 |
$results = $crud->read( $args ); |
| 451 |
|
| 452 |
if( ! $results ){ |
| 453 |
return $return; |
| 454 |
} |
| 455 |
|
| 456 |
$results = array_shift( $results ); |
| 457 |
$userId = array_key_exists('user_id', $results) ? $results['user_id'] : NULL; |
| 458 |
|
| 459 |
if( ! $userId ){ |
| 460 |
return $return; |
| 461 |
} |
| 462 |
|
| 463 |
$return = $this->usersQuery->findById( $userId ); |
| 464 |
return $return; |
| 465 |
} |
| 466 |
|
| 467 |
public function findEmployeeByUser( HC3_Users_Model $user ) |
| 468 |
{ |
| 469 |
$return = NULL; |
| 470 |
|
| 471 |
$userId = $user->getId(); |
| 472 |
if( ! $userId ){ |
| 473 |
return $return; |
| 474 |
} |
| 475 |
|
| 476 |
$crud = $this->crudFactory->make('employee'); |
| 477 |
$args = array(); |
| 478 |
$args[] = array('user_id', '=', $userId ); |
| 479 |
$results = $crud->read( $args ); |
| 480 |
|
| 481 |
if( ! $results ){ |
| 482 |
return $return; |
| 483 |
} |
| 484 |
|
| 485 |
$results = array_shift( $results ); |
| 486 |
$employeeId = array_key_exists('id', $results) ? $results['id'] : NULL; |
| 487 |
|
| 488 |
if( ! $employeeId ){ |
| 489 |
return $return; |
| 490 |
} |
| 491 |
|
| 492 |
$return = $this->employeesQuery->findById( $employeeId ); |
| 493 |
return $return; |
| 494 |
} |
| 495 |
} |