/**
  * Public function for displaying the add check list form
  * 
  * @param HTTP:: location code, control id, date
  * @return data array
  */
 function add_check_list($check_list = null)
 {
     if ($check_list == null) {
         $type = phpgw::get_var('type');
         $control_id = phpgw::get_var('control_id');
         $deadline_ts = phpgw::get_var('deadline_ts');
         $deadline_current = phpgw::get_var('deadline_current', 'bool');
         $serie_id = phpgw::get_var('serie_id', 'int');
         if ($deadline_current) {
             $year = date('Y');
             $month = date('m');
             $a_date = "{$year}-{$month}-23";
             $deadline_ts = mktime(00, 00, 00, $month, date('t', strtotime($a_date)), $year);
             unset($year);
             unset($month);
             unset($a_date);
             /*look for checklist with $deadline_ts = $deadline_current*/
             $check_list_id = $this->so_control->get_check_list_id_for_deadline($serie_id, $deadline_ts);
             if ($check_list_id) {
                 $this->redirect(array('menuaction' => 'controller.uicheck_list.edit_check_list', 'check_list_id' => $check_list_id));
             }
         }
         $check_list = new controller_check_list();
         $check_list->set_control_id($control_id);
         $check_list->set_deadline($deadline_ts);
     } else {
         if ($check_list->get_component_id() > 0) {
             $type = "component";
         } else {
             $type = "location";
         }
     }
     if (!($location_code = $check_list->get_location_code())) {
         $location_code = phpgw::get_var('location_code');
         $check_list->set_location_code($location_code);
         $location_array = execMethod('property.bolocation.read_single', array('location_code' => $check_list->get_location_code()));
         $level = $this->location_finder->get_location_level($location_code);
     }
     if ($type == "component") {
         if ($check_list != null) {
             $location_id = phpgw::get_var('location_id');
             $check_list->set_location_id($location_id);
             $component_id = phpgw::get_var('component_id');
             $check_list->set_component_id($component_id);
         }
         $component_arr = execMethod('property.soentity.read_single_eav', array('location_id' => $location_id, 'id' => $component_id));
         $location_code = $component_arr['location_code'];
         $check_list->set_location_code($location_code);
         $location_array = execMethod('property.bolocation.read_single', array('location_code' => $check_list->get_location_code()));
         $level = $this->location_finder->get_location_level($location_code);
         $location_name = execMethod('property.bolocation.get_location_name', $component_arr['location_code']);
         $short_desc = $location_name . '::' . execMethod('property.soentity.get_short_description', array('location_id' => $location_id, 'id' => $component_id));
         $component = new controller_component();
         $component->set_id($component_id);
         $component->set_location_id($location_id);
         $component->set_location_code($component_arr['location_code']);
         $component->set_xml_short_desc($short_desc);
         $component_array = $component->toArray();
         $building_location_code = $this->location_finder->get_building_location_code($component_arr['location_code']);
         $type = "component";
     } else {
         $type = "location";
     }
     $repeat_descr = '';
     if ($serie = $this->so_control->get_serie($serie_id)) {
         $repeat_type_array = array("0" => lang('day'), "1" => lang('week'), "2" => lang('month'), "3" => lang('year'));
         $repeat_descr = "{$repeat_type_array[$serie['repeat_type']]}/{$serie['repeat_interval']}";
     }
     $control = $this->so_control->get_single($check_list->get_control_id());
     if ($repeat_descr) {
         $repeat_descr .= " :: " . $control->get_title();
         $control->set_title($repeat_descr);
     }
     if (!($responsible_user_id = phpgw::get_var('assigned_to', 'int'))) {
         $responsible_user_id = execMethod('property.soresponsible.get_responsible_user_id', array('responsibility_id' => $control->get_responsibility_id(), 'location_code' => $location_code));
     }
     $year = date("Y", $deadline_ts);
     $month_nr = date("n", $deadline_ts);
     $level = $this->location_finder->get_location_level($location_code);
     $user_role = true;
     // Fetches buildings on property
     $buildings_on_property = $this->location_finder->get_buildings_on_property($user_role, $location_code, $level);
     $users = $GLOBALS['phpgw']->acl->get_user_list_right(PHPGW_ACL_ADD, $this->acl_location);
     $user_list_options = array();
     foreach ($users as $user) {
         $user_list_options[] = array('id' => $user['account_id'], 'name' => $user['account_lastname'] . ', ' . $user['account_firstname'], 'selected' => $responsible_user_id == $user['account_id'] ? 1 : 0);
     }
     $config = CreateObject('phpgwapi.config', 'controller');
     $config->read();
     $required_actual_hours = isset($config->config_data['required_actual_hours']) && $config->config_data['required_actual_hours'] ? $config->config_data['required_actual_hours'] : false;
     $request_ical_event = isset($config->config_data['request_ical_event']) && $config->config_data['request_ical_event'] ? $config->config_data['request_ical_event'] : false;
     $data = array('user_list' => array('options' => $user_list_options), 'location_array' => $location_array, 'component_array' => $component_array, 'control' => $control, 'check_list' => $check_list, 'buildings_on_property' => $buildings_on_property, 'type' => $type, 'current_year' => $year, 'current_month_nr' => $month_nr, 'current_month_name' => lang("month {$month_nr} capitalized"), 'building_location_code' => $building_location_code, 'location_level' => $level, 'check_list_type' => 'add_check_list', 'serie_id' => $serie_id, 'required_actual_hours' => $required_actual_hours, 'integration' => $this->_get_component_integration($location_id, $component_arr), 'request_ical_event' => $request_ical_event);
     $GLOBALS['phpgw']->jqcal->add_listener('planned_date');
     $GLOBALS['phpgw']->jqcal->add_listener('completed_date');
     self::add_javascript('controller', 'controller', 'custom_ui.js');
     self::add_javascript('controller', 'controller', 'ajax.js');
     self::add_javascript('controller', 'controller', 'check_list.js');
     self::render_template_xsl(array('check_list/add_check_list', 'check_list/fragments/nav_control_plan', 'check_list/fragments/check_list_top_section', 'check_list/fragments/add_check_list_menu', 'check_list/fragments/select_buildings_on_property'), $data);
 }