Browse Source

Adding CSV View Helper

Paolo Libertini 6 years ago
parent
commit
ce69a14450
1 changed files with 113 additions and 0 deletions
  1. 113 0
      library/Zend/Controller/Action/Helper/Csv.php

+ 113 - 0
library/Zend/Controller/Action/Helper/Csv.php

@@ -0,0 +1,113 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Controller
+ * @subpackage Zend_Controller_Action_Helper
+ * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * @see Zend_Controller_Action_Helper_Abstract
+ */
+require_once 'Zend/Controller/Action/Helper/Abstract.php';
+
+/**
+ * Simplify Csv context switching based on requested format
+ *
+ * @uses       Zend_Controller_Action_Helper_Abstract
+ * @category   Zend
+ * @package    Zend_Controller
+ * @subpackage Zend_Controller_Action_Helper
+ * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ */
+class Zend_Controller_Action_Helper_Csv extends Zend_Controller_Action_Helper_Abstract
+{
+    /**
+     * Perform helper when called as $this->_helper->Csv() from an action controller
+     *
+     * @param  array $aryData
+     * @param  string $strName
+     * @param  bool $bolCols; default true; zeigt Spaltenüberschriften
+     * @return void
+     */
+    public function direct($aryData = array(), $strName = "csv", $bolCols = true) {
+        $this->printExcel($aryData, $strName, $bolCols);
+    }
+
+    /**
+     * array via fputcsv() zu csv
+     *
+     * @param  array $aryData
+     * @param  string $strName
+     * @param  bool $bolCols
+     * @return void
+     */
+    public function printExcel($aryData = array(), $strName = "csv", $bolCols = true) {
+
+        if (!is_array($aryData) || empty($aryData)) {
+            exit(1);
+        }
+
+        // header
+        header('Content-Description: File Transfer');
+        header('Content-Type: text/csv; charset=utf-8');
+        header("Content-Disposition: attachment; filename=export_" . $strName . "_" . date("Ymd_H-i") . ".csv");
+        header('Content-Transfer-Encoding: binary');
+        header('Expires: 0');
+        header('Cache-control: private, must-revalidate');
+        header("Pragma: public");
+
+        // Spaltenüberschriften
+        if ($bolCols) {
+            $aryCols = array_keys($aryData[0]);
+            array_unshift($aryData, $aryCols);
+        }
+
+        // Ausgabepuffer für fputcsv
+        ob_start();
+
+        // output Stream für fputcsv
+        $fp = fopen("php://output", "w");
+        if (is_resource($fp)) {
+            foreach ($aryData as $aryLine) {
+                // ";" für Excel
+                fputcsv($fp, $aryLine, ';', '"');
+            }
+
+            $strContent = ob_get_clean();
+
+            // Excel SYLK-Bug
+            // http://support.microsoft.com/kb/323626/de
+            $strContent = preg_replace('/^ID/', 'id', $strContent);
+
+            $strContent = utf8_decode($strContent);
+            $intLength = mb_strlen($strContent, 'utf-8');
+
+            // length
+            header('Content-Length: ' . $intLength);
+
+            // kein fclose($fp);
+
+            echo $strContent;
+            exit(0);
+        }
+        ob_end_clean();
+        exit(1);
+    }
+
+}