/**
  * Public function for saving control details 
  *
  * @param HTTP:: control id, control details fields
  * @return redirect to function view_control_groups
  */
 public function save_control_details()
 {
     if (!$this->add && !$this->edit) {
         $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uicontrol.index'));
     }
     $control_id = phpgw::get_var('control_id');
     // Update existing control details
     $delete_control_groups = false;
     if (isset($control_id) && $control_id > 0) {
         $control = $this->so->get_single($control_id);
         $control_area_id_in_db = $control->get_control_area_id();
         $control->populate();
         $control_area_id_from_req = $control->get_control_area_id();
         // DELETE EARLIER SAVED CONTROL GROUPS
         // If control are is different from a previous registration - delete related groups
         if ($control_area_id_in_db > 0 & $control_area_id_in_db != $control_area_id_from_req) {
             $delete_control_groups = true;
         }
     } else {
         $control = new controller_control();
         $control->populate();
     }
     // SAVE CONTROL DETAILS
     if ($control->validate()) {
         if ($delete_control_groups) {
             // Deleting earlier saved control groups
             $this->so_control_group_list->delete_control_groups($control_id);
             $saved_control_items = $this->so_control_item_list->get_control_items_by_control($control_id);
             foreach ($saved_control_items as $control_item) {
                 $this->so_control_item_list->delete($control->get_id(), $control_item->get_id());
             }
         }
         $control_id = $this->so->store($control);
         $this->redirect(array('menuaction' => 'controller.uicontrol.view_control_groups', 'control_id' => $control_id));
     } else {
         $this->view_control_details($control);
     }
 }
 /**
  * Get array with check lists for a component within time period and for a specified repeat type
  *
  * @param $location_code location code
  * @param $from_date_ts start time period
  * @param $to_date_ts end time period
  * @param $repeat_type_expr repeat type expression
  * @return array with check list objects
  */
 function get_check_lists_for_component($location_id, $component_id, $from_date_ts, $to_date_ts, $repeat_type_expr = null)
 {
     $location_id = (int) $location_id;
     $component_id = (int) $component_id;
     $sql = "SELECT c.id as c_id, ";
     $sql .= "cl.id as cl_id, cl.status as cl_status, cl.comment as cl_comment, deadline, planned_date, completed_date, assigned_to, ";
     $sql .= "cl.component_id, cl.location_id, cl.location_code as cl_location_code, num_open_cases, num_pending_cases, cl.serie_id ";
     $sql .= "FROM controller_control c ";
     $sql .= "LEFT JOIN controller_check_list cl on cl.control_id = c.id ";
     $sql .= "WHERE cl.location_id = {$location_id} ";
     $sql .= "AND cl.component_id = {$component_id} ";
     if ($repeat_type != null) {
         $sql .= "AND c.repeat_type {$repeat_type_expr} ";
     }
     $sql .= "AND (deadline > {$from_date_ts}) AND (deadline < {$to_date_ts}) ";
     $sql .= "ORDER BY c.id;";
     $this->db->query($sql);
     $control_id = 0;
     $control = null;
     $controls_array = array();
     while ($this->db->next_record()) {
         if ($this->db->f('c_id') != $control_id) {
             if ($control_id != 0) {
                 $control->set_check_lists_array($check_lists_array);
                 $controls_array[] = $control;
             }
             $control = new controller_control($this->unmarshal($this->db->f('c_id'), 'int'));
             $check_lists_array = array();
         }
         $check_list = new controller_check_list($this->unmarshal($this->db->f('cl_id'), 'int'));
         $check_list->set_status($this->unmarshal($this->db->f('cl_status'), 'int'));
         $check_list->set_comment($this->unmarshal($this->db->f('cl_comment', true), 'string'));
         $check_list->set_deadline($this->unmarshal($this->db->f('deadline'), 'int'));
         $check_list->set_planned_date($this->unmarshal($this->db->f('planned_date'), 'int'));
         $check_list->set_completed_date($this->unmarshal($this->db->f('completed_date'), 'int'));
         $check_list->set_component_id($this->unmarshal($this->db->f('component_id'), 'int'));
         $check_list->set_location_id($this->unmarshal($this->db->f('location_id'), 'int'));
         $check_list->set_location_code($this->unmarshal($this->db->f('cl_location_code', true), 'string'));
         $check_list->set_num_open_cases($this->unmarshal($this->db->f('num_open_cases'), 'int'));
         $check_list->set_num_pending_cases($this->unmarshal($this->db->f('num_pending_cases'), 'int'));
         $check_list->set_assigned_to($this->unmarshal($this->db->f('assigned_to'), 'int'));
         $check_list->set_serie_id($this->db->f('serie_id'));
         $check_lists_array[] = $check_list;
         $control_id = $control->get_id();
     }
     if ($control != null) {
         $control->set_check_lists_array($check_lists_array);
         $controls_array[] = $control;
     }
     return $controls_array;
 }