From fa9011fcb2d255a3d7de30aab90c7cddc296bea5 Mon Sep 17 00:00:00 2001 From: wangchunsheng Date: Mon, 2 May 2011 09:03:20 +0000 Subject: [PATCH] * rewrite the export feature of csv, html and xml. --- module/file/config.php | 5 ++ module/file/control.php | 104 ++++++++++---------------- module/file/view/export2csv.html.php | 18 +++++ module/file/view/export2html.html.php | 43 +++++++++++ module/file/view/export2xml.html.php | 36 +++++++++ 5 files changed, 142 insertions(+), 64 deletions(-) create mode 100644 module/file/config.php create mode 100644 module/file/view/export2csv.html.php create mode 100644 module/file/view/export2html.html.php create mode 100644 module/file/view/export2xml.html.php diff --git a/module/file/config.php b/module/file/config.php new file mode 100644 index 0000000000..55333f611e --- /dev/null +++ b/module/file/config.php @@ -0,0 +1,5 @@ +file->mimes['xml'] = 'text/xml'; +$config->file->mimes['html'] = 'text/html'; +$config->file->mimes['csv'] = 'text/csv'; +$config->file->mimes['default'] = 'application/octet-stream'; diff --git a/module/file/control.php b/module/file/control.php index 98f06f56bf..db9c5252d2 100644 --- a/module/file/control.php +++ b/module/file/control.php @@ -89,12 +89,8 @@ class file extends control if(file_exists($file->realPath)) { $fileName = $file->title . '.' . $file->extension; - if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) $fileName = urlencode($fileName); - header('Content-Description: File Transfer'); - header('Content-type: application/octet-stream'); - header("Content-Disposition: attachment; filename=$fileName"); $fileData = file_get_contents($file->realPath); - echo $fileData; + $this->sendDownHeader($fileName, $file->extension, $fileData); } else { @@ -111,16 +107,9 @@ class file extends control */ public function export2CSV() { - $fileName = $this->post->fileName; - $csvData = $this->post->rows; - $output = ''; - - /* format csvData form array to string. */ - $output .= '"'. implode('","', $this->post->fields) . '"' . "\n"; - foreach($csvData as $value) - { - $output .= '"'. implode('","', (array)$value) . '"' . "\n"; - } + $this->view->fields = $this->post->fields; + $this->view->rows = $this->post->rows; + $output = $this->parse('file', 'export2csv'); /* If the language is zh-cn, convert to gbk. */ $clientLang = $this->app->getClientLang(); @@ -136,13 +125,7 @@ class file extends control } } - if(strpos($fileName, '.csv') === false) $fileName .= '.csv'; - header('Content-type: application/csv'); - header("Content-Disposition: attachment; filename=$fileName"); - header("Pragma: no-cache"); - header("Expires: 0"); - echo $output; - die(); + $this->sendDownHeader($this->post->fileName, 'csv', $output); } /** @@ -153,30 +136,12 @@ class file extends control */ public function export2XML() { - $xmlData = $this->post->rows; - $fileName = $this->post->fileName; - $fields = $this->post->fields; - $output = ''; - $content = ''; - $xmlMark = ''; - $tmpArray = array(); + $this->view->fields = $this->post->fields; + $this->view->rows = $this->post->rows; + + $output = $this->parse('file', 'export2XML'); - /* format xmlData from array to xml. */ - $content .= "" . implode("", $fields) . ""; - foreach($xmlData as $row) - { - $tmpArray[] = "" . implode("", (array)$row) . ""; - } - $content .= "" . implode("", $tmpArray) . ""; - $output = $xmlMark . $content . ''; - - if(strpos($fileName, '.xml') === false) $fileName .= '.xml'; - header('Content-type: application/xml'); - header("Content-Disposition: attachment; filename=$fileName"); - header("Pragma: no-cache"); - header("Expires: 0"); - echo $output; - die(); + $this->sendDownHeader($this->post->fileName, 'xml', $output); } /** @@ -187,30 +152,41 @@ class file extends control */ public function export2HTML() { - $htmlData = $this->post->rows; - $fileName = $this->post->fileName; - $fields = $this->post->fields; - $output = ''; - $content = ''; - $tmpArray = array(); + $this->view->fields = $this->post->fields; + $this->view->rows = $this->post->rows; + $this->view->fileName = $this->post->fileName; + $output = $this->parse('file', 'export2Html'); - /* format htmlData from array to html. */ - $content .= "" . implode("", $fields) . ""; - foreach($htmlData as $tbody) - { - $tmpArray[] = "" . implode("", (array)$tbody) . ""; - } - $content .= "" . implode("", $tmpArray) . ""; - $output = "$fileName$content
"; + $this->sendDownHeader($this->post->fileName, 'html', $output); + } - if(strpos($fileName, '.html') === false) $fileName .= '.html'; - header('Content-type: application/html'); + /** + * Send the download header to the client. + * + * @param string $fileName + * @param string $extension + * @access public + * @return void + */ + public function sendDownHeader($fileName, $fileType, $content) + { + /* Append the extension name auto. */ + $extension = '.' . $fileType; + if(strpos($fileName, $extension) === false) $fileName .= $extension; + + /* urlencode the filename for ie. */ + if(strpos($this->server->http_user_agent, 'MSIE') !== false) $fileName = urlencode($fileName); + + /* Judge the content type. */ + $mimes = $this->config->file->mimes; + $contentType = isset($mimes[$fileType]) ? $mimes[$fileType] : $mimes['default']; + + header("Content-type: $contentType"); header("Content-Disposition: attachment; filename=$fileName"); header("Pragma: no-cache"); header("Expires: 0"); - echo $output; - die(); - } + die($content); + } /** * Delete a file. diff --git a/module/file/view/export2csv.html.php b/module/file/view/export2csv.html.php new file mode 100644 index 0000000000..013b3c0972 --- /dev/null +++ b/module/file/view/export2csv.html.php @@ -0,0 +1,18 @@ + + * @package file + * @version $Id$ + * @link http://www.zentao.net + */ +?> + + * @package file + * @version $Id$ + * @link http://www.zentao.net + */ +?> + + + + +<?php echo $fileName;?> + + + + $fieldLabel\n"; + } + ?> + +\n"; + foreach($row as $fieldName => $fieldValue) + { + echo "\n"; + } + echo "\n"; +} +?> +
$fieldValue
+ + diff --git a/module/file/view/export2xml.html.php b/module/file/view/export2xml.html.php new file mode 100644 index 0000000000..ade8ee492a --- /dev/null +++ b/module/file/view/export2xml.html.php @@ -0,0 +1,36 @@ + + * @package file + * @version $Id$ + * @link http://www.zentao.net + */ +?> +\n";?> + + $fieldLabel) +{ + echo " <$fieldName>$fieldLabel\n"; +} +?> + + +\n"; + foreach($row as $fieldName => $fieldValue) + { + $fieldValue = htmlspecialchars($fieldValue); + echo " <$fieldName>$fieldValue\n"; + } + echo " \n"; +} +?> + +