/**
  * Get array with control group objects represented as objects or arrays   
  * 
  * @param	$control_id control id
  * @param	$returnType representation of returned control grups, as objects or as arrays  
  * @return a control_group_list
  */
 function get_control_groups_by_control($control_id, $returnType = "object")
 {
     $control_id = (int) $control_id;
     $sql = "SELECT cg.*, cgl.order_nr ";
     $sql .= "FROM controller_control_group_list cgl, controller_control_group cg ";
     $sql .= "WHERE cgl.control_id={$control_id} ";
     $sql .= "AND cgl.control_group_id=cg.id ";
     $sql .= "ORDER BY cgl.order_nr ASC";
     $this->db->query($sql);
     $control_group_list = array();
     while ($this->db->next_record()) {
         $control_group = new controller_control_group($this->unmarshal($this->db->f('id'), 'int'));
         $control_group->set_group_name($this->unmarshal($this->db->f('group_name', true), 'string'));
         $control_group->set_procedure_id($this->unmarshal($this->db->f('procedure_id'), 'int'));
         $control_group->set_control_area_id($this->unmarshal($this->db->f('control_area_id'), 'int'));
         $control_group->set_building_part_id($this->unmarshal($this->db->f('building_part_id'), 'int'));
         $control_group->set_component_location_id($this->unmarshal($this->db->f('component_location_id'), 'int'));
         $component_criteria = $this->db->f('component_criteria') ? unserialize($this->db->f('component_criteria', true)) : array();
         $control_group->set_component_criteria($component_criteria);
         if ($returnType == "array") {
             $control_group_list[] = $control_group->toArray();
         } else {
             $control_group_list[] = $control_group;
         }
     }
     return $control_group_list;
 }
 public function edit()
 {
     $tabs = array('control_group' => array('label' => lang('Control_group'), 'link' => '#control_group'), 'control_items' => array('label' => lang('Control_items'), 'link' => '#control_items'));
     $tab_to_display = 'control_group';
     $control_group_id = phpgw::get_var('id', 'int');
     $new_control_group = false;
     if ($control_group_id) {
         $control_group = $this->so->get_single($control_group_id);
     } else {
         $control_group = new controller_control_group();
         $new_control_group = true;
     }
     if (isset($_POST['save_control_group'])) {
         if (!$this->add && !$this->edit) {
             phpgwapi_cache::message_set('No access', 'error');
             $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uicontrol_group.index'));
         }
         $entity_id = phpgw::get_var('entity_id', 'int');
         $category_id = phpgw::get_var('category_id', 'int');
         $component_location_id = $GLOBALS['phpgw']->locations->get_id('property', ".entity.{$entity_id}.{$category_id}");
         if (isset($control_group)) {
             $control_group->set_group_name(phpgw::get_var('group_name'));
             $control_group->set_procedure_id(phpgw::get_var('procedure'));
             $control_group->set_control_area_id(phpgw::get_var('control_area'));
             $control_group->set_building_part_id(phpgw::get_var('building_part'));
             $control_group->set_component_location_id($component_location_id);
             $attributes = phpgw::get_var('attributes');
             $attributes_operator = phpgw::get_var('attributes_operator');
             $criteria = array();
             if (is_array($attributes)) {
                 foreach ($attributes as $_key => $_value) {
                     if ($_value) {
                         $criteria[$_key]['value'] = $_value;
                         $criteria[$_key]['operator'] = $attributes_operator[$_key];
                     }
                 }
             }
             $control_group->set_component_criteria($criteria);
             if (isset($control_group_id) && $control_group_id > 0) {
                 $ctrl_group_id = $control_group_id;
                 if ($this->so->store($control_group)) {
                     $message = lang('messages_saved_form');
                 } else {
                     $error = lang('messages_form_error');
                 }
             } else {
                 $ctrl_group_id = $this->so->add($control_group);
                 if ($ctrl_group_id) {
                     $message = lang('messages_saved_form');
                 } else {
                     $error = lang('messages_form_error');
                 }
             }
             if ($new_control_group) {
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uicontrol_group.edit', 'id' => $ctrl_group_id));
             } else {
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uicontrol_group.view', 'id' => $ctrl_group_id));
             }
         }
     } else {
         if (isset($_POST['cancel_control_group'])) {
             if (isset($control_group_id) && $control_group_id > 0) {
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uicontrol_group.view', 'id' => $control_group_id));
             } else {
                 $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uicontrol_group.index'));
             }
         } else {
             if (isset($_POST['remove_control_group_items'])) {
                 if (!$this->add && !$this->edit) {
                     phpgwapi_cache::message_set('No access', 'error');
                     $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uicontrol_group.index'));
                 }
                 $control_item_ids = array();
                 // Fetching selected control items
                 $control_tag_ids = phpgw::get_var('item_remove_ids');
                 foreach ($control_tag_ids as $control_item_id) {
                     $curr_control_item = $this->so_control_item->get_single($control_item_id);
                     $curr_control_item->set_control_group_id(null);
                     $this->so_control_item->store($curr_control_item);
                 }
                 // Sigurd: START as categories
                 $cats = CreateObject('phpgwapi.categories', -1, 'controller', '.control');
                 $cats->supress_info = true;
                 $control_areas = $cats->formatted_xslt_list(array('format' => 'filter', 'globals' => true, 'use_acl' => $this->_category_acl));
                 array_unshift($control_areas['cat_list'], array('cat_id' => '', 'name' => lang('select value')));
                 $control_area_array = array();
                 foreach ($control_areas['cat_list'] as $cat_list) {
                     $control_area_array[] = array('id' => $cat_list['cat_id'], 'name' => $cat_list['name'], 'selected' => $control_group->get_control_area_id() == $cat_list['cat_id'] ? 1 : 0);
                 }
                 // END as categories
                 $procedure_array = $this->so_procedure->get_procedures(0, 0, 'title', 'ASC', null, null, array('control_areas' => $control_group->get_control_area_id()));
                 if ($this->flash_msgs) {
                     $msgbox_data = $GLOBALS['phpgw']->common->msgbox_data($this->flash_msgs);
                     $msgbox_data = $GLOBALS['phpgw']->common->msgbox($msgbox_data);
                 }
                 foreach ($procedure_array as $procedure) {
                     if ($control_group->get_procedure_id() && $procedure->get_id() == $control_group->get_procedure_id()) {
                         $procedure_options[] = array('id' => $procedure->get_id(), 'name' => $procedure->get_title(), 'selected' => 'yes');
                     } else {
                         $procedure_options[] = array('id' => $procedure->get_id(), 'name' => $procedure->get_title());
                     }
                 }
                 array_unshift($procedure_options, array('id' => '', 'name' => lang('select value')));
                 $building_part_options = $this->so->get_building_part_select_array($control_group->get_building_part_id());
                 $control_group_array = $control_group->toArray();
                 $control_items_array = $this->so_control_item->get(null, null, 'controller_control_item.control_area_id', true, null, null, array('available' => 'yes'));
                 $control_items = array();
                 foreach ($control_items_array as $control_item) {
                     $control_items[] = $control_item->serialize();
                 }
                 $selected_control_items_array = controller_socontrol_item_list::get_instance()->get_control_items($control_group->get_id());
                 $selected_control_items = array();
                 foreach ($selected_control_items_array as $ctrl_item) {
                     $selected_control_items[] = $ctrl_item->serialize();
                 }
                 phpgwapi_yui::tabview_setup('control_group_tabview');
                 $data = array('tabs' => phpgwapi_yui::tabview_generate($tabs, 'control_items'), 'value_id' => !empty($control_group) ? $control_group->get_id() : 0, 'editable' => true, 'procedure' => array('options' => $procedure_options), 'control_area' => array('options' => $control_area_array), 'control_group' => $control_group_array, 'control_items' => $control_items, 'selected_control_items' => $selected_control_items, 'building_part' => array('building_part_options' => $building_part_options));
                 $GLOBALS['phpgw_info']['flags']['app_header'] = lang('controller') . '::' . lang('Control_group');
                 phpgwapi_jquery::load_widget('core');
                 self::add_javascript('controller', 'yahoo', 'control_tabs.js');
                 self::add_javascript('controller', 'controller', 'ajax.js');
                 self::render_template_xsl(array('control_group/control_group_tabs', 'control_group/control_group', 'control_group/control_group_items'), $data);
             } else {
                 if (isset($_POST['save_control_group_items'])) {
                     if (!$this->add && !$this->edit) {
                         phpgwapi_cache::message_set('No access', 'error');
                         $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uicontrol_group.index'));
                     }
                     $tab_to_display = 'control_group_items';
                     //update control items with control group id
                     //$control_group_id = phpgw::get_var('control_group_id');
                     $control_item_ids = array();
                     // Fetching selected control items
                     $control_tag_ids = phpgw::get_var('control_tag_ids');
                     foreach ($control_tag_ids as $control_item_id) {
                         $curr_control_item = $this->so_control_item->get_single($control_item_id);
                         $curr_control_item->set_control_group_id($control_group_id);
                         $this->so_control_item->store($curr_control_item);
                     }
                     $GLOBALS['phpgw']->redirect_link('/index.php', array('menuaction' => 'controller.uicontrol_group.view', 'id' => $control_group_id));
                 } else {
                     // Sigurd: START as categories
                     $cats = CreateObject('phpgwapi.categories', -1, 'controller', '.control');
                     $cats->supress_info = true;
                     $control_areas = $cats->formatted_xslt_list(array('format' => 'filter', 'globals' => true, 'use_acl' => $this->_category_acl));
                     array_unshift($control_areas['cat_list'], array('cat_id' => '', 'name' => lang('select value')));
                     $control_area_array = array();
                     foreach ($control_areas['cat_list'] as $cat_list) {
                         $control_area_array[] = array('id' => $cat_list['cat_id'], 'name' => $cat_list['name'], 'selected' => $control_group->get_control_area_id() == $cat_list['cat_id'] ? 1 : 0);
                     }
                     // END as categories
                     $procedure_array = $this->so_procedure->get_procedures(0, 0, 'title', 'ASC', null, null, array('control_areas' => $control_group->get_control_area_id()));
                     if ($this->flash_msgs) {
                         $msgbox_data = $GLOBALS['phpgw']->common->msgbox_data($this->flash_msgs);
                         $msgbox_data = $GLOBALS['phpgw']->common->msgbox($msgbox_data);
                     }
                     foreach ($procedure_array as $procedure) {
                         $procedure_options[] = array('id' => $procedure->get_id(), 'name' => $procedure->get_title(), 'selected' => $procedure->get_id() == $control_group->get_procedure_id() ? 1 : 0);
                     }
                     array_unshift($procedure_options, array('id' => '', 'name' => lang('select value')));
                     $building_part_options = $this->so->get_building_part_select_array($control_group->get_building_part_id());
                     $control_group_array = $control_group->toArray();
                     $control_items_array = $this->so_control_item->get(null, null, 'controller_control_item.control_area_id', true, null, null, array('available' => 'yes'));
                     $control_items = array();
                     foreach ($control_items_array as $control_item) {
                         $control_items[] = $control_item->serialize();
                     }
                     $selected_control_items_array = controller_socontrol_item_list::get_instance()->get_control_items($control_group->get_id());
                     $selected_control_items = array();
                     foreach ($selected_control_items_array as $ctrl_item) {
                         $selected_control_items[] = $ctrl_item->serialize();
                     }
                     phpgwapi_yui::tabview_setup('control_group_tabview');
                     //--- sigurd 10.juni 13
                     $entity_so = CreateObject('property.soadmin_entity');
                     $custom = createObject('phpgwapi.custom_fields');
                     $entity_list = $entity_so->read(array('allrows' => true));
                     $attributes = array();
                     array_unshift($entity_list, array('id' => '', 'name' => lang('select value')));
                     $component_location_id = $control_group->get_component_location_id();
                     if ($component_location_id) {
                         $loc_arr = $GLOBALS['phpgw']->locations->get_name($component_location_id);
                         $entity_arr = explode('.', $loc_arr['location']);
                         $entity = $entity_so->read_single($entity_arr[2]);
                         $category = $entity_so->read_single_category($entity_arr[2], $entity_arr[3]);
                         foreach ($entity_list as &$e) {
                             if ($e['id'] == $entity['id']) {
                                 $e['selected'] = 1;
                             }
                         }
                         $category_list = $entity_so->read_category(array('allrows' => true, 'entity_id' => $entity_arr[2]));
                         array_unshift($category_list, array('id' => '', 'name' => lang('select value')));
                         foreach ($category_list as &$c) {
                             if ($c['id'] == $category['id']) {
                                 $c['selected'] = 1;
                             }
                         }
                         $attributes = $custom->find('property', ".entity.{$entity_arr[2]}.{$entity_arr[3]}", 0, '', '', '', true, true);
                         $operator1 = array(array('id' => 'eq', 'name' => '='));
                         $operator2 = array(array('id' => 'eq', 'name' => '='), array('id' => 'lt', 'name' => '<'), array('id' => 'gt', 'name' => '>'));
                         $component_criteria = $control_group->get_component_criteria();
                         foreach ($attributes as $key => &$a) {
                             switch ($a['datatype']) {
                                 case 'LB':
                                 case 'R':
                                 case 'CH':
                                     $a['operator'] = array('options' => $operator1);
                                     break;
                                 default:
                                     $a['operator'] = array('options' => $operator2);
                             }
                             if (isset($component_criteria[$key]['value']) && $component_criteria[$key]['value']) {
                                 $a['value'] = $component_criteria[$key]['value'];
                                 if (isset($a['choice']) && $a['choice']) {
                                     foreach ($a['choice'] as &$choise) {
                                         $choise['selected'] = $choise['id'] == $component_criteria[$key]['value'] ? 1 : 0;
                                     }
                                 }
                             }
                             if (isset($component_criteria[$key]['operator']) && $component_criteria[$key]['operator']) {
                                 if (isset($a['operator']) && $a['operator']) {
                                     foreach ($a['operator']['options'] as &$_operator) {
                                         $_operator['selected'] = $_operator['id'] == $component_criteria[$key]['operator'] ? 1 : 0;
                                     }
                                 }
                             }
                         }
                     }
                     //---
                     $data = array('entities' => array('options' => $entity_list), 'categories' => array('options' => $category_list), 'attributes' => $attributes, 'tabs' => phpgwapi_yui::tabview_generate($tabs, $tab_to_display), 'value_id' => !empty($control_group) ? $control_group->get_id() : 0, 'editable' => true, 'procedure' => array('options' => $procedure_options), 'control_area' => array('options' => $control_area_array), 'control_group' => $control_group_array, 'control_items' => $control_items, 'selected_control_items' => $selected_control_items, 'building_part' => array('building_part_options' => $building_part_options));
                     $GLOBALS['phpgw_info']['flags']['app_header'] = lang('controller') . '::' . lang('Control_group');
                     /*
                     				$GLOBALS['phpgw']->richtext->replace_element('what_to_do');
                     				$GLOBALS['phpgw']->richtext->replace_element('how_to_do');
                     				$GLOBALS['phpgw']->richtext->generate_script();
                     */
                     //			$GLOBALS['phpgw']->js->validate_file( 'yahoo', 'controller.item', 'controller' );
                     phpgwapi_jquery::load_widget('core');
                     self::add_javascript('controller', 'yahoo', 'control_tabs.js');
                     self::add_javascript('controller', 'controller', 'ajax.js');
                     self::add_javascript('controller', 'controller', 'control_group_to_component.js');
                     self::render_template_xsl(array('control_group/control_group_tabs', 'control_group/control_group', 'control_group/control_group_items'), $data);
                 }
             }
         }
     }
 }
 /**
  * Get an array of control groups within specified control area
  * 
  * @param $control_area_id control area
  * @return array of control group as arrays
  */
 function get_control_groups_by_control_area($control_area_id)
 {
     $cat_id = (int) $control_area_id;
     $cats = CreateObject('phpgwapi.categories', -1, 'controller', '.control');
     $cat_path = $cats->get_path($cat_id);
     foreach ($cat_path as $_category) {
         $cat_filter[] = $_category['id'];
     }
     $filter_control_area = "control_area_id IN (" . implode(',', $cat_filter) . ')';
     $sql = "SELECT * FROM controller_control_group WHERE {$filter_control_area}";
     $this->db->query($sql);
     $controls_array = array();
     while ($this->db->next_record()) {
         $control_group = new controller_control_group((int) $this->db->f('id'));
         $control_group->set_group_name($this->unmarshal($this->db->f('group_name', true), 'string'));
         $control_group->set_procedure_id($this->unmarshal($this->db->f('procedure_id'), 'int'));
         $control_group->set_procedure_name($this->unmarshal($this->db->f('procedure_title', true), 'string'));
         $control_group->set_control_area_id($this->unmarshal($this->db->f('control_area_id'), 'int'));
         $category = execMethod('phpgwapi.categories.return_single', $this->unmarshal($this->db->f('control_area_id', 'int')));
         $control_group->set_control_area_name($category[0]['name']);
         $control_group->set_building_part_id($this->unmarshal($this->db->f('building_part_id'), 'string'));
         $control_group->set_building_part_descr($this->unmarshal($this->db->f('building_part_descr', true), 'string'));
         $control_group->set_component_location_id($this->unmarshal($this->db->f('component_location_id'), 'int'));
         $control_groups_array[] = $control_group->toArray();
     }
     if (count($control_groups_array) > 0) {
         return $control_groups_array;
     } else {
         return null;
     }
 }
 /**
  * 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_group');
     }
     return self::$so;
 }