| 1 |
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 |
|
| 3 |
class Timeoffs_controller extends Backend_controller_crud |
| 4 |
{ |
| 5 |
function __construct() |
| 6 |
{ |
| 7 |
$this->conf = array( |
| 8 |
'model' => 'Timeoff_model', |
| 9 |
'path' => 'admin/timeoffs', |
| 10 |
'entity' => 'timeoff', |
| 11 |
); |
| 12 |
parent::__construct( User_model::LEVEL_MANAGER ); |
| 13 |
} |
| 14 |
|
| 15 |
function action() |
| 16 |
{ |
| 17 |
$action = $this->input->post('action'); |
| 18 |
$ids = $this->input->post('id'); |
| 19 |
|
| 20 |
if( $ids && $action ) |
| 21 |
{ |
| 22 |
$ok_count = 0; |
| 23 |
$failed_count = 0; |
| 24 |
$objects = array(); |
| 25 |
|
| 26 |
$this->{$this->model} |
| 27 |
->where_in( 'id', $ids ) |
| 28 |
->get()->all; |
| 29 |
$total_count = count( $ids ); |
| 30 |
|
| 31 |
$msg = array(); |
| 32 |
$msg[] = lang('timeoff'); |
| 33 |
switch( $action ) |
| 34 |
{ |
| 35 |
case 'active': |
| 36 |
$msg[] = lang( 'common_approve' ); |
| 37 |
foreach( $this->{$this->model} as $o ) |
| 38 |
{ |
| 39 |
$o->status = TIMEOFF_MODEL::STATUS_ACTIVE; |
| 40 |
if( $o->save() ) |
| 41 |
$ok_count++; |
| 42 |
} |
| 43 |
break; |
| 44 |
|
| 45 |
case 'denied': |
| 46 |
$msg[] = lang( 'common_reject' ); |
| 47 |
foreach( $this->{$this->model} as $o ) |
| 48 |
{ |
| 49 |
$o->status = TIMEOFF_MODEL::STATUS_DENIED; |
| 50 |
if( $o->save() ) |
| 51 |
$ok_count++; |
| 52 |
} |
| 53 |
break; |
| 54 |
|
| 55 |
case 'delete': |
| 56 |
$msg[] = lang( 'common_delete' ); |
| 57 |
foreach( $this->{$this->model} as $o ) |
| 58 |
{ |
| 59 |
if( $o->delete() ) |
| 60 |
$ok_count++; |
| 61 |
} |
| 62 |
break; |
| 63 |
} |
| 64 |
|
| 65 |
$failed_count = $total_count - $ok_count; |
| 66 |
if( $ok_count ) |
| 67 |
$msg[] = lang('common_ok') . ' [' . $ok_count . ']'; |
| 68 |
if( $failed_count > 0 ) |
| 69 |
$msg[] = lang('common_failed') . ' [' . $failed_count . ']'; |
| 70 |
|
| 71 |
$msg = join( ': ', $msg ); |
| 72 |
if( $failed_count > 0 ) |
| 73 |
$this->session->set_flashdata( 'error', $msg ); |
| 74 |
else |
| 75 |
$this->session->set_flashdata( 'message', $msg ); |
| 76 |
} |
| 77 |
elseif( ! $ids ) |
| 78 |
{ |
| 79 |
$msg = lang('common_no_selected'); |
| 80 |
$this->session->set_flashdata( 'error', $msg ); |
| 81 |
} |
| 82 |
|
| 83 |
$redirect_to = method_exists($this, 'after_save') ? $this->after_save() : array($this->conf['path']); |
| 84 |
$this->redirect( $redirect_to ); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
function add() |
| 89 |
{ |
| 90 |
$this->hc_time->setNow(); |
| 91 |
$this->{$this->model}->date = $this->hc_time->formatDate_Db(); |
| 92 |
|
| 93 |
$args = func_get_args(); |
| 94 |
call_user_func_array( array($this, 'parent::add'), $args ); |
| 95 |
} |
| 96 |
|
| 97 |
function edit( $id, $view = 'edit' ) |
| 98 |
{ |
| 99 |
$this->data['fixed'] = array('user'); |
| 100 |
return parent::edit( $id, $view ); |
| 101 |
} |
| 102 |
|
| 103 |
function save() |
| 104 |
{ |
| 105 |
$args = func_get_args(); |
| 106 |
$id = count($args) ? $args[0] : 0; |
| 107 |
|
| 108 |
if( $id ) |
| 109 |
{ |
| 110 |
call_user_func_array( array($this, 'parent::save'), $args ); |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
/* new one */ |
| 115 |
$post = call_user_func_array( array($this, 'save_prepare'), $args ); |
| 116 |
$relations = $this->save_prepare_relations(); |
| 117 |
|
| 118 |
$also_get = array( |
| 119 |
'repeat', |
| 120 |
'date_start', |
| 121 |
); |
| 122 |
reset( $also_get ); |
| 123 |
foreach( $also_get as $ag ) |
| 124 |
{ |
| 125 |
$post[ $ag ] = $this->input->post( $ag ); |
| 126 |
} |
| 127 |
|
| 128 |
/* check repeat */ |
| 129 |
$repeat = $this->input->post( 'repeat' ); |
| 130 |
|
| 131 |
/* add validation */ |
| 132 |
switch( $repeat ) |
| 133 |
{ |
| 134 |
case 'multiple': |
| 135 |
$this->{$this->model}->date_start = $post['date_start']; |
| 136 |
$this->{$this->model}->date = $this->{$this->model}->date_start; |
| 137 |
$this->{$this->model}->start = 0; |
| 138 |
$this->{$this->model}->end = 24*60*60; |
| 139 |
$this->{$this->model}->validation['date_start'] = $this->{$this->model}->validation['date']; |
| 140 |
unset( $this->{$this->model}->validation['date'] ); |
| 141 |
unset( $this->{$this->model}->validation['start'] ); |
| 142 |
break; |
| 143 |
|
| 144 |
case 'single': |
| 145 |
default: |
| 146 |
$this->{$this->model}->date_end = $this->{$this->model}->date; |
| 147 |
$_POST['date_end'] = $this->{$this->model}->date_end; |
| 148 |
break; |
| 149 |
} |
| 150 |
|
| 151 |
$this->{$this->model}->validate($relations); |
| 152 |
if( ! $this->{$this->model}->valid ) |
| 153 |
{ |
| 154 |
$this->hc_form->set_errors( $this->{$this->model}->error->all ); |
| 155 |
$this->hc_form->set_defaults( $post ); |
| 156 |
|
| 157 |
if( $this->{$this->model}->id ) |
| 158 |
$this->edit( $this->{$this->model}->id ); |
| 159 |
else |
| 160 |
{ |
| 161 |
array_shift( $args ); |
| 162 |
call_user_func_array( array($this, 'add'), $args ); |
| 163 |
} |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
if( $this->{$this->model}->save($relations) ) |
| 168 |
{ |
| 169 |
$notes = $this->input->post( 'notes' ); |
| 170 |
if( strlen($notes) > 0 ) |
| 171 |
{ |
| 172 |
$note = new Note_Model; |
| 173 |
$note->content = $notes; |
| 174 |
$note->save( |
| 175 |
array( |
| 176 |
'author' => $this->auth->user(), |
| 177 |
'timeoff' => $this->{$this->model} |
| 178 |
) |
| 179 |
); |
| 180 |
} |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
$this->hc_form->set_errors( $this->{$this->model}->error->all ); |
| 185 |
$this->hc_form->set_defaults( $post ); |
| 186 |
|
| 187 |
if( $this->{$this->model}->id ) |
| 188 |
$this->edit( $this->{$this->model}->id ); |
| 189 |
else |
| 190 |
{ |
| 191 |
array_shift( $args ); |
| 192 |
call_user_func_array( array($this, 'add'), $args ); |
| 193 |
} |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
// redirect to list |
| 198 |
$msg = lang('timeoff') . ': ' . lang('common_add'); |
| 199 |
$this->session->set_flashdata( 'message', $msg . ': ' . lang('common_ok') ); |
| 200 |
|
| 201 |
$redirect_to = method_exists($this, 'after_save') ? $this->after_save() : array($this->conf['path']); |
| 202 |
$this->redirect( $redirect_to ); |
| 203 |
// $this->_save_complete( TRUE, $id, $post, $args ); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
function index( $status = 0 ) |
| 208 |
{ |
| 209 |
$model = $this->{$this->model}; |
| 210 |
if( ! $status ) |
| 211 |
$status = TIMEOFF_MODEL::STATUS_PENDING; |
| 212 |
|
| 213 |
/* all statuses counts */ |
| 214 |
$statuses = array(); |
| 215 |
$res = $this->{$this->model} |
| 216 |
->select('status') |
| 217 |
->select_func('COUNT', '@id', 'count') |
| 218 |
->group_by('status') |
| 219 |
->order_by('status', 'ASC') |
| 220 |
->get(); |
| 221 |
|
| 222 |
foreach( $res as $r ) |
| 223 |
{ |
| 224 |
if( $r->count > 0 ) |
| 225 |
$statuses[ $r->status ] = $r->count; |
| 226 |
} |
| 227 |
|
| 228 |
if( isset($statuses[TIMEOFF_MODEL::STATUS_ACTIVE]) && ($statuses[TIMEOFF_MODEL::STATUS_ACTIVE] > 0) ) |
| 229 |
{ |
| 230 |
$this->hc_time->setNow(); |
| 231 |
/* if this week then not yet expired */ |
| 232 |
$this->hc_time->setStartWeek(); |
| 233 |
$check_with = $this->hc_time->formatDate_Db(); |
| 234 |
|
| 235 |
// count archive |
| 236 |
$archived_count = $this->{$this->model} |
| 237 |
->where('status', TIMEOFF_MODEL::STATUS_ACTIVE) |
| 238 |
->where('date_end <', $check_with) |
| 239 |
->count(); |
| 240 |
|
| 241 |
if( $archived_count > 0 ) |
| 242 |
{ |
| 243 |
$statuses[TIMEOFF_MODEL::STATUS_ARCHIVE] = $archived_count; |
| 244 |
$statuses[TIMEOFF_MODEL::STATUS_ACTIVE] = $statuses[TIMEOFF_MODEL::STATUS_ACTIVE] - $archived_count; |
| 245 |
if( $statuses[TIMEOFF_MODEL::STATUS_ACTIVE] <= 0 ) |
| 246 |
unset( $statuses[TIMEOFF_MODEL::STATUS_ACTIVE] ); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/* no timeoffs so far */ |
| 251 |
if( ! $statuses ) |
| 252 |
{ |
| 253 |
ci_redirect( $this->conf['path'] . '/add' ); |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
$this->data['statuses'] = $statuses; |
| 258 |
|
| 259 |
/* load */ |
| 260 |
if( ! isset($statuses[$status]) ) |
| 261 |
{ |
| 262 |
$all_statuses = array_keys( $statuses ); |
| 263 |
$status = $all_statuses[0]; |
| 264 |
} |
| 265 |
|
| 266 |
switch( $status ) |
| 267 |
{ |
| 268 |
case TIMEOFF_MODEL::STATUS_ARCHIVE: |
| 269 |
$this->{$this->model}->where('status', TIMEOFF_MODEL::STATUS_ACTIVE); |
| 270 |
$this->{$this->model}->where('date_end <', $check_with); |
| 271 |
break; |
| 272 |
case TIMEOFF_MODEL::STATUS_ACTIVE: |
| 273 |
$this->{$this->model}->where('status', $status); |
| 274 |
$this->{$this->model}->where('date_end >=', $check_with); |
| 275 |
break; |
| 276 |
default: |
| 277 |
$this->{$this->model}->where('status', $status); |
| 278 |
break; |
| 279 |
} |
| 280 |
$this->data['entries'] = $this->{$this->model}->get()->all; |
| 281 |
|
| 282 |
$this->data['status'] = $status; |
| 283 |
$this->set_include( 'index' ); |
| 284 |
$this->data['index_child'] = $this->get_view('index_child'); |
| 285 |
|
| 286 |
$this->load->view( $this->template, $this->data); |
| 287 |
} |
| 288 |
|
| 289 |
function status( $id, $status ) |
| 290 |
{ |
| 291 |
if( ! $this->{$this->model}->id ) |
| 292 |
$this->{$this->model}->get_by_id($id); |
| 293 |
$this->{$this->model}->status = $status; |
| 294 |
|
| 295 |
if( $this->{$this->model}->save() ) |
| 296 |
{ |
| 297 |
$msg = lang('timeoff') . ': ' . $this->{$this->model}->prop_text('status'); |
| 298 |
$this->session->set_flashdata( 'message', $msg ); |
| 299 |
} |
| 300 |
else |
| 301 |
{ |
| 302 |
$this->session->set_flashdata( 'error', $this->{$this->model}->error->string ); |
| 303 |
} |
| 304 |
|
| 305 |
$redirect_to = method_exists($this, 'after_save') ? $this->after_save() : array($this->conf['path']); |
| 306 |
$this->redirect( $redirect_to ); |
| 307 |
return; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
/* End of file customers.php */ |
| 312 |
/* Location: ./application/controllers/admin/categories.php */ |