PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / sh4 / shifts / command.php

command.php in ShiftController Employee Shift Scheduling 4.9.26, at sh4/shifts/command.php

264 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 interface SH4_Shifts_Command_
3 {
4 public function draft( SH4_Shifts_Model $model );
5 public function publish( SH4_Shifts_Model $model );
6 public function unpublish( SH4_Shifts_Model $model );
7 public function delete( SH4_Shifts_Model $model );
8 public function deleteAll();
9 public function create(
10 SH4_Calendars_Model $calendar,
11 $start,
12 $end,
13 SH4_Employees_Model $employee,
14 $breakStart = NULL,
15 $breakEnd = NULL,
16 $status = SH4_Shifts_Model::STATUS_DRAFT
17 );
18 public function changeEmployee( SH4_Shifts_Model $model, SH4_Employees_Model $employee );
19 public function changeCalendar( SH4_Shifts_Model $model, SH4_Calendars_Model $calendar );
20 public function reschedule( SH4_Shifts_Model $model, $newStart, $newEnd, $newStartBreak = NULL, $newEndBreak = NULL );
21 }
22
23 class SH4_Shifts_Command implements SH4_Shifts_Command_
24 {
25 public function __construct(
26 SH4_Shifts_Query $shiftsQuery,
27 HC3_Hooks $hooks,
28 HC3_CrudFactory $crudFactory
29 )
30 {
31 $this->shiftsQuery = $hooks->wrap( $shiftsQuery );
32 $this->crud = $hooks->wrap( $crudFactory->make('shift') );
33 $this->self = $hooks->wrap( $this );
34 }
35
36 public function create(
37 SH4_Calendars_Model $calendar,
38 $start,
39 $end,
40 SH4_Employees_Model $employee,
41 $breakStart = NULL,
42 $breakEnd = NULL,
43 $status = SH4_Shifts_Model::STATUS_DRAFT
44 )
45 {
46 $calendarId = $calendar->getId();
47 $employeeId = $employee ? $employee->getId() : NULL;
48
49 $array = array(
50 'calendar_id' => $calendarId,
51 'employee_id' => $employeeId,
52 'starts_at' => $start,
53 'ends_at' => $end,
54 'break_starts_at' => $breakStart,
55 'break_ends_at' => $breakEnd,
56 'status' => $status,
57 );
58
59 $return = $this->crud->create( $array );
60 $return = $return['id'];
61 return $return;
62 }
63
64 public function createNew( SH4_Shifts_Model $model )
65 {
66 $id = $model->getId();
67 if( ! $id ){
68 $id = $this->self->create(
69 $model->getCalendar(),
70 $model->getStart(),
71 $model->getEnd(),
72 $model->getEmployee(),
73 $model->getBreakStart(),
74 $model->getBreakEnd()
75 );
76 $model->setId( $id );
77 }
78
79 $this->shiftsQuery->keep( $model );
80 return $id;
81 }
82
83 public function publish( SH4_Shifts_Model $model )
84 {
85 $id = $model->getId();
86 if( ! $id ){
87 $id = $this->self->create(
88 $model->getCalendar(),
89 $model->getStart(),
90 $model->getEnd(),
91 $model->getEmployee(),
92 $model->getBreakStart(),
93 $model->getBreakEnd()
94 );
95 $model->setId( $id );
96 }
97
98 $array = array(
99 'status' => SH4_Shifts_Model::STATUS_PUBLISH
100 );
101
102 $this->crud->update( $id, $array );
103 return $id;
104 }
105
106 public function draft( SH4_Shifts_Model $model )
107 {
108 $id = $model->getId();
109
110 if( ! $id ){
111 $id = $this->self->create(
112 $model->getCalendar(),
113 $model->getStart(),
114 $model->getEnd(),
115 $model->getEmployee(),
116 $model->getBreakStart(),
117 $model->getBreakEnd()
118 );
119 $model->setId( $id );
120 }
121
122 $array = array(
123 'status' => SH4_Shifts_Model::STATUS_DRAFT
124 );
125
126 $this->crud->update( $id, $array );
127 return $id;
128 }
129
130 public function unpublish( SH4_Shifts_Model $model )
131 {
132 $id = $model->getId();
133 if( ! $id ){
134 return;
135 }
136
137 $array = array(
138 'status' => SH4_Shifts_Model::STATUS_DRAFT
139 );
140
141 $this->crud->update( $id, $array );
142 return $id;
143 }
144
145 public function delete( SH4_Shifts_Model $model )
146 {
147 $id = $model->getId();
148 if( ! $id ){
149 return;
150 }
151 return $this->crud->delete( $id );
152 }
153
154 public function deleteAll()
155 {
156 return $this->crud->deleteAll();
157 }
158
159 public function changeEmployee( SH4_Shifts_Model $model, SH4_Employees_Model $employee )
160 {
161 $id = $model->getId();
162 if( ! $id ){
163 return;
164 }
165
166 // unpublish to notify the current employee
167 $wasPublished = $model->isPublished();
168
169 $oldEmployee = $model->getEmployee();
170 if( $oldEmployee && $oldEmployee->getId() ){
171 $this->self->unpublish( $model );
172 }
173
174 $employeeId = $employee->getId();
175 $array = array(
176 'employee_id' => $employeeId,
177 );
178
179 /* quick tweak, later change it to something better */
180 $refObject = new ReflectionObject( $model );
181
182 $refProperty = $refObject->getProperty( 'employee' );
183 $refProperty->setAccessible( TRUE );
184 $refProperty->setValue( $model, $employee );
185
186 $ret = $this->crud->update( $id, $array );
187
188 if( $employeeId ){
189 if( $wasPublished ){
190 $this->self->publish( $model );
191 }
192 else {
193 $this->self->draft( $model );
194 }
195 }
196
197 return $ret;
198 }
199
200 public function changeCalendar( SH4_Shifts_Model $model, SH4_Calendars_Model $calendar )
201 {
202 $id = $model->getId();
203 if( ! $id ){
204 return;
205 }
206
207 $calendarId = $calendar->getId();
208 $array = array(
209 'calendar_id' => $calendarId,
210 );
211
212 /* quick tweak, later change it to something better */
213 $refObject = new ReflectionObject( $model );
214
215 $refProperty = $refObject->getProperty( 'calendar' );
216 $refProperty->setAccessible( TRUE );
217 $refProperty->setValue( $model, $calendar );
218
219 return $this->crud->update( $id, $array );
220 }
221
222 public function reschedule( SH4_Shifts_Model $model, $newStart, $newEnd, $newStartBreak = NULL, $newEndBreak = NULL )
223 {
224 $id = $model->getId();
225 if( ! $id ){
226 return;
227 }
228
229 $array = array(
230 'starts_at' => $newStart,
231 'ends_at' => $newEnd,
232 'break_starts_at' => NULL,
233 'break_ends_at' => NULL
234 );
235
236 if( NULL !== $newStartBreak ){
237 $array['break_starts_at'] = $newStartBreak;
238 }
239 if( NULL !== $newEndBreak ){
240 $array['break_ends_at'] = $newEndBreak;
241 }
242
243 /* quick tweak, later change it to something better */
244 $refObject = new ReflectionObject( $model );
245
246 $refProperty = $refObject->getProperty( 'start' );
247 $refProperty->setAccessible( TRUE );
248 $refProperty->setValue( $model, $array['starts_at'] );
249
250 $refProperty = $refObject->getProperty( 'end' );
251 $refProperty->setAccessible( TRUE );
252 $refProperty->setValue( $model, $array['ends_at'] );
253
254 $refProperty = $refObject->getProperty( 'breakStart' );
255 $refProperty->setAccessible( TRUE );
256 $refProperty->setValue( $model, $array['break_starts_at'] );
257
258 $refProperty = $refObject->getProperty( 'breakEnd' );
259 $refProperty->setAccessible( TRUE );
260 $refProperty->setValue( $model, $array['break_ends_at'] );
261
262 return $this->crud->update( $id, $array );
263 }
264 }