function add_custom_report()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     tpl_assign('url', get_url('reporting', 'add_custom_report'));
     $report_data = array_var($_POST, 'report');
     if (is_array($report_data)) {
         tpl_assign('report_data', $report_data);
         $conditions = array_var($_POST, 'conditions');
         if (!is_array($conditions)) {
             $conditions = array();
         }
         tpl_assign('conditions', $conditions);
         $columns = array_var($_POST, 'columns');
         if (is_array($columns) && count($columns) > 0) {
             tpl_assign('columns', $columns);
             $newReport = new Report();
             $member_ids = json_decode(array_var($_POST, 'members'));
             if (!is_array($member_ids) || count($member_ids) == 0) {
                 flash_error(lang('must choose at least one member'));
                 ajx_current("empty");
                 return;
             }
             $members = Members::findAll(array("conditions" => array("`id` IN(?)", $member_ids)));
             if (!$newReport->canAdd(logged_user(), $members)) {
                 flash_error(lang('no access permissions'));
                 ajx_current("empty");
                 return;
             }
             // if
             $newReport->setObjectName($report_data['name']);
             $newReport->setDescription($report_data['description']);
             $newReport->setReportObjectTypeId($report_data['report_object_type_id']);
             $newReport->setOrderBy($report_data['order_by']);
             $newReport->setIsOrderByAsc($report_data['order_by_asc'] == 'asc');
             try {
                 DB::beginWork();
                 $newReport->save();
                 $allowed_columns = $this->get_allowed_columns($report_data['report_object_type_id'], true);
                 foreach ($conditions as $condition) {
                     if ($condition['deleted'] == "1") {
                         continue;
                     }
                     foreach ($allowed_columns as $ac) {
                         if ($condition['field_name'] == $ac['id']) {
                             $newCondition = new ReportCondition();
                             $newCondition->setReportId($newReport->getId());
                             $newCondition->setCustomPropertyId($condition['custom_property_id']);
                             $newCondition->setFieldName($condition['field_name']);
                             $newCondition->setCondition($condition['condition']);
                             $condValue = array_key_exists('value', $condition) ? $condition['value'] : '';
                             if ($condition['field_type'] == 'boolean') {
                                 $newCondition->setValue(array_key_exists('value', $condition));
                             } else {
                                 if ($condition['field_type'] == 'date') {
                                     if ($condValue != '') {
                                         $dtFromWidget = DateTimeValueLib::dateFromFormatAndString(user_config_option('date_format'), $condValue);
                                         $newCondition->setValue(date("m/d/Y", $dtFromWidget->getTimestamp()));
                                     }
                                 } else {
                                     $newCondition->setValue($condValue);
                                 }
                             }
                             $newCondition->setIsParametrizable(isset($condition['is_parametrizable']));
                             $newCondition->save();
                         }
                     }
                 }
                 asort($columns);
                 //sort the array by column order
                 foreach ($columns as $column => $order) {
                     if ($order > 0) {
                         $newColumn = new ReportColumn();
                         $newColumn->setReportId($newReport->getId());
                         if (is_numeric($column)) {
                             $newColumn->setCustomPropertyId($column);
                         } else {
                             $newColumn->setFieldName($column);
                         }
                         $newColumn->save();
                     }
                 }
                 $object_controller = new ObjectController();
                 $object_controller->add_to_members($newReport, $member_ids);
                 DB::commit();
                 flash_success(lang('custom report created'));
                 ajx_current('back');
             } catch (Exception $e) {
                 DB::rollback();
                 flash_error($e->getMessage());
                 ajx_current("empty");
             }
         }
     }
     $selected_type = array_var($_GET, 'type', '');
     $types = array(array("", lang("select one")));
     $object_types = ObjectTypes::getAvailableObjectTypes();
     foreach ($object_types as $ot) {
         $types[] = array($ot->getId(), lang($ot->getName()));
     }
     if ($selected_type != '') {
         tpl_assign('allowed_columns', $this->get_allowed_columns($selected_type));
     }
     tpl_assign('object_types', $types);
     tpl_assign('selected_type', $selected_type);
     $new_report = new Report();
     tpl_assign('object', $new_report);
 }
 function add_custom_report()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     tpl_assign('url', get_url('reporting', 'add_custom_report'));
     $report_data = array_var($_POST, 'report');
     if (is_array($report_data)) {
         tpl_assign('report_data', $report_data);
         $conditions = array_var($_POST, 'conditions');
         if (!is_array($conditions)) {
             $conditions = array();
         }
         tpl_assign('conditions', $conditions);
         $columns = array_var($_POST, 'columns');
         if (is_array($columns) && count($columns) > 0) {
             tpl_assign('columns', $columns);
             $newReport = new Report();
             if (!$newReport->canAdd(logged_user())) {
                 flash_error(lang('no access permissions'));
                 ajx_current("empty");
                 return;
             }
             // if
             $newReport->setName($report_data['name']);
             $newReport->setDescription($report_data['description']);
             $newReport->setObjectType($report_data['object_type']);
             $newReport->setOrderBy($report_data['order_by']);
             $newReport->setIsOrderByAsc($report_data['order_by_asc'] == 'asc');
             try {
                 DB::beginWork();
                 $newReport->save();
                 $allowed_columns = $this->get_allowed_columns($report_data['object_type'], true);
                 foreach ($conditions as $condition) {
                     if ($condition['deleted'] == "1") {
                         continue;
                     }
                     foreach ($allowed_columns as $ac) {
                         if ($condition['field_name'] == $ac['id']) {
                             $newCondition = new ReportCondition();
                             $newCondition->setReportId($newReport->getId());
                             $newCondition->setCustomPropertyId($condition['custom_property_id']);
                             $newCondition->setFieldName($condition['field_name']);
                             $newCondition->setCondition($condition['condition']);
                             $condValue = array_key_exists('value', $condition) ? $condition['value'] : '';
                             if ($condition['field_type'] == 'boolean') {
                                 $newCondition->setValue(array_key_exists('value', $condition));
                             } else {
                                 if ($condition['field_type'] == 'date') {
                                     if ($condValue != '') {
                                         $dtFromWidget = DateTimeValueLib::dateFromFormatAndString(user_config_option('date_format'), $condValue);
                                         $newCondition->setValue(date("m/d/Y", $dtFromWidget->getTimestamp()));
                                     }
                                 } else {
                                     $newCondition->setValue($condValue);
                                 }
                             }
                             $newCondition->setIsParametrizable(isset($condition['is_parametrizable']));
                             $newCondition->save();
                         }
                     }
                 }
                 asort($columns);
                 //sort the array by column order
                 foreach ($columns as $column => $order) {
                     if ($order > 0) {
                         $newColumn = new ReportColumn();
                         $newColumn->setReportId($newReport->getId());
                         if (is_numeric($column)) {
                             $newColumn->setCustomPropertyId($column);
                         } else {
                             $newColumn->setFieldName($column);
                         }
                         $newColumn->save();
                     }
                 }
                 DB::commit();
                 flash_success(lang('custom report created'));
                 ajx_current('back');
             } catch (Exception $e) {
                 DB::rollback();
                 flash_error($e->getMessage());
                 ajx_current("empty");
             }
         }
     }
     $selected_type = array_var($_GET, 'type', '');
     $types = array(array("", lang("select one")), array("Companies", lang("companies")), array("Contacts", lang("contacts")), array("MailContents", lang("email type")), array("ProjectEvents", lang("events")), array("ProjectFiles", lang("file")), array("ProjectMilestones", lang("milestone")), array("ProjectMessages", lang("message")), array("ProjectTasks", lang("task")), array("Users", lang("user")), array("ProjectWebpages", lang("webpage")), array("Projects", lang("workspace")));
     if ($selected_type != '') {
         tpl_assign('allowed_columns', $this->get_allowed_columns($selected_type));
     }
     tpl_assign('object_types', $types);
     tpl_assign('selected_type', $selected_type);
 }
 function add_custom_report()
 {
     if (logged_user()->isGuest()) {
         flash_error(lang('no access permissions'));
         ajx_current("empty");
         return;
     }
     tpl_assign('url', get_url('reporting', 'add_custom_report'));
     $report_data = array_var($_POST, 'report');
     if (is_array($report_data)) {
         foreach ($report_data as $k => &$v) {
             $v = remove_scripts($v);
         }
         tpl_assign('report_data', $report_data);
         $conditions = array_var($_POST, 'conditions');
         if (!is_array($conditions)) {
             $conditions = array();
         }
         tpl_assign('conditions', $conditions);
         $columns = array_var($_POST, 'columns');
         if (is_array($columns) && count($columns) > 0) {
             tpl_assign('columns', $columns);
             $newReport = new Report();
             $member_ids = json_decode(array_var($_POST, 'members'));
             $notAllowedMember = '';
             if (!logged_user()->isManager() && !logged_user()->isAdminGroup() && !$newReport->canAdd(logged_user(), active_context(), $notAllowedMember)) {
                 if (str_starts_with($notAllowedMember, '-- req dim --')) {
                     flash_error(lang('must choose at least one member of', str_replace_first('-- req dim --', '', $notAllowedMember, $in)));
                 } else {
                     trim($notAllowedMember) == "" ? flash_error(lang('you must select where to keep', lang('the report'))) : flash_error(lang('no context permissions to add', lang("report"), $notAllowedMember));
                 }
                 ajx_current("empty");
                 return;
             }
             $newReport->setObjectName($report_data['name']);
             $newReport->setDescription($report_data['description']);
             $newReport->setReportObjectTypeId($report_data['report_object_type_id']);
             $newReport->setOrderBy($report_data['order_by']);
             $newReport->setIsOrderByAsc($report_data['order_by_asc'] == 'asc');
             $newReport->setIgnoreContext(array_var($report_data, 'ignore_context') == 'checked');
             try {
                 DB::beginWork();
                 $newReport->save();
                 $allowed_columns = $this->get_allowed_columns($report_data['report_object_type_id'], true);
                 foreach ($conditions as $condition) {
                     if ($condition['deleted'] == "1") {
                         continue;
                     }
                     foreach ($allowed_columns as $ac) {
                         if ($condition['field_name'] == $ac['id']) {
                             $newCondition = new ReportCondition();
                             $newCondition->setReportId($newReport->getId());
                             $newCondition->setCustomPropertyId($condition['custom_property_id']);
                             $newCondition->setFieldName($condition['field_name']);
                             $newCondition->setCondition($condition['condition']);
                             $condValue = array_key_exists('value', $condition) ? $condition['value'] : '';
                             if ($condition['field_type'] == 'boolean') {
                                 $newCondition->setValue(array_key_exists('value', $condition) ? '1' : '0');
                             } else {
                                 if ($condition['field_type'] == 'date') {
                                     if ($condValue != '') {
                                         $dtFromWidget = DateTimeValueLib::dateFromFormatAndString(user_config_option('date_format'), $condValue);
                                         $newCondition->setValue(date("m/d/Y", $dtFromWidget->getTimestamp()));
                                     }
                                 } else {
                                     $newCondition->setValue($condValue);
                                 }
                             }
                             $newCondition->setIsParametrizable(isset($condition['is_parametrizable']));
                             $newCondition->save();
                         }
                     }
                 }
                 asort($columns);
                 //sort the array by column order
                 foreach ($columns as $column => $order) {
                     if ($order > 0) {
                         $newColumn = new ReportColumn();
                         $newColumn->setReportId($newReport->getId());
                         if (is_numeric($column)) {
                             $newColumn->setCustomPropertyId($column);
                         } else {
                             $newColumn->setFieldName($column);
                         }
                         $newColumn->save();
                     }
                 }
                 $no_need_to_add_to_members = count($member_ids) == 0 && (logged_user()->isManager() || logged_user()->isAdminGroup());
                 if (!$no_need_to_add_to_members) {
                     $object_controller = new ObjectController();
                     $object_controller->add_to_members($newReport, $member_ids);
                 } else {
                     $newReport->addToSharingTable();
                 }
                 DB::commit();
                 flash_success(lang('custom report created'));
                 ajx_current('back');
             } catch (Exception $e) {
                 DB::rollback();
                 flash_error($e->getMessage());
                 ajx_current("empty");
             }
         }
     }
     $selected_type = array_var($_GET, 'type', '');
     $types = array(array("", lang("select one")));
     $object_types = ObjectTypes::getAvailableObjectTypes();
     $object_types[] = ObjectTypes::findByName('timeslot');
     Hook::fire('custom_reports_object_types', array('object_types' => $object_types), $object_types);
     foreach ($object_types as $ot) {
         $types[] = array($ot->getId(), lang($ot->getName()));
     }
     if ($selected_type != '') {
         tpl_assign('allowed_columns', $this->get_allowed_columns($selected_type));
     }
     tpl_assign('object_types', $types);
     tpl_assign('selected_type', $selected_type);
     $new_report = new Report();
     tpl_assign('object', $new_report);
 }