Exemplo n.º 1
0
 private function _grab_state($args, $tab = 'browse')
 {
     $t = HC_Lib::time();
     $state = array('range' => 'week', 'date' => $t->formatDate_Db(), 'by' => NULL, 'location' => array(), 'staff' => array(), 'type' => NULL, 'wide' => NULL, 'filter' => NULL);
     $default_params = $this->default_params;
     $supplied = hc_parse_args($args);
     $supplied = array_merge($default_params, $supplied);
     foreach ($supplied as $k => $v) {
         if (in_array($k, array('staff', 'location', 'type'))) {
             if (strpos($v, '.') !== FALSE) {
                 $v = explode('.', $v);
             } elseif (strpos($v, ',') !== FALSE) {
                 $v = explode(',', $v);
             } else {
                 $v = array($v);
             }
         }
         $state[$k] = $v;
     }
     /* check _current_user_id_ */
     if (isset($state['staff'])) {
         $check_current_user = array('_current_user_id_', '_current_user_id', 'current_user_id', 'current_user', '_current_user_');
         $current_user_key = '';
         foreach ($check_current_user as $cuk) {
             if (in_array($cuk, $state['staff'])) {
                 $current_user_key = $cuk;
                 break;
             }
         }
         if ($current_user_key) {
             $current_user_id = 0;
             if ($test_user = $this->auth->user()) {
                 $current_user_id = $test_user->id;
             }
             $state['staff'] = HC_Lib::replace_in_array($state['staff'], $current_user_key, $current_user_id);
         }
     }
     /* fixed ? */
     $force_fixed = array('location', 'staff');
     foreach ($default_params as $k => $v) {
         if (in_array($k, $force_fixed)) {
             $this->fix[$k] = $state[$k];
         }
     }
     switch ($state['range']) {
         case 'all':
             $t = HC_Lib::time();
             $shifts = HC_App::model('shift');
             $min_date = $shifts->select_min('date')->get()->date;
             $max_date = $shifts->select_max('date_end')->get()->date_end;
             if ($min_date && $max_date) {
                 $state['date'] = $min_date . '_' . $max_date;
             }
             break;
         case 'upcoming':
             $t = HC_Lib::time();
             $shifts = HC_App::model('shift');
             $min_date = $t->setNow()->formatDate_Db();
             $max_date = $shifts->select_max('date_end')->get()->date_end;
             if ($min_date && $max_date) {
                 $state['date'] = $min_date . '_' . $max_date;
             }
             break;
         case 'day':
         case 'week':
         case 'month':
             if (!$state['date']) {
                 $t->setNow();
                 $state['date'] = $t->formatDate_Db();
             }
             break;
         case 'custom':
             if (strpos($state['date'], '_') !== FALSE) {
                 list($start_date, $end_date) = explode('_', $state['date']);
             } else {
                 $start_date = $end_date = $state['date'];
             }
             $t->setNow();
             if (!$start_date) {
                 $start_date = $t->formatDate_Db();
             }
             if (!$end_date) {
                 $end_date = $t->formatDate_Db();
             }
             $state['date'] = join('_', array($start_date, $end_date));
             break;
         default:
             // attempt to parse the range text in time
             $start_date = $state['date'];
             if (!$start_date) {
                 $t->setNow();
                 $start_date = $t->formatDate_Db();
             }
             $t->setDateDb($start_date);
             $t->modify('+' . $state['range']);
             $end_date = $t->formatDate_Db();
             $state['date'] = join('_', array($start_date, $end_date));
             $state['range'] = 'custom';
             break;
     }
     /* if custom dates supplied */
     if (isset($supplied['customdates']) && $supplied['customdates']) {
         $post = $this->input->post();
         if (isset($post['start_date']) && isset($post['end_date'])) {
             if ($post['end_date'] <= $post['start_date']) {
                 $post['end_date'] = $post['start_date'];
             }
             $state['date'] = $post['start_date'] . '_' . $post['end_date'];
             $state['range'] = 'custom';
             unset($state['customdates']);
             $link = HC_Lib::link($this->views_path . '/' . $tab, $state);
             $redirect_to = $link->url();
             $this->redirect($redirect_to);
             return;
         }
     }
     /* something fixed ? */
     foreach ($this->fix as $fk => $fv) {
         if ($fv) {
             unset($state[$fk]);
         }
     }
     return $state;
 }