PluginProbe
ShiftController Employee Shift Scheduling / 4.9.59
ShiftController Employee Shift Scheduling v4.9.59
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
← All changes | sh4/app/query.php +67 -115 trunk4.9.59 View file →
@@ -24,17 +24,8 @@
24 24 class SH4_App_Query implements SH4_App_IQuery
25 25 {
26 26 public $self, $settings, $permission, $shiftTypesQuery, $calendarsQuery, $employeesQuery, $cp, $t, $usersQuery, $crudFactory;
27 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 28 public function __construct(
38 29 HC3_Settings $settings,
39 30 HC3_IPermission $permission,
40 31
@@ -60,36 +51,8 @@
60 51 $this->usersQuery = $hooks->wrap( $usersQuery );
61 52 $this->cp = $hooks->wrap( $cp );
62 53
63 54 $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 55 }
93 56
94 57 public function findCalendarsForChange( SH4_Shifts_Model $shift )
95 58 {
@@ -293,11 +256,10 @@
293 256 }
294 257
295 258 public function findCalendarsManagedByUser( HC3_Users_Model $user )
296 259 {
297 - // $ret = $this->calendarsQuery->findActive();
298 - // $ret = $this->self->filterCalendarsManagedByUser( $ret, $user );
299 - $ret = $this->self->filterCalendarsManagedByUser( $this->repoActiveCalendar, $user );
260 + $ret = $this->calendarsQuery->findActive();
261 + $ret = $this->self->filterCalendarsManagedByUser( $ret, $user );
300 262 return $ret;
301 263 }
302 264
303 265 public function filterCalendarsViewedByUser( array $ret, HC3_Users_Model $user )
@@ -320,11 +282,10 @@
320 282 }
321 283
322 284 public function findCalendarsViewedByUser( HC3_Users_Model $user )
323 285 {
324 - // $ret = $this->calendarsQuery->findActive();
325 - // $ret = $this->self->filterCalendarsViewedByUser( $ret, $user );
326 - $ret = $this->self->filterCalendarsViewedByUser( $this->repoActiveCalendar, $user );
286 + $ret = $this->calendarsQuery->findActive();
287 + $ret = $this->self->filterCalendarsViewedByUser( $ret, $user );
327 288 return $ret;
328 289 }
329 290
330 291 public function findManagersForCalendar( SH4_Calendars_Model $calendar )
@@ -393,36 +354,34 @@
393 354 }
394 355
395 356 public function findEmployeesForCalendar( SH4_Calendars_Model $calendar )
396 357 {
397 - $ret = array();
358 + $return = array();
398 359
399 360 $calendarId = $calendar->getId();
400 361 $settingName = 'calendar_' . $calendarId . '_employee';
401 362
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 );
363 + $employeesIds = $this->settings->get( $settingName, TRUE );
364 + if( $employeesIds ){
365 + $return = $this->employeesQuery->findManyActiveById( $employeesIds );
406 366 }
407 367
408 - return $ret;
368 + return $return;
409 369 }
410 370
411 371 public function findShiftTypesForCalendar( SH4_Calendars_Model $calendar )
412 372 {
413 - $ret = array();
373 + $return = array();
414 374
415 375 $calendarId = $calendar->getId();
416 376 $settingName = 'calendar_' . $calendarId . '_shifttype';
417 377
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) );
378 + $shiftTypesIds = $this->settings->get( $settingName, TRUE );
379 + if( $shiftTypesIds ){
380 + $return = $this->shiftTypesQuery->findManyById( $shiftTypesIds );
422 381 }
423 382
424 - return $ret;
383 + return $return;
425 384 }
426 385
427 386 public function filterCalendarsForEmployee( array $ret, SH4_Employees_Model $employee )
428 387 {
@@ -441,103 +400,96 @@
441 400 }
442 401
443 402 public function findCalendarsForEmployee( SH4_Employees_Model $employee )
444 403 {
445 - // $ret = $this->calendarsQuery->findActive();
446 - // $ret = $this->self->filterCalendarsForEmployee( $ret, $employee );
447 - $ret = $this->self->filterCalendarsForEmployee( $this->repoActiveCalendar, $employee );
404 + $ret = $this->calendarsQuery->findActive();
405 + $ret = $this->self->filterCalendarsForEmployee( $ret, $employee );
448 406 return $ret;
449 407 }
450 408
451 409 public function findAllUsersWithEmployee()
452 410 {
453 - $ret = array();
411 + $return = array();
454 412
455 - $usersIds = array_keys( $this->repoUserEmployee );
413 + $crud = $this->crudFactory->make('employee');
456 414
457 - // $crud = $this->crudFactory->make('employee');
415 + $args = array();
416 + $results = $crud->read( $args );
417 + if( ! $results ){
418 + return $return;
419 + }
458 420
459 - // $args = array();
460 - // $results = $crud->read( $args );
461 - // if( ! $results ){
462 - // return $ret;
463 - // }
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 + }
464 429
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 430 if( ! $usersIds ){
475 - return $ret;
431 + return $return;
476 432 }
477 433
478 - $ret = $this->usersQuery->findManyById( $usersIds );
479 - return $ret;
434 + $return = $this->usersQuery->findManyById( $usersIds );
435 + return $return;
480 436 }
481 437
482 438 public function findUserByEmployee( SH4_Employees_Model $employee )
483 439 {
484 - $ret = null;
440 + $return = NULL;
485 441
486 442 $employeeId = $employee->getId();
487 443 if( ! $employeeId ){
488 - return $ret;
444 + return $return;
489 445 }
490 446
491 - $userId = isset( $this->repoEmployeeUser[$employeeId] ) ? $this->repoEmployeeUser[$employeeId] : null;
447 + $crud = $this->crudFactory->make('employee');
448 + $args = array();
449 + $args[] = array('id', '=', $employeeId );
450 + $results = $crud->read( $args );
492 451
493 - // $crud = $this->crudFactory->make('employee');
494 - // $args = array();
495 - // $args[] = array('id', '=', $employeeId );
496 - // $results = $crud->read( $args );
452 + if( ! $results ){
453 + return $return;
454 + }
497 455
498 - // if( ! $results ){
499 - // return $ret;
500 - // }
456 + $results = array_shift( $results );
457 + $userId = array_key_exists('user_id', $results) ? $results['user_id'] : NULL;
501 458
502 - // $results = array_shift( $results );
503 - // $userId = array_key_exists('user_id', $results) ? $results['user_id'] : null;
504 -
505 459 if( ! $userId ){
506 - return $ret;
460 + return $return;
507 461 }
508 462
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;
463 + $return = $this->usersQuery->findById( $userId );
464 + return $return;
517 465 }
518 466
519 - public function findEmployeeByUserId( $userId )
467 + public function findEmployeeByUser( HC3_Users_Model $user )
520 468 {
521 - $ret = null;
469 + $return = NULL;
470 +
471 + $userId = $user->getId();
522 472 if( ! $userId ){
523 - return $ret;
473 + return $return;
524 474 }
525 475
526 - $employeeId = isset( $this->repoUserEmployee[$userId] ) ? $this->repoUserEmployee[$userId] : null;
527 - if( ! $employeeId ){
528 - return $ret;
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;
529 483 }
530 484
531 - if( ! isset($this->repoEmployee[$employeeId]) ){
532 - $this->repoEmployee[$employeeId] = $this->employeesQuery->findById( $employeeId );
485 + $results = array_shift( $results );
486 + $employeeId = array_key_exists('id', $results) ? $results['id'] : NULL;
487 +
488 + if( ! $employeeId ){
489 + return $return;
533 490 }
534 - $ret = $this->repoEmployee[$employeeId];
535 491
536 - return $ret;
537 - }
538 -
539 - public function findEmployeeByUser( HC3_Users_Model $user )
540 - {
541 - return $this->self->findEmployeeByUserId( $user->getId() );
492 + $return = $this->employeesQuery->findById( $employeeId );
493 + return $return;
542 494 }
543 495 }