* rewrite the export feature of csv, html and xml.

This commit is contained in:
wangchunsheng
2011-05-02 09:03:20 +00:00
parent a6fd343d4b
commit fa9011fcb2
5 changed files with 142 additions and 64 deletions
+5
View File
@@ -0,0 +1,5 @@
<?php
$config->file->mimes['xml'] = 'text/xml';
$config->file->mimes['html'] = 'text/html';
$config->file->mimes['csv'] = 'text/csv';
$config->file->mimes['default'] = 'application/octet-stream';
+37 -61
View File
@@ -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 = '<?xml version="1.0" encoding="utf-8"?><xml>';
$tmpArray = array();
$this->view->fields = $this->post->fields;
$this->view->rows = $this->post->rows;
/* format xmlData from array to xml. */
$content .= "<fields><field>" . implode("</field><field>", $fields) . "</field></fields>";
foreach($xmlData as $row)
{
$tmpArray[] = "<row>" . implode("</row><row>", (array)$row) . "</row>";
}
$content .= "<rows>" . implode("</rows><rows>", $tmpArray) . "</rows>";
$output = $xmlMark . $content . '</xml>';
$output = $this->parse('file', 'export2XML');
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,29 +152,40 @@ 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 .= "<thead><th>" . implode("</th><th>", $fields) . "</th></thead>";
foreach($htmlData as $tbody)
{
$tmpArray[] = "<td>" . implode("</td><td>", (array)$tbody) . "</td>";
$this->sendDownHeader($this->post->fileName, 'html', $output);
}
$content .= "<tbody><tr>" . implode("</tr></tbody><tbody><tr>", $tmpArray) . "</tr></tbody>";
$output = "<html><head><title>$fileName</title></head><body><table>$content</table></body></html>";
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);
}
/**
+18
View File
@@ -0,0 +1,18 @@
<?php
/**
* The export2csv view file of file module of ZenTaoPMS.
*
* @copyright Copyright 2009-2010 QingDao Nature Easy Soft Network Technology Co,LTD (www.cnezsoft.com)
* @license LGPL (http://www.gnu.org/licenses/lgpl.html)
* @author Congzhi Chen <congzhi@cnezsoft.com>
* @package file
* @version $Id$
* @link http://www.zentao.net
*/
?>
<?php
echo '"'. implode('","', $fields) . '"' . "\n";
foreach($rows as $row)
{
echo '"'. implode('","', (array)$row) . '"' . "\n";
}
+43
View File
@@ -0,0 +1,43 @@
<?php
/**
* The export2html view file of file module of ZenTaoPMS.
*
* @copyright Copyright 2009-2010 QingDao Nature Easy Soft Network Technology Co,LTD (www.cnezsoft.com)
* @license LGPL (http://www.gnu.org/licenses/lgpl.html)
* @author Congzhi Chen <congzhi@cnezsoft.com>
* @package file
* @version $Id$
* @link http://www.zentao.net
*/
?>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<style>
table, th, td{font-size:12px; border:1px solid gray; border-collapse:collapse;}
</style>
<title><?php echo $fileName;?></title>
<body>
<table>
<tr>
<?php
foreach($fields as $fieldLabel)
{
echo "<th><nobr>$fieldLabel</nobr></th>\n";
}
?>
</tr>
<?php
foreach($rows as $row)
{
echo "<tr>\n";
foreach($row as $fieldName => $fieldValue)
{
echo "<td>$fieldValue</td>\n";
}
echo "</tr>\n";
}
?>
</table>
</body>
</html>
+36
View File
@@ -0,0 +1,36 @@
<?php
/**
* The export2xml view file of file module of ZenTaoPMS.
*
* @copyright Copyright 2009-2010 QingDao Nature Easy Soft Network Technology Co,LTD (www.cnezsoft.com)
* @license LGPL (http://www.gnu.org/licenses/lgpl.html)
* @author Congzhi Chen <congzhi@cnezsoft.com>
* @package file
* @version $Id$
* @link http://www.zentao.net
*/
?>
<?php echo "<?xml version='1.0' encoding='utf-8'?><xml>\n";?>
<fields>
<?php
foreach($fields as $fieldName => $fieldLabel)
{
echo " <$fieldName>$fieldLabel</$fieldName>\n";
}
?>
</fields>
<rows>
<?php
foreach($rows as $row)
{
echo " <row>\n";
foreach($row as $fieldName => $fieldValue)
{
$fieldValue = htmlspecialchars($fieldValue);
echo " <$fieldName>$fieldValue</$fieldName>\n";
}
echo " </row>\n";
}
?>
</rows>
</xml>