コード例 #1
0
ファイル: csvexport.php プロジェクト: jfquestiaux/fabrik
 /**
  * Write the current batch section of the CSV file
  *
  * @param   int  $total       Total # of records
  * @param   bool $canDownload Can we also download the file (at end of export)
  *
  * @return  null
  */
 public function writeFile($total, $canDownload = false)
 {
     $params = $this->model->getParams();
     $input = $this->app->input;
     // F3 turn off error reporting as this is an ajax call
     error_reporting(0);
     jimport('joomla.filesystem.file');
     $start = $input->getInt('start', 0);
     $filePath = $this->getFilePath();
     $str = '';
     if (JFile::exists($filePath)) {
         if ($start === 0) {
             JFile::delete($filePath);
         } else {
             $str = file_get_contents($filePath);
         }
     } else {
         // Fabrik3 odd cant pass 2nd param by reference if we try to write '' so assign it to $tmp first
         $tmp = '';
         $ok = JFile::write($filePath, $tmp);
         if (!$ok) {
             $this->reportWriteError($filePath);
             exit;
         }
         // with UTF8 Excel needs BOM
         $str = $input->get('excel') == 1 && $this->getEncoding() == 'UTF-8' ? "" : '';
     }
     $table = $this->model->getTable();
     $this->model->render();
     $this->removePkVal();
     $this->outPutFormat = $input->get('excel') == 1 ? 'excel' : 'csv';
     $config = JComponentHelper::getParams('com_fabrik');
     $this->delimiter = $this->outPutFormat == 'excel' ? COM_FABRIK_EXCEL_CSV_DELIMITER : COM_FABRIK_CSV_DELIMITER;
     $this->delimiter = $config->get('csv_delimiter', $this->delimiter);
     $local_delimiter = $this->model->getParams()->get('csv_local_delimiter');
     if ($local_delimiter != '') {
         $this->delimiter = $local_delimiter;
     }
     if ($this->delimiter === '\\t') {
         $this->delimiter = "\t";
     }
     $end_of_line = $this->model->getParams()->get('csv_end_of_line');
     if ($end_of_line == 'r') {
         $end_of_line = "\r";
     } else {
         $end_of_line = "\n";
     }
     if ($start === 0) {
         $headings = $this->getHeadings();
         if (empty($headings)) {
             $url = $input->server->get('HTTP_REFERER', '');
             $this->app->enqueueMessage(FText::_('No data to export'));
             $this->app->redirect($url);
             return;
         }
         $str .= implode($headings, $this->delimiter) . $end_of_line;
     }
     $incRaw = $input->get('incraw', true);
     $incData = $input->get('inctabledata', true);
     $data = $this->model->getData();
     $exportFormat = $this->model->getParams()->get('csvfullname');
     $shortKey = FabrikString::shortColName($table->db_primary_key);
     $a = array();
     foreach ($data as $group) {
         foreach ($group as $row) {
             $a = ArrayHelper::fromObject($row);
             if ($exportFormat == 1) {
                 unset($a[$shortKey]);
             }
             if (!$incRaw) {
                 foreach ($a as $key => $val) {
                     if (substr($key, JString::strlen($key) - 4, JString::strlen($key)) == '_raw') {
                         unset($a[$key]);
                     }
                 }
             }
             if (!$incData) {
                 foreach ($a as $key => $val) {
                     if (substr($key, JString::strlen($key) - 4, JString::strlen($key)) != '_raw') {
                         unset($a[$key]);
                     }
                 }
             }
             if ($incData && $incRaw) {
                 foreach ($a as $key => $val) {
                     // Remove Un-needed repeat join element values.
                     if (array_key_exists($key . '___params', $a)) {
                         unset($a[$key . '___params']);
                     }
                     if (array_key_exists($key . '_id', $a)) {
                         unset($a[$key . '_id']);
                     }
                 }
             }
             if ($input->get('inccalcs') == 1) {
                 array_unshift($a, ' ');
             }
             $this->carriageReturnFix($a);
             if ($params->get('csv_format_json', '1') === '1') {
                 array_walk($a, array($this, 'implodeJSON'), $end_of_line);
             }
             $str .= implode($this->delimiter, array_map(array($this, 'quote'), array_values($a)));
             $str .= $end_of_line;
         }
     }
     $res = new stdClass();
     $res->total = $total;
     $res->count = $start + $this->getStep();
     $res->file = basename($filePath);
     $res->limitStart = $start;
     $res->limitLength = $this->getStep();
     if ($res->count >= $res->total) {
         $this->addCalculations($a, $str);
     }
     error_reporting(0);
     $ok = JFile::write($filePath, $str);
     if (!$ok) {
         $this->reportWriteError($filePath);
         exit;
     } else {
         if (!$canDownload) {
             echo json_encode($res);
         }
     }
 }