Esempio n. 1
0
 /**
  * Function saves a Report
  */
 function save()
 {
     $adb = PearDatabase::getInstance();
     $currentUser = Users_Record_Model::getCurrentUserModel();
     $export_sql = false;
     //$export_sql = true;
     $export_to_file = false;
     //$export_to_file = true;
     $debug_save = false;
     //$debug_save = true;
     $request = new Vtiger_Request($_REQUEST, $_REQUEST);
     $request_data = $request->getAll();
     if ($export_sql) {
         ITS4YouReports::sshow($_REQUEST);
         //exit;
     }
     $reportid = $this->getId();
     $r4u_sesstion_name = ITS4YouReports::getITS4YouReportStoreName();
     $r4u_sesstion_unset = ITS4YouReports::unsetITS4YouReportsSerialize($r4u_sesstion_name);
     if ($debug_save) {
         echo "<pre>UNSET Session: ";
         print_r($r4u_sesstion_unset);
         echo "</pre>";
     }
     $ITS4YouReports = ITS4YouReports::getStoredITS4YouReport();
     $std_filter_columns = $ITS4YouReports->getStdFilterColumns();
     //<<<<<<<report>>>>>>>>>
     global $default_charset;
     $reportname = $request->get('reportname');
     $reportname = html_entity_decode($reportname, ENT_QUOTES, $default_charset);
     $reportdescription = $request->get("reportdesc");
     $reportdescription = html_entity_decode($reportdescription, ENT_QUOTES, $default_charset);
     $reporttype = $request->get("reporttype");
     $folderid = $request->get("reportfolder");
     $isDuplicate = $request->get("isDuplicate");
     //<<<<<<<report>>>>>>>>>
     //<<<<<<<selectcolumn>>>>>>>>>
     $selectedcolumnstring = $request->get("selectedColumnsString");
     $selectedcolumnstring = str_replace("@AMPKO@", "&", $selectedcolumnstring);
     $saveselectedcolumns = explode(";", trim($selectedcolumnstring, ";"));
     $selectedcolumns = array();
     foreach ($saveselectedcolumns as $sc) {
         $selectedcolumns[] = $sc;
     }
     //<<<<<<<selectcolumn>>>>>>>>>
     //<<<<<<<selectedSummaries>>>>>>>>>
     $selectedSummariesString = $request->get("selectedSummariesString");
     $selectedSummariesString = str_replace("@AMPKO@", "&", $selectedSummariesString);
     $selectedSummaries_array = explode(";", trim($selectedSummariesString, ";"));
     $selectedSummaries = array();
     foreach ($selectedSummaries_array as $sm) {
         $selectedSummaries[] = $sm;
     }
     //<<<<<<<selectedSummaries>>>>>>>>>
     //<<<<<<<SummariesOrderBy>>>>>>>>>
     $summaries_orderby = $request->get("summaries_orderby_columnString");
     $summaries_orderby_type = $request->get("summaries_orderby_type");
     //<<<<<<<SummariesOrderBy>>>>>>>>>
     // ITS4YOU-CR SlOl 13. 3. 2014 10:17:31
     $lbl_array = array();
     $lbl_url_string = $request->get("labels_to_go");
     $lbl_url_string = str_replace("@AMPKO@", "&", $lbl_url_string);
     $lbl_url_string = urldecode($lbl_url_string);
     $lbl_url_string = html_entity_decode($lbl_url_string, ENT_QUOTES, $default_charset);
     if ($lbl_url_string != "") {
         $lbl_url_arr = explode('$_@_$', $lbl_url_string);
         foreach ($lbl_url_arr as $key => $lbl_value) {
             if (strpos($lbl_value, '_SC_lLbLl_') !== false) {
                 $temp = explode('_SC_lLbLl_', $lbl_value);
                 $temp_lbls = explode('_lLGbGLl_', $temp[1]);
                 $lbl_key = $temp_lbls[0];
                 $lbl_value = $temp_lbls[1];
                 $lbl_array["SC"][$lbl_key] = $lbl_value;
             }
             if (strpos($lbl_value, '_SM_lLbLl_') !== false) {
                 $temp = explode('_SM_lLbLl_', $lbl_value);
                 $temp_lbls = explode('_lLGbGLl_', $temp[1]);
                 $lbl_key = $temp_lbls[0];
                 $lbl_value = $temp_lbls[1];
                 $lbl_array["SM"][$lbl_key] = $lbl_value;
             }
             if (strpos($lbl_value, '_CT_lLbLl_') !== false) {
                 $temp = explode('_CT_lLbLl_', $lbl_value);
                 $temp_lbls = explode('_lLGbGLl_', $temp[1]);
                 $lbl_key = $temp_lbls[0];
                 $lbl_value = $temp_lbls[1];
                 $lbl_array["CT"][$lbl_key] = $lbl_value;
             }
         }
     }
     // ITS4YOU-END 13. 3. 2014 10:17:32
     //<<<<<<<reportsortcol>>>>>>>>>
     $sort_by1 = decode_html($request->get("Group1"));
     $sort_order1 = $request->get("Sort1");
     $sort_by2 = decode_html($request->get("Group2"));
     $sort_order2 = $request->get("Sort2");
     $sort_by3 = decode_html($request->get("Group3"));
     $sort_order3 = $request->get("Sort3");
     $timeline_type2 = $request->get("timeline_type2");
     $timeline_type3 = $request->get("timeline_type3");
     if ($request->has("TimeLineColumn_Group1") && !$request->isEmpty("TimeLineColumn_Group1") && $sort_by1 != "none") {
         $TimeLineColumn_Group1 = $request->get("TimeLineColumn_Group1");
         $TimeLineColumn_Group1_arr = explode("@vlv@", $TimeLineColumn_Group1);
         $TimeLineColumn_str1 = $TimeLineColumn_Group1_arr[0];
         $TimeLineColumn_frequency1 = $TimeLineColumn_Group1_arr[1];
     }
     if ($request->has("TimeLineColumn_Group2") && !$request->isEmpty("TimeLineColumn_Group2") && $sort_by2 != "none") {
         $TimeLineColumn_Group2 = $request->get("TimeLineColumn_Group2");
         $TimeLineColumn_Group2_arr = explode("@vlv@", $TimeLineColumn_Group2);
         $TimeLineColumn_str2 = $TimeLineColumn_Group2_arr[0];
         $TimeLineColumn_frequency2 = $TimeLineColumn_Group2_arr[1];
     }
     if ($request->has("TimeLineColumn_Group3") && !$request->isEmpty("TimeLineColumn_Group3") && $sort_by3 != "none") {
         $TimeLineColumn_Group3 = $request->get("TimeLineColumn_Group3");
         $TimeLineColumn_Group3_arr = explode("@vlv@", $TimeLineColumn_Group3);
         $TimeLineColumn_str3 = $TimeLineColumn_Group3_arr[0];
         $TimeLineColumn_frequency3 = $TimeLineColumn_Group3_arr[1];
     }
     $sort_by_column = decode_html($request->get("SortByColumn"));
     $sort_order_column = $request->get("SortOrderColumn");
     //<<<<<<<reportsortcol>>>>>>>>>
     //<<<<<<<reportmodules>>>>>>>>>
     $pmodule = $request->get("primarymodule");
     $smodule = trim($request->get("secondarymodule"), ":");
     //<<<<<<<reportmodules>>>>>>>>>
     //<<<<<<<shared entities>>>>>>>>>
     $sharetype = $request->get("sharing");
     $shared_entities = $request->get("sharingSelectedColumnsString");
     //<<<<<<<shared entities>>>>>>>>>
     //<<<<<<<columnstototal>>>>>>>>>>
     if ($request->has("curl_to_go") && !$request->isEmpty("curl_to_go")) {
         $columnstototal = explode('$_@_$', $request->get("curl_to_go"));
     }
     //<<<<<<<columnstototal>>>>>>>>>
     //<<<<<<<advancedfilter>>>>>>>>check
     $json = new Zend_Json();
     $std_filter_columns = $ITS4YouReports->getStdFilterColumns();
     $advft_criteria_groups = $request->get('advft_criteria_groups');
     $new_criteria_groups = $gi_change = array();
     if (!empty($advft_criteria_groups)) {
         $new_gi = 1;
         foreach ($advft_criteria_groups as $advftg_i => $advftg_array) {
             if (!empty($advftg_array)) {
                 $gi_change[$advftg_i] = $new_gi;
                 $new_criteria_groups[$new_gi] = $advftg_array;
                 $new_gi++;
             }
         }
     }
     $advft_criteria_groups = $new_criteria_groups;
     //advft_criteria_groups = $json->decode($advft_criteria_groups);
     $new_advft_criteria = array();
     $advft_criteria = $request->get('advft_criteria');
     if (!empty($advft_criteria)) {
         $new_i = 0;
         foreach ($advft_criteria as $advft_i => $advft_array) {
             if (!empty($advft_array)) {
                 $advft_array["groupid"] = $gi_change[$advft_array["groupid"]];
                 $new_advft_criteria[$new_i] = $advft_array;
                 $new_i++;
             }
         }
     }
     $advft_criteria = $new_advft_criteria;
     //$advft_criteria = $json->decode($advft_criteria);
     //<<<<<<<advancedfilter>>>>>>>>
     //<<<<<<<groupconditioncolumn>>>>>>>>
     $groupft_criteria = $request->get('groupft_criteria');
     //$groupft_criteria = $json->decode($groupft_criteria);
     //<<<<<<<groupconditioncolumn>>>>>>>>
     //<<<<<<<limit>>>>>>>>
     $limit = $summaries_limit = 0;
     if ($request->has("columns_limit") && !$request->isEmpty("columns_limit")) {
         $limit = $request->get("columns_limit");
     }
     if ($request->has("summaries_limit") && !$request->isEmpty("summaries_limit")) {
         $summaries_limit = $request->get("summaries_limit");
     }
     //<<<<<<<limit>>>>>>>>
     //<<<<<<<scheduled report>>>>>>>>
     $isReportScheduled = $request->get('isReportScheduled');
     $scheduledFormat = "";
     $r_key_arr = array();
     if ($isReportScheduled == 'on' || $isReportScheduled == '1') {
         foreach ($request_data as $r_key => $r_val) {
             if (strpos($r_key, "scheduledReportFormat_") !== false) {
                 $r_key_arr[] = substr($r_key, 22);
             }
         }
     }
     if (!empty($r_key_arr)) {
         $scheduledFormat = implode(";", $r_key_arr);
     }
     $selectedRecipients = $request->get('selectedRecipientsString');
     $selectedRecipients = Zend_JSON::encode($selectedRecipients);
     //$scheduledFormat = $request->get('scheduledReportFormat');
     $scheduledInterval = $request->get('scheduledIntervalString');
     $scheduledInterval = Zend_JSON::encode($scheduledInterval);
     //<<<<<<<scheduled report>>>>>>>>
     // ITS4YOU-CR SlOl 20. 3. 2014 12:02:47
     for ($tg_i = 1; $tg_i < 4; $tg_i++) {
         if ($request->has("TimeLineColumn_Group{$tg_i}") && !$request->isEmpty("TimeLineColumn_Group{$tg_i}") && $request->get("TimeLineColumn_Group{$tg_i}") != "none") {
             $tg_col_str = $request->get("TimeLineColumn_Group{$tg_i}");
             $tg_col_arr = explode("@vlv@", $tg_col_str);
             $timelinecols_arr[$tg_i] = $tg_col_str;
             $timelinecols_frequency[$tg_i] = $tg_col_arr[1];
         }
     }
     // ITS4YOU-END 20. 3. 2014 12:02:48
     // ITS4YOU-CR SlOl | 2.7.2014 15:18
     //
     // chartType1 data_series1
     if ($request->has("chartType1") && !$request->isEmpty("chartType1")) {
         $charttitle = $request->get("charttitle");
         $x_group = $request->get("x_group");
         $charts = array();
         for ($chi = 1; $chi < 4; $chi++) {
             // charttype 	dataseries 	charttitle 	chart_seq 	x_group
             if ($request->has("chartType{$chi}") && !$request->isEmpty("chartType{$chi}")) {
                 $charttype = $request->get("chartType{$chi}");
                 if ($charttype != "none") {
                     $ch_params = array("charttype" => $charttype, "dataseries" => $request->get("data_series{$chi}"), "charttitle" => $charttitle, "chart_seq" => $chi, "x_group" => $x_group);
                     $charts[$chi] = $ch_params;
                 } else {
                     break;
                 }
             } else {
                 break;
             }
         }
     }
     /*
      if ($request->has("chartType") && !$request->isEmpty("chartType")) {
      $chartType = $request->get("chartType");
      if ($chartType != "" && $chartType != "none") {
      $data_series = $request->get("data_series");
      $charttitle = $request->get("charttitle");
      }
      }
     */
     // ITS4YOU-END 2.7.2014 15:18
     // ITS4YOU-CR SlOl | 20.8.2015 11:30
     if ($reporttype == "custom_report") {
         $customSql = ITS4YouReports::validateCustomSql($request->get("reportcustomsql"));
     }
     // ITS4YOU-END 20.8.2015 11:30
     if ($reportid != "" && $isDuplicate != "true") {
         $d_selectedcolumns = "DELETE FROM its4you_reports4you_selectcolumn WHERE its4you_reports4you_selectcolumn.queryid = ?";
         $d_columnsqlresult = $adb->pquery($d_selectedcolumns, array($reportid));
         if (!empty($selectedcolumns)) {
             for ($i = 0; $i < count($selectedcolumns); $i++) {
                 if (!empty($selectedcolumns[$i])) {
                     $icolumnsql = "INSERT INTO its4you_reports4you_selectcolumn (QUERYID,COLUMNINDEX,COLUMNNAME) VALUES (?,?,?)";
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $params = array($reportid, $i, decode_html($selectedcolumns[$i]));
                     $params_tofile = array("@reportid", $i, decode_html($selectedcolumns[$i]));
                     $report_array['its4you_reports4you_selectcolumn'][] = $params_tofile;
                     $icolumnsqlresult = $adb->pquery($icolumnsql, $params);
                     $export_sql === true ? $adb->setDebug(false) : "";
                 }
             }
         }
         // ITS4YOU-CR SlOl 7. 3. 2014 11:24:46 Summaries Save
         $d_selectedSummaries = "DELETE FROM its4you_reports4you_summaries WHERE reportsummaryid = ?";
         $d_Summariessqlqfresult = $adb->pquery($d_selectedSummaries, array($reportid));
         if (!empty($selectedSummaries)) {
             for ($i = 0; $i < count($selectedSummaries); $i++) {
                 if (!empty($selectedSummaries[$i])) {
                     $iSmmariessql = "INSERT INTO its4you_reports4you_summaries (reportsummaryid,summarytype,columnname) VALUES (?,?,?)";
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $params = array($reportid, $i, decode_html($selectedSummaries[$i]));
                     $params_tofile = array("@reportid", $i, decode_html($selectedSummaries[$i]));
                     $report_array['its4you_reports4you_summaries'][] = $params_tofile;
                     $iSummariessqlresult = $adb->pquery($iSmmariessql, $params);
                     $export_sql === true ? $adb->setDebug(false) : "";
                 }
             }
         }
         // ITS4YOU-END 7. 3. 2014 11:24:48
         // ITS4YOU-CR SlOl 24. 3. 2014 8:52:40
         $d_selected_s_orderby = "DELETE FROM its4you_reports4you_summaries_orderby WHERE reportid = ?";
         $d_selected_s_orderbyresult = $adb->pquery($d_selected_s_orderby, array($reportid));
         if ($summaries_orderby != "" && $summaries_orderby_type != "") {
             $d_selected_s_orderby_sql = "INSERT INTO  its4you_reports4you_summaries_orderby (reportid,columnindex,summaries_orderby,summaries_orderby_type) VALUES (?,?,?,?)";
             $export_sql === true ? $adb->setDebug(true) : "";
             $params = array($reportid, 0, $summaries_orderby, $summaries_orderby_type);
             $params_tofile = array("@reportid", 0, $summaries_orderby, $summaries_orderby_type);
             $report_array['its4you_reports4you_summaries_orderby'] = $params_tofile;
             $d_selected_s_orderby_result = $adb->pquery($d_selected_s_orderby_sql, $params);
             $export_sql === true ? $adb->setDebug(false) : "";
         }
         // ITS4YOU-END 24. 3. 2014 8:52:42
         // ITS4YOU-CR SlOl 13. 3. 2014 11:34:24
         $d_selectedLabels = "DELETE FROM its4you_reports4you_labels WHERE reportid = ?";
         $d_Labelssqlqfresult = $adb->pquery($d_selectedLabels, array($reportid));
         if (!empty($lbl_array)) {
             foreach ($lbl_array as $type => $type_array) {
                 foreach ($type_array as $column_str => $column_lbl) {
                     $iLabelssql = "INSERT INTO its4you_reports4you_labels (reportid,type,columnname,columnlabel) VALUES (?,?,?,?)";
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $params = array($reportid, $type, $column_str, $column_lbl);
                     $params_tofile = array("@reportid", $type, $column_str, $column_lbl);
                     $report_array['its4you_reports4you_labels'][] = $params_tofile;
                     $iLabelssqlresult = $adb->pquery($iLabelssql, $params);
                     $export_sql === true ? $adb->setDebug(false) : "";
                 }
             }
         }
         $d_selectedqfcolumns = "DELETE FROM its4you_reports4you_selectqfcolumn WHERE queryid = ?";
         $d_columnsqlqfresult = $adb->pquery($d_selectedqfcolumns, array($reportid));
         if (!empty($selectedqfcolumns)) {
             for ($i = 0; $i < count($selectedqfcolumns); $i++) {
                 if (!empty($selectedqfcolumns[$i])) {
                     $icolumnsql = "INSERT INTO its4you_reports4you_selectqfcolumn (QUERYID,COLUMNINDEX,COLUMNNAME) VALUES (?,?,?)";
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $params = array($reportid, $i, decode_html($selectedqfcolumns[$i]));
                     $params_tofile = array("@reportid", $i, decode_html($selectedqfcolumns[$i]));
                     $report_array['its4you_reports4you_selectqfcolumn'][] = $params_tofile;
                     $icolumnsqlresult = $adb->pquery($icolumnsql, $params);
                     $export_sql === true ? $adb->setDebug(false) : "";
                 }
             }
         }
         $d_shared = "DELETE FROM its4you_reports4you_sharing WHERE reports4youid = ?";
         $d_sharedresult = $adb->pquery($d_shared, array($reportid));
         if ($shared_entities != "") {
             if ($sharetype == "share") {
                 $selectedsharecolumn = explode("|", $shared_entities);
                 for ($i = 0; $i < count($selectedsharecolumn) - 1; $i++) {
                     $temp = split("::", $selectedsharecolumn[$i]);
                     $icolumnsql = "INSERT INTO its4you_reports4you_sharing (reports4youid,shareid,setype) VALUES (?,?,?)";
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $params = array($reportid, $temp[1], $temp[0]);
                     $params_tofile = array("@reportid", $temp[1], $temp[0]);
                     $report_array['its4you_reports4you_sharing'][] = $params_tofile;
                     $icolumnsqlresult = $adb->pquery($icolumnsql, $params);
                     $export_sql === true ? $adb->setDebug(false) : "";
                 }
             }
         }
         if ($reportid != "") {
             $ireportsql = "UPDATE its4you_reports4you SET reports4youname=?, description=?, folderid=?, reporttype=?, columns_limit=?, summaries_limit=? WHERE reports4youid=?";
             $ireportparams = array($reportname, $reportdescription, $folderid, $reporttype, $limit, $summaries_limit, $reportid);
             $params_tofile = array("@reportid", $reportname, $reportdescription, $folderid, $reporttype, 0, $limit, $summaries_limit);
             // ITS4YOU-END
             $export_sql === true ? $adb->setDebug(true) : "";
             $report_array['its4you_reports4you'] = $params_tofile;
             $ireportresult = $adb->pquery($ireportsql, $ireportparams);
             $export_sql === true ? $adb->setDebug(false) : "";
             if ($ireportresult != false) {
                 if ($reporttype != "custom_report") {
                     //<<<<reportmodules>>>>>>>
                     $d_modules = "DELETE FROM its4you_reports4you_modules WHERE reportmodulesid = ?";
                     $d_modulesresult = $adb->pquery($d_modules, array($reportid));
                     $ireportmodulesql = "INSERT INTO its4you_reports4you_modules (REPORTMODULESID,PRIMARYMODULE,SECONDARYMODULES) VALUES (?,?,?)";
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $params = array($reportid, $pmodule, $smodule);
                     $params_tofile = array("@reportid", $pmodule, $smodule);
                     $report_array['its4you_reports4you_modules'] = $params_tofile;
                     $ireportmoduleresult = $adb->pquery($ireportmodulesql, $params);
                     $export_sql === true ? $adb->setDebug(false) : "";
                     //<<<<reportmodules>>>>>>>
                     //<<<<step3 its4you_reports4you_sortcol>>>>>>>
                     $d_sortcol1 = "DELETE FROM its4you_reports4you_sortcol WHERE reportid = ? AND sortcolid=?";
                     $d_sortcol1_result = $adb->pquery($d_sortcol1, array($reportid, 1));
                     if ($sort_by1 != "") {
                         $sort_by1sql = "INSERT INTO its4you_reports4you_sortcol (SORTCOLID,REPORTID,COLUMNNAME,SORTORDER,timeline_columnstr,timeline_columnfreq) VALUES (?,?,?,?,?,?)";
                         $export_sql === true ? $adb->setDebug(true) : "";
                         $params = array(1, $reportid, $sort_by1, $sort_order1, $TimeLineColumn_str1, $TimeLineColumn_frequency1);
                         $params_tofile = array(1, "@reportid", $sort_by1, $sort_order1, "rows", $TimeLineColumn_str1, $TimeLineColumn_frequency1);
                         $report_array['its4you_reports4you_sortcol'][] = $params_tofile;
                         $sort_by1result = $adb->pquery($sort_by1sql, $params);
                         $export_sql === true ? $adb->setDebug(false) : "";
                     }
                     $d_sortcol2 = "DELETE FROM its4you_reports4you_sortcol WHERE reportid = ? AND sortcolid=?";
                     $d_sortcol2_result = $adb->pquery($d_sortcol2, array($reportid, 2));
                     if ($sort_by2 != "") {
                         $sort_by2sql = "INSERT INTO its4you_reports4you_sortcol (SORTCOLID,REPORTID,COLUMNNAME,SORTORDER,timeline_type,timeline_columnstr,timeline_columnfreq) VALUES (?,?,?,?,?,?,?)";
                         $export_sql === true ? $adb->setDebug(true) : "";
                         $params = array(2, $reportid, $sort_by2, $sort_order2, $timeline_type2, $TimeLineColumn_str2, $TimeLineColumn_frequency2);
                         $params_tofile = array(2, "@reportid", $sort_by2, $sort_order2, $timeline_type2, $TimeLineColumn_str2, $TimeLineColumn_frequency2);
                         $report_array['its4you_reports4you_sortcol'][] = $params_tofile;
                         $sort_by2result = $adb->pquery($sort_by2sql, $params);
                         $export_sql === true ? $adb->setDebug(false) : "";
                     }
                     $d_sortcol3 = "DELETE FROM its4you_reports4you_sortcol WHERE reportid = ? AND sortcolid=?";
                     $d_sortcol3_result = $adb->pquery($d_sortcol3, array($reportid, 3));
                     if ($sort_by3 != "") {
                         $sort_by3sql = "INSERT INTO its4you_reports4you_sortcol (SORTCOLID,REPORTID,COLUMNNAME,SORTORDER,timeline_type,timeline_columnstr,timeline_columnfreq) VALUES (?,?,?,?,?,?,?)";
                         $export_sql === true ? $adb->setDebug(true) : "";
                         $params = array(3, $reportid, $sort_by3, $sort_order3, $timeline_type3, $TimeLineColumn_str3, $TimeLineColumn_frequency3);
                         $params_tofile = array(3, "@reportid", $sort_by3, $sort_order3, $timeline_type3, $TimeLineColumn_str3, $TimeLineColumn_frequency3);
                         $report_array['its4you_reports4you_sortcol'][] = $params_tofile;
                         $sort_by3result = $adb->pquery($sort_by3sql, $params);
                         $export_sql === true ? $adb->setDebug(false) : "";
                     }
                     $d_sortcol4 = "DELETE FROM its4you_reports4you_sortcol WHERE reportid = ? AND sortcolid=?";
                     $d_sortcol4_result = $adb->pquery($d_sortcol4, array($reportid, 4));
                     if ($sort_by_column != "") {
                         $sort_by_columnsql = "INSERT INTO its4you_reports4you_sortcol (SORTCOLID,REPORTID,COLUMNNAME,SORTORDER) VALUES (?,?,?,?)";
                         $export_sql === true ? $adb->setDebug(true) : "";
                         $params = array(4, $reportid, $sort_by_column, $sort_order_column);
                         $params_tofile = array(4, "@reportid", $sort_by_column, $sort_order_column, "", "", "");
                         $report_array['its4you_reports4you_sortcol'][] = $params_tofile;
                         ${$sort_by_columnresult} = $adb->pquery($sort_by_columnsql, $params);
                         $export_sql === true ? $adb->setDebug(false) : "";
                     }
                     //<<<<step3 its4you_reports4you_sortcol>>>>>>>
                     //<<<<step5 standarfilder>>>>>>>
                     $d_datefilter = "DELETE FROM its4you_reports4you_datefilter WHERE datefilterid = ?";
                     $d_datefilter_result = $adb->pquery($d_datefilter, array($reportid));
                     $ireportmodulesql = "INSERT INTO its4you_reports4you_datefilter (DATEFILTERID,DATECOLUMNNAME,DATEFILTER,STARTDATE,ENDDATE) VALUES (?,?,?,?,?)";
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $params = array($reportid, $stdDateFilterField, $stdDateFilter, $startdate, $enddate);
                     $params_tofile = array("@reportid", $stdDateFilterField, $stdDateFilter, $startdate, $enddate);
                     $report_array['its4you_reports4you_datefilter'] = $params_tofile;
                     $ireportmoduleresult = $adb->pquery($ireportmodulesql, $params);
                     $export_sql === true ? $adb->setDebug(false) : "";
                     //<<<<step5 standarfilder>>>>>>>
                     //<<<<step4 columnstototal>>>>>>>
                     $d_summary = "DELETE FROM its4you_reports4you_summary WHERE reportsummaryid = ?";
                     $d_summary_result = $adb->pquery($d_summary, array($reportid));
                     for ($i = 0; $i < count($columnstototal); $i++) {
                         $ireportsummarysql = "INSERT INTO its4you_reports4you_summary (REPORTSUMMARYID,SUMMARYTYPE,COLUMNNAME) VALUES (?,?,?)";
                         $export_sql === true ? $adb->setDebug(true) : "";
                         $params = array($reportid, $i, $columnstototal[$i]);
                         $params_tofile = array("@reportid", $i, $columnstototal[$i]);
                         $report_array['its4you_reports4you_summary'][] = $params_tofile;
                         $ireportsummaryresult = $adb->pquery($ireportsummarysql, $params);
                         $export_sql === true ? $adb->setDebug(false) : "";
                     }
                     //<<<<step4 columnstototal>>>>>>>
                     //<<<<step5 advancedfilter>>>>>>>
                     $default_charset = vglobal("default_charset");
                     $this->saveAdvancedFilters($advft_criteria, $advft_criteria_groups, $std_filter_columns, $export_sql);
                     $d_adv_criteria = "DELETE FROM its4you_reports4you_relcriteria_summaries WHERE reportid = ?";
                     $d_adv_criteria_result = $adb->pquery($d_adv_criteria, array($reportid));
                     foreach ($groupft_criteria as $column_index => $column_condition) {
                         if (empty($column_condition)) {
                             continue;
                         }
                         $adv_filter_column = $column_condition["columnname"];
                         $adv_filter_comparator = $column_condition["comparator"];
                         $adv_filter_value = $column_condition["value"];
                         $adv_filter_column_condition = $column_condition["column_condition"];
                         $adv_filter_groupid = $column_condition["groupid"];
                         $column_info = explode(":", $adv_filter_column);
                         $temp_val = explode(",", $adv_filter_value);
                         if (($column_info[4] == 'D' || $column_info[4] == 'T' && $column_info[1] != 'time_start' && $column_info[1] != 'time_end' || $column_info[4] == 'DT') && ($column_info[4] != '' && $adv_filter_value != '')) {
                             $val = array();
                             for ($x = 0; $x < count($temp_val); $x++) {
                                 if (trim($temp_val[$x]) != '') {
                                     $date = new DateTimeField(trim($temp_val[$x]));
                                     if ($column_info[4] == 'D') {
                                         $val[$x] = DateTimeField::convertToDBFormat(trim($temp_val[$x]));
                                     } elseif ($column_info[4] == 'DT') {
                                         $val[$x] = $date->getDBInsertDateTimeValue();
                                     } else {
                                         $val[$x] = $date->getDBInsertTimeValue();
                                     }
                                 }
                             }
                             $adv_filter_value = implode(",", $val);
                         }
                         $irelcriteriasql = "INSERT INTO its4you_reports4you_relcriteria_summaries(reportid,COLUMNINDEX,COLUMNNAME,COMPARATOR,VALUE,GROUPID,COLUMN_CONDITION) VALUES (?,?,?,?,?,?,?)";
                         $export_sql === true ? $adb->setDebug(true) : "";
                         $params = array($reportid, $column_index, $adv_filter_column, $adv_filter_comparator, $adv_filter_value, $adv_filter_groupid, $adv_filter_column_condition);
                         $params_tofile = array("@reportid", $column_index, $adv_filter_column, $adv_filter_comparator, $adv_filter_value, $adv_filter_groupid, $adv_filter_column_condition);
                         $report_array['its4you_reports4you_relcriteria_summaries'][] = $params_tofile;
                         $irelcriteriaresult = $adb->pquery($irelcriteriasql, $params);
                         $export_sql === true ? $adb->setDebug(false) : "";
                         // Update the condition expression for the group to which the condition column belongs
                         $groupConditionExpression = '';
                         if (!empty($advft_criteria_groups[$adv_filter_groupid]["conditionexpression"])) {
                             $groupConditionExpression = $advft_criteria_groups[$adv_filter_groupid]["conditionexpression"];
                         }
                         $groupConditionExpression = $groupConditionExpression . ' ' . $column_index . ' ' . $adv_filter_column_condition;
                         $advft_criteria_groups[$adv_filter_groupid]["conditionexpression"] = $groupConditionExpression;
                     }
                     //<<<<step5 advancedfilter>>>>>>>
                 }
                 $owner = vtlib_purify($_REQUEST["template_owner"]);
                 $sharingtype = vtlib_purify($_REQUEST["sharing"]);
                 if ($owner != "" && $owner != "") {
                     $limitsql = "UPDATE its4you_reports4you_settings SET owner=?, sharingtype=? WHERE reportid=?";
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $params = array($owner, $sharingtype, $reportid);
                     $params_tofile = array("@reportid", $owner, $sharingtype);
                     $report_array['its4you_reports4you_settings'] = $params_tofile;
                     $limitresult = $adb->pquery($limitsql, $params);
                     $export_sql === true ? $adb->setDebug(false) : "";
                 }
                 //<<<<step7 scheduledReport>>>>>>>
                 if ($isReportScheduled == 'off' || $isReportScheduled == '0' || $isReportScheduled == '') {
                     $deleteScheduledReportSql = "DELETE FROM its4you_reports4you_scheduled_reports WHERE reportid=?";
                     $adb->pquery($deleteScheduledReportSql, array($reportid));
                 } else {
                     $checkScheduledResult = $adb->pquery('SELECT 1 FROM its4you_reports4you_scheduled_reports WHERE reportid=?', array($reportid));
                     if ($adb->num_rows($checkScheduledResult) > 0) {
                         $scheduledReportSql = 'UPDATE its4you_reports4you_scheduled_reports SET recipients=?,schedule=?,format=? WHERE reportid=?';
                         $export_sql === true ? $adb->setDebug(true) : "";
                         $params = array($selectedRecipients, $scheduledInterval, $scheduledFormat, $reportid);
                         $params_tofile = array("@reportid", $selectedRecipients, $scheduledInterval, $scheduledFormat, date("Y-m-d H:i:s"));
                         $report_array['its4you_reports4you_scheduled_reports'] = $params_tofile;
                         $adb->pquery($scheduledReportSql, $params);
                         $export_sql === true ? $adb->setDebug(false) : "";
                     } else {
                         $scheduleReportSql = 'INSERT INTO its4you_reports4you_scheduled_reports (reportid,recipients,schedule,format,next_trigger_time) VALUES (?,?,?,?,?)';
                         $export_sql === true ? $adb->setDebug(true) : "";
                         $params = array($reportid, $selectedRecipients, $scheduledInterval, $scheduledFormat, date("Y-m-d H:i:s"));
                         $params_tofile = array("@reportid", $selectedRecipients, $scheduledInterval, $scheduledFormat, date("Y-m-d H:i:s"));
                         $report_array['its4you_reports4you_scheduled_reports'] = $params_tofile;
                         $adb->pquery($scheduleReportSql, $params);
                         $export_sql === true ? $adb->setDebug(false) : "";
                     }
                 }
                 //<<<<step7 scheduledReport>>>>>>>
                 if ($reporttype != "custom_report") {
                     //<<<<step12 Report Charts >>>>>>>
                     $deleteChartsSql = "DELETE FROM its4you_reports4you_charts WHERE reports4youid=?";
                     $adb->pquery($deleteChartsSql, array($reportid));
                     if (!empty($charts)) {
                         foreach ($charts as $chi => $ch_params) {
                             $ChartsSql = 'INSERT INTO its4you_reports4you_charts (reports4youid,charttype,dataseries,charttitle,chart_seq,x_group) VALUES (?,?,?,?,?,?)';
                             $ch_params_tofile = array_merge(array("reports4youid" => "@reportid"), $ch_params);
                             $ch_params = array_merge(array($reportid), $ch_params);
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $report_array['its4you_reports4you_charts'][] = $ch_params_tofile;
                             $adb->pquery($ChartsSql, array($ch_params));
                             $export_sql === true ? $adb->setDebug(false) : "";
                         }
                     }
                     /*
                      if ($chartType != "" && $chartType != "none") {
                      $ChartsSql = 'INSERT INTO its4you_reports4you_charts (reports4youid,charttype,dataseries,charttitle) VALUES (?,?,?,?)';
                      $export_sql === true ? $adb->setDebug(true) : "";
                      $adb->pquery($ChartsSql, array($reportid, $chartType, $data_series, $charttitle));
                      $export_sql === true ? $adb->setDebug(false) : "";
                      }
                     */
                     //<<<<step12 Report Charts >>>>>>>
                 }
                 //<<<<step13 Custom Report SQL >>>>>>>
                 if ($reporttype == "custom_report") {
                     $deleteCustomSql = "DELETE FROM its4you_reports4you_customsql WHERE reports4youid=?";
                     $adb->pquery($deleteCustomSql, array($reportid));
                     $CustomSqlQry = 'INSERT INTO its4you_reports4you_customsql (reports4youid,custom_sql) VALUES (?,?)';
                     $params = array($reportid, $customSql);
                     $params_tofile = array("@reportid", $customSql);
                     $report_array['its4you_reports4you_customsql'] = $params_tofile;
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $adb->pquery($CustomSqlQry, array($params));
                     $export_sql === true ? $adb->setDebug(false) : "";
                 }
                 //<<<<step13 Custom Report SQL >>>>>>>
             } else {
                 $errormessage = "<font color='red'><B>Error Message<ul>\n                                            <li><font color='red'>Error while inserting the record</font>\n                                            </ul></B></font> <br>";
                 echo $errormessage;
                 die;
             }
         }
         if ($export_sql === true) {
             ITS4YouReports::sshow("EDIT");
         }
     } else {
         $genQueryId = $adb->getUniqueID("its4you_reports4you_selectquery");
         if ($genQueryId < 1) {
             $adb->pquery("DELETE FROM its4you_reports4you_selectquery_seq ", array());
             $adb->pquery("SELECT @max_reportid:=max(reports4youid)+1 FROM `its4you_reports4you`;", array());
             $adb->pquery("INSERT INTO its4you_reports4you_selectquery_seq (id) VALUES (@max_reportid); ", array());
             $genQueryId = $adb->getUniqueID("its4you_reports4you_selectquery");
         }
         $reportid = $genQueryId;
         $this->setId($reportid);
         if ($genQueryId != "") {
             $iquerysql = "insert into its4you_reports4you_selectquery (QUERYID,STARTINDEX,NUMOFOBJECTS) values (?,?,?)";
             $export_sql === true ? $adb->setDebug(true) : "";
             $params = array($genQueryId, 0, 0);
             $params_tofile = array("@reportid", 0, 0);
             $report_array['its4you_reports4you_selectquery'] = $params_tofile;
             $iquerysqlresult = $adb->pquery($iquerysql, $params);
             $export_sql === true ? $adb->setDebug(false) : "";
             if ($iquerysqlresult != false) {
                 //<<<<step2 vtiger_rep4u_selectcolumn>>>>>>>>
                 if (!empty($selectedcolumns)) {
                     for ($i = 0; $i < count($selectedcolumns); $i++) {
                         if (!empty($selectedcolumns[$i])) {
                             $icolumnsql = "insert into its4you_reports4you_selectcolumn (QUERYID,COLUMNINDEX,COLUMNNAME) values (?,?,?)";
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $params = array($genQueryId, $i, decode_html($selectedcolumns[$i]));
                             $params_tofile = array("@reportid", $i, decode_html($selectedcolumns[$i]));
                             $report_array['its4you_reports4you_selectcolumn'][] = $params_tofile;
                             $icolumnsqlresult = $adb->pquery($icolumnsql, $params);
                             $export_sql === true ? $adb->setDebug(false) : "";
                         }
                     }
                 }
                 //<<<< its4you_reports4you_summaries>>>>>>>>
                 if (!empty($selectedSummaries)) {
                     for ($i = 0; $i < count($selectedSummaries); $i++) {
                         if (!empty($selectedSummaries[$i])) {
                             $iSmmariessql = "INSERT INTO its4you_reports4you_summaries (reportsummaryid,summarytype,columnname) VALUES (?,?,?)";
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $params = array($genQueryId, $i, decode_html($selectedSummaries[$i]));
                             $params_tofile = array("@reportid", $i, decode_html($selectedSummaries[$i]));
                             $report_array['its4you_reports4you_summaries'][] = $params_tofile;
                             $iSummariessqlresult = $adb->pquery($iSmmariessql, $params);
                             $export_sql === true ? $adb->setDebug(false) : "";
                         }
                     }
                 }
                 if ($summaries_orderby != "" && $summaries_orderby_type != "") {
                     $d_selected_s_orderby_sql = "INSERT INTO  its4you_reports4you_summaries_orderby (reportid,columnindex,summaries_orderby,summaries_orderby_type) VALUES (?,?,?,?)";
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $params = array($genQueryId, 0, $summaries_orderby, $summaries_orderby_type);
                     $params_tofile = array("@reportid", 0, $summaries_orderby, $summaries_orderby_type);
                     $report_array['its4you_reports4you_summaries_orderby'] = $params_tofile;
                     $d_selected_s_orderby_result = $adb->pquery($d_selected_s_orderby_sql, $params);
                     $export_sql === true ? $adb->setDebug(false) : "";
                 }
                 if (!empty($lbl_array)) {
                     foreach ($lbl_array as $type => $type_array) {
                         foreach ($type_array as $column_str => $column_lbl) {
                             $iLabelssql = "INSERT INTO its4you_reports4you_labels (reportid,type,columnname,columnlabel) VALUES (?,?,?,?)";
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $params = array($genQueryId, $type, $column_str, $column_lbl);
                             $params_tofile = array("@reportid", $type, $column_str, $column_lbl);
                             $report_array['its4you_reports4you_labels'][] = $params_tofile;
                             $iLabelssqlresult = $adb->pquery($iLabelssql, $params);
                             $export_sql === true ? $adb->setDebug(false) : "";
                         }
                     }
                 }
                 if (!empty($selectedqfcolumns)) {
                     for ($i = 0; $i < count($selectedqfcolumns); $i++) {
                         if (!empty($selectedqfcolumns[$i])) {
                             $icolumnsql = "insert into its4you_reports4you_selectqfcolumn (QUERYID,COLUMNINDEX,COLUMNNAME) values (?,?,?)";
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $params = array($genQueryId, $i, decode_html($selectedqfcolumns[$i]));
                             $params_tofile = array("@reportid", $i, decode_html($selectedqfcolumns[$i]));
                             $report_array['its4you_reports4you_selectqfcolumn'][] = $params_tofile;
                             $icolumnsqlresult = $adb->pquery($icolumnsql, $params);
                             $export_sql === true ? $adb->setDebug(false) : "";
                         }
                     }
                 }
                 if ($shared_entities != "") {
                     if ($sharetype == "share") {
                         $selectedsharecolumn = explode("|", $shared_entities);
                         for ($i = 0; $i < count($selectedsharecolumn) - 1; $i++) {
                             $temp = split("::", $selectedsharecolumn[$i]);
                             $icolumnsql = "INSERT INTO its4you_reports4you_sharing (reports4youid,shareid,setype) VALUES (?,?,?)";
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $params = array($reportid, $temp[1], $temp[0]);
                             $params_tofile = array("@reportid", $temp[1], $temp[0]);
                             $report_array['its4you_reports4you_sharing'][] = $params_tofile;
                             $icolumnsqlresult = $adb->pquery($icolumnsql, $params);
                             $export_sql === true ? $adb->setDebug(false) : "";
                         }
                     }
                 }
                 if ($genQueryId != "") {
                     $ireportsql = "insert into its4you_reports4you (reports4youid,reports4youname,description,folderid,reporttype,columns_limit,summaries_limit) values (?,?,?,?,?,?,?)";
                     $ireportparams = array($genQueryId, $reportname, $reportdescription, $folderid, $reporttype, $limit, $summaries_limit);
                     $params_tofile = array("@reportid", $reportname, $reportdescription, $folderid, $reporttype, 0, $limit, $summaries_limit);
                     // ITS4YOU-END
                     $export_sql === true ? $adb->setDebug(true) : "";
                     $report_array['its4you_reports4you'] = $params_tofile;
                     $ireportresult = $adb->pquery($ireportsql, $ireportparams);
                     $export_sql === true ? $adb->setDebug(false) : "";
                     if ($ireportresult != false) {
                         if ($reporttype != "custom_report") {
                             //<<<<reportmodules>>>>>>>
                             $ireportmodulesql = "insert into its4you_reports4you_modules (REPORTMODULESID,PRIMARYMODULE,SECONDARYMODULES) values (?,?,?)";
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $params = array($genQueryId, $pmodule, $smodule);
                             $params_tofile = array("@reportid", $pmodule, $smodule);
                             $report_array['its4you_reports4you_modules'] = $params_tofile;
                             $ireportmoduleresult = $adb->pquery($ireportmodulesql, $params);
                             $export_sql === true ? $adb->setDebug(false) : "";
                             //<<<<reportmodules>>>>>>>
                             //<<<<step3 its4you_reports4you_sortcol>>>>>>>
                             if ($sort_by1 != "") {
                                 $sort_by1sql = "insert into its4you_reports4you_sortcol (SORTCOLID,REPORTID,COLUMNNAME,SORTORDER,timeline_columnstr,timeline_columnfreq) values (?,?,?,?,?,?)";
                                 $export_sql === true ? $adb->setDebug(true) : "";
                                 $params = array(1, $genQueryId, $sort_by1, $sort_order1, $TimeLineColumn_str1, $TimeLineColumn_frequency1);
                                 $params_tofile = array(1, "@reportid", $sort_by1, $sort_order1, "rows", $TimeLineColumn_str1, $TimeLineColumn_frequency1);
                                 $report_array['its4you_reports4you_sortcol'][] = $params_tofile;
                                 $sort_by1result = $adb->pquery($sort_by1sql, $params);
                                 $export_sql === true ? $adb->setDebug(false) : "";
                             }
                             if ($sort_by2 != "") {
                                 $sort_by2sql = "insert into its4you_reports4you_sortcol (SORTCOLID,REPORTID,COLUMNNAME,SORTORDER,timeline_type,timeline_columnstr,timeline_columnfreq) values (?,?,?,?,?,?,?)";
                                 $export_sql === true ? $adb->setDebug(true) : "";
                                 $params = array(2, $genQueryId, $sort_by2, $sort_order2, $timeline_type2, $TimeLineColumn_str2, $TimeLineColumn_frequency2);
                                 $params_tofile = array(2, "@reportid", $sort_by2, $sort_order2, $timeline_type2, $TimeLineColumn_str2, $TimeLineColumn_frequency2);
                                 $report_array['its4you_reports4you_sortcol'][] = $params_tofile;
                                 $sort_by2result = $adb->pquery($sort_by2sql, $params);
                                 $export_sql === true ? $adb->setDebug(false) : "";
                             }
                             if ($sort_by3 != "") {
                                 $sort_by3sql = "insert into its4you_reports4you_sortcol (SORTCOLID,REPORTID,COLUMNNAME,SORTORDER,timeline_type,timeline_columnstr,timeline_columnfreq) values (?,?,?,?,?,?,?)";
                                 $export_sql === true ? $adb->setDebug(true) : "";
                                 $params = array(3, $genQueryId, $sort_by3, $sort_order3, $timeline_type3, $TimeLineColumn_str3, $TimeLineColumn_frequency3);
                                 $params_tofile = array(3, "@reportid", $sort_by3, $sort_order3, $timeline_type3, $TimeLineColumn_str3, $TimeLineColumn_frequency3);
                                 $report_array['its4you_reports4you_sortcol'][] = $params_tofile;
                                 $sort_by3result = $adb->pquery($sort_by3sql, $params);
                                 $export_sql === true ? $adb->setDebug(false) : "";
                             }
                             if ($sort_by_column != "") {
                                 $sort_by_columnsql = "insert into its4you_reports4you_sortcol (SORTCOLID,REPORTID,COLUMNNAME,SORTORDER) values (?,?,?,?)";
                                 $export_sql === true ? $adb->setDebug(true) : "";
                                 $params = array(4, $genQueryId, $sort_by_column, $sort_order_column);
                                 $params_tofile = array(4, "@reportid", $sort_by_column, $sort_order_column, "", "", "");
                                 $report_array['its4you_reports4you_sortcol'][] = $params_tofile;
                                 $sort_by_columnresult = $adb->pquery($sort_by_columnsql, $params);
                                 $export_sql === true ? $adb->setDebug(false) : "";
                             }
                             //<<<<step3 its4you_reports4you_sortcol>>>>>>>
                             //<<<<step5 standarfilder>>>>>>>
                             $ireportmodulesql = "insert into its4you_reports4you_datefilter (DATEFILTERID,DATECOLUMNNAME,DATEFILTER,STARTDATE,ENDDATE) values (?,?,?,?,?)";
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $params = array($genQueryId, $stdDateFilterField, $stdDateFilter, $startdate, $enddate);
                             $params_tofile = array("@reportid", $stdDateFilterField, $stdDateFilter, $startdate, $enddate);
                             $report_array['its4you_reports4you_datefilter'] = $params_tofile;
                             $ireportmoduleresult = $adb->pquery($ireportmodulesql, $params);
                             $export_sql === true ? $adb->setDebug(false) : "";
                             //<<<<step5 standarfilder>>>>>>>
                             //<<<<step4 columnstototal>>>>>>>
                             for ($i = 0; $i < count($columnstototal); $i++) {
                                 $ireportsummarysql = "insert into its4you_reports4you_summary (REPORTSUMMARYID,SUMMARYTYPE,COLUMNNAME) values (?,?,?)";
                                 $export_sql === true ? $adb->setDebug(true) : "";
                                 $params = array($genQueryId, $i, $columnstototal[$i]);
                                 $params_tofile = array("@reportid", $i, $columnstototal[$i]);
                                 $report_array['its4you_reports4you_summary'][] = $params_tofile;
                                 $ireportsummaryresult = $adb->pquery($ireportsummarysql, $params);
                                 $export_sql === true ? $adb->setDebug(false) : "";
                             }
                             //<<<<step4 columnstototal>>>>>>>
                             //<<<<step5 advancedfilter>>>>>>>
                             // DOKONC SAVE TO FILE OLDO
                             $this->saveAdvancedFilters($advft_criteria, $advft_criteria_groups, $std_filter_columns, $export_sql);
                             foreach ($groupft_criteria as $column_index => $column_condition) {
                                 if (empty($column_condition)) {
                                     continue;
                                 }
                                 $adv_filter_column = $column_condition["columnname"];
                                 $adv_filter_comparator = $column_condition["comparator"];
                                 $adv_filter_value = $column_condition["value"];
                                 $adv_filter_column_condition = $column_condition["column_condition"];
                                 $adv_filter_groupid = $column_condition["groupid"];
                                 $column_info = explode(":", $adv_filter_column);
                                 $temp_val = explode(",", $adv_filter_value);
                                 if (($column_info[4] == 'D' || $column_info[4] == 'T' && $column_info[1] != 'time_start' && $column_info[1] != 'time_end' || $column_info[4] == 'DT') && ($column_info[4] != '' && $adv_filter_value != '')) {
                                     $val = array();
                                     for ($x = 0; $x < count($temp_val); $x++) {
                                         if (trim($temp_val[$x]) != '') {
                                             $date = new DateTimeField(trim($temp_val[$x]));
                                             if ($column_info[4] == 'D') {
                                                 $val[$x] = DateTimeField::convertToDBFormat(trim($temp_val[$x]));
                                             } elseif ($column_info[4] == 'DT') {
                                                 $val[$x] = $date->getDBInsertDateTimeValue();
                                             } else {
                                                 $val[$x] = $date->getDBInsertTimeValue();
                                             }
                                         }
                                     }
                                     $adv_filter_value = implode(",", $val);
                                 }
                                 $irelcriteriasql = "INSERT INTO its4you_reports4you_relcriteria_summaries(reportid,COLUMNINDEX,COLUMNNAME,COMPARATOR,VALUE,GROUPID,COLUMN_CONDITION) VALUES (?,?,?,?,?,?,?)";
                                 $export_sql === true ? $adb->setDebug(true) : "";
                                 $params = array($reportid, $column_index, $adv_filter_column, $adv_filter_comparator, $adv_filter_value, $adv_filter_groupid, $adv_filter_column_condition);
                                 $params_tofile = array("@reportid", $column_index, $adv_filter_column, $adv_filter_comparator, $adv_filter_value, $adv_filter_groupid, $adv_filter_column_condition);
                                 $report_array['its4you_reports4you_relcriteria_summaries'][] = $params_tofile;
                                 $irelcriteriaresult = $adb->pquery($irelcriteriasql, $params);
                                 $export_sql === true ? $adb->setDebug(false) : "";
                                 // Update the condition expression for the group to which the condition column belongs
                                 $groupConditionExpression = '';
                                 if (!empty($advft_criteria_groups[$adv_filter_groupid]["conditionexpression"])) {
                                     $groupConditionExpression = $advft_criteria_groups[$adv_filter_groupid]["conditionexpression"];
                                 }
                                 $groupConditionExpression = $groupConditionExpression . ' ' . $column_index . ' ' . $adv_filter_column_condition;
                                 $advft_criteria_groups[$adv_filter_groupid]["conditionexpression"] = $groupConditionExpression;
                             }
                             //<<<<step5 advancedfilter>>>>>>>
                         }
                         //<<<<step6 sharing >>>>>>>
                         $owner = vtlib_purify($_REQUEST["template_owner"]);
                         $sharingtype = vtlib_purify($_REQUEST["sharing"]);
                         if ($owner != "" && $owner != "") {
                             $limitsql = "insert into its4you_reports4you_settings (reportid,owner,sharingtype) VALUES (?,?,?)";
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $params = array($genQueryId, $owner, $sharingtype);
                             $params_tofile = array("@reportid", $owner, $sharingtype);
                             $report_array['its4you_reports4you_settings'] = $params_tofile;
                             $limitresult = $adb->pquery($limitsql, $params);
                             $export_sql === true ? $adb->setDebug(false) : "";
                         }
                         //<<<<step6 sharing >>>>>>>
                         //<<<<step7 scheduledReport>>>>>>>
                         if ($isReportScheduled == 'off' || $isReportScheduled == '0' || $isReportScheduled == '') {
                             $deleteScheduledReportSql = "DELETE FROM its4you_reports4you_scheduled_reports WHERE reportid=?";
                             $adb->pquery($deleteScheduledReportSql, array($reportid));
                         } else {
                             $checkScheduledResult = $adb->pquery('SELECT 1 FROM its4you_reports4you_scheduled_reports WHERE reportid=?', array($reportid));
                             if ($adb->num_rows($checkScheduledResult) > 0) {
                                 $scheduledReportSql = 'UPDATE its4you_reports4you_scheduled_reports SET recipients=?,schedule=?,format=? WHERE reportid=?';
                                 $export_sql === true ? $adb->setDebug(true) : "";
                                 $params = array($selectedRecipients, $scheduledInterval, $scheduledFormat, $reportid);
                                 $params_tofile = array("@reportid", $selectedRecipients, $scheduledInterval, $scheduledFormat, date("Y-m-d H:i:s"));
                                 $report_array['its4you_reports4you_scheduled_reports'] = $params_tofile;
                                 $adb->pquery($scheduledReportSql, $params);
                                 $export_sql === true ? $adb->setDebug(false) : "";
                             } else {
                                 $scheduleReportSql = 'INSERT INTO its4you_reports4you_scheduled_reports (reportid,recipients,schedule,format,next_trigger_time) VALUES (?,?,?,?,?)';
                                 $export_sql === true ? $adb->setDebug(true) : "";
                                 $params = array($reportid, $selectedRecipients, $scheduledInterval, $scheduledFormat, date("Y-m-d H:i:s"));
                                 $params_tofile = array("@reportid", $selectedRecipients, $scheduledInterval, $scheduledFormat, date("Y-m-d H:i:s"));
                                 $report_array['its4you_reports4you_scheduled_reports'] = $params_tofile;
                                 $adb->pquery($scheduleReportSql, $params);
                                 $export_sql === true ? $adb->setDebug(false) : "";
                             }
                         }
                         if ($reporttype != "custom_report") {
                             //<<<<step12 Report Charts >>>>>>>
                             $deleteChartsSql = "DELETE FROM its4you_reports4you_charts WHERE reports4youid=?";
                             $adb->pquery($deleteChartsSql, array($reportid));
                             /*
                              if ($chartType != "" && $chartType != "none") {
                              $ChartsSql = 'INSERT INTO its4you_reports4you_charts (reports4youid,charttype,dataseries,charttitle) VALUES (?,?,?,?)';
                              $export_sql === true ? $adb->setDebug(true) : "";
                              $adb->pquery($ChartsSql, array($reportid, $chartType, $data_series, $charttitle));
                              $export_sql === true ? $adb->setDebug(false) : "";
                              }
                             */
                             if (!empty($charts)) {
                                 foreach ($charts as $chi => $ch_params) {
                                     $ChartsSql = 'INSERT INTO its4you_reports4you_charts (reports4youid,charttype,dataseries,charttitle,chart_seq,x_group) VALUES (?,?,?,?,?,?)';
                                     $ch_params = array_merge(array($reportid), $ch_params);
                                     $ch_params_tofile = array_merge(array("@reportid"), $ch_params);
                                     $export_sql === true ? $adb->setDebug(true) : "";
                                     $params = array($ch_params);
                                     $report_array['its4you_reports4you_charts'][] = array($ch_params_tofile);
                                     $adb->pquery($ChartsSql, $params);
                                     $export_sql === true ? $adb->setDebug(false) : "";
                                 }
                             }
                             //<<<<step12 Report Charts >>>>>>>
                         }
                         //<<<<step13 Custom Report SQL >>>>>>>
                         if ($reporttype == "custom_report") {
                             $CustomSqlQry = 'INSERT INTO its4you_reports4you_customsql (reports4youid,custom_sql) VALUES (?,?)';
                             $params = array($reportid, $customSql);
                             $params_tofile = array("@reportid", $customSql);
                             $report_array['its4you_reports4you_customsql'] = $params_tofile;
                             $export_sql === true ? $adb->setDebug(true) : "";
                             $adb->pquery($CustomSqlQry, array($params));
                             $export_sql === true ? $adb->setDebug(false) : "";
                         }
                         //<<<<step13 Custom Report SQL >>>>>>>
                     } else {
                         $errormessage = "<font color='red'><B>Error Message<ul>\n                                                            <li><font color='red'>Error while inserting the record</font>\n                                                            </ul></B></font> <br>";
                         echo $errormessage;
                         die;
                     }
                 }
             } else {
                 $errormessage = "<font color='red'><B>Error Message<ul>\n                                            <li><font color='red'>Error while inserting the record (QUERYID)</font>\n                                            </ul></B></font> <br>";
                 echo $errormessage;
                 die;
             }
         }
         if ($export_sql === true && $isDuplicate != "true") {
             ITS4YouReports::sshow("EDIT");
         } elseif ($export_sql === true) {
             ITS4YouReports::sshow("DUPLICATE");
         }
     }
     if ($export_to_file === true) {
         $report_array_json = Zend_JSON::encode($report_array);
         //			ITS4YouReports::sshow($report_array['its4you_reports4you_charts']);
         //			ITS4YouReports::sshow($report_array_json);
         $fileReportContent = "<?php " . '$report_array_json = ' . "'" . $report_array_json . "'; ";
         $file_path = "test/ITS4YouReports/ITS4YouReports_" . $this->getId() . ".php";
         $exportOfReport = fopen($file_path, 'w');
         fwrite($exportOfReport, $fileReportContent);
         fclose($exportOfReport);
         echo "DONE {$file_path}";
         exit;
     }
     if ($export_sql === true) {
         exit;
     }
     return true;
     // ini_set("display_errors", 1);error_reporting(63);
 }
