| 1 |
<?php if (! defined('ABSPATH')) exit; // Exit if accessed directly |
| 2 |
interface SH4_Shifts_Model_ |
| 3 |
{ |
| 4 |
public function getId(); |
| 5 |
public function setId( $id ); |
| 6 |
public function getTitle(); |
| 7 |
public function getCalendar(); |
| 8 |
public function getEmployee(); |
| 9 |
|
| 10 |
public function getStart(); |
| 11 |
public function getEnd(); |
| 12 |
public function setStart( $start ); |
| 13 |
public function setEnd( $end ); |
| 14 |
|
| 15 |
public function getBreakStart(); |
| 16 |
public function getBreakEnd(); |
| 17 |
|
| 18 |
public function getStartInDay(); |
| 19 |
public function getEndInDay(); |
| 20 |
public function getBreakStartInDay(); |
| 21 |
public function getBreakEndInDay(); |
| 22 |
|
| 23 |
public function getDateStart(); |
| 24 |
public function getDateEnd(); |
| 25 |
|
| 26 |
public function isMultiDay(); |
| 27 |
|
| 28 |
public function isPublished(); |
| 29 |
public function isDraft(); |
| 30 |
public function isOpen(); |
| 31 |
|
| 32 |
public function setRawData( $data ); |
| 33 |
public function setRawDataByKey( $key, $value ); |
| 34 |
public function getRawData( $key ); |
| 35 |
public function getRawDataKeys(); |
| 36 |
|
| 37 |
public function getGroupingId(); |
| 38 |
} |
| 39 |
|
| 40 |
class SH4_Shifts_Model implements SH4_Shifts_Model_ |
| 41 |
{ |
| 42 |
const STATUS_PUBLISH = 'publish'; |
| 43 |
const STATUS_DRAFT = 'draft'; |
| 44 |
|
| 45 |
public $id = NULL; |
| 46 |
public $calendar = NULL; |
| 47 |
|
| 48 |
public $start = NULL; |
| 49 |
public $end = NULL; |
| 50 |
public $breakStart = NULL; |
| 51 |
public $breakEnd = NULL; |
| 52 |
|
| 53 |
public $employee = NULL; |
| 54 |
public $status = self::STATUS_DRAFT; |
| 55 |
|
| 56 |
public $raw = array(); |
| 57 |
|
| 58 |
public function __construct( $id, SH4_Calendars_Model $calendar, $start, $end, SH4_Employees_Model $employee, $breakStart = NULL, $breakEnd = NULL, $status = self::STATUS_DRAFT ) |
| 59 |
{ |
| 60 |
$this->id = $id; |
| 61 |
$this->calendar = $calendar; |
| 62 |
$this->start = $start; |
| 63 |
$this->end = $end; |
| 64 |
$this->breakStart = $breakStart; |
| 65 |
$this->breakEnd = $breakEnd; |
| 66 |
$this->employee = $employee; |
| 67 |
$this->status = $status; |
| 68 |
} |
| 69 |
|
| 70 |
public function getGroupingId() |
| 71 |
{ |
| 72 |
$ret = array(); |
| 73 |
|
| 74 |
$ret[] = $this->getStart(); |
| 75 |
$ret[] = $this->getEnd(); |
| 76 |
$ret[] = $this->getCalendar()->getId(); |
| 77 |
$ret[] = $this->isPublished() ? 1 : 0; |
| 78 |
|
| 79 |
$moreKeys = $this->getRawDataKeys(); |
| 80 |
$moreKeys = array_filter( $moreKeys, function($e){ return ('meta_' === substr($e, 0, strlen('meta_'))); } ); |
| 81 |
|
| 82 |
foreach( $moreKeys as $k ){ |
| 83 |
$v = $this->getRawData( $k ); |
| 84 |
if( is_string($v) ){ |
| 85 |
$ret[] = $v; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
$ret = join( '-', $ret ); |
| 90 |
return $ret; |
| 91 |
} |
| 92 |
|
| 93 |
public function setRawData( $data ) |
| 94 |
{ |
| 95 |
$this->raw = $data; |
| 96 |
return $this; |
| 97 |
} |
| 98 |
|
| 99 |
public function setRawDataByKey( $key, $value ) |
| 100 |
{ |
| 101 |
$this->raw[$key] = $value; |
| 102 |
return $this; |
| 103 |
} |
| 104 |
|
| 105 |
public function getRawData( $key ) |
| 106 |
{ |
| 107 |
$return = array_key_exists( $key, $this->raw ) ? $this->raw[$key] : NULL; |
| 108 |
return $return; |
| 109 |
} |
| 110 |
|
| 111 |
public function getRawDataKeys() |
| 112 |
{ |
| 113 |
return array_keys( $this->raw ); |
| 114 |
} |
| 115 |
|
| 116 |
public function getId() |
| 117 |
{ |
| 118 |
return $this->id; |
| 119 |
} |
| 120 |
|
| 121 |
public function setId( $id ) |
| 122 |
{ |
| 123 |
$this->id = $id; |
| 124 |
return $this; |
| 125 |
} |
| 126 |
|
| 127 |
public function getStatus() |
| 128 |
{ |
| 129 |
return $this->status; |
| 130 |
} |
| 131 |
|
| 132 |
public function isPublished() |
| 133 |
{ |
| 134 |
return ($this->status == self::STATUS_PUBLISH); |
| 135 |
} |
| 136 |
|
| 137 |
public function isDraft() |
| 138 |
{ |
| 139 |
return ($this->status == self::STATUS_DRAFT); |
| 140 |
} |
| 141 |
|
| 142 |
public function getTitle() |
| 143 |
{ |
| 144 |
$return = array(); |
| 145 |
$return[] = $this->getStart(); |
| 146 |
$return[] = $this->getEnd(); |
| 147 |
$return = join('-', $return); |
| 148 |
return $return; |
| 149 |
} |
| 150 |
|
| 151 |
public function getCalendar() |
| 152 |
{ |
| 153 |
return $this->calendar; |
| 154 |
} |
| 155 |
|
| 156 |
public function getEmployee() |
| 157 |
{ |
| 158 |
return $this->employee; |
| 159 |
} |
| 160 |
|
| 161 |
public function isOpen() |
| 162 |
{ |
| 163 |
$employee = $this->getEmployee(); |
| 164 |
$employeeId = $employee->getId(); |
| 165 |
|
| 166 |
$return = ( $employeeId == 0 ) ? TRUE : FALSE; |
| 167 |
return $return; |
| 168 |
} |
| 169 |
|
| 170 |
public function getStart() |
| 171 |
{ |
| 172 |
return $this->start; |
| 173 |
} |
| 174 |
|
| 175 |
public function getEnd() |
| 176 |
{ |
| 177 |
return $this->end; |
| 178 |
} |
| 179 |
|
| 180 |
public function setStart( $start ) |
| 181 |
{ |
| 182 |
$this->start = $start; |
| 183 |
return $this; |
| 184 |
} |
| 185 |
|
| 186 |
public function setEnd( $end ) |
| 187 |
{ |
| 188 |
$this->end = $end; |
| 189 |
return $this; |
| 190 |
} |
| 191 |
|
| 192 |
public function getBreakStart() |
| 193 |
{ |
| 194 |
return $this->breakStart; |
| 195 |
} |
| 196 |
|
| 197 |
public function getBreakEnd() |
| 198 |
{ |
| 199 |
return $this->breakEnd; |
| 200 |
} |
| 201 |
|
| 202 |
public function getStartInDay() |
| 203 |
{ |
| 204 |
$full = $this->getStart(); |
| 205 |
|
| 206 |
$hour = substr( $full, 8, 2 ); |
| 207 |
$minute = substr( $full, 10, 2 ); |
| 208 |
$return = $hour * 60 * 60 + $minute * 60; |
| 209 |
|
| 210 |
return $return; |
| 211 |
} |
| 212 |
|
| 213 |
public function getEndInDay() |
| 214 |
{ |
| 215 |
$start = $this->getStart(); |
| 216 |
$end = $this->getEnd(); |
| 217 |
|
| 218 |
$full = $this->getEnd(); |
| 219 |
|
| 220 |
$hour = substr( $full, 8, 2 ); |
| 221 |
$minute = substr( $full, 10, 2 ); |
| 222 |
$return = $hour * 60 * 60 + $minute * 60; |
| 223 |
|
| 224 |
if( $return ){ |
| 225 |
$d1 = substr( $start, 0, 8 ); |
| 226 |
$d2 = substr( $end, 0, 8 ); |
| 227 |
|
| 228 |
// overnight shifts |
| 229 |
if( $d2 > $d1 ){ |
| 230 |
$string1 = substr($start, 0, 4) . '-' . substr($start, 4, 2) . '-' . substr($start, 6, 2) . ' ' . substr($start, 8, 2) . ':' . substr($start, 10, 2) . ':00'; |
| 231 |
$string2 = substr($end, 0, 4) . '-' . substr($end, 4, 2) . '-' . substr($end, 6, 2) . ' ' . substr($end, 8, 2) . ':' . substr($end, 10, 2) . ':00'; |
| 232 |
|
| 233 |
$date = new DateTime( $string1 ); |
| 234 |
$date2 = new DateTime( $string2 ); |
| 235 |
|
| 236 |
$return = $date2->getTimestamp() - $date->getTimestamp(); |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
// overnight shifts |
| 241 |
/* |
| 242 |
$startInDay = $this->getStartInDay(); |
| 243 |
if( $return <= $startInDay ){ |
| 244 |
$return = 24*60*60 + $return; |
| 245 |
} |
| 246 |
*/ |
| 247 |
} |
| 248 |
|
| 249 |
if( 0 == $return ){ |
| 250 |
$return = 24*60*60; |
| 251 |
} |
| 252 |
|
| 253 |
return $return; |
| 254 |
} |
| 255 |
|
| 256 |
public function getBreakStartInDay() |
| 257 |
{ |
| 258 |
$ret = null; |
| 259 |
|
| 260 |
$full = (string) $this->getBreakStart(); |
| 261 |
if( ! $full ){ |
| 262 |
return $ret; |
| 263 |
} |
| 264 |
|
| 265 |
$hour = (int) substr( $full, 8, 2 ); |
| 266 |
$minute = (int) substr( $full, 10, 2 ); |
| 267 |
$ret = $hour * 60 * 60 + $minute * 60; |
| 268 |
|
| 269 |
return $ret; |
| 270 |
} |
| 271 |
|
| 272 |
public function getBreakEndInDay() |
| 273 |
{ |
| 274 |
$ret = null; |
| 275 |
|
| 276 |
$full = (string) $this->getBreakEnd(); |
| 277 |
if( ! $full ){ |
| 278 |
return $ret; |
| 279 |
} |
| 280 |
|
| 281 |
$hour = (int) substr( $full, 8, 2 ); |
| 282 |
$minute = (int) substr( $full, 10, 2 ); |
| 283 |
$ret = $hour * 60 * 60 + $minute * 60; |
| 284 |
|
| 285 |
if( 0 == $ret ){ |
| 286 |
$ret = 24*60*60; |
| 287 |
} |
| 288 |
|
| 289 |
return $ret; |
| 290 |
} |
| 291 |
|
| 292 |
public function getDateStart() |
| 293 |
{ |
| 294 |
$full = $this->getStart(); |
| 295 |
$return = substr( $full, 0, 8 ); |
| 296 |
return $return; |
| 297 |
} |
| 298 |
|
| 299 |
public function getDateEnd() |
| 300 |
{ |
| 301 |
$full = $this->getEnd(); |
| 302 |
$return = substr( $full, 0, 8 ); |
| 303 |
return $return; |
| 304 |
} |
| 305 |
|
| 306 |
public function isMultiDay() |
| 307 |
{ |
| 308 |
$startInDay = $this->getStartInDay(); |
| 309 |
$endInDay = $this->getEndInDay(); |
| 310 |
|
| 311 |
$return = FALSE; |
| 312 |
if( (0 == $startInDay) && ((24*60*60 == $endInDay) OR (0 == $endInDay)) ){ |
| 313 |
$return = TRUE; |
| 314 |
} |
| 315 |
|
| 316 |
return $return; |
| 317 |
} |
| 318 |
} |