| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_New_IQuery |
| 3 |
{ |
| 4 |
public function findAllCalendars(); |
| 5 |
public function findAllEmployees(); |
| 6 |
} |
| 7 |
|
| 8 |
class SH4_New_Query implements SH4_New_IQuery |
| 9 |
{ |
| 10 |
public $self, $auth, $appQuery, $calendarsPermissions, $calendarsQuery, $employeesQuery; |
| 11 |
|
| 12 |
public function __construct( |
| 13 |
HC3_Hooks $hooks, |
| 14 |
HC3_Auth $auth, |
| 15 |
|
| 16 |
SH4_App_Query $appQuery, |
| 17 |
|
| 18 |
SH4_Calendars_Permissions $calendarsPermissions, |
| 19 |
SH4_Calendars_Query $calendarsQuery, |
| 20 |
SH4_Employees_Query $employeesQuery |
| 21 |
) |
| 22 |
{ |
| 23 |
$this->auth = $auth; |
| 24 |
|
| 25 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 26 |
|
| 27 |
$this->calendarsPermissions = $hooks->wrap( $calendarsPermissions ); |
| 28 |
$this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 29 |
$this->employeesQuery = $hooks->wrap( $employeesQuery ); |
| 30 |
} |
| 31 |
|
| 32 |
public function findAllEmployees() |
| 33 |
{ |
| 34 |
$return = array(); |
| 35 |
|
| 36 |
$currentUser = $this->auth->getCurrentUser(); |
| 37 |
$currentUserId = $currentUser->getId(); |
| 38 |
if( ! $currentUserId ){ |
| 39 |
return $return; |
| 40 |
} |
| 41 |
|
| 42 |
$allowed = array(); |
| 43 |
|
| 44 |
// as employee |
| 45 |
$employee = $this->appQuery->findEmployeeByUser( $currentUser ); |
| 46 |
if( $employee ){ |
| 47 |
$employeeId = $employee->getId(); |
| 48 |
$allowed[ $employeeId ] = $employee; |
| 49 |
} |
| 50 |
|
| 51 |
// as manager |
| 52 |
$managedCalendars = $this->appQuery->findCalendarsManagedByUser( $currentUser ); |
| 53 |
foreach( $managedCalendars as $calendar ){ |
| 54 |
$thisEmployees = $this->appQuery->findEmployeesForCalendar( $calendar ); |
| 55 |
$allowed = $allowed + $thisEmployees; |
| 56 |
} |
| 57 |
|
| 58 |
$allowedIds = array_keys( $allowed ); |
| 59 |
|
| 60 |
if( $allowedIds ){ |
| 61 |
$return = $this->employeesQuery->findManyActiveById( $allowedIds ); |
| 62 |
} |
| 63 |
|
| 64 |
return $return; |
| 65 |
} |
| 66 |
|
| 67 |
public function findAllCalendars() |
| 68 |
{ |
| 69 |
$return = array(); |
| 70 |
|
| 71 |
$currentUser = $this->auth->getCurrentUser(); |
| 72 |
$currentUserId = $currentUser->getId(); |
| 73 |
if( ! $currentUserId ){ |
| 74 |
return $return; |
| 75 |
} |
| 76 |
|
| 77 |
$allowed = array(); |
| 78 |
|
| 79 |
// as employee |
| 80 |
$employee = $this->appQuery->findEmployeeByUser( $currentUser ); |
| 81 |
if( $employee ){ |
| 82 |
$employeeCalendars = $this->appQuery->findCalendarsForEmployee( $employee ); |
| 83 |
foreach( $employeeCalendars as $thisCalendar ){ |
| 84 |
$calendarId = $thisCalendar->getId(); |
| 85 |
|
| 86 |
$checkPerms = array( 'employee_create_own_draft', 'employee_create_own_publish' ); |
| 87 |
foreach( $checkPerms as $perm ){ |
| 88 |
if( $this->calendarsPermissions->get($thisCalendar, $perm) ){ |
| 89 |
$allowed[ $calendarId ] = $thisCalendar; |
| 90 |
break; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
// as manager |
| 97 |
$managedCalendars = $this->appQuery->findCalendarsManagedByUser( $currentUser ); |
| 98 |
if( $managedCalendars ){ |
| 99 |
$allowed = $allowed + $managedCalendars; |
| 100 |
} |
| 101 |
|
| 102 |
$allowedIds = array_keys( $allowed ); |
| 103 |
|
| 104 |
if( $allowedIds ){ |
| 105 |
$return = $this->calendarsQuery->findManyActiveById( $allowedIds ); |
| 106 |
} |
| 107 |
|
| 108 |
return $return; |
| 109 |
} |
| 110 |
} |