| @@ -10,9 +10,8 @@ | ||
| 10 | 10 | |
| 11 | 11 | public function findCalendarsManagedByUser( HC3_Users_Model $user ); |
| 12 | 12 | public function findCalendarsViewedByUser( HC3_Users_Model $user ); |
| 13 | 13 | |
| 14 | - public function filterEmployeesForCalendar( array $employeeList, SH4_Calendars_Model $calendar ); | |
| 15 | 14 | public function findEmployeesForCalendar( SH4_Calendars_Model $calendar ); |
| 16 | 15 | public function findCalendarsForEmployee( SH4_Employees_Model $employee ); |
| 17 | 16 | |
| 18 | 17 | public function findShiftTypesForCalendar( SH4_Calendars_Model $calendar ); |
| @@ -22,19 +21,8 @@ | ||
| 22 | 21 | } |
| 23 | 22 | |
| 24 | 23 | class SH4_App_Query implements SH4_App_IQuery |
| 25 | 24 | { |
| 26 | - public $self, $settings, $permission, $shiftTypesQuery, $calendarsQuery, $employeesQuery, $cp, $t, $usersQuery, $crudFactory; | |
| 27 | - | |
| 28 | - public $repoEmployee = array(); | |
| 29 | - public $repoActiveEmployee = array(); | |
| 30 | - public $repoActiveCalendar = array(); | |
| 31 | - public $repoShiftType = array(); | |
| 32 | - public $repoUser = array(); | |
| 33 | - | |
| 34 | - public $repoEmployeeUser = array(); | |
| 35 | - public $repoUserEmployee = array(); | |
| 36 | - | |
| 37 | 25 | public function __construct( |
| 38 | 26 | HC3_Settings $settings, |
| 39 | 27 | HC3_IPermission $permission, |
| 40 | 28 | |
| @@ -60,36 +48,8 @@ | ||
| 60 | 48 | $this->usersQuery = $hooks->wrap( $usersQuery ); |
| 61 | 49 | $this->cp = $hooks->wrap( $cp ); |
| 62 | 50 | |
| 63 | 51 | $this->self = $hooks->wrap( $this ); |
| 64 | - | |
| 65 | - // employee | |
| 66 | - $this->repoActiveEmployee = $this->employeesQuery->findActive(); | |
| 67 | - $this->repoEmployee = $this->repoActiveEmployee; | |
| 68 | - | |
| 69 | - // calendar | |
| 70 | - $this->repoActiveCalendar = $this->calendarsQuery->findActive(); | |
| 71 | - | |
| 72 | - // shift type | |
| 73 | - $this->repoShiftType = $this->shiftTypesQuery->findAll(); | |
| 74 | - | |
| 75 | - // employee-user | |
| 76 | - global $wpdb; | |
| 77 | - $sql = 'SELECT post_id AS employee_id, meta_value AS user_id FROM ' . $wpdb->postmeta . ' wpostmeta' . ' JOIN ' . $wpdb->posts . ' wposts ON wposts.ID = wpostmeta.post_id WHERE wpostmeta.meta_key="user_id" AND wposts.post_type="sh4_employee"'; | |
| 78 | - $res = $wpdb->get_results( $sql, ARRAY_A ); | |
| 79 | - foreach( $res as $e ){ | |
| 80 | - if (!isset($e['user_id'])) { | |
| 81 | - continue; | |
| 82 | - } | |
| 83 | - if( ! $e['employee_id'] ) continue; | |
| 84 | - if( ! $e['user_id'] ) continue; | |
| 85 | - $this->repoEmployeeUser[ $e['employee_id'] ] = $e['user_id']; | |
| 86 | - $this->repoUserEmployee[ $e['user_id'] ?? '' ] = $e['employee_id']; | |
| 87 | - } | |
| 88 | - | |
| 89 | - if( $this->repoUserEmployee ){ | |
| 90 | - $this->repoUser = $this->usersQuery->findManyById( array_keys($this->repoUserEmployee) ); | |
| 91 | - } | |
| 92 | 52 | } |
| 93 | 53 | |
| 94 | 54 | public function findCalendarsForChange( SH4_Shifts_Model $shift ) |
| 95 | 55 | { |
| @@ -272,62 +232,50 @@ | ||
| 272 | 232 | |
| 273 | 233 | return $return; |
| 274 | 234 | } |
| 275 | 235 | |
| 276 | - public function filterCalendarsManagedByUser( array $ret, HC3_Users_Model $user ) | |
| 236 | + public function findCalendarsManagedByUser( HC3_Users_Model $user ) | |
| 277 | 237 | { |
| 238 | + $return = $this->calendarsQuery->findActive(); | |
| 239 | + | |
| 278 | 240 | $isAdmin = $this->permission->isAdmin( $user ); |
| 279 | 241 | if( $isAdmin ){ |
| 280 | - return $ret; | |
| 242 | + return $return; | |
| 281 | 243 | } |
| 282 | 244 | |
| 283 | 245 | $userId = $user->getId(); |
| 284 | - foreach( $ret as $calendar ){ | |
| 246 | + foreach( $return as $calendar ){ | |
| 285 | 247 | $calendarId = $calendar->getId(); |
| 286 | 248 | $managers = $this->self->findManagersForCalendar( $calendar ); |
| 287 | 249 | if( ! isset($managers[$userId])){ |
| 288 | - unset($ret[$calendarId]); | |
| 250 | + unset($return[$calendarId]); | |
| 289 | 251 | } |
| 290 | 252 | } |
| 291 | 253 | |
| 292 | - return $ret; | |
| 254 | + return $return; | |
| 293 | 255 | } |
| 294 | 256 | |
| 295 | - public function findCalendarsManagedByUser( HC3_Users_Model $user ) | |
| 257 | + public function findCalendarsViewedByUser( HC3_Users_Model $user ) | |
| 296 | 258 | { |
| 297 | - // $ret = $this->calendarsQuery->findActive(); | |
| 298 | - // $ret = $this->self->filterCalendarsManagedByUser( $ret, $user ); | |
| 299 | - $ret = $this->self->filterCalendarsManagedByUser( $this->repoActiveCalendar, $user ); | |
| 300 | - return $ret; | |
| 301 | - } | |
| 259 | + $return = $this->calendarsQuery->findActive(); | |
| 302 | 260 | |
| 303 | - public function filterCalendarsViewedByUser( array $ret, HC3_Users_Model $user ) | |
| 304 | - { | |
| 305 | 261 | $isAdmin = $this->permission->isAdmin( $user ); |
| 306 | 262 | if( $isAdmin ){ |
| 307 | - return $ret; | |
| 263 | + return $return; | |
| 308 | 264 | } |
| 309 | 265 | |
| 310 | 266 | $userId = $user->getId(); |
| 311 | - foreach( $ret as $calendar ){ | |
| 267 | + foreach( $return as $calendar ){ | |
| 312 | 268 | $calendarId = $calendar->getId(); |
| 313 | 269 | $viewers = $this->self->findViewersForCalendar( $calendar ); |
| 314 | 270 | if( ! isset($viewers[$userId])){ |
| 315 | - unset($ret[$calendarId]); | |
| 271 | + unset($return[$calendarId]); | |
| 316 | 272 | } |
| 317 | 273 | } |
| 318 | 274 | |
| 319 | - return $ret; | |
| 275 | + return $return; | |
| 320 | 276 | } |
| 321 | 277 | |
| 322 | - public function findCalendarsViewedByUser( HC3_Users_Model $user ) | |
| 323 | - { | |
| 324 | - // $ret = $this->calendarsQuery->findActive(); | |
| 325 | - // $ret = $this->self->filterCalendarsViewedByUser( $ret, $user ); | |
| 326 | - $ret = $this->self->filterCalendarsViewedByUser( $this->repoActiveCalendar, $user ); | |
| 327 | - return $ret; | |
| 328 | - } | |
| 329 | - | |
| 330 | 278 | public function findManagersForCalendar( SH4_Calendars_Model $calendar ) |
| 331 | 279 | { |
| 332 | 280 | $return = $this->permission->findAdmins(); |
| 333 | 281 | |
| @@ -367,177 +315,140 @@ | ||
| 367 | 315 | |
| 368 | 316 | return $return; |
| 369 | 317 | } |
| 370 | 318 | |
| 371 | - public function filterEmployeesForCalendar( array $employeeList, SH4_Calendars_Model $calendar ) | |
| 372 | - { | |
| 373 | - $ret = array(); | |
| 374 | - | |
| 375 | - $calendarId = $calendar->getId(); | |
| 376 | - $settingName = 'calendar_' . $calendarId . '_employee'; | |
| 377 | - $employeeIds = $this->settings->get( $settingName, true ); | |
| 378 | - | |
| 379 | - if( ! $employeeIds ){ | |
| 380 | - return $ret; | |
| 381 | - } | |
| 382 | - | |
| 383 | - $ret = $employeeList; | |
| 384 | - | |
| 385 | - $ids = array_keys( $ret ); | |
| 386 | - foreach( $ids as $id ){ | |
| 387 | - if( ! in_array($id, $employeeIds) ){ | |
| 388 | - unset( $ret[$id] ); | |
| 389 | - } | |
| 390 | - } | |
| 391 | - | |
| 392 | - return $ret; | |
| 393 | - } | |
| 394 | - | |
| 395 | 319 | public function findEmployeesForCalendar( SH4_Calendars_Model $calendar ) |
| 396 | 320 | { |
| 397 | - $ret = array(); | |
| 321 | + $return = array(); | |
| 398 | 322 | |
| 399 | 323 | $calendarId = $calendar->getId(); |
| 400 | 324 | $settingName = 'calendar_' . $calendarId . '_employee'; |
| 401 | 325 | |
| 402 | - $listEmployeeId = $this->settings->get( $settingName, true ); | |
| 403 | - if( $listEmployeeId ){ | |
| 404 | - $ret = array_intersect_key( $this->repoActiveEmployee, array_combine($listEmployeeId, $listEmployeeId) ); | |
| 405 | - // $ret = $this->employeesQuery->findManyActiveById( $listEmployeeId ); | |
| 326 | + $employeesIds = $this->settings->get( $settingName, TRUE ); | |
| 327 | + if( $employeesIds ){ | |
| 328 | + $return = $this->employeesQuery->findManyActiveById( $employeesIds ); | |
| 406 | 329 | } |
| 407 | 330 | |
| 408 | - return $ret; | |
| 331 | + return $return; | |
| 409 | 332 | } |
| 410 | 333 | |
| 411 | 334 | public function findShiftTypesForCalendar( SH4_Calendars_Model $calendar ) |
| 412 | 335 | { |
| 413 | - $ret = array(); | |
| 336 | + $return = array(); | |
| 414 | 337 | |
| 415 | 338 | $calendarId = $calendar->getId(); |
| 416 | 339 | $settingName = 'calendar_' . $calendarId . '_shifttype'; |
| 417 | 340 | |
| 418 | - $listShiftTypeId = $this->settings->get( $settingName, true ); | |
| 419 | - if( $listShiftTypeId ){ | |
| 420 | - // $ret = $this->shiftTypesQuery->findManyById( $listShiftTypeId ); | |
| 421 | - $ret = array_intersect_key( $this->repoShiftType, array_combine($listShiftTypeId, $listShiftTypeId) ); | |
| 341 | + $shiftTypesIds = $this->settings->get( $settingName, TRUE ); | |
| 342 | + if( $shiftTypesIds ){ | |
| 343 | + $return = $this->shiftTypesQuery->findManyById( $shiftTypesIds ); | |
| 422 | 344 | } |
| 423 | 345 | |
| 424 | - return $ret; | |
| 346 | + return $return; | |
| 425 | 347 | } |
| 426 | 348 | |
| 427 | - public function filterCalendarsForEmployee( array $ret, SH4_Employees_Model $employee ) | |
| 349 | + public function findCalendarsForEmployee( SH4_Employees_Model $employee ) | |
| 428 | 350 | { |
| 351 | + $return = array(); | |
| 429 | 352 | $employeeId = $employee->getId(); |
| 430 | 353 | |
| 431 | - $calendarIds = array_keys($ret); | |
| 432 | - foreach( $calendarIds as $calendarId ){ | |
| 354 | + $ids = array(); | |
| 355 | + $calendars = $this->calendarsQuery->findActive(); | |
| 356 | + | |
| 357 | + foreach( $calendars as $calendar ){ | |
| 358 | + $calendarId = $calendar->getId(); | |
| 433 | 359 | $settingName = 'calendar_' . $calendarId . '_employee'; |
| 434 | 360 | $employeesIds = $this->settings->get( $settingName, TRUE ); |
| 435 | - if( ! in_array($employeeId, $employeesIds) ){ | |
| 436 | - unset( $ret[$calendarId] ); | |
| 361 | + if( in_array($employeeId, $employeesIds) ){ | |
| 362 | + $return[ $calendarId ] = $calendar; | |
| 437 | 363 | } |
| 438 | 364 | } |
| 439 | - | |
| 440 | - return $ret; | |
| 365 | + return $return; | |
| 441 | 366 | } |
| 442 | 367 | |
| 443 | - public function findCalendarsForEmployee( SH4_Employees_Model $employee ) | |
| 444 | - { | |
| 445 | - // $ret = $this->calendarsQuery->findActive(); | |
| 446 | - // $ret = $this->self->filterCalendarsForEmployee( $ret, $employee ); | |
| 447 | - $ret = $this->self->filterCalendarsForEmployee( $this->repoActiveCalendar, $employee ); | |
| 448 | - return $ret; | |
| 449 | - } | |
| 450 | - | |
| 451 | 368 | public function findAllUsersWithEmployee() |
| 452 | 369 | { |
| 453 | - $ret = array(); | |
| 370 | + $return = array(); | |
| 454 | 371 | |
| 455 | - $usersIds = array_keys( $this->repoUserEmployee ); | |
| 372 | + $crud = $this->crudFactory->make('employee'); | |
| 456 | 373 | |
| 457 | - // $crud = $this->crudFactory->make('employee'); | |
| 374 | + $args = array(); | |
| 375 | + $results = $crud->read( $args ); | |
| 376 | + if( ! $results ){ | |
| 377 | + return $return; | |
| 378 | + } | |
| 458 | 379 | |
| 459 | - // $args = array(); | |
| 460 | - // $results = $crud->read( $args ); | |
| 461 | - // if( ! $results ){ | |
| 462 | - // return $ret; | |
| 463 | - // } | |
| 380 | + $usersIds = array(); | |
| 381 | + foreach( $results as $r ){ | |
| 382 | + $userId = array_key_exists('user_id', $r) ? $r['user_id'] : NULL; | |
| 383 | + if( ! $userId ){ | |
| 384 | + continue; | |
| 385 | + } | |
| 386 | + $usersIds[ $userId ] = $userId; | |
| 387 | + } | |
| 464 | 388 | |
| 465 | - // $usersIds = array(); | |
| 466 | - // foreach( $results as $r ){ | |
| 467 | - // $userId = array_key_exists('user_id', $r) ? $r['user_id'] : NULL; | |
| 468 | - // if( ! $userId ){ | |
| 469 | - // continue; | |
| 470 | - // } | |
| 471 | - // $usersIds[ $userId ] = $userId; | |
| 472 | - // } | |
| 473 | - | |
| 474 | 389 | if( ! $usersIds ){ |
| 475 | - return $ret; | |
| 390 | + return $return; | |
| 476 | 391 | } |
| 477 | 392 | |
| 478 | - $ret = $this->usersQuery->findManyById( $usersIds ); | |
| 479 | - return $ret; | |
| 393 | + $return = $this->usersQuery->findManyById( $usersIds ); | |
| 394 | + return $return; | |
| 480 | 395 | } |
| 481 | 396 | |
| 482 | 397 | public function findUserByEmployee( SH4_Employees_Model $employee ) |
| 483 | 398 | { |
| 484 | - $ret = null; | |
| 399 | + $return = NULL; | |
| 485 | 400 | |
| 486 | 401 | $employeeId = $employee->getId(); |
| 487 | 402 | if( ! $employeeId ){ |
| 488 | - return $ret; | |
| 403 | + return $return; | |
| 489 | 404 | } |
| 490 | 405 | |
| 491 | - $userId = isset( $this->repoEmployeeUser[$employeeId] ) ? $this->repoEmployeeUser[$employeeId] : null; | |
| 406 | + $crud = $this->crudFactory->make('employee'); | |
| 407 | + $args = array(); | |
| 408 | + $args[] = array('id', '=', $employeeId ); | |
| 409 | + $results = $crud->read( $args ); | |
| 492 | 410 | |
| 493 | - // $crud = $this->crudFactory->make('employee'); | |
| 494 | - // $args = array(); | |
| 495 | - // $args[] = array('id', '=', $employeeId ); | |
| 496 | - // $results = $crud->read( $args ); | |
| 411 | + if( ! $results ){ | |
| 412 | + return $return; | |
| 413 | + } | |
| 497 | 414 | |
| 498 | - // if( ! $results ){ | |
| 499 | - // return $ret; | |
| 500 | - // } | |
| 415 | + $results = array_shift( $results ); | |
| 416 | + $userId = array_key_exists('user_id', $results) ? $results['user_id'] : NULL; | |
| 501 | 417 | |
| 502 | - // $results = array_shift( $results ); | |
| 503 | - // $userId = array_key_exists('user_id', $results) ? $results['user_id'] : null; | |
| 504 | - | |
| 505 | 418 | if( ! $userId ){ |
| 506 | - return $ret; | |
| 419 | + return $return; | |
| 507 | 420 | } |
| 508 | 421 | |
| 509 | - // $ret = $this->usersQuery->findById( $userId ); | |
| 510 | - | |
| 511 | - if( ! isset($this->repoUser[$userId]) ){ | |
| 512 | - $this->repoUser[$userId] = $this->usersQuery->findById( $userId ); | |
| 513 | - } | |
| 514 | - $ret = $this->repoUser[$userId]; | |
| 515 | - | |
| 516 | - return $ret; | |
| 422 | + $return = $this->usersQuery->findById( $userId ); | |
| 423 | + return $return; | |
| 517 | 424 | } |
| 518 | 425 | |
| 519 | - public function findEmployeeByUserId( $userId ) | |
| 426 | + public function findEmployeeByUser( HC3_Users_Model $user ) | |
| 520 | 427 | { |
| 521 | - $ret = null; | |
| 428 | + $return = NULL; | |
| 429 | + | |
| 430 | + $userId = $user->getId(); | |
| 522 | 431 | if( ! $userId ){ |
| 523 | - return $ret; | |
| 432 | + return $return; | |
| 524 | 433 | } |
| 525 | 434 | |
| 526 | - $employeeId = isset( $this->repoUserEmployee[$userId] ) ? $this->repoUserEmployee[$userId] : null; | |
| 527 | - if( ! $employeeId ){ | |
| 528 | - return $ret; | |
| 435 | + $crud = $this->crudFactory->make('employee'); | |
| 436 | + $args = array(); | |
| 437 | + $args[] = array('user_id', '=', $userId ); | |
| 438 | + $results = $crud->read( $args ); | |
| 439 | + | |
| 440 | + if( ! $results ){ | |
| 441 | + return $return; | |
| 529 | 442 | } |
| 530 | 443 | |
| 531 | - if( ! isset($this->repoEmployee[$employeeId]) ){ | |
| 532 | - $this->repoEmployee[$employeeId] = $this->employeesQuery->findById( $employeeId ); | |
| 444 | + $results = array_shift( $results ); | |
| 445 | + $employeeId = array_key_exists('id', $results) ? $results['id'] : NULL; | |
| 446 | + | |
| 447 | + if( ! $employeeId ){ | |
| 448 | + return $return; | |
| 533 | 449 | } |
| 534 | - $ret = $this->repoEmployee[$employeeId]; | |
| 535 | 450 | |
| 536 | - return $ret; | |
| 537 | - } | |
| 538 | - | |
| 539 | - public function findEmployeeByUser( HC3_Users_Model $user ) | |
| 540 | - { | |
| 541 | - return $this->self->findEmployeeByUserId( $user->getId() ); | |
| 451 | + $return = $this->employeesQuery->findById( $employeeId ); | |
| 452 | + return $return; | |
| 542 | 453 | } |
| 543 | 454 | } |