| 1 |
<?php |
| 2 |
include_once( dirname(__FILE__) . '/_timeblock_model.php'); |
| 3 |
class Shift_model extends _Timeblock_model |
| 4 |
{ |
| 5 |
var $table = 'shifts'; |
| 6 |
var $default_order_by = array('date' => 'ASC', 'start' => 'ASC', 'id' => 'ASC'); |
| 7 |
var $has_one = array( |
| 8 |
'user' => array( |
| 9 |
'class' => 'user_model', |
| 10 |
'other_field' => 'shift', |
| 11 |
), |
| 12 |
'location' => array( |
| 13 |
'class' => 'location_model', |
| 14 |
'other_field' => 'shift', |
| 15 |
), |
| 16 |
); |
| 17 |
|
| 18 |
const STATUS_ACTIVE = 1; |
| 19 |
const STATUS_DRAFT = 2; |
| 20 |
|
| 21 |
var $prop_text = array( |
| 22 |
'status' => array( |
| 23 |
self::STATUS_ACTIVE => array( 'lang:shift_status_active', 'success' ), |
| 24 |
self::STATUS_DRAFT => array( 'lang:shift_status_draft', 'warning' ), |
| 25 |
), |
| 26 |
); |
| 27 |
|
| 28 |
var $validation = array( |
| 29 |
'date' => array( |
| 30 |
'label' => 'lang:time_date', |
| 31 |
'rules' => array('required', 'trim'), |
| 32 |
), |
| 33 |
'location' => array( |
| 34 |
'label' => 'lang:location', |
| 35 |
'rules' => array('required'), |
| 36 |
), |
| 37 |
'end' => array( |
| 38 |
'label' => 'lang:shift_end', |
| 39 |
'rules' => array('check_time', 'conflict'), |
| 40 |
), |
| 41 |
'status' => array( |
| 42 |
'label' => 'lang:shift_status', |
| 43 |
'rules' => array( |
| 44 |
'enum' => array( |
| 45 |
self::STATUS_ACTIVE, |
| 46 |
self::STATUS_DRAFT, |
| 47 |
) |
| 48 |
), |
| 49 |
), |
| 50 |
); |
| 51 |
|
| 52 |
var $my_fields = array( |
| 53 |
array( |
| 54 |
'name' => 'date', |
| 55 |
'type' => 'date', |
| 56 |
'label' => 'lang:time_date', |
| 57 |
// 'hide' => TRUE, |
| 58 |
), |
| 59 |
array( |
| 60 |
'name' => 'location', |
| 61 |
'label' => 'lang:location', |
| 62 |
'type' => 'dropdown', |
| 63 |
), |
| 64 |
array( |
| 65 |
'name' => 'user', |
| 66 |
'label' => 'lang:user_level_staff', |
| 67 |
'type' => 'dropdown', |
| 68 |
), |
| 69 |
array( |
| 70 |
'name' => 'start', |
| 71 |
'type' => 'time', |
| 72 |
'label' => 'lang:shift_start', |
| 73 |
'required' => TRUE, |
| 74 |
'rules' => array(), |
| 75 |
), |
| 76 |
array( |
| 77 |
'name' => 'end', |
| 78 |
'type' => 'time', |
| 79 |
'label' => 'lang:shift_end', |
| 80 |
'required' => TRUE, |
| 81 |
'rules' => array('check_time', 'conflict'), |
| 82 |
), |
| 83 |
array( |
| 84 |
'name' => 'status', |
| 85 |
'label' => 'lang:shift_status', |
| 86 |
'hide' => TRUE, |
| 87 |
), |
| 88 |
); |
| 89 |
|
| 90 |
public function view_text() |
| 91 |
{ |
| 92 |
$return = parent::view_text(); |
| 93 |
unset( $return['start'] ); |
| 94 |
unset( $return['end'] ); |
| 95 |
unset( $return['status'] ); |
| 96 |
$return['date'][1] = $this->date_view(); |
| 97 |
|
| 98 |
/* optimize it sometime later */ |
| 99 |
$lm = new Location_Model; |
| 100 |
$location_count = $lm->count(); |
| 101 |
if( $location_count < 2 ) |
| 102 |
{ |
| 103 |
unset( $return['location'] ); |
| 104 |
} |
| 105 |
|
| 106 |
return $return; |
| 107 |
} |
| 108 |
|
| 109 |
public function title( $html = FALSE ) |
| 110 |
{ |
| 111 |
$return = ''; |
| 112 |
if( $html ) |
| 113 |
{ |
| 114 |
$return .= '<i class="icon-time"></i> '; |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
$return .= lang('shift') . ': '; |
| 119 |
} |
| 120 |
|
| 121 |
$return .= $this->date_view(); |
| 122 |
return $return; |
| 123 |
} |
| 124 |
|
| 125 |
public function date_view() |
| 126 |
{ |
| 127 |
$return = ''; |
| 128 |
$CI =& ci_get_instance(); |
| 129 |
$CI->hc_time->setDateDb( $this->date ); |
| 130 |
|
| 131 |
$return .= $CI->hc_time->formatDate(); |
| 132 |
$return .= ' [' . $CI->hc_time->formatTimeOfDay($this->start) . ' - ' . $CI->hc_time->formatTimeOfDay($this->end) . ']'; |
| 133 |
return $return; |
| 134 |
} |
| 135 |
|
| 136 |
public function id_label() |
| 137 |
{ |
| 138 |
return $this->prop_text('status', TRUE); |
| 139 |
} |
| 140 |
|
| 141 |
public function get_duration() |
| 142 |
{ |
| 143 |
if( $this->end > $this->start ) |
| 144 |
$return = $this->end - $this->start; |
| 145 |
else |
| 146 |
$return = $this->end + (24*60*60 - $this->start); |
| 147 |
return $return; |
| 148 |
} |
| 149 |
|
| 150 |
/* remove archived users if any */ |
| 151 |
public function get_form_fields() |
| 152 |
{ |
| 153 |
$return = parent::get_form_fields(); |
| 154 |
|
| 155 |
$remove_users = array(); |
| 156 |
$um = new User_Model; |
| 157 |
$um |
| 158 |
->select( 'id' ) |
| 159 |
->where( 'active', USER_MODEL::STATUS_ARCHIVE ) |
| 160 |
->get(); |
| 161 |
foreach( $um as $u ) |
| 162 |
{ |
| 163 |
$remove_users[] = $u->id; |
| 164 |
} |
| 165 |
|
| 166 |
if( $remove_users ) |
| 167 |
{ |
| 168 |
reset( $remove_users ); |
| 169 |
foreach( $remove_users as $rid ) |
| 170 |
{ |
| 171 |
unset( $return['user']['options'][$rid] ); |
| 172 |
} |
| 173 |
} |
| 174 |
return $return; |
| 175 |
} |
| 176 |
|
| 177 |
public function find_staff() |
| 178 |
{ |
| 179 |
/* should have date, start, end set */ |
| 180 |
$um = new User_Model; |
| 181 |
$all_staff = $um |
| 182 |
->where('active', USER_MODEL::STATUS_ACTIVE) |
| 183 |
->get()->all; |
| 184 |
|
| 185 |
$return = array(); |
| 186 |
foreach( $all_staff as $u ) |
| 187 |
{ |
| 188 |
$u->warning = array(); |
| 189 |
$return[ $u->id ] = $u; |
| 190 |
} |
| 191 |
|
| 192 |
/* find users with shifts that fully overlap this one */ |
| 193 |
$um->clear(); |
| 194 |
$bad_staff = $um |
| 195 |
->distinct() |
| 196 |
->select('id') |
| 197 |
->where_in( 'id', array_keys($return) ) |
| 198 |
->where_related( 'shift', 'date', $this->date ) |
| 199 |
->where_related( 'shift', 'start <=', $this->start ) |
| 200 |
->where_related( 'shift', 'end >=', $this->end ) |
| 201 |
->get()->all; |
| 202 |
foreach( $bad_staff as $u ) |
| 203 |
{ |
| 204 |
unset( $return[$u->id] ); |
| 205 |
} |
| 206 |
|
| 207 |
/* find overlapping timeoffs */ |
| 208 |
$tm = new Timeoff_Model; |
| 209 |
$timeoffs = $tm |
| 210 |
->where_related( 'user', 'id', array_keys($return) ) |
| 211 |
->include_related( 'user', 'id' ) |
| 212 |
|
| 213 |
->where( 'date <=', $this->date ) |
| 214 |
->where( 'date_end >=', $this->date ) |
| 215 |
->where( 'start <', $this->end ) |
| 216 |
->where( 'end >', $this->start ) |
| 217 |
->get()->all; |
| 218 |
|
| 219 |
foreach( $timeoffs as $to ) |
| 220 |
{ |
| 221 |
$return[$to->user_id]->warning = $to; |
| 222 |
} |
| 223 |
|
| 224 |
return $return; |
| 225 |
} |
| 226 |
|
| 227 |
/* gets conflicting shifts and timeoffs */ |
| 228 |
public function conflicts( $shifts = NULL, $timeoffs = NULL ) |
| 229 |
{ |
| 230 |
return parent::conflicts( TRUE, TRUE, $shifts, $timeoffs ); |
| 231 |
} |
| 232 |
|
| 233 |
function publish() |
| 234 |
{ |
| 235 |
$this->status = SHIFT_MODEL::STATUS_ACTIVE; |
| 236 |
return $this->save(); |
| 237 |
} |
| 238 |
|
| 239 |
function unpublish() |
| 240 |
{ |
| 241 |
$this->status = SHIFT_MODEL::STATUS_DRAFT; |
| 242 |
return $this->save(); |
| 243 |
} |
| 244 |
|
| 245 |
protected function _before_delete() |
| 246 |
{ |
| 247 |
$CI =& ci_get_instance(); |
| 248 |
|
| 249 |
/* delete notes */ |
| 250 |
if( $CI->hc_modules->exists('notes') ) |
| 251 |
{ |
| 252 |
$this->note->get()->delete_all(); |
| 253 |
} |
| 254 |
|
| 255 |
/* delete trades */ |
| 256 |
if( $CI->hc_modules->exists('shift_trades') ) |
| 257 |
{ |
| 258 |
$this->trade->get()->delete_all(); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
protected function _after_save() |
| 263 |
{ |
| 264 |
$changes = $this->get_changes(); |
| 265 |
if( isset($changes['id']) ) |
| 266 |
{ |
| 267 |
$log = array( |
| 268 |
'action_name' => 'create', |
| 269 |
'action_details' => '' |
| 270 |
); |
| 271 |
$this->_add_log( $log ); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|