auth = $auth; $this->appQuery = $hooks->wrap( $appQuery ); $this->calendarsPermissions = $hooks->wrap( $calendarsPermissions ); $this->calendarsQuery = $hooks->wrap( $calendarsQuery ); $this->employeesQuery = $hooks->wrap( $employeesQuery ); } public function findAllEmployees() { $return = array(); $currentUser = $this->auth->getCurrentUser(); $currentUserId = $currentUser->getId(); if( ! $currentUserId ){ return $return; } $allowed = array(); // as employee $employee = $this->appQuery->findEmployeeByUser( $currentUser ); if( $employee ){ $employeeId = $employee->getId(); $allowed[ $employeeId ] = $employee; } // as manager $managedCalendars = $this->appQuery->findCalendarsManagedByUser( $currentUser ); foreach( $managedCalendars as $calendar ){ $thisEmployees = $this->appQuery->findEmployeesForCalendar( $calendar ); $allowed = $allowed + $thisEmployees; } $allowedIds = array_keys( $allowed ); if( $allowedIds ){ $return = $this->employeesQuery->findManyActiveById( $allowedIds ); } return $return; } public function findAllCalendars() { $return = array(); $currentUser = $this->auth->getCurrentUser(); $currentUserId = $currentUser->getId(); if( ! $currentUserId ){ return $return; } $allowed = array(); // as employee $employee = $this->appQuery->findEmployeeByUser( $currentUser ); if( $employee ){ $employeeCalendars = $this->appQuery->findCalendarsForEmployee( $employee ); foreach( $employeeCalendars as $thisCalendar ){ $calendarId = $thisCalendar->getId(); $checkPerms = array( 'employee_create_own_draft', 'employee_create_own_publish' ); foreach( $checkPerms as $perm ){ if( $this->calendarsPermissions->get($thisCalendar, $perm) ){ $allowed[ $calendarId ] = $thisCalendar; break; } } } } // as manager $managedCalendars = $this->appQuery->findCalendarsManagedByUser( $currentUser ); if( $managedCalendars ){ $allowed = $allowed + $managedCalendars; } $allowedIds = array_keys( $allowed ); if( $allowedIds ){ $return = $this->calendarsQuery->findManyActiveById( $allowedIds ); } return $return; } }