| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Calendars_Html_Admin_Controller_Employees |
| 4 |
{ |
| 5 |
public function __construct( |
| 6 |
HC3_Hooks $hooks, |
| 7 |
HC3_Post $post, |
| 8 |
|
| 9 |
SH4_Employees_Query $employeesQuery, |
| 10 |
SH4_Calendars_Query $calendarsQuery, |
| 11 |
|
| 12 |
SH4_App_Query $appQuery, |
| 13 |
SH4_App_Command $appCommand |
| 14 |
) |
| 15 |
{ |
| 16 |
$this->post = $hooks->wrap( $post ); |
| 17 |
|
| 18 |
$this->calendarsQuery = $hooks->wrap( $calendarsQuery ); |
| 19 |
$this->employeesQuery = $hooks->wrap( $employeesQuery ); |
| 20 |
|
| 21 |
$this->appQuery = $hooks->wrap( $appQuery ); |
| 22 |
$this->appCommand = $hooks->wrap( $appCommand ); |
| 23 |
} |
| 24 |
|
| 25 |
public function execute( $calendarId ) |
| 26 |
{ |
| 27 |
$calendar = $this->calendarsQuery->findById( $calendarId ); |
| 28 |
|
| 29 |
$currentEmployees = $this->appQuery->findEmployeesForCalendar( $calendar ); |
| 30 |
$currentEmployeesIds = array_keys( $currentEmployees ); |
| 31 |
|
| 32 |
$employeesIds = $this->post->get('employee'); |
| 33 |
if( ! $employeesIds ){ |
| 34 |
$employeesIds = array(); |
| 35 |
} |
| 36 |
|
| 37 |
$toAddIds = array_diff( $employeesIds, $currentEmployeesIds ); |
| 38 |
$toRemoveIds = array_diff( $currentEmployeesIds, $employeesIds ); |
| 39 |
|
| 40 |
if( $toAddIds ){ |
| 41 |
$toAdd = $this->employeesQuery->findManyActiveById( $toAddIds ); |
| 42 |
|
| 43 |
foreach( $toAdd as $employee ){ |
| 44 |
$this->appCommand->addEmployeeToCalendar( $employee, $calendar ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if( $toRemoveIds ){ |
| 49 |
$toRemove = $this->employeesQuery->findManyActiveById( $toRemoveIds ); |
| 50 |
foreach( $toRemove as $employee ){ |
| 51 |
$this->appCommand->removeEmployeeFromCalendar( $employee, $calendar ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
$return = array( 'admin/calendars', '__Calendar Updated__' ); |
| 56 |
return $return; |
| 57 |
} |
| 58 |
} |