| 1 |
<?php |
| 2 |
class Shift_HC_Model extends MY_model |
| 3 |
{ |
| 4 |
var $table = 'shifts'; |
| 5 |
var $default_order_by = array( |
| 6 |
'date' => 'ASC', |
| 7 |
'start' => 'ASC', |
| 8 |
'date_end' => 'ASC', |
| 9 |
'end' => 'ASC', |
| 10 |
'id' => 'ASC' |
| 11 |
); |
| 12 |
var $has_one = array( |
| 13 |
'user' => array( |
| 14 |
'class' => 'user', |
| 15 |
'other_field' => 'shift', |
| 16 |
), |
| 17 |
'location' => array( |
| 18 |
'class' => 'location', |
| 19 |
'other_field' => 'shift', |
| 20 |
), |
| 21 |
); |
| 22 |
|
| 23 |
const STATUS_ACTIVE = 1; |
| 24 |
const STATUS_DRAFT = 2; |
| 25 |
|
| 26 |
const TYPE_SHIFT = 1; |
| 27 |
const TYPE_TIMEOFF = 2; |
| 28 |
|
| 29 |
var $validation = array( |
| 30 |
'date' => array('required', 'trim', 'check_date_end'), |
| 31 |
'location' => array('check_location'), |
| 32 |
'end' => array('required', 'check_time', 'check_date_end', 'check_break'), |
| 33 |
'end' => array('required', 'check_date_end', 'check_break'), |
| 34 |
'lunch_break' => array('check_break'), |
| 35 |
'status' => array( |
| 36 |
'enum' => array( |
| 37 |
self::STATUS_ACTIVE, |
| 38 |
self::STATUS_DRAFT, |
| 39 |
) |
| 40 |
), |
| 41 |
'type' => array( |
| 42 |
'enum' => array( |
| 43 |
self::TYPE_SHIFT, |
| 44 |
self::TYPE_TIMEOFF, |
| 45 |
) |
| 46 |
), |
| 47 |
); |
| 48 |
|
| 49 |
protected function _prepare_get() |
| 50 |
{ |
| 51 |
parent::_prepare_get(); |
| 52 |
$this |
| 53 |
->include_related( 'location', array('id', 'name', 'show_order', 'description', 'color'), TRUE, TRUE ) |
| 54 |
->include_related( 'user', array('id', 'first_name', 'last_name', 'active'), TRUE, TRUE ) |
| 55 |
->order_by( 'date', 'ASC' ) |
| 56 |
->order_by( 'start', 'ASC' ) |
| 57 |
->order_by( 'user_last_name IS NULL', 'ASC' ) |
| 58 |
->order_by( 'user_last_name', 'ASC' ) |
| 59 |
->order_by( 'user_first_name', 'ASC' ) |
| 60 |
->order_by( 'date_end', 'ASC' ) |
| 61 |
->order_by( 'end', 'ASC' ) |
| 62 |
->order_by( 'status', 'ASC' ) |
| 63 |
->order_by( 'location_show_order', 'ASC' ) |
| 64 |
; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
public function count($exclude_ids = NULL, $column = NULL, $related_id = NULL) |
| 69 |
{ |
| 70 |
$this->_prepare_get(); |
| 71 |
return parent::count($exclude_ids, $column, $related_id); |
| 72 |
} |
| 73 |
*/ |
| 74 |
|
| 75 |
public function get_changes( $relations = NULL ) |
| 76 |
{ |
| 77 |
$return = parent::get_changes( $relations ); |
| 78 |
$conf = HC_App::app_conf(); |
| 79 |
$show_end_time = $conf->get('show_end_time'); |
| 80 |
if( ! $show_end_time ){ |
| 81 |
unset($return['end']); |
| 82 |
} |
| 83 |
return $return; |
| 84 |
} |
| 85 |
|
| 86 |
public function get( $limit = NULL, $offset = NULL ) |
| 87 |
{ |
| 88 |
$this->_prepare_get(); |
| 89 |
return parent::get( $limit, $offset ); |
| 90 |
} |
| 91 |
|
| 92 |
public function get_iterated( $limit = NULL, $offset = NULL ) |
| 93 |
{ |
| 94 |
$this->_prepare_get(); |
| 95 |
return parent::get_iterated( $limit, $offset ); |
| 96 |
} |
| 97 |
|
| 98 |
public function from_array( $data ) |
| 99 |
{ |
| 100 |
$return = parent::from_array( $data ); |
| 101 |
$this->skip_validation( FALSE ); |
| 102 |
$this->validate(); |
| 103 |
$this->skip_validation( FALSE ); |
| 104 |
return $return; |
| 105 |
} |
| 106 |
|
| 107 |
public function get_duration( $use_break = TRUE ) |
| 108 |
{ |
| 109 |
if( |
| 110 |
( $this->date_end && ($this->date_end > $this->date) ) |
| 111 |
OR |
| 112 |
( $this->end <= $this->start ) |
| 113 |
){ |
| 114 |
$return = $this->end + (24*60*60 - $this->start); |
| 115 |
} |
| 116 |
else { |
| 117 |
$return = $this->end - $this->start; |
| 118 |
} |
| 119 |
|
| 120 |
if( $use_break ){ |
| 121 |
if( isset($this->lunch_break) && $this->lunch_break ){ |
| 122 |
$return = $return - $this->lunch_break; |
| 123 |
} |
| 124 |
} |
| 125 |
return $return; |
| 126 |
} |
| 127 |
|
| 128 |
/* checking conflicts */ |
| 129 |
public function overlaps( $two ) |
| 130 |
{ |
| 131 |
$return = TRUE; |
| 132 |
|
| 133 |
$one_start = $this->date . sprintf('%05d', $this->start); |
| 134 |
$one_end = $this->date_end . sprintf('%05d', $this->end); |
| 135 |
$two_start = $two->date . sprintf('%05d', $two->start); |
| 136 |
$two_end = $two->date_end . sprintf('%05d', $two->end); |
| 137 |
|
| 138 |
if( |
| 139 |
( $one_end <= $two_start ) |
| 140 |
OR |
| 141 |
( $one_start >= $two_end ) |
| 142 |
){ |
| 143 |
$return = FALSE; |
| 144 |
} |
| 145 |
|
| 146 |
if( $return ){ |
| 147 |
/* if me */ |
| 148 |
if( |
| 149 |
( $this->id ) && |
| 150 |
( $this->id == $two->id ) |
| 151 |
){ |
| 152 |
$return = FALSE; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return $return; |
| 157 |
} |
| 158 |
|
| 159 |
public function covers( $two ) |
| 160 |
{ |
| 161 |
$return = FALSE; |
| 162 |
|
| 163 |
$one_start = $this->date . sprintf('%05d', $this->start); |
| 164 |
$one_end = $this->date_end . sprintf('%05d', $this->end); |
| 165 |
$two_start = $two->date . sprintf('%05d', $two->start); |
| 166 |
$two_end = $two->date_end . sprintf('%05d', $two->end); |
| 167 |
|
| 168 |
if( |
| 169 |
( $one_end >= $two_end ) |
| 170 |
&& |
| 171 |
( $one_start <= $two_start ) |
| 172 |
){ |
| 173 |
$return = TRUE; |
| 174 |
} |
| 175 |
|
| 176 |
if( $return ){ |
| 177 |
/* if me */ |
| 178 |
if( |
| 179 |
( $this->id ) && |
| 180 |
( $this->id == $two->id ) |
| 181 |
){ |
| 182 |
$return = FALSE; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
return $return; |
| 187 |
} |
| 188 |
|
| 189 |
/* validation */ |
| 190 |
public function _related_check_location( $field ) |
| 191 |
{ |
| 192 |
if( $this->type == self::TYPE_TIMEOFF ){ |
| 193 |
return TRUE; |
| 194 |
} |
| 195 |
|
| 196 |
if( $this->location && $this->location->exists() ){ |
| 197 |
$return = TRUE; |
| 198 |
} |
| 199 |
else { |
| 200 |
$return = HCM::__('Required field'); |
| 201 |
} |
| 202 |
return $return; |
| 203 |
} |
| 204 |
|
| 205 |
public function _check_date_end( $field ) |
| 206 |
{ |
| 207 |
if( ! $this->date ){ |
| 208 |
return TRUE; |
| 209 |
} |
| 210 |
|
| 211 |
if( $this->end <= $this->start ){ |
| 212 |
$t = HC_Lib::time(); |
| 213 |
$t->setDateDb( $this->date ); |
| 214 |
$t->modify( '+1 day' ); |
| 215 |
$this->date_end = $t->formatDate_Db(); |
| 216 |
} |
| 217 |
else { |
| 218 |
$this->date_end = $this->date; |
| 219 |
} |
| 220 |
return TRUE; |
| 221 |
} |
| 222 |
|
| 223 |
public function _check_time( $field ) |
| 224 |
{ |
| 225 |
$return = ( $this->end != $this->start ) ? TRUE : FALSE; |
| 226 |
if( ! $return ){ |
| 227 |
$return = HCM::__('The end time should differ from the start time'); |
| 228 |
} |
| 229 |
return $return; |
| 230 |
} |
| 231 |
|
| 232 |
public function _check_break( $field ) |
| 233 |
{ |
| 234 |
$return = ( $this->get_duration(FALSE) > $this->lunch_break ) ? TRUE : FALSE; |
| 235 |
if( ! $return ){ |
| 236 |
$return = HCM::__('The break should not be longer than the shift itself'); |
| 237 |
} |
| 238 |
return $return; |
| 239 |
} |
| 240 |
|
| 241 |
public function load( $state = array() ) |
| 242 |
{ |
| 243 |
$t = HC_Lib::time(); |
| 244 |
|
| 245 |
$state_date = isset($state['date']) ? $state['date'] : ''; |
| 246 |
list( $start_date, $end_date ) = $t->getDatesRange( $state_date, $state['range'] ); |
| 247 |
|
| 248 |
if( $start_date && $end_date ){ |
| 249 |
// $shifts->where('date_end >=', $start_date); |
| 250 |
// $shifts->where('date <=', $end_date); |
| 251 |
$this->where('date_end >=', $start_date); |
| 252 |
$this->where('date <=', $end_date); |
| 253 |
$this->where('date >=', $start_date); |
| 254 |
} |
| 255 |
elseif( $start_date && $end_date === 0 ){ |
| 256 |
if( isset($state['include_yesterday']) && $state['include_yesterday'] ){ |
| 257 |
$this->group_start(); |
| 258 |
$this->where('date =', $start_date); |
| 259 |
$this->or_where('date_end =', $start_date); |
| 260 |
$this->group_end(); |
| 261 |
} |
| 262 |
else { |
| 263 |
$this->where('date =', $start_date); |
| 264 |
} |
| 265 |
} |
| 266 |
elseif( $start_date && ($end_date === NULL) ){ |
| 267 |
$this->where('date_end >=', $start_date); |
| 268 |
} |
| 269 |
|
| 270 |
/* location */ |
| 271 |
$where_location = array(); |
| 272 |
if( isset($state['location']) ){ |
| 273 |
if( ! is_array($state['location']) ){ |
| 274 |
$state['location'] = array($state['location']); |
| 275 |
} |
| 276 |
$where_location = $state['location']; |
| 277 |
} |
| 278 |
|
| 279 |
/* type */ |
| 280 |
if( isset($state['by']) && ($state['by'] == 'location') ){ |
| 281 |
$this->where('type', $this->_const('TYPE_SHIFT')); |
| 282 |
} |
| 283 |
/* |
| 284 |
if( $where_location ){ |
| 285 |
$this->where('type', $this->_const('TYPE_SHIFT')); |
| 286 |
} |
| 287 |
*/ |
| 288 |
if( $where_location ){ |
| 289 |
$this->group_start(); |
| 290 |
$this->or_where('type', $this->_const('TYPE_TIMEOFF')); |
| 291 |
$this->or_where_in_related('location', 'id', $where_location); |
| 292 |
$this->group_end(); |
| 293 |
} |
| 294 |
|
| 295 |
/* staff */ |
| 296 |
$where_staff = array(); |
| 297 |
if( isset($state['staff']) ){ |
| 298 |
if( ! is_array($state['staff']) ){ |
| 299 |
$state['staff'] = array($state['staff']); |
| 300 |
} |
| 301 |
$where_staff = $state['staff']; |
| 302 |
} |
| 303 |
|
| 304 |
if( count($where_staff) ){ |
| 305 |
if( in_array(0, $where_staff) ){ |
| 306 |
$this->group_start(); |
| 307 |
$this->or_where_related('user', 'id', NULL, TRUE); |
| 308 |
$this->or_where_related('user', 'id', 0); |
| 309 |
$this->or_where_in_related('user', 'id', $where_staff); |
| 310 |
$this->group_end(); |
| 311 |
} |
| 312 |
else { |
| 313 |
$this->where_related('user', 'id', $where_staff); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
if( isset($state['type']) && ($state['type'] !== NULL) ){ |
| 318 |
$this_types = array(); |
| 319 |
$this_statuses = array(); |
| 320 |
foreach( $state['type'] as $stype ){ |
| 321 |
if( strpos($stype, '_') === FALSE ){ |
| 322 |
$this_type = $stype; |
| 323 |
$this_types[$this_type] = 1; |
| 324 |
} |
| 325 |
else { |
| 326 |
list( $this_type, $this_status ) = explode('_', $stype); |
| 327 |
$this_types[$this_type] = 1; |
| 328 |
$this_statuses[$this_status] = 1; |
| 329 |
} |
| 330 |
} |
| 331 |
if( $this_types ){ |
| 332 |
$this->where_in('type', array_keys($this_types)); |
| 333 |
} |
| 334 |
if( $this_statuses ){ |
| 335 |
$this->where_in('status', array_keys($this_statuses)); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
/* status */ |
| 340 |
if( isset($state['status']) ){ |
| 341 |
$this->where('status', $state['status']); |
| 342 |
} |
| 343 |
|
| 344 |
/* extensions */ |
| 345 |
$extensions = HC_App::extensions(); |
| 346 |
$current_filter = ''; |
| 347 |
if( isset($state['filter']) ){ |
| 348 |
$current_filter = $state['filter']; |
| 349 |
} |
| 350 |
|
| 351 |
$return = $this; |
| 352 |
|
| 353 |
/* preprocess */ |
| 354 |
if( $current_filter ){ |
| 355 |
if( $extensions->has(array('list/filter', $current_filter)) ){ |
| 356 |
$return = $extensions->run( |
| 357 |
array('list/filter', $current_filter), |
| 358 |
'pre', |
| 359 |
$return |
| 360 |
); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/* NOW GET */ |
| 365 |
$return->get(); |
| 366 |
// $shifts->get_iterated(); |
| 367 |
// $return->check_last_query(); |
| 368 |
|
| 369 |
/* extensions with postprocess */ |
| 370 |
if( $current_filter ){ |
| 371 |
if( $extensions->has(array('list/filter', $current_filter)) ){ |
| 372 |
$return = $extensions->run( |
| 373 |
array('list/filter', $current_filter), |
| 374 |
'post', |
| 375 |
$return |
| 376 |
); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
return $return; |
| 381 |
} |
| 382 |
|
| 383 |
protected function _before_delete() |
| 384 |
{ |
| 385 |
$CI =& ci_get_instance(); |
| 386 |
|
| 387 |
/* delete notes */ |
| 388 |
if( $CI->hc_modules->exists('notes') ){ |
| 389 |
$this->note->get()->delete_all(); |
| 390 |
} |
| 391 |
|
| 392 |
/* delete logaudit */ |
| 393 |
if( $CI->hc_modules->exists('logaudit') ){ |
| 394 |
$logaudit = HC_App::model("logaudit"); |
| 395 |
$logaudit |
| 396 |
->where('object_class', $this->my_class()) |
| 397 |
->where('object_id', $this->id) |
| 398 |
->delete() |
| 399 |
; |
| 400 |
} |
| 401 |
|
| 402 |
/* delete trade */ |
| 403 |
if( $CI->hc_modules->exists('trades') ){ |
| 404 |
$this->trade_request->get()->delete_all(); |
| 405 |
$this->offer_request->get()->delete_all(); |
| 406 |
$this->offer_offer->get()->delete_all(); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
public function __get($name) |
| 411 |
{ |
| 412 |
switch( $name ){ |
| 413 |
case 'time_id': |
| 414 |
return $this->start . '-' . $this->end; |
| 415 |
break; |
| 416 |
} |
| 417 |
return parent::__get($name); |
| 418 |
} |
| 419 |
} |