/**
  * 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);
     }
 }
 function populate(int $control_id, &$control)
 {
     if ($control == null) {
         $start_date = date("d.m.Y", $this->db->f('start_date'));
         $end_date = date("d.m.Y", $this->db->f('end_date'));
         $control = new controller_control((int) $control_id);
         $control->set_title($this->unmarshal($this->db->f('title', true), 'string'));
         $control->set_description($this->unmarshal($this->db->f('description', true), 'string'));
         $control->set_start_date($start_date);
         $control->set_end_date($end_date);
         $control->set_procedure_id($this->unmarshal($this->db->f('procedure_id'), 'int'));
         $control->set_procedure_name($this->unmarshal($this->db->f('procedure_name', true), 'string'));
         $control->set_requirement_id($this->unmarshal($this->db->f('requirement_id'), 'int'));
         $control->set_costresponsibility_id($this->unmarshal($this->db->f('costresponsibility_id'), 'int'));
         $control->set_responsibility_id($this->unmarshal($this->db->f('responsibility_id'), 'int'));
         $control->set_control_area_id($this->unmarshal($this->db->f('control_area_id'), 'int'));
         $control->set_control_area_name($this->unmarshal($this->db->f('control_area_name', true), 'string'));
         $control->set_equipment_type_id($this->unmarshal($this->db->f('equipment_type_id'), 'int'));
         $control->set_equipment_id($this->unmarshal($this->db->f('equipment_id'), 'int'));
         $control->set_location_code($this->unmarshal($this->db->f('location_code', true), 'string'));
         $control->set_location_id($this->unmarshal($this->db->f('location_id'), 'string'));
         $control->set_repeat_type($this->unmarshal($this->db->f('repeat_type'), 'int'));
         $control->set_repeat_interval($this->unmarshal($this->db->f('repeat_interval'), 'int'));
     }
     return $control;
 }
 /**
  * Get a static reference to the storage object associated with this model object
  * 
  * @return the storage object
  */
 public static function get_so()
 {
     if (self::$so == null) {
         self::$so = CreateObject('controller.socontrol');
     }
     return self::$so;
 }
 /**
  * Get single control
  * 
  * @param	$id	id of the control to return
  * @return a controller_control object
  */
 function get_single($id)
 {
     $id = (int) $id;
     $joins .= " {$this->left_join} controller_procedure ON (c.procedure_id = controller_procedure.id)";
     $joins .= " {$this->left_join} fm_responsibility_role ON (c.responsibility_id = fm_responsibility_role.id)";
     $sql = "SELECT c.*, controller_procedure.title AS procedure_name, fm_responsibility_role.name AS responsibility_name ";
     $sql .= "FROM controller_control c {$joins} ";
     $sql .= "WHERE c.id = " . $id;
     $this->db->query($sql, __LINE__, __FILE__);
     $this->db->next_record();
     $control = new controller_control((int) $id);
     $control->set_title($this->unmarshal($this->db->f('title', true), 'string'));
     $control->set_description($this->unmarshal($this->db->f('description', true), 'string'));
     $control->set_start_date($this->unmarshal($this->db->f('start_date'), 'int'));
     $control->set_end_date($this->unmarshal($this->db->f('end_date'), 'int'));
     $control->set_procedure_id($this->unmarshal($this->db->f('procedure_id'), 'int'));
     $control->set_procedure_name($this->unmarshal($this->db->f('procedure_name', true), 'string'));
     $control->set_requirement_id($this->unmarshal($this->db->f('requirement_id'), 'int'));
     $control->set_costresponsibility_id($this->unmarshal($this->db->f('costresponsibility_id'), 'int'));
     $control->set_responsibility_id($this->unmarshal($this->db->f('responsibility_id'), 'int'));
     $control->set_responsibility_name($this->unmarshal($this->db->f('responsibility_name', true), 'string'));
     $control->set_control_area_id($this->unmarshal($this->db->f('control_area_id'), 'int'));
     $control->set_repeat_type($this->unmarshal($this->db->f('repeat_type'), 'int'));
     $control->set_repeat_type_label($control->get_repeat_type());
     $control->set_repeat_interval($this->unmarshal($this->db->f('repeat_interval'), 'int'));
     $category = execMethod('phpgwapi.categories.return_single', $this->unmarshal($this->db->f('control_area_id'), 'int'));
     $control->set_control_area_name($category[0]['name']);
     return $control;
 }