Exemplo n.º 1
0
 function execute($format, $parameter_values = array())
 {
     global $sugar_config, $current_user;
     global $zuckerreports_config;
     $result = FALSE;
     $rt = new ReportTemplate();
     $base = substr($this->filename, 0, strrpos($this->filename, "."));
     $date = date("ymd_His");
     $this->report_result_type = "FORWARD";
     if ($this->extension == "stw" || $this->extension == "odt") {
         $this->report_result_name = $date . "_" . $base . ".zro";
     } else {
         if ($this->extension == "doc") {
             $this->report_result_name = $date . "_" . $base . ".zrw";
         }
     }
     $this->report_result_name = strtolower(join("_", explode(" ", $this->report_result_name)));
     $this->report_result = $this->archive_dir . "/" . $this->report_result_name;
     $tempdir = $this->get_temp_dir() . create_guid();
     $cmdfile = $tempdir . "/cmd.xml";
     mkdir($tempdir, 0700);
     if ($rt->isWindows()) {
         $abs_report_result = realpath(str_replace("/", "\\", $this->archive_dir)) . "\\" . $this->report_result_name;
         $abs_tempdir = realpath(str_replace("/", "\\", $tempdir));
     } else {
         $abs_report_result = realpath($this->archive_dir) . "/" . $this->report_result_name;
         $abs_tempdir = realpath($tempdir);
     }
     if ($this->extension == "stw" || $this->extension == "odt") {
         $data_format = "CSV";
     } else {
         if ($this->extension == "doc") {
             $data_format = "SIMPLEHTML";
         }
     }
     $data_success = false;
     if ($this->querytemplate->object_name == "ListingTemplate") {
         $this->querytemplate->archive_dir = $tempdir;
         $this->querytemplate->include_header = TRUE;
         $this->querytemplate->col_delim = ",";
         $this->querytemplate->row_delim = "\n";
         $data_success = $this->querytemplate->execute($data_format, $parameter_values);
     } else {
         if ($this->querytemplate->object_name == "QueryTemplate") {
             $this->querytemplate->archive_dir = $tempdir;
             $this->querytemplate->include_header = TRUE;
             $this->querytemplate->col_delim = ",";
             $this->querytemplate->row_delim = "\n";
             $data_success = $this->querytemplate->execute($data_format, $parameter_values);
         }
     }
     if ($data_success) {
         $datafile = $this->querytemplate->report_result_name;
         copy($this->get_resources_dir() . $this->filename, $tempdir . "/" . $this->filename);
         $f = fopen($cmdfile, "w");
         fwrite($f, "<ZuckerReportsCommand><ZuckerReports>\n");
         if ($this->extension == "stw") {
             fwrite($f, " <Application>StarWriter</Application>\n");
         } else {
             if ($this->extension == "odt") {
                 fwrite($f, " <Application>OpenOffice</Application>\n");
             } else {
                 if ($this->extension == "doc") {
                     fwrite($f, " <Application>Word</Application>\n");
                 }
             }
         }
         fwrite($f, " <Destination>" . $format . "</Destination>\n");
         if ($format == "Save") {
             fwrite($f, " <SavePath>" . $this->save_path . "</SavePath>\n");
         }
         fwrite($f, " <DataPath>" . $datafile . "</DataPath>\n");
         fwrite($f, " <TemplatePath>" . $this->filename . "</TemplatePath>\n");
         fwrite($f, " <Sugar>\n");
         fwrite($f, "  <SiteUrl>" . $sugar_config['site_url'] . "/</SiteUrl>\n");
         fwrite($f, " </Sugar>\n");
         fwrite($f, " <Parameter name=\"SUGAR_USER_ID\" value=\"" . $current_user->id . "\"/>\n");
         fwrite($f, " <Parameter name=\"SUGAR_USER_NAME\" value=\"" . $current_user->user_name . "\"/>\n");
         fwrite($f, " <Parameter name=\"SUGAR_SESSION_ID\" value=\"" . $_REQUEST['PHPSESSID'] . "\"/>\n");
         foreach ($parameter_values as $name => $value) {
             fwrite($f, " <Parameter name=\"" . $name . "\" value=\"" . $value . "\"/>\n");
         }
         fwrite($f, "</ZuckerReports></ZuckerReportsCommand>\n");
         fclose($f);
         $zip_pattern = $zuckerreports_config["zip_cmdline"];
         if (empty($zip_pattern) || $zip_pattern == "java") {
             $success = $rt->execute_java("sun.tools.jar.Main cfM " . $this->report_result . " -C " . $tempdir . " .");
         } else {
             if ($zip_pattern == "zip") {
                 if ($rt->isWindows()) {
                     $exec_path = realpath("modules\\ZuckerReports\\resources") . "\\zip.exe";
                 } else {
                     $exec_path = "zip";
                 }
                 $cmdline = "cd \"" . $abs_tempdir . "\" && \"" . $exec_path . "\" \"" . $abs_report_result . "\" *";
                 $success = $rt->execute_cmd($cmdline);
             } else {
                 $cmdline = $zip_pattern;
                 $cmdline = str_replace("%DIR%", $abs_tempdir, $cmdline);
                 $cmdline = str_replace("%FILE%", $abs_report_result, $cmdline);
                 $success = $rt->execute_cmd($cmdline);
             }
         }
         if ($success) {
             $result = TRUE;
         } else {
             $this->report_output = $rt->report_output;
             $result = FALSE;
         }
     } else {
         $this->report_output = $this->querytemplate->report_output;
         $result = FALSE;
     }
     $rt->rec_delete($tempdir);
     return $result;
 }