/**
  * Public function for saving a check list
  * 
  * @param HTTP:: location code, control id, status etc.. (check list details) 
  * @return data array
  */
 function save_check_list()
 {
     $check_list_id = phpgw::get_var('check_list_id');
     if (!$this->add && !$this->edit) {
         phpgwapi_cache::message_set('No access', 'error');
         $this->redirect(array('menuaction' => 'controller.uicheck_list.edit_check_list', 'check_list_id' => $check_list_id));
     }
     $control_id = phpgw::get_var('control_id', 'int');
     $serie_id = phpgw::get_var('serie_id', 'int');
     $status = (int) phpgw::get_var('status');
     $type = phpgw::get_var('type');
     $deadline_date = phpgw::get_var('deadline_date', 'string');
     $planned_date = phpgw::get_var('planned_date', 'string');
     $completed_date = phpgw::get_var('completed_date', 'string');
     $comment = phpgw::get_var('comment', 'string');
     $assigned_to = phpgw::get_var('assigned_to', 'int');
     $billable_hours = phpgw::get_var('billable_hours', 'float');
     $deadline_date_ts = date_converter::date_to_timestamp($deadline_date);
     $error = false;
     if ($planned_date != '') {
         $planned_date_ts = date_converter::date_to_timestamp($planned_date);
     } else {
         $planned_date_ts = $deadline_date_ts;
     }
     if ($completed_date != '') {
         $completed_date_ts = phpgwapi_datetime::date_to_timestamp($completed_date);
         $status = controller_check_list::STATUS_DONE;
     } else {
         $completed_date_ts = 0;
     }
     if ($check_list_id > 0) {
         $check_list = $this->so->get_single($check_list_id);
         if ($status == controller_check_list::STATUS_DONE) {
             if (!$this->_check_for_required($check_list)) {
                 $this->redirect(array('menuaction' => 'controller.uicheck_list.edit_check_list', 'check_list_id' => $check_list_id));
             }
         }
     } else {
         if ($status == controller_check_list::STATUS_DONE) {
             $status = controller_check_list::STATUS_NOT_DONE;
             $completed_date_ts = 0;
             $error_message = "Status kunne ikke settes til utført - prøv igjen";
             $error = true;
             phpgwapi_cache::message_set($error_message, 'error');
         }
         $check_list = new controller_check_list();
         $check_list->set_control_id($control_id);
         $location_code = phpgw::get_var('location_code');
         $check_list->set_location_code($location_code);
         $check_list->set_serie_id($serie_id);
         if ($type == "component") {
             $location_id = phpgw::get_var('location_id');
             $component_id = phpgw::get_var('component_id');
             $check_list->set_location_id($location_id);
             $check_list->set_component_id($component_id);
         }
     }
     $check_list->set_comment($comment);
     $check_list->set_deadline($deadline_date_ts);
     $check_list->set_planned_date($planned_date_ts);
     $check_list->set_completed_date($completed_date_ts);
     $orig_assigned_to = $check_list->get_assigned_to();
     $check_list->set_assigned_to($assigned_to);
     $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;
     if ($status == controller_check_list::STATUS_DONE && $required_actual_hours && $check_list->get_billable_hours() == 0 && !$billable_hours) {
         phpgwapi_cache::message_set(lang("Please enter billable hours"), 'error');
         $error = true;
     } else {
         $check_list->set_delta_billable_hours($billable_hours);
     }
     if ($status == controller_check_list::STATUS_DONE && $this->_check_for_required($check_list) && !$error) {
         $check_list->set_status($status);
     } else {
         if ($status == controller_check_list::STATUS_CANCELED && !$error) {
             $check_list->set_status($status);
         }
     }
     if (!$error && $check_list->validate()) {
         $check_list_id = $this->so->store($check_list);
         $serie = $this->so_control->get_serie($check_list->get_serie_id());
         /**
          * Add an iCal-event if there is a serie - and the checklist is visited the first time - or assigned is changed
          */
         if (phpgw::get_var('request_ical_event', 'bool') && $check_list_id && $serie) {
             $bocommon = CreateObject('property.bocommon');
             $current_prefs_user = $bocommon->create_preferences('property', $GLOBALS['phpgw_info']['user']['account_id']);
             $from_address = "{$GLOBALS['phpgw_info']['user']['fullname']}<{$current_prefs_user['email']}>";
             $from_name = $GLOBALS['phpgw_info']['user']['fullname'];
             $to_name = $GLOBALS['phpgw']->accounts->id2name($assigned_to);
             $prefs_target = $bocommon->create_preferences('property', $assigned_to);
             $to_address = $prefs_target['email'];
             if (!($start_date = $check_list->get_planned_date())) {
                 $start_date = $check_list->get_deadline();
             }
             $startTime = $start_date + 8 * 3600;
             $endTime = $startTime + (double) $serie['service_time'] * 3600 + (double) $serie['controle_time'] * 3600;
             if ($check_list->get_component_id() > 0) {
                 $component_arr = execMethod('property.soentity.read_single_eav', array('location_id' => $check_list->get_location_id(), 'id' => $check_list->get_component_id()));
                 $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' => $check_list->get_location_id(), 'id' => $check_list->get_component_id()));
                 $location = $location_name;
             }
             $repeat_type_array = array("0" => lang('day'), "1" => lang('week'), "2" => lang('month'), "3" => lang('year'));
             $subject = "{$repeat_type_array[$serie['repeat_type']]}/{$serie['repeat_interval']}";
             $subject .= "::{$serie['title']}::{$short_desc}";
             $link_backend = $GLOBALS['phpgw']->link('/index.php', array('menuaction' => 'controller.uicheck_list.add_check_list', 'control_id' => $check_list->get_control_id(), 'location_id' => $check_list->get_location_id(), 'component_id' => $check_list->get_component_id(), 'serie_id' => $check_list->get_serie_id(), 'type' => 'component', 'assigned_to' => $check_list->get_assigned_to(), 'deadline_current' => true), false, true, true);
             $link_mobilefrontend = $GLOBALS['phpgw']->link('/mobilefrontend/index.php', array('menuaction' => 'controller.uicheck_list.add_check_list', 'control_id' => $check_list->get_control_id(), 'location_id' => $check_list->get_location_id(), 'component_id' => $check_list->get_component_id(), 'serie_id' => $check_list->get_serie_id(), 'type' => 'component', 'assigned_to' => $check_list->get_assigned_to(), 'deadline_current' => true), false, true, true);
             $html_description = "<a href ='{$link_mobilefrontend}'>Serie#" . $check_list->get_serie_id() . '::Mobilefrontend</a><br/><br/>';
             $html_description .= "<a href ='{$link_backend}'>Serie#" . $check_list->get_serie_id() . '::Backend</a>';
             $_serie_id = $check_list->get_serie_id();
             $text_description = str_replace('&amp;', '&', "Serie#{$_serie_id}::Mobilefrontend:\\n{$link_mobilefrontend}\\n\\nSerie#{$_serie_id}::Backend:\\n{$link_backend}");
             if ($from_address && $to_address) {
                 $this->sendIcalEvent($from_name, $from_address, $to_name, $to_address, $startTime, $endTime, $subject, $html_description, $text_description, $location);
             } else {
                 phpgwapi_cache::message_set("Mangler epostadresse til avsender eller addresat - eller begge", 'error');
             }
         }
         if ($check_list_id > 0) {
             $this->redirect(array('menuaction' => 'controller.uicheck_list.edit_check_list', 'check_list_id' => $check_list_id));
         } else {
             $this->edit_check_list($check_list);
         }
     } else {
         if ($check_list->get_id() > 0) {
             $this->edit_check_list($check_list);
         } else {
             $this->redirect(array('menuaction' => 'controller.uicheck_list.add_check_list', 'control_id' => $control_id, 'location_id' => $location_id, 'component_id' => $component_id, 'serie_id' => $serie_id, 'deadline_ts' => $deadline_date_ts, 'type' => $type, 'assigned_to' => $assigned_to, 'status' => $status));
         }
     }
 }