function getReportOutputAsCsv($reportClass, $inputParams)
 {
     $config = CRM_Core_Config::singleton();
     $config->keyDisable = TRUE;
     $controller = new CRM_Core_Controller_Simple($reportClass, ts('some title'));
     $tmpReportVal = explode('_', $reportClass);
     $reportName = array_pop($tmpReportVal);
     $reportObj =& $controller->_pages[$reportName];
     $tmpGlobals = array();
     $tmpGlobals['_REQUEST']['force'] = 1;
     $tmpGlobals['_GET'][$config->userFrameworkURLVar] = 'civicrm/placeholder';
     $tmpGlobals['_SERVER']['QUERY_STRING'] = '';
     if (!empty($inputParams['fields'])) {
         $fields = implode(',', $inputParams['fields']);
         $tmpGlobals['_GET']['fld'] = $fields;
         $tmpGlobals['_GET']['ufld'] = 1;
     }
     if (!empty($inputParams['filters'])) {
         foreach ($inputParams['filters'] as $key => $val) {
             $tmpGlobals['_GET'][$key] = $val;
         }
     }
     if (!empty($inputParams['group_bys'])) {
         $groupByFields = implode(' ', $inputParams['group_bys']);
         $tmpGlobals['_GET']['gby'] = $groupByFields;
     }
     CRM_Utils_GlobalStack::singleton()->push($tmpGlobals);
     try {
         $reportObj->storeResultSet();
         $reportObj->buildForm();
         $rows = $reportObj->getResultSet();
         $tmpFile = $this->createTempDir() . CRM_Utils_File::makeFileName('CiviReport.csv');
         $csvContent = CRM_Report_Utils_Report::makeCsv($reportObj, $rows);
         file_put_contents($tmpFile, $csvContent);
     } catch (Exception $e) {
         // print_r($e->getCause()->getUserInfo());
         CRM_Utils_GlobalStack::singleton()->pop();
         throw $e;
     }
     CRM_Utils_GlobalStack::singleton()->pop();
     return $tmpFile;
 }
Пример #2
0
 /**
  * End post processing.
  *
  * @param array|null $rows
  */
 public function endPostProcess(&$rows = NULL)
 {
     if ($this->_storeResultSet) {
         $this->_resultSet = $rows;
     }
     if ($this->_outputMode == 'print' || $this->_outputMode == 'pdf' || $this->_sendmail) {
         $content = $this->compileContent();
         $url = CRM_Utils_System::url("civicrm/report/instance/{$this->_id}", "reset=1", TRUE);
         if ($this->_sendmail) {
             $config = CRM_Core_Config::singleton();
             $attachments = array();
             if ($this->_outputMode == 'csv') {
                 $content = $this->_formValues['report_header'] . '<p>' . ts('Report URL') . ": {$url}</p>" . '<p>' . ts('The report is attached as a CSV file.') . '</p>' . $this->_formValues['report_footer'];
                 $csvFullFilename = $config->templateCompileDir . CRM_Utils_File::makeFileName('CiviReport.csv');
                 $csvContent = CRM_Report_Utils_Report::makeCsv($this, $rows);
                 file_put_contents($csvFullFilename, $csvContent);
                 $attachments[] = array('fullPath' => $csvFullFilename, 'mime_type' => 'text/csv', 'cleanName' => 'CiviReport.csv');
             }
             if ($this->_outputMode == 'pdf') {
                 // generate PDF content
                 $pdfFullFilename = $config->templateCompileDir . CRM_Utils_File::makeFileName('CiviReport.pdf');
                 file_put_contents($pdfFullFilename, CRM_Utils_PDF_Utils::html2pdf($content, "CiviReport.pdf", TRUE, array('orientation' => 'landscape')));
                 // generate Email Content
                 $content = $this->_formValues['report_header'] . '<p>' . ts('Report URL') . ": {$url}</p>" . '<p>' . ts('The report is attached as a PDF file.') . '</p>' . $this->_formValues['report_footer'];
                 $attachments[] = array('fullPath' => $pdfFullFilename, 'mime_type' => 'application/pdf', 'cleanName' => 'CiviReport.pdf');
             }
             if (CRM_Report_Utils_Report::mailReport($content, $this->_id, $this->_outputMode, $attachments)) {
                 CRM_Core_Session::setStatus(ts("Report mail has been sent."), ts('Sent'), 'success');
             } else {
                 CRM_Core_Session::setStatus(ts("Report mail could not be sent."), ts('Mail Error'), 'error');
             }
             return TRUE;
         } elseif ($this->_outputMode == 'print') {
             echo $content;
         } else {
             if ($chartType = CRM_Utils_Array::value('charts', $this->_params)) {
                 $config = CRM_Core_Config::singleton();
                 //get chart image name
                 $chartImg = $this->_chartId . '.png';
                 //get image url path
                 $uploadUrl = str_replace('/persist/contribute/', '/persist/', $config->imageUploadURL) . 'openFlashChart/';
                 $uploadUrl .= $chartImg;
                 //get image doc path to overwrite
                 $uploadImg = str_replace('/persist/contribute/', '/persist/', $config->imageUploadDir) . 'openFlashChart/' . $chartImg;
                 //Load the image
                 $chart = imagecreatefrompng($uploadUrl);
                 //convert it into formatted png
                 CRM_Utils_System::setHttpHeader('Content-type', 'image/png');
                 //overwrite with same image
                 imagepng($chart, $uploadImg);
                 //delete the object
                 imagedestroy($chart);
             }
             CRM_Utils_PDF_Utils::html2pdf($content, "CiviReport.pdf", FALSE, array('orientation' => 'landscape'));
         }
         CRM_Utils_System::civiExit();
     } elseif ($this->_outputMode == 'csv') {
         CRM_Report_Utils_Report::export2csv($this, $rows);
     } elseif ($this->_outputMode == 'group') {
         $group = $this->_params['groups'];
         $this->add2group($group);
     }
 }