ea-Geiercode
[ class tree: ea-Geier ] [ index: ea-Geier ] [ all elements ]

Source for file csv.html.php

Documentation is available at csv.html.php

  1. <?php 
  2. /**
  3.  * smarty functions for csv export
  4.  *
  5.  * LICENSE:
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
  13.  * GNU General Public License for more details.
  14.  * You should have received a copy of the GNU General Public License along
  15.  * with this program; if not, write to the Free Software Foundation, Inc.,
  16.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17.  *
  18.  * @package       ea-Geier
  19.  * @subpackage code
  20.  * @author       m2mtech <tech@m2m.at>
  21.  * @copyright  2007 m2m server software gmbh
  22.  * @license       http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
  23.  * @version       $Id: csv.html.php 144 2007-09-04 11:38:15Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * prepares smarty for CSV export
  29.  *
  30.  * @param    eaSmarty    template object
  31.  * @param    string        name of CSV file
  32.  * @param    boolean        send no header info
  33.  */
  34. function eaPrepareCVSSmarty(&$smarty, $filename, $noHeader = false) {
  35.     $conf =& $GLOBALS['conf'];
  36.     $smarty->assign('CVSdelimiter', $conf['txtCSVDelimiter']);
  37.     $smarty->assign('CVSnewline', $conf['txtCSVNewLine']);
  38.     $smarty->register_prefilter('eaPrepareCVSSmartyPre');
  39.     $smarty->register_modifier('CVSenquote', 'eaPrepareCVSSmartyModEnquote');
  40.     $smarty->register_modifier('neg', 'eaPrepareCVSSmartyModEnquoteNegative');
  41.     $smarty->default_modifiers[] = 'CVSenquote';
  42.     
  43.     if ($noHeader) return true;
  44.     header('Content-Type: text/x-csv');
  45.     header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  46.     header('Content-Disposition: attachment; filename="' . $filename . '"');
  47.     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  48.     header('Pragma: public');
  49.     
  50.     return true;
  51. }
  52.  
  53. /**
  54.  * enquotes fields containing special charactors
  55.  *
  56.  * @param    string    field content
  57.  * @return    string    quoted field
  58.  */
  59. function eaPrepareCVSSmartyModEnquote($string) {
  60.     $conf =& $GLOBALS['conf'];
  61.     if (!preg_match('/[\n\r' . preg_quote($conf['txtCSVDelimiter'] . $conf['txtCSVQuote']) . ']/', $string)) return $string;
  62.  
  63.     return $conf['txtCSVQuote'] . preg_replace('/' . preg_quote($conf['txtCSVQuote']) . '/', $conf['txtCSVEscQuote'], $string) . $conf['txtCSVQuote'];
  64. }
  65.  
  66. /**
  67.  * enquotes fields containing special charactors and add a minus sign
  68.  *
  69.  * @param    string    field content
  70.  * @return    string    quoted field
  71.  */
  72.     if (substr($string, 0, 1) != '-') $string = '-' . $string;
  73.  
  74.     return eaPrepareCVSSmartyModEnquote($string);
  75. }
  76.  
  77. /**
  78.  * prepare template for CSV export
  79.  *
  80.  * places new-line & delimiter variables
  81.  * changes variables in foreach fields due to problem with default modifiers
  82.  *
  83.  * @param    string    field content
  84.  * @return    string    quoted field
  85.  */
  86. function eaPrepareCVSSmartyPre($tpl_source, &$smarty) {
  87.     // replace new lines ||
  88.     $return = preg_replace('/\|\|/', '{$CVSnewline|smarty:nodefaults}', $tpl_source); 
  89.     // replace delimiters |
  90.     $return = preg_replace('/\|(?!(smarty|neg))/', '{$CVSdelimiter|smarty:nodefaults}', $return); 
  91.     
  92.     // cleanup foreach
  93.     $return = preg_replace('/({foreach[^\$]+\$[^\s]+)/', '${1}|smarty:nodefaults', $return); 
  94.  
  95.     return $return;
  96. }
  97.  
  98. ?>

Documentation generated on Sun, 09 Sep 2007 17:08:17 +0200 by phpDocumentor 1.3.1