public static function run($debug = false, $mailOut = true) { try { self::$_debug = $debug; if ($debug) { echo '<pre>'; } $objPHPExcel = self::_getOutput(); if (!$objPHPExcel instanceof PHPExcel) { throw new Exception('System Error: can NOT generate CSV without PHPExcel object!'); } // Set document properties $filePath = self::$_rootDir . '/' . md5(new UDate()) . '.csv'; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV')->setDelimiter(',')->setEnclosure('"')->setLineEnding("\r\n")->setSheetIndex(0); ob_start(); $objWriter->save('php://output'); $excelOutput = ob_get_clean(); $class = get_called_class(); $asset = Asset::registerAsset($class::_getAttachedFileName(), $excelOutput, Asset::TYPE_TMP); if ($mailOut === true) { self::_mailOut($asset); } return $asset; } catch (Exception $ex) { echo $ex->getMessage(); die('ERROR!'); } }
/** * Add model rows to file. Can be batched * @param array $data Data submitted by export form * @param array $modelId Model Id when multiple models are passed * @param string $tempFilename The temporary filename while the file is being written * @param array $filter Filter (limit) to use */ public function addRows($data, $modelId, $tempFilename, $filter) { $name = $this->getName(); if (!(isset($data[$name]) && isset($data[$name]['format']) && in_array('formatAnswer', $data[$name]['format']))) { $this->modelFilterAttributes = array('formatFunction', 'dateFormat', 'storageFormat', 'itemDisplay'); } parent::addRows($data, $modelId, $tempFilename, $filter); }
/** * Preprocess the model to add specific options */ protected function preprocessModel() { parent::preprocessModel(); $labeledCols = $this->getLabeledColumns(); foreach ($labeledCols as $columnName) { $options = array(); $type = $this->model->get($columnName, 'type'); switch ($type) { case \MUtil_Model::TYPE_DATE: $options['dateFormat'] = 'yyyy-MM-dd'; break; case \MUtil_Model::TYPE_DATETIME: $options['dateFormat'] = 'dd-MM-yyyy HH:mm:ss'; break; case \MUtil_Model::TYPE_TIME: $options['dateFormat'] = 'HH:mm:ss'; break; case \MUtil_Model::TYPE_NUMERIC: break; //When no type set... assume string //When no type set... assume string case \MUtil_Model::TYPE_STRING: default: $type = \MUtil_Model::TYPE_STRING; $options['formatFunction'] = 'formatString'; break; } $options['type'] = $type; $this->model->set($columnName, $options); } }