| 1 |
<?php |
| 2 |
include_once( dirname(__FILE__) . '/_timeblock_model.php'); |
| 3 |
class Timeoff_model extends _Timeblock_model |
| 4 |
{ |
| 5 |
const STATUS_ACTIVE = 1; |
| 6 |
const STATUS_PENDING = 2; |
| 7 |
const STATUS_DENIED = 3; |
| 8 |
const STATUS_ARCHIVE = 4; |
| 9 |
const STATUS_CONFLICT = 5; |
| 10 |
|
| 11 |
var $table = 'timeoffs'; |
| 12 |
var $default_order_by = array('date' => 'ASC', 'start' => 'ASC', 'id' => 'ASC'); |
| 13 |
|
| 14 |
var $has_one = array( |
| 15 |
'user' => array( |
| 16 |
'class' => 'user_model', |
| 17 |
'other_field' => 'timeoff', |
| 18 |
) |
| 19 |
); |
| 20 |
|
| 21 |
var $validation = array( |
| 22 |
'user' => array( |
| 23 |
'label' => 'lang:user_level_staff', |
| 24 |
'rules' => array('required'), |
| 25 |
), |
| 26 |
'date' => array( |
| 27 |
'label' => 'lang:time_date_from', |
| 28 |
'rules' => array('required', 'trim', 'less_equal_than_field' => 'date_end'), |
| 29 |
), |
| 30 |
'date_end' => array( |
| 31 |
'label' => 'lang:time_date_to', |
| 32 |
'rules' => array( |
| 33 |
'required', 'trim', 'greater_equal_than_field' => 'date', |
| 34 |
// 'conflict' |
| 35 |
), |
| 36 |
), |
| 37 |
'start' => array( |
| 38 |
'label' => 'lang:shift_start', |
| 39 |
'rules' => array('required', 'trim', 'less_than_field' => 'end'), |
| 40 |
), |
| 41 |
'end' => array( |
| 42 |
'label' => 'lang:shift_end', |
| 43 |
'rules' => array( |
| 44 |
'required', 'trim', 'greater_than_field' => 'start', |
| 45 |
// 'conflict' |
| 46 |
), |
| 47 |
), |
| 48 |
'status' => array( |
| 49 |
'label' => 'lang:timeoff_status', |
| 50 |
'rules' => array( |
| 51 |
'enum' => array( |
| 52 |
self::STATUS_ACTIVE, |
| 53 |
self::STATUS_PENDING, |
| 54 |
self::STATUS_DENIED, |
| 55 |
) |
| 56 |
), |
| 57 |
), |
| 58 |
); |
| 59 |
|
| 60 |
var $prop_text = array( |
| 61 |
'status' => array( |
| 62 |
self::STATUS_ACTIVE => array( 'lang:timeoff_status_active', 'success' ), |
| 63 |
self::STATUS_PENDING => array( 'lang:timeoff_status_pending', 'warning' ), |
| 64 |
self::STATUS_DENIED => array( 'lang:timeoff_status_denied', 'info' ), |
| 65 |
self::STATUS_ARCHIVE => array( 'lang:timeoff_status_archive', 'default' ) |
| 66 |
), |
| 67 |
); |
| 68 |
|
| 69 |
var $my_fields = array( |
| 70 |
array( |
| 71 |
'name' => 'user', |
| 72 |
'type' => 'dropdown', |
| 73 |
'label' => 'lang:user_level_staff', |
| 74 |
), |
| 75 |
array( |
| 76 |
'name' => 'status', |
| 77 |
'label' => 'lang:timeoff_status', |
| 78 |
'type' => 'dropdown', |
| 79 |
), |
| 80 |
array( |
| 81 |
'name' => 'date', |
| 82 |
'type' => 'date', |
| 83 |
'required' => TRUE, |
| 84 |
'label' => 'lang:time_date', |
| 85 |
), |
| 86 |
array( |
| 87 |
'name' => 'date_end', |
| 88 |
'type' => 'date', |
| 89 |
'required' => TRUE, |
| 90 |
'label' => 'lang:time_date_to', |
| 91 |
), |
| 92 |
array( |
| 93 |
'name' => 'start', |
| 94 |
'type' => 'time', |
| 95 |
'required' => TRUE, |
| 96 |
'label' => 'lang:shift_start', |
| 97 |
), |
| 98 |
array( |
| 99 |
'name' => 'end', |
| 100 |
'type' => 'time', |
| 101 |
'required' => TRUE, |
| 102 |
'label' => 'lang:shift_end', |
| 103 |
), |
| 104 |
); |
| 105 |
|
| 106 |
public function title( $html = FALSE ) |
| 107 |
{ |
| 108 |
$return = ''; |
| 109 |
if( $html ) |
| 110 |
{ |
| 111 |
$return .= '<i class="fa-fw fa fa-coffee"></i> '; |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
$return .= lang('timeoff') . ': '; |
| 116 |
} |
| 117 |
|
| 118 |
$return .= $this->date_view(); |
| 119 |
return $return; |
| 120 |
} |
| 121 |
|
| 122 |
public function view_text( $skip = array() ) |
| 123 |
{ |
| 124 |
$return = parent::view_text( $skip ); |
| 125 |
unset( $return['date_end'] ); |
| 126 |
unset( $return['start'] ); |
| 127 |
unset( $return['end'] ); |
| 128 |
$return['date'][1] = $this->date_view(); |
| 129 |
return $return; |
| 130 |
} |
| 131 |
|
| 132 |
public function date_view() |
| 133 |
{ |
| 134 |
$return = ''; |
| 135 |
$CI =& ci_get_instance(); |
| 136 |
$CI->hc_time->setDateDb( $this->date ); |
| 137 |
|
| 138 |
$return .= $CI->hc_time->formatWeekdayShort() . ', ' . $CI->hc_time->formatDate(); |
| 139 |
if( $this->date == $this->date_end ) |
| 140 |
{ |
| 141 |
$return .= ' [' . $CI->hc_time->formatPeriodOfDay($this->start, $this->end) . ']'; |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
$CI->hc_time->setDateDb( $this->date_end ); |
| 146 |
$return .= ' - ' . $CI->hc_time->formatWeekdayShort() . ', ' . $CI->hc_time->formatDate();; |
| 147 |
} |
| 148 |
return $return; |
| 149 |
} |
| 150 |
|
| 151 |
function is_passed() |
| 152 |
{ |
| 153 |
$CI =& ci_get_instance(); |
| 154 |
$CI->hc_time->setNow(); |
| 155 |
/* if this week then not yet expired */ |
| 156 |
$CI->hc_time->setStartWeek(); |
| 157 |
$check_with = $CI->hc_time->formatDate_Db(); |
| 158 |
|
| 159 |
$return = ( $check_with > $this->date_end ) ? TRUE : FALSE; |
| 160 |
return $return; |
| 161 |
} |
| 162 |
|
| 163 |
public function id_label() |
| 164 |
{ |
| 165 |
return $this->prop_text('status', TRUE); |
| 166 |
} |
| 167 |
|
| 168 |
function get_status() |
| 169 |
{ |
| 170 |
$return = $this->status; |
| 171 |
switch( $this->status ) |
| 172 |
{ |
| 173 |
case self::STATUS_ACTIVE: |
| 174 |
if( $this->is_passed() ) |
| 175 |
$return = self::STATUS_ARCHIVE; |
| 176 |
break; |
| 177 |
} |
| 178 |
return $return; |
| 179 |
} |
| 180 |
|
| 181 |
public function conflicts( $shifts = NULL, $timeoffs = NULL, $force_date = NULL ) |
| 182 |
{ |
| 183 |
return parent::conflicts( TRUE, FALSE, $shifts, $timeoffs, $force_date ); |
| 184 |
} |
| 185 |
|
| 186 |
public function get_duration() |
| 187 |
{ |
| 188 |
if( $this->date_end == $this->date ) |
| 189 |
$return = $this->end - $this->start; |
| 190 |
else |
| 191 |
{ |
| 192 |
$CI =& ci_get_instance(); |
| 193 |
$CI->hc_time->setDateDb( $this->date_end ); |
| 194 |
$days_differ = $CI->hc_time->differ( $this->date ); |
| 195 |
$return = 24*60*60 * ($days_differ + 1); |
| 196 |
} |
| 197 |
return $return; |
| 198 |
} |
| 199 |
|
| 200 |
protected function _before_delete() |
| 201 |
{ |
| 202 |
$CI =& ci_get_instance(); |
| 203 |
|
| 204 |
/* delete notes */ |
| 205 |
if( $CI->hc_modules->exists('notes') ) |
| 206 |
{ |
| 207 |
$this->note->get()->delete_all(); |
| 208 |
} |
| 209 |
} |
| 210 |
} |