| 1 |
<?php |
| 2 |
abstract class _Timeblock_model extends MY_model |
| 3 |
{ |
| 4 |
/* gets conflicting shifts and timeoffs */ |
| 5 |
public function conflicts( |
| 6 |
$use_shifts = TRUE, |
| 7 |
$use_timeoffs = TRUE, |
| 8 |
$shifts = NULL, |
| 9 |
$timeoffs = NULL, |
| 10 |
$force_date = NULL |
| 11 |
) |
| 12 |
{ |
| 13 |
$return = array(); |
| 14 |
|
| 15 |
/* if no user is set yet then none */ |
| 16 |
if( ! isset($this->user_id) ) |
| 17 |
{ |
| 18 |
if( is_object($this->user) ) |
| 19 |
{ |
| 20 |
if( ! $this->user->id ) |
| 21 |
{ |
| 22 |
$this->user->get(); |
| 23 |
} |
| 24 |
} |
| 25 |
else |
| 26 |
{ |
| 27 |
$user_id = $this->user; |
| 28 |
$this->user = new User_model; |
| 29 |
$this->user->get_by_id( $user_id ); |
| 30 |
} |
| 31 |
$this->user_id = $this->user->id; |
| 32 |
} |
| 33 |
if( ! $this->user_id ) |
| 34 |
return $return; |
| 35 |
|
| 36 |
if( ($this->my_class() == 'timeoff') && in_array($this->status, array(TIMEOFF_MODEL::STATUS_DENIED) ) ) |
| 37 |
return $return; |
| 38 |
|
| 39 |
if( ($shifts === NULL) OR ($timeoffs === NULL) ) |
| 40 |
{ |
| 41 |
if( is_object($this->user) ) |
| 42 |
{ |
| 43 |
if( ! $this->user->id ) |
| 44 |
{ |
| 45 |
$this->user->get(); |
| 46 |
} |
| 47 |
} |
| 48 |
else |
| 49 |
{ |
| 50 |
$user_id = $this->user; |
| 51 |
$this->user = new User_model; |
| 52 |
$this->user->get_by_id( $user_id ); |
| 53 |
$this->user_id = $this->user->id; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
$date = $this->date; |
| 58 |
$start = $this->start; |
| 59 |
$end = $this->end; |
| 60 |
|
| 61 |
$CI =& ci_get_instance(); |
| 62 |
|
| 63 |
if( $force_date ) |
| 64 |
{ |
| 65 |
$CI->hc_time->setDateDb( $force_date ); |
| 66 |
$CI->hc_time->modify( '-1 day' ); |
| 67 |
$yesterday = $CI->hc_time->formatDate_Db(); |
| 68 |
|
| 69 |
$CI->hc_time->modify( '+2 days' ); |
| 70 |
$tomorrow = $CI->hc_time->formatDate_Db(); |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
$CI->hc_time->setDateDb( $date ); |
| 75 |
$CI->hc_time->modify( '-1 day' ); |
| 76 |
$yesterday = $CI->hc_time->formatDate_Db(); |
| 77 |
|
| 78 |
if( $this->date_end ) |
| 79 |
{ |
| 80 |
$CI->hc_time->setDateDb( $this->date_end ); |
| 81 |
$CI->hc_time->modify( '+1 day' ); |
| 82 |
$tomorrow = $CI->hc_time->formatDate_Db(); |
| 83 |
} |
| 84 |
else |
| 85 |
{ |
| 86 |
$CI->hc_time->modify( '+2 days' ); |
| 87 |
$tomorrow = $CI->hc_time->formatDate_Db(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/* if we load shifts here */ |
| 92 |
if( $use_shifts ) |
| 93 |
{ |
| 94 |
if( $shifts === NULL ) |
| 95 |
{ |
| 96 |
$this->user->shift |
| 97 |
->include_related( 'user', 'id' ) |
| 98 |
->where( 'date >=', $yesterday ) |
| 99 |
->where( 'date <=', $tomorrow ); |
| 100 |
|
| 101 |
if( $this->id && ($this->my_class() == 'shift') ) |
| 102 |
$this->user->shift->where( 'id <>', $this->id ); |
| 103 |
|
| 104 |
$shifts = $this->user->shift->get()->all; |
| 105 |
} |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
$shifts = array(); |
| 110 |
} |
| 111 |
|
| 112 |
/* if we load timeoffs here */ |
| 113 |
if( $use_timeoffs ) |
| 114 |
{ |
| 115 |
if( $timeoffs === NULL ) |
| 116 |
{ |
| 117 |
$this->user->timeoff |
| 118 |
->include_related( 'user', 'id' ) |
| 119 |
->where( 'date <=', $tomorrow ) |
| 120 |
->where( 'date_end >=', $yesterday ) |
| 121 |
->where_not_in( 'status', array(TIMEOFF_MODEL::STATUS_DENIED) ); |
| 122 |
if( $this->id && ($this->my_class() == 'timeoff') ) |
| 123 |
$this->user->timeoff->where( 'id <>', $this->id ); |
| 124 |
|
| 125 |
$timeoffs = $this->user->timeoff->get()->all; |
| 126 |
} |
| 127 |
} |
| 128 |
else |
| 129 |
{ |
| 130 |
$timeoffs = array(); |
| 131 |
} |
| 132 |
|
| 133 |
/* now check shifts */ |
| 134 |
reset( $shifts ); |
| 135 |
foreach( $shifts as $sh ) |
| 136 |
{ |
| 137 |
if( $sh->date > $tomorrow ) |
| 138 |
break; |
| 139 |
if( $sh->date < $yesterday ) |
| 140 |
continue; |
| 141 |
|
| 142 |
if( $sh->user_id != $this->user_id ) |
| 143 |
continue; |
| 144 |
if( ($sh->id == $this->id) && ($sh->my_class() == $this->my_class()) ) |
| 145 |
continue; |
| 146 |
|
| 147 |
if( $sh->date == $yesterday ) |
| 148 |
{ |
| 149 |
if( ($sh->end - 24*60*60) > $start ) // overnight |
| 150 |
$return[] = $sh; |
| 151 |
} |
| 152 |
elseif( $sh->date == $tomorrow ) |
| 153 |
{ |
| 154 |
if( ($end - 24*60*60) > $sh->start ) // this overnight |
| 155 |
$return[] = $sh; |
| 156 |
} |
| 157 |
elseif( $sh->date == $date ) |
| 158 |
{ |
| 159 |
if( ($sh->start < $end) && ($sh->end > $start) ) // overlaps today |
| 160 |
{ |
| 161 |
$return[] = $sh; |
| 162 |
} |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
$return[] = $sh; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/* now check timeoffs */ |
| 171 |
reset( $timeoffs ); |
| 172 |
foreach( $timeoffs as $sh ) |
| 173 |
{ |
| 174 |
if( $sh->date > $tomorrow ) |
| 175 |
break; |
| 176 |
if( $sh->date_end < $yesterday ) |
| 177 |
continue; |
| 178 |
|
| 179 |
if( $sh->user_id != $this->user_id ) |
| 180 |
continue; |
| 181 |
if( ($sh->id == $this->id) && ($sh->my_class() == $this->my_class()) ) |
| 182 |
continue; |
| 183 |
|
| 184 |
if( $sh->date_end == $yesterday ) |
| 185 |
{ |
| 186 |
if( ($sh->end - 24*60*60) > $start ) // overnight |
| 187 |
$return[] = $sh; |
| 188 |
} |
| 189 |
elseif( $sh->date == $tomorrow ) |
| 190 |
{ |
| 191 |
if( ($end - 24*60*60) > $sh->start ) // this overnight |
| 192 |
$return[] = $sh; |
| 193 |
} |
| 194 |
elseif( $sh->date == $date ) |
| 195 |
{ |
| 196 |
if( ($sh->start < $end) && ($sh->end > $start) ) // overlaps today |
| 197 |
{ |
| 198 |
$return[] = $sh; |
| 199 |
} |
| 200 |
} |
| 201 |
else |
| 202 |
{ |
| 203 |
$return[] = $sh; |
| 204 |
} |
| 205 |
} |
| 206 |
return $return; |
| 207 |
} |
| 208 |
|
| 209 |
/* validation */ |
| 210 |
public function _conflict( $field ) |
| 211 |
{ |
| 212 |
// check if this staff is having a timeoff or other shift |
| 213 |
if ( isset($this->{$field}) ) |
| 214 |
{ |
| 215 |
$conflicts = $this->conflicts(); |
| 216 |
if( $conflicts ) |
| 217 |
{ |
| 218 |
$result = array(); |
| 219 |
reset( $conflicts ); |
| 220 |
foreach( $conflicts as $c ) |
| 221 |
{ |
| 222 |
$result[] = $c->title(); |
| 223 |
} |
| 224 |
$result = join( '<br>', $result ); |
| 225 |
return $result; |
| 226 |
} |
| 227 |
return TRUE; |
| 228 |
} |
| 229 |
return FALSE; |
| 230 |
} |
| 231 |
|
| 232 |
public function _check_time( $field ) |
| 233 |
{ |
| 234 |
// if overnight |
| 235 |
if( $this->end < $this->start ) |
| 236 |
{ |
| 237 |
$this->end = 24 * 60 * 60 + $this->end; |
| 238 |
} |
| 239 |
|
| 240 |
$return = ( $this->end != $this->start ) ? TRUE : FALSE; |
| 241 |
if( ! $return ) |
| 242 |
{ |
| 243 |
$return = lang('shift_error_end_start'); |
| 244 |
} |
| 245 |
return $return; |
| 246 |
} |
| 247 |
} |
| 248 |
|