| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_App_ICommand |
| 3 |
{ |
| 4 |
public function linkEmployeeToUser( SH4_Employees_Model $employee, HC3_Users_Model $user ); |
| 5 |
public function unlinkEmployeeFromUser( SH4_Employees_Model $employee ); |
| 6 |
|
| 7 |
public function addManagerToCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ); |
| 8 |
public function removeManagerFromCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ); |
| 9 |
|
| 10 |
public function addViewerToCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ); |
| 11 |
public function removeViewerFromCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ); |
| 12 |
|
| 13 |
public function addEmployeeToCalendar( SH4_Employees_Model $employee, SH4_Calendars_Model $calendar ); |
| 14 |
public function removeEmployeeFromCalendar( SH4_Employees_Model $employee, SH4_Calendars_Model $calendar ); |
| 15 |
|
| 16 |
public function addShiftTypeToCalendar( SH4_ShiftTypes_Model $shiftType, SH4_Calendars_Model $calendar ); |
| 17 |
public function removeShiftTypeFromCalendar( SH4_ShiftTypes_Model $shiftType, SH4_Calendars_Model $calendar ); |
| 18 |
|
| 19 |
public function uninstall(); |
| 20 |
} |
| 21 |
|
| 22 |
class SH4_App_Command implements SH4_App_ICommand |
| 23 |
{ |
| 24 |
public function __construct( |
| 25 |
HC3_CrudFactory $crudFactory, |
| 26 |
HC3_Settings $settings, |
| 27 |
HC3_Hooks $hooks, |
| 28 |
|
| 29 |
SH4_ShiftTypes_Command $shiftTypesCommand, |
| 30 |
SH4_Calendars_Command $calendarsCommand, |
| 31 |
SH4_Employees_Command $employeesCommand, |
| 32 |
SH4_Shifts_Command $shiftsCommand |
| 33 |
) |
| 34 |
{ |
| 35 |
$this->crudFactory = $hooks->wrap( $crudFactory ); |
| 36 |
$this->settings = $hooks->wrap( $settings ); |
| 37 |
|
| 38 |
$this->settings = $hooks->wrap( $settings ); |
| 39 |
$this->shiftTypesCommand = $hooks->wrap( $shiftTypesCommand ); |
| 40 |
$this->calendarsCommand = $hooks->wrap( $calendarsCommand ); |
| 41 |
$this->employeesCommand = $hooks->wrap( $employeesCommand ); |
| 42 |
$this->shiftsCommand = $hooks->wrap( $shiftsCommand ); |
| 43 |
} |
| 44 |
|
| 45 |
public function uninstall() |
| 46 |
{ |
| 47 |
$this->shiftTypesCommand->deleteAll(); |
| 48 |
$this->calendarsCommand->deleteAll(); |
| 49 |
$this->employeesCommand->deleteAll(); |
| 50 |
$this->shiftsCommand->deleteAll(); |
| 51 |
$this->settings->resetAll(); |
| 52 |
} |
| 53 |
|
| 54 |
public function addManagerToCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ) |
| 55 |
{ |
| 56 |
$userId = $user->getId(); |
| 57 |
$calendarId = $calendar->getId(); |
| 58 |
$settingName = 'calendar_' . $calendarId . '_manager'; |
| 59 |
|
| 60 |
$managersIds = $this->settings->get( $settingName, TRUE ); |
| 61 |
if( ! in_array($userId, $managersIds) ){ |
| 62 |
$managersIds[] = $userId; |
| 63 |
} |
| 64 |
|
| 65 |
$this->settings->set( $settingName, $managersIds ); |
| 66 |
} |
| 67 |
|
| 68 |
public function removeManagerFromCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ) |
| 69 |
{ |
| 70 |
$userId = $user->getId(); |
| 71 |
$calendarId = $calendar->getId(); |
| 72 |
$settingName = 'calendar_' . $calendarId . '_manager'; |
| 73 |
|
| 74 |
$managersIds = $this->settings->get( $settingName, TRUE ); |
| 75 |
$managersIds = HC3_Functions::removeFromArray( $managersIds, $userId ); |
| 76 |
|
| 77 |
$this->settings->set( $settingName, $managersIds ); |
| 78 |
} |
| 79 |
|
| 80 |
public function addViewerToCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ) |
| 81 |
{ |
| 82 |
$userId = $user->getId(); |
| 83 |
$calendarId = $calendar->getId(); |
| 84 |
$settingName = 'calendar_' . $calendarId . '_viewer'; |
| 85 |
|
| 86 |
$managersIds = $this->settings->get( $settingName, TRUE ); |
| 87 |
if( ! in_array($userId, $managersIds) ){ |
| 88 |
$managersIds[] = $userId; |
| 89 |
} |
| 90 |
|
| 91 |
$this->settings->set( $settingName, $managersIds ); |
| 92 |
} |
| 93 |
|
| 94 |
public function removeViewerFromCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ) |
| 95 |
{ |
| 96 |
$userId = $user->getId(); |
| 97 |
$calendarId = $calendar->getId(); |
| 98 |
$settingName = 'calendar_' . $calendarId . '_viewer'; |
| 99 |
|
| 100 |
$managersIds = $this->settings->get( $settingName, TRUE ); |
| 101 |
$managersIds = HC3_Functions::removeFromArray( $managersIds, $userId ); |
| 102 |
|
| 103 |
$this->settings->set( $settingName, $managersIds ); |
| 104 |
} |
| 105 |
|
| 106 |
public function addEmployeeToCalendar( SH4_Employees_Model $employee, SH4_Calendars_Model $calendar ) |
| 107 |
{ |
| 108 |
$employeeId = $employee->getId(); |
| 109 |
$calendarId = $calendar->getId(); |
| 110 |
$settingName = 'calendar_' . $calendarId . '_employee'; |
| 111 |
|
| 112 |
$employeesIds = $this->settings->get( $settingName, TRUE ); |
| 113 |
if( ! in_array($employeeId, $employeesIds) ){ |
| 114 |
$employeesIds[] = $employeeId; |
| 115 |
} |
| 116 |
|
| 117 |
$this->settings->set( $settingName, $employeesIds ); |
| 118 |
} |
| 119 |
|
| 120 |
public function addShiftTypeToCalendar( SH4_ShiftTypes_Model $shiftType, SH4_Calendars_Model $calendar ) |
| 121 |
{ |
| 122 |
$shiftTypeId = $shiftType->getId(); |
| 123 |
$calendarId = $calendar->getId(); |
| 124 |
$settingName = 'calendar_' . $calendarId . '_shifttype'; |
| 125 |
|
| 126 |
$shiftTypesIds = $this->settings->get( $settingName, TRUE ); |
| 127 |
if( ! in_array($shiftTypeId, $shiftTypesIds) ){ |
| 128 |
$shiftTypesIds[] = $shiftTypeId; |
| 129 |
} |
| 130 |
|
| 131 |
$this->settings->set( $settingName, $shiftTypesIds ); |
| 132 |
} |
| 133 |
|
| 134 |
public function removeShiftTypeFromCalendar( SH4_ShiftTypes_Model $shiftType, SH4_Calendars_Model $calendar ) |
| 135 |
{ |
| 136 |
$shiftTypeId = $shiftType->getId(); |
| 137 |
$calendarId = $calendar->getId(); |
| 138 |
$settingName = 'calendar_' . $calendarId . '_shifttype'; |
| 139 |
|
| 140 |
$shiftTypesIds = $this->settings->get( $settingName, TRUE ); |
| 141 |
$shiftTypesIds = HC3_Functions::removeFromArray( $shiftTypesIds, $shiftTypeId ); |
| 142 |
|
| 143 |
$this->settings->set( $settingName, $shiftTypesIds ); |
| 144 |
} |
| 145 |
|
| 146 |
public function removeEmployeeFromCalendar( SH4_Employees_Model $employee, SH4_Calendars_Model $calendar ) |
| 147 |
{ |
| 148 |
$employeeId = $employee->getId(); |
| 149 |
$calendarId = $calendar->getId(); |
| 150 |
$settingName = 'calendar_' . $calendarId . '_employee'; |
| 151 |
|
| 152 |
$employeesIds = $this->settings->get( $settingName, TRUE ); |
| 153 |
$employeesIds = HC3_Functions::removeFromArray( $employeesIds, $employeeId ); |
| 154 |
|
| 155 |
$this->settings->set( $settingName, $employeesIds ); |
| 156 |
} |
| 157 |
|
| 158 |
public function linkEmployeeToUser( SH4_Employees_Model $employee, HC3_Users_Model $user ) |
| 159 |
{ |
| 160 |
$employeeId = $employee->getId(); |
| 161 |
if( ! $employeeId ){ |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$userId = $user->getId(); |
| 166 |
if( ! $userId ){ |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
$crud = $this->crudFactory->make('employee'); |
| 171 |
|
| 172 |
$values = array( 'user_id' => $userId ); |
| 173 |
$results = $crud->update( $employeeId, $values ); |
| 174 |
} |
| 175 |
|
| 176 |
public function unlinkEmployeeFromUser( SH4_Employees_Model $employee ) |
| 177 |
{ |
| 178 |
$employeeId = $employee->getId(); |
| 179 |
if( ! $employeeId ){ |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
$crud = $this->crudFactory->make('employee'); |
| 184 |
|
| 185 |
$values = array( 'user_id' => NULL ); |
| 186 |
$results = $crud->update( $employeeId, $values ); |
| 187 |
} |
| 188 |
} |