Location: PHPKode > scripts > Zend Framework Data Grid > zend-framework-data-grid/Bvb/Grid/Deploy/Csv.php
<?php

/**
 * Mascker
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License 2.0
 * It is  available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/gpl-2.0.php
 * 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 hide@address.com so we can send you a copy immediately.
 *
 * @package    Mascker_Grid
 * @copyright  Copyright (c) Mascker (http://www.petala-azul.com)
 * @license    http://www.opensource.org/licenses/gpl-2.0.php   GNU General Public License 2.0
 * @version    0.1  mascker $
 * @author     Mascker (Bento Vilas Boas) <hide@address.com > 
 */

class Bvb_Grid_Deploy_Csv extends Bvb_Grid_DataGrid
{

    protected $dir;

    protected $title;

    protected $options = array ();

    protected $output = 'csv';


    /*
    * @param array $data
    */
    function __construct($db, $title, $dir, $options = array('download'))
    {

        if (! in_array ( 'csv', $this->export ))
        {
            echo $this->__ ( "You dont' have permission to export the results to this format" );
            die ();
        }
        
        $this->dir = rtrim ( $dir, "/" ) . "/";
        $this->title = $title;
        $this->options = $options;
        
        parent::__construct ( $db );
    }


    /**
     * [Para podemros utiliza]
     *
     * @param string $var
     * @param string $value
     */
    
    function __set($var, $value)
    {

        parent::__set ( $var, $value );
    }


    function buildTitltesCsv($titles)
    {

        foreach ( $titles as $title )
        {
            
            $grid .= '"' . $title ['value'] . '",';
        }
        
        return substr ( $grid, 0, - 1 ) . "\n";
    
    }


    function buildSqlexpCsv($sql)
    {

        if (is_array ( $sql ))
        {
            
            foreach ( $sql as $exp )
            {
                $grid .= '"' . $exp ['value'] . '",';
            }
        }
        

        return substr ( $grid, 0, - 1 ) . " \n";
    
    }


    function buildGridCsv($grids)
    {

        $grid = '';
        foreach ( $grids as $value )
        {
            
            foreach ( $value as $final )
            {
                $grid .= '"' . $final ['value'] . '",';
            }
            
            $grid = substr ( $grid, 0, - 1 ) . " \n";
        }
        
        return $grid;
    
    }


    function deploy()
    {

        $this->setPagination ( 0 );
        
        parent::deploy ();
        
        $grid .= self::buildTitltesCsv ( parent::buildTitles () );
        $grid .= self::buildGridCsv ( parent::buildGrid () );
        $grid .= self::buildSqlexpCsv ( parent::buildSqlExp () );
        

        file_put_contents ( $this->dir . $this->title . ".csv", $grid );
        
        
        if (in_array ( 'download', $this->options ))
        {
            header ( 'Content-type: text/plain' );
            header ( 'Content-Disposition: attachment; filename="' . $this->title . '.csv"' );
            readfile ( $this->dir . $this->title . '.csv' );
        }
        

        if (! in_array ( 'save', $this->options ))
        {
            unlink ( $this->dir . $this->title . '.csv' );
        }
        
        die ();
    
    }

}




Return current item: Zend Framework Data Grid