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