| 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 $crudFactory, $settings, $shiftTypesCommand, $calendarsCommand, $employeesCommand, $shiftsCommand; |
| 25 |
|
| 26 |
public function __construct( |
| 27 |
HC3_CrudFactory $crudFactory, |
| 28 |
HC3_Settings $settings, |
| 29 |
HC3_Hooks $hooks, |
| 30 |
|
| 31 |
SH4_ShiftTypes_Command $shiftTypesCommand, |
| 32 |
SH4_Calendars_Command $calendarsCommand, |
| 33 |
SH4_Employees_Command $employeesCommand, |
| 34 |
SH4_Shifts_Command $shiftsCommand |
| 35 |
) |
| 36 |
{ |
| 37 |
$this->crudFactory = $hooks->wrap( $crudFactory ); |
| 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 |
|
| 62 |
$iis = array_keys( $managersIds ); |
| 63 |
foreach( $iis as $ii ){ |
| 64 |
if( ! is_numeric($managersIds[$ii]) ){ |
| 65 |
unset( $managersIds[$ii] ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
if( ! in_array($userId, $managersIds) ){ |
| 70 |
$managersIds[] = $userId; |
| 71 |
} |
| 72 |
|
| 73 |
$this->settings->set( $settingName, $managersIds ); |
| 74 |
} |
| 75 |
|
| 76 |
public function removeManagerFromCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ) |
| 77 |
{ |
| 78 |
$userId = $user->getId(); |
| 79 |
$calendarId = $calendar->getId(); |
| 80 |
$settingName = 'calendar_' . $calendarId . '_manager'; |
| 81 |
|
| 82 |
$managersIds = $this->settings->get( $settingName, TRUE ); |
| 83 |
$managersIds = HC3_Functions::removeFromArray( $managersIds, $userId ); |
| 84 |
|
| 85 |
$this->settings->set( $settingName, $managersIds ); |
| 86 |
} |
| 87 |
|
| 88 |
public function addViewerToCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ) |
| 89 |
{ |
| 90 |
$userId = $user->getId(); |
| 91 |
$calendarId = $calendar->getId(); |
| 92 |
$settingName = 'calendar_' . $calendarId . '_viewer'; |
| 93 |
|
| 94 |
$managersIds = $this->settings->get( $settingName, TRUE ); |
| 95 |
|
| 96 |
$iis = array_keys( $managersIds ); |
| 97 |
foreach( $iis as $ii ){ |
| 98 |
if( ! is_numeric($managersIds[$ii]) ){ |
| 99 |
unset( $managersIds[$ii] ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if( ! in_array($userId, $managersIds) ){ |
| 104 |
$managersIds[] = $userId; |
| 105 |
} |
| 106 |
|
| 107 |
$this->settings->set( $settingName, $managersIds ); |
| 108 |
} |
| 109 |
|
| 110 |
public function removeViewerFromCalendar( HC3_Users_Model $user, SH4_Calendars_Model $calendar ) |
| 111 |
{ |
| 112 |
$userId = $user->getId(); |
| 113 |
$calendarId = $calendar->getId(); |
| 114 |
$settingName = 'calendar_' . $calendarId . '_viewer'; |
| 115 |
|
| 116 |
$managersIds = $this->settings->get( $settingName, TRUE ); |
| 117 |
$managersIds = HC3_Functions::removeFromArray( $managersIds, $userId ); |
| 118 |
|
| 119 |
$this->settings->set( $settingName, $managersIds ); |
| 120 |
} |
| 121 |
|
| 122 |
public function addEmployeeToCalendar( SH4_Employees_Model $employee, SH4_Calendars_Model $calendar ) |
| 123 |
{ |
| 124 |
$employeeId = $employee->getId(); |
| 125 |
$calendarId = $calendar->getId(); |
| 126 |
$settingName = 'calendar_' . $calendarId . '_employee'; |
| 127 |
|
| 128 |
$employeesIds = $this->settings->get( $settingName, TRUE ); |
| 129 |
|
| 130 |
$iis = array_keys( $employeesIds ); |
| 131 |
foreach( $iis as $ii ){ |
| 132 |
if( ! is_numeric($employeesIds[$ii]) ){ |
| 133 |
unset( $employeesIds[$ii] ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if( ! in_array($employeeId, $employeesIds) ){ |
| 138 |
$employeesIds[] = $employeeId; |
| 139 |
} |
| 140 |
|
| 141 |
$this->settings->set( $settingName, $employeesIds ); |
| 142 |
} |
| 143 |
|
| 144 |
public function addShiftTypeToCalendar( SH4_ShiftTypes_Model $shiftType, SH4_Calendars_Model $calendar ) |
| 145 |
{ |
| 146 |
$shiftTypeId = $shiftType->getId(); |
| 147 |
$calendarId = $calendar->getId(); |
| 148 |
$settingName = 'calendar_' . $calendarId . '_shifttype'; |
| 149 |
|
| 150 |
$shiftTypesIds = $this->settings->get( $settingName, TRUE ); |
| 151 |
|
| 152 |
$iis = array_keys( $shiftTypesIds ); |
| 153 |
foreach( $iis as $ii ){ |
| 154 |
if( ! is_numeric($shiftTypesIds[$ii]) ){ |
| 155 |
unset( $shiftTypesIds[$ii] ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if( ! in_array($shiftTypeId, $shiftTypesIds) ){ |
| 160 |
$shiftTypesIds[] = $shiftTypeId; |
| 161 |
} |
| 162 |
|
| 163 |
$this->settings->set( $settingName, $shiftTypesIds ); |
| 164 |
} |
| 165 |
|
| 166 |
public function removeShiftTypeFromCalendar( SH4_ShiftTypes_Model $shiftType, SH4_Calendars_Model $calendar ) |
| 167 |
{ |
| 168 |
$shiftTypeId = $shiftType->getId(); |
| 169 |
$calendarId = $calendar->getId(); |
| 170 |
$settingName = 'calendar_' . $calendarId . '_shifttype'; |
| 171 |
|
| 172 |
$shiftTypesIds = $this->settings->get( $settingName, TRUE ); |
| 173 |
$shiftTypesIds = HC3_Functions::removeFromArray( $shiftTypesIds, $shiftTypeId ); |
| 174 |
|
| 175 |
$this->settings->set( $settingName, $shiftTypesIds ); |
| 176 |
} |
| 177 |
|
| 178 |
public function removeEmployeeFromCalendar( SH4_Employees_Model $employee, SH4_Calendars_Model $calendar ) |
| 179 |
{ |
| 180 |
$employeeId = $employee->getId(); |
| 181 |
$calendarId = $calendar->getId(); |
| 182 |
$settingName = 'calendar_' . $calendarId . '_employee'; |
| 183 |
|
| 184 |
$employeesIds = $this->settings->get( $settingName, TRUE ); |
| 185 |
$employeesIds = HC3_Functions::removeFromArray( $employeesIds, $employeeId ); |
| 186 |
|
| 187 |
$this->settings->set( $settingName, $employeesIds ); |
| 188 |
} |
| 189 |
|
| 190 |
public function linkEmployeeToUser( SH4_Employees_Model $employee, HC3_Users_Model $user ) |
| 191 |
{ |
| 192 |
$employeeId = $employee->getId(); |
| 193 |
if( ! $employeeId ){ |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
$userId = $user->getId(); |
| 198 |
if( ! $userId ){ |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
$crud = $this->crudFactory->make('employee'); |
| 203 |
|
| 204 |
$values = array( 'user_id' => $userId ); |
| 205 |
$results = $crud->update( $employeeId, $values ); |
| 206 |
} |
| 207 |
|
| 208 |
public function unlinkEmployeeFromUser( SH4_Employees_Model $employee ) |
| 209 |
{ |
| 210 |
$employeeId = $employee->getId(); |
| 211 |
if( ! $employeeId ){ |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
$crud = $this->crudFactory->make('employee'); |
| 216 |
|
| 217 |
$values = array( 'user_id' => NULL ); |
| 218 |
$results = $crud->update( $employeeId, $values ); |
| 219 |
} |
| 220 |
} |