Esempio n. 2
0
//$debug_save = true;
/*global $current_user;if($current_user->id=="1"){
	$debug_save = true;
}*/
$export_sql = false;
//$export_sql = true;
/*global $current_user;if($current_user->id=="1"){
	$export_sql = true;
}*/
if ($debug_save) {
    echo "<pre>";
    print_r($_REQUEST);
    echo "</pre>";
    $adb->setDebug(true);
}
$r4u_sesstion_name = ITS4YouReports::getITS4YouReportStoreName();
$r4u_sesstion_unset = ITS4YouReports::unsetITS4YouReportsSerialize($r4u_sesstion_name);
if ($debug_save) {
    echo "<pre>UNSET Session: ";
    print_r($r4u_sesstion_unset);
    echo "</pre>";
}
$ITS4YouReports = ITS4YouReports::getStoredITS4YouReport();
global $default_charset;
//<<<<<<<report>>>>>>>>>
$reportname = vtlib_purify($_REQUEST["reportname"]);
$reportname = html_entity_decode($reportname, ENT_QUOTES, $default_charset);
$reportdescription = vtlib_purify($_REQUEST["reportdesc"]);
$reportdescription = html_entity_decode($reportdescription, ENT_QUOTES, $default_charset);
$reporttype = vtlib_purify($_REQUEST["reportType"]);
$folderid = vtlib_purify($_REQUEST["reportfolder"]);
Esempio n. 3
0
 /** Function to get vtiger_fields for the given module and block
  *  This function gets the vtiger_fields for the given module
  *  It accepts the module and the block as arguments and 
  *  returns the array column lists
  *  Array module_columnlist[ vtiger_fieldtablename:fieldcolname:module_fieldlabel1:fieldname:fieldtypeofdata]=fieldlabel
  */
 function getColumnsListbyBlock($module, $block, $relfieldid = "")
 {
     $r4u_columnlist_name = ITS4YouReports::getITS4YouReportStoreName($module, $block);
     $r4u_rel_fields_name = ITS4YouReports::getITS4YouReportStoreName("adv_rel_fields");
     $r4u_sel_fields_name = ITS4YouReports::getITS4YouReportStoreName("adv_sel_fields");
     unset($_SESSION[$r4u_columnlist_name]);
     unset($_SESSION[$r4u_rel_fields_name]);
     unset($_SESSION[$r4u_sel_fields_name]);
     //return false;
     if ($r4u_columnlist_name != "" && isset($_SESSION[$r4u_columnlist_name]) && !empty($_SESSION[$r4u_columnlist_name])) {
         $module_columnlist = unserialize($_SESSION[$r4u_columnlist_name]);
         $this->adv_rel_fields = unserialize($_SESSION[$r4u_rel_fields_name]);
         $this->adv_sel_fields = unserialize($_SESSION[$r4u_sel_fields_name]);
         return $module_columnlist;
     } else {
         unset($_SESSION[$r4u_columnlist_name]);
         //unset($_SESSION[$r4u_rel_fields_name]);
         //unset($_SESSION[$r4u_sel_fields_name]);
         $adb = PearDatabase::getInstance();
         if (is_string($block)) {
             $block = explode(",", $block);
         }
         $tabid = getTabid($module);
         if ($module == 'Calendar') {
             $tabid = array('9', '16');
         }
         $params = array($tabid, $block);
         $user_privileges_path = 'user_privileges/user_privileges_' . $this->current_user->id . '.php';
         if (file_exists($user_privileges_path)) {
             require $user_privileges_path;
         }
         //Security Check
         if ($is_admin == true || $profileGlobalPermission[1] == 0 || $profileGlobalPermission[2] == 0) {
             $sql = "select * from vtiger_field where vtiger_field.tabid in (" . generateQuestionMarks($tabid) . ") and vtiger_field.block in (" . generateQuestionMarks($block) . ") and vtiger_field.displaytype in (1,2,3) and vtiger_field.presence in (0,2) ";
             //fix for Ticket #4016
             if ($module == "Calendar") {
                 $sql .= " group by vtiger_field.fieldlabel order by sequence";
             } else {
                 $sql .= " order by sequence";
             }
         } else {
             $profileList = getCurrentUserProfileList();
             $sql = "select * from vtiger_field inner join vtiger_profile2field on vtiger_profile2field.fieldid=vtiger_field.fieldid inner join vtiger_def_org_field on vtiger_def_org_field.fieldid=vtiger_field.fieldid where vtiger_field.tabid in (" . generateQuestionMarks($tabid) . ")  and vtiger_field.block in (" . generateQuestionMarks($block) . ") and vtiger_field.displaytype in (1,2,3) and vtiger_profile2field.visible=0 and vtiger_def_org_field.visible=0 and vtiger_field.presence in (0,2)";
             if (count($profileList) > 0) {
                 $sql .= " and vtiger_profile2field.profileid in (" . generateQuestionMarks($profileList) . ")";
                 array_push($params, $profileList);
             }
             //fix for Ticket #4016
             if ($module == "Calendar") {
                 $sql .= " group by vtiger_field.fieldid,vtiger_field.fieldlabel order by sequence";
             } else {
                 $sql .= " group by vtiger_field.fieldid order by sequence";
             }
         }
         //$adb->setDebug(true);
         $f_result = $adb->pquery($sql, $params);
         //$adb->setDebug(false);
         $noofrows = $adb->num_rows($f_result);
         $lead_converted_added = false;
         for ($i = 0; $i < $noofrows; $i++) {
             //$_SESSION_r4u_rel_fields = unserialize($_SESSION[$r4u_rel_fields_name]);
             //$_SESSION_r4u_sel_fields = unserialize($_SESSION[$r4u_sel_fields_name]);
             $fieldid = $adb->query_result($f_result, $i, "fieldid");
             // ITS4YOU-UP SlOl 1. 10. 2013 10:46:35
             $fieldtablename = $adb->query_result($f_result, $i, "tablename");
             if ($this->primarymodule != $module) {
                 $fieldtablename = $fieldtablename;
                 if ($relfieldid != "") {
                     $fieldtablename .= "_{$relfieldid}";
                 }
             }
             $fieldcolname = $adb->query_result($f_result, $i, "columnname");
             $fieldname = $adb->query_result($f_result, $i, "fieldname");
             $fieldtype = $adb->query_result($f_result, $i, "typeofdata");
             $uitype = $adb->query_result($f_result, $i, "uitype");
             $fieldtype = explode("~", $fieldtype);
             // Fix to get table alias for orderby and groupby sql
             /* if ($relfieldid == "" && in_array($uitype, array("10",))) {
                $relfieldid = $fieldid;
                } */
             $fieldtypeofdata = $fieldtype[0];
             //Here we Changing the displaytype of the field. So that its criteria will be displayed correctly in Reports Advance Filter.
             $fieldtypeofdata = ChangeTypeOfData_Filter($fieldtablename, $fieldcolname, $fieldtypeofdata);
             if ($uitype == 68 || $uitype == 59) {
                 $fieldtypeofdata = 'V';
             }
             $fieldlabel = $adb->query_result($f_result, $i, "fieldlabel");
             $fieldlabel1 = $fieldlabel;
             if ($relfieldid != "") {
                 $relfieldid_str = $relfieldid;
             }
             // this is defining module id for uitype 10
             if ($relfieldid != "" && $this->primarymodule != $module) {
                 $rel_field_row = $adb->fetchByAssoc($adb->pquery("SELECT uitype FROM vtiger_field WHERE fieldid = ? ", array($relfieldid)), 0);
                 $rel_field_uitype = $rel_field_row["uitype"];
                 if ($rel_field_uitype == 10) {
                     $relfieldid_str = getTabid($module) . ":" . $relfieldid;
                 }
             }
             $module_lbl = vtranslate($module, $module);
             $optionvalue = $fieldtablename . ":" . $fieldcolname . ":" . $module . "_" . $fieldlabel1 . ":" . $fieldname . ":" . $fieldtypeofdata . ($relfieldid != "" ? ":" . $relfieldid_str : "");
             // $optionvalue = $fieldtablename.":".$fieldcolname.":".$module."_".$fieldlabel1.":".$fieldname.":".$fieldtypeofdata;
             $adv_rel_field_val = '$' . $module . '#' . $fieldname . '$' . "::" . $module_lbl . " " . $fieldlabel;
             $this->adv_rel_fields[$fieldtypeofdata][] = $adv_rel_field_val;
             // ITS4YOU-CR SlOl 26. 3. 2014 10:57:41
             if (in_array($uitype, ITS4YouReports::$s_uitypes) && !array_key_exists($optionvalue, $this->adv_sel_fields) && !in_array($module, array("Users"))) {
                 $this->adv_sel_fields[$optionvalue] = true;
             }
             // ITS4YOU-END 26. 3. 2014 10:57:44
             $translate_module = $module;
             //added to escape attachments fields in Reports as we have multiple attachments
             if ($module != 'HelpDesk' || $fieldname != 'filename') {
                 $module_columnlist[$optionvalue] = vtranslate($fieldlabel, $translate_module);
             }
             // ITS4YOU-CR SlOl - IS CONVERTED FIELD FOR LEADS
             if ($module == "Leads" && $block == 13 && $i == $noofrows - 1 && $lead_converted_added != true) {
                 $sc_col_str = "vtiger_leaddetails:converted:" . $module . "_Converted:converted:C";
                 $this->adv_sel_fields[$sc_col_str] = true;
                 $lead_converted_added = true;
                 $module_columnlist[$sc_col_str] = vtranslate("Converted", $module);
             }
             // CONVERTED END
             unset($_SESSION[$r4u_rel_fields_name]);
             $_SESSION[$r4u_rel_fields_name] = serialize($this->adv_rel_fields);
             unset($_SESSION[$r4u_sel_fields_name]);
             $_SESSION[$r4u_sel_fields_name] = serialize($this->adv_sel_fields);
         }
         $blockname = getBlockName($block);
         if ($blockname == 'LBL_RELATED_PRODUCTS' && in_array($module, self::$inventory_modules)) {
             if ($relfieldid != "") {
                 $rel_field_row = $adb->fetchByAssoc($adb->pquery("SELECT uitype FROM vtiger_field WHERE fieldid = ? ", array($relfieldid)), 0);
                 $rel_field_uitype = $rel_field_row["uitype"];
                 if ($rel_field_uitype == 10) {
                     $relfieldid_str = ":" . getTabid($module) . ":" . $relfieldid;
                 } else {
                     $relfieldid_str = ":" . $relfieldid;
                 }
             }
             $fieldtablename = 'vtiger_inventoryproductrel';
             if ($relfieldid != "") {
                 $fieldtablename .= "_{$relfieldid}";
             }
             $fields = array('prodname' => $module_lbl . " " . vtranslate('LBL_PRODUCT_SERVICE_NAME', $this->currentModule), 'ps_productcategory' => $module_lbl . " " . vtranslate('LBL_ITEM_CATEGORY', $this->currentModule), 'ps_productno' => $module_lbl . " " . vtranslate('LBL_ITEM_NO', $this->currentModule), 'comment' => $module_lbl . " " . vtranslate('Comments', $module), 'quantity' => $module_lbl . " " . vtranslate('Quantity', $module), 'listprice' => $module_lbl . " " . vtranslate('List Price', $module), 'ps_producttotal' => $module_lbl . " " . vtranslate('LBL_PRODUCT_TOTAL', $this->currentModule), 'discount' => $module_lbl . " " . vtranslate('Discount', $module), 'ps_productstotalafterdiscount' => $module_lbl . " " . vtranslate('LBL_PRODUCTTOTALAFTERDISCOUNT', $this->currentModule), 'ps_productvatsum' => $module_lbl . " " . vtranslate('LBL_PRODUCT_VAT_SUM', $this->currentModule), 'ps_producttotalsum' => $module_lbl . " " . vtranslate('LBL_PRODUCT_TOTAL_VAT', $this->currentModule));
             $fields_datatype = array('prodname' => 'V', 'ps_productcategory' => 'V', 'ps_productno' => 'V', 'comment' => 'V', 'prodname' => 'V', 'quantity' => 'I', 'listprice' => 'I', 'ps_producttotal' => 'I', 'discount' => 'I', 'ps_productstotalafterdiscount' => 'I', 'ps_productvatsum' => 'I', 'ps_producttotalsum' => 'I');
             $module_lbl = vtranslate($module, $module);
             foreach ($fields as $fieldcolname => $label) {
                 $fieldtypeofdata = $fields_datatype[$fieldcolname];
                 $optionvalue = $fieldtablename . ":" . $fieldcolname . ":" . $module . "_" . $label . ":" . $fieldcolname . ":" . $fieldtypeofdata . $relfieldid_str;
                 $module_columnlist[$optionvalue] = $label;
                 // $module_lbl." ".
             }
         }
         $_SESSION[$r4u_columnlist_name] = serialize($module_columnlist);
         return $module_columnlist;
     }
 }