PluginProbe
ShiftController Employee Shift Scheduling / 4.9.24
ShiftController Employee Shift Scheduling v4.9.24
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 / shifttypes / command.php

command.php in ShiftController Employee Shift Scheduling 4.9.24, at sh4/shifttypes/command.php

261 lines 6.4 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_ShiftTypes_Command_
3 {
4 public function changeTitle( SH4_ShiftTypes_Model $model, $title );
5 public function changeTime( SH4_ShiftTypes_Model $model, $start, $end, $breakStart = NULL, $breakEnd = NULL );
6 public function delete( SH4_ShiftTypes_Model $model );
7 public function deleteAll();
8
9 public function createHours( $title, $start, $end, $breakStart = NULL, $breakEnd = NULL );
10 public function createDays( $title, $minDays, $maxDays );
11 }
12
13 class SH4_ShiftTypes_Command implements SH4_ShiftTypes_Command_
14 {
15 public function __construct(
16 HC3_Hooks $hooks,
17 SH4_ShiftTypes_Query $query,
18 HC3_CrudFactory $crudFactory
19 )
20 {
21 $this->crud = $hooks->wrap( $crudFactory->make('shifttype') );
22 $this->query = $hooks->wrap( $query );
23 }
24
25 public function createHours( $title, $start, $end, $breakStart = NULL, $breakEnd = NULL )
26 {
27 $errors = array();
28
29 // title is set
30 if( ! strlen($title) ){
31 $errors['title'] = '__Required Field__';
32 }
33
34 // duplicated titles
35 if( ! isset($errors['title']) ){
36 $args = array();
37 $args[] = array( 'title', '=', $title );
38
39 $already = $this->crud->count( $args );
40 if( $already ){
41 $msg = '__This value is already used__';
42 $msg .= ': ' . strip_tags( $title );
43 $errors['title'] = $msg;
44 }
45 }
46
47 // time already exists
48 if( ! isset($errors['time']) ){
49 $args = array();
50 $args[] = array( 'starts_at', '=', $start );
51 $args[] = array( 'ends_at', '=', $end );
52 $args[] = array( 'range', '=', 'hours' );
53
54 // also check break start/end
55 $matches = $this->query->read( $args );
56 foreach( $matches as $e ){
57 if( ( $breakStart === $e->getBreakStart() ) && ( $breakEnd === $e->getBreakEnd() ) ){
58 $msg = '__This value is already used__';
59 $errors['time'] = $msg;
60 break;
61 }
62 }
63 }
64
65 // break
66 if( ! isset($errors['break']) ){
67 if( (NULL !== $breakStart) && (NULL !== $breakEnd) ){
68 if( ($end > 24*60*60) && ($breakStart < $start) ){
69 $breakStart = 24*60*60 + $breakStart;
70 $breakEnd = 24*60*60 + $breakEnd;
71 }
72
73 if( ($breakStart >= $end) OR ($breakStart < $start) ){
74 $msg = '__Lunch break should be within shift hours.__';
75 $errors['break'] = $msg;
76 }
77 if( ($breakEnd > $end) OR ($breakEnd <= $start) ){
78 $msg = '__Lunch break should be within shift hours.__';
79 $errors['break'] = $msg;
80 }
81 }
82 }
83
84 if( $errors ){
85 throw new HC3_ExceptionArray( $errors );
86 }
87
88 $array = array(
89 'title' => $title,
90 'starts_at' => $start,
91 'ends_at' => $end,
92 'break_starts_at' => $breakStart,
93 'break_ends_at' => $breakEnd,
94 'range' => SH4_ShiftTypes_Model::RANGE_HOURS,
95 );
96
97 $return = $this->crud->create( $array );
98 $return = $return['id'];
99 return $return;
100 }
101
102 public function createDays( $title, $minDays, $maxDays )
103 {
104 $errors = array();
105
106 // title is set
107 if( ! strlen($title) ){
108 $errors['title'] = '__Required Field__';
109 }
110
111 // duplicated titles
112 if( ! isset($errors['title']) ){
113 $args = array();
114 $args[] = array( 'title', '=', $title );
115
116 $already = $this->crud->count( $args );
117 if( $already ){
118 $msg = '__This value is already used__';
119 $msg .= ': ' . strip_tags( $title );
120 $errors['title'] = $msg;
121 }
122 }
123
124 if( ! isset($errors['end']) ){
125 if( $maxDays < $minDays ){
126 $msg = '__Max value should not be less than min value__';
127 $errors['end'] = $msg;
128 }
129 }
130
131 if( $errors ){
132 throw new HC3_ExceptionArray( $errors );
133 }
134
135 $array = array(
136 'title' => $title,
137 'starts_at' => $minDays,
138 'ends_at' => $maxDays,
139 'range' => SH4_ShiftTypes_Model::RANGE_DAYS,
140 );
141
142 $return = $this->crud->create( $array );
143 $return = $return['id'];
144 return $return;
145 }
146
147 public function changeTitle( SH4_ShiftTypes_Model $model, $title )
148 {
149 $id = $model->getId();
150 if( ! $id ){
151 return;
152 }
153
154 $errors = array();
155
156 // title is set
157 if( ! strlen($title) ){
158 $errors['title'] = '__Required Field__';
159 }
160
161 // duplicated titles
162 if( ! isset($errors['title']) ){
163 $args = array();
164
165 $args[] = array( 'title', '=', $title );
166 $args[] = array( 'id', '<>', $id );
167
168 $already = $this->crud->count( $args );
169 if( $already ){
170 $msg = '__This value is already used__';
171 $msg .= ': ' . strip_tags( $title );
172 $errors['title'] = $msg;
173 }
174 }
175
176 if( $errors ){
177 throw new HC3_ExceptionArray( $errors );
178 }
179
180 $array = array(
181 'title' => $title
182 );
183
184 return $this->crud->update( $id, $array );
185 }
186
187 public function changeTime( SH4_ShiftTypes_Model $model, $start, $end, $breakStart = NULL, $breakEnd = NULL )
188 {
189 $id = $model->getId();
190 if( ! $id ){
191 return;
192 }
193
194 $errors = array();
195
196 // time already exists
197 if( ! isset($errors['time']) ){
198 $args = array();
199 $args[] = array( 'starts_at', '=', $start );
200 $args[] = array( 'ends_at', '=', $end );
201 $args[] = array( 'range', '=', 'hours' );
202 $args[] = array( 'id', '<>', $id );
203
204 // also check break start/end
205 $matches = $this->query->read( $args );
206 foreach( $matches as $e ){
207 if( ( $breakStart === $e->getBreakStart() ) && ( $breakEnd === $e->getBreakEnd() ) ){
208 $msg = '__This value is already used__';
209 $errors['time'] = $msg;
210 break;
211 }
212 }
213 }
214
215 // break
216 if( ! isset($errors['break']) ){
217 if( (NULL !== $breakStart) && (NULL !== $breakEnd) ){
218 if( ($end > 24*60*60) && ($breakStart < $start) ){
219 $breakStart = 24*60*60 + $breakStart;
220 $breakEnd = 24*60*60 + $breakEnd;
221 }
222 if( ($breakStart >= $end) OR ($breakStart < $start) ){
223 $msg = '__Lunch break should be within shift hours.__';
224 $errors['break'] = $msg;
225 }
226 if( ($breakEnd > $end) OR ($breakEnd <= $start) ){
227 $msg = '__Lunch break should be within shift hours.__';
228 $errors['break'] = $msg;
229 }
230 }
231 }
232
233 if( $errors ){
234 throw new HC3_ExceptionArray( $errors );
235 }
236
237 $array = array(
238 'starts_at' => $start,
239 'ends_at' => $end,
240 'break_starts_at' => $breakStart,
241 'break_ends_at' => $breakEnd,
242 );
243
244 return $this->crud->update( $id, $array );
245 }
246
247 public function delete( SH4_ShiftTypes_Model $model )
248 {
249 $id = $model->getId();
250 if( ! $id ){
251 return;
252 }
253 return $this->crud->delete( $id );
254 }
255
256 public function deleteAll()
257 {
258 return $this->crud->deleteAll();
259 }
260
261 }