| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
#[AllowDynamicProperties] |
| 3 |
class SH4_Employees_Html_Admin_Controller_Calendars |
| 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( $employeeId ) |
| 26 |
{ |
| 27 |
$employee = $this->employeesQuery->findById( $employeeId ); |
| 28 |
|
| 29 |
$current = $this->appQuery->findCalendarsForEmployee( $employee ); |
| 30 |
$currentIds = array_keys( $current ); |
| 31 |
|
| 32 |
$newIds = $this->post->get('calendar'); |
| 33 |
if( ! $newIds ){ |
| 34 |
$newIds = array(); |
| 35 |
} |
| 36 |
|
| 37 |
$toAddIds = array_diff( $newIds, $currentIds ); |
| 38 |
$toRemoveIds = array_diff( $currentIds, $newIds ); |
| 39 |
|
| 40 |
if( $toAddIds ){ |
| 41 |
$toAdd = $this->calendarsQuery->findManyActiveById( $toAddIds ); |
| 42 |
foreach( $toAdd as $calendar ){ |
| 43 |
$this->appCommand->addEmployeeToCalendar( $employee, $calendar ); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if( $toRemoveIds ){ |
| 48 |
$toRemove = $this->calendarsQuery->findManyActiveById( $toRemoveIds ); |
| 49 |
foreach( $toRemove as $calendar ){ |
| 50 |
$this->appCommand->removeEmployeeFromCalendar( $employee, $calendar ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
$return = array( 'admin/employees', '__Employee Updated__' ); |
| 55 |
return $return; |
| 56 |
} |
| 57 |
} |