2017 © Pedro Peláez
 

cakephp-plugin cakephp-csvview

A CSV View class for CakePHP 3.x

image

kongka/cakephp-csvview

A CSV View class for CakePHP 3.x

  • Friday, July 13, 2018
  • by kongkannika
  • Repository
  • 0 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 58 Forks
  • 0 Open issues
  • 17 Versions
  • 0 % Grown

The README.md

Build Status Coverage Status Total Downloads Latest Stable Version Software License, (*1)

CsvView Plugin

Quickly enable CSV output of your model data., (*2)

Background

I needed to quickly export CSVs of stuff in the database. Using a view class to iterate manually would be a chore to replicate for each export method, so I figured it would be much easier to do this with a custom view class, like JsonView or XmlView., (*3)

Requirements

  • CakePHP 3.x
  • PHP 5.4.16 or greater
  • Patience

Installation

[Using Composer], (*4)

composer require kongka/cakephp-csvview:dev-master

Enable plugin

Load the plugin in your app's config/bootstrap.php file:, (*5)

Plugin::load('CsvView', ['routes' => true]);

Usage

To export a flat array as a CSV, one could write the following code:, (*6)

public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];
    $_serialize = 'data';

    $this->viewBuilder()->setClassName('CsvView.Csv');
    $this->set(compact('data', '_serialize'));
}

All variables that are to be included in the csv must be specified in the $_serialize view variable, exactly how JsonView or XmlView work., (*7)

It is possible to have multiple variables in the csv output:, (*8)

public function export()
{
    $data = [['a', 'b', 'c']];
    $data_two = [[1, 2, 3]];
    $data_three = [['you', 'and', 'me']];

    $_serialize = ['data', 'data_two', 'data_three'];

    $this->viewBuilder()->setClassName('CsvView.Csv');
    $this->set(compact('data', 'data_two', 'data_three', '_serialize'));
}

If you want headers or footers in your CSV output, you can specify either a $_header or $_footer view variable. Both are completely optional:, (*9)

public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];

    $_serialize = 'data';
    $_header = ['Column 1', 'Column 2', 'Column 3'];
    $_footer = ['Totals', '400', '$3000'];

    $this->viewBuilder()->setClassName('CsvView.Csv');
    $this->set(compact('data', '_serialize', '_header', '_footer'));
}

You can also specify the delimiter, end of line, newline, escape characters and byte order mark (BOM) sequence using $_delimiter, $_eol, $_newline, $_enclosure and $_bom respectively:, (*10)

public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];

    $_serialize = 'data';
    $_delimiter = chr(9); //tab
    $_enclosure = '"';
    $_newline = '\r\n';
    $_eol = '~';
    $_bom = true;

    $this->viewBuilder()->setClassName('CsvView.Csv');
    $this->set(compact('data', '_serialize', '_delimiter', '_enclosure', '_newline', '_eol', '_bom'));
}

The defaults for these variables are:, (*11)

  • _delimiter: ,
  • _enclosure: "
  • _newline: \n
  • _eol: \n
  • _bom: false
  • _setSeparator: false

The _eol variable is the one used to generate newlines in the output. _newline, however, is the character that should replace the newline characters in the actual data. It is recommended to use the string representation of the newline character to avoid rendering invalid output., (*12)

Some reader software incorrectly renders UTF-8 encoded files which do not contain byte order mark (BOM) byte sequence. The _bom variable is the one used to add byte order mark (BOM) byte sequence beginning of the generated CSV output stream. See Wikipedia article about byte order mark for more information., (*13)

The _setSeparator flag can be used to set the separator explicitly in the first line of the CSV. Some readers need this in order to display the CSV correctly., (*14)

If you have complex model data, you can use the $_extract view variable to specify the individual Hash::extract()-compatible paths or a callable for each record:, (*15)

public function export()
{
    $posts = $this->Post->find('all');
    $_serialize = 'posts';
    $_header = ['Post ID', 'Title', 'Created'];
    $_extract = [
        'id',
        function ($row) {
            return $row['title'];
        },
        'created'
    ];

    $this->viewBuilder()->setClassName('CsvView.Csv');
    $this->set(compact('posts', '_serialize', '_header', '_extract'));
}

If your model data contains some null values or missing keys, you can use the $_null variable, just like you'd use $_delimiter, $_eol, and $_enclosure, to set how null values should be displayed in the CSV., (*16)

$_null defaults to ''., (*17)

You can use Router::extensions() and the RequestHandlerComponent to automatically have the CsvView class switched in as follows:, (*18)

// In your routes.php file:
Router::extensions('csv');

// In your controller:
public $components = [
    'RequestHandler' => [
        'viewClassMap' => ['csv' => 'CsvView.Csv']
    ]
];

public function export()
{
    $posts = $this->Post->find('all');
    $this->set(compact('post'));

    if ($this->request->params['_ext'] === 'csv') {
        $_serialize = 'posts';
        $_header = array('Post ID', 'Title', 'Created');
        $_extract = array('id', 'title', 'created');

        $this->set(compact('_serialize', '_header', '_extract'));
    }
}

Access /posts/export.csv to get the data as csv and /posts/export to get normal page as usually., (*19)

For really complex CSVs, you can also simply use your own view files. To do so, either leave $_serialize unspecified or set it to null. The view files will be located in the csv subdirectory of your current controller:, (*20)

// View used will be in src/Template/Posts/csv/export.ctp
public function export()
{
    $posts = $this->Post->find('all');
    $_serialize = null;
    $this->viewBuilder()->setClassName('CsvView.Csv');
    $this->set(compact('posts', '_serialize'));
}

Setting a different encoding to the file

if you need to have a different encoding in you csv file you have to set the encoding of your data you are passing to the view and also set the encoding you want for the csv file. This can be done by using _dataEncoding and _csvEncoding:, (*21)

The defaults are:, (*22)

  • _dataEncoding: UTF-8
  • _csvEncoding: UTF-8

** Only if those two variable are different your data will be converted to another encoding., (*23)

CsvView uses the iconv extension by default to encode your data. You can change the php extension used to encode your data by setting the _extension option:, (*24)

$this->set('_extension', 'mbstring');

The currently supported encoding extensions are as follows:, (*25)

  • iconv
  • mbstring

Setting the downloaded file name

By default, the downloaded file will be named after the last segment of the URL used to generate it. Eg: example.com/my_controller/my_action would download my_action.csv, while example.com/my_controller/my_action/first_param would download first_param.csv., (*26)

In IE you are required to set the filename, otherwise it will download as a text file., (*27)

To set a custom file name, use the Response::download method. The following snippet can be used to change the downloaded file from export.csv to my_file.csv:, (*28)

public function export()
{
    $data = [
        ['a', 'b', 'c'],
        [1, 2, 3],
        ['you', 'and', 'me'],
    ];
    $_serialize = 'data';

    $this->response = $this->response->withDownload('my_file.csv'); // <= setting the file name
    $this->viewBuilder()->setClassName('CsvView.Csv');
    $this->set(compact('data', '_serialize'));
}

Using a specific View Builder

In some cases, it is better not to use the current model's View Builder $this->viewBuilder as any call to $this->render() will compromise any subsequent rendering., (*29)

For example, in the course of your current controller's action, if you need to render some data as CSV in order to simply save it into a file on the server., (*30)

Do not forget to add to your controller:, (*31)

use Cake\View\View;
use Cake\View\ViewBuilder;

So you can create a specific View Builder:, (*32)


// Your data array $data = []; // Params $_serialize = 'data'; $_delimiter = ','; $_enclosure = '"'; $_newline = '\r\n'; // Create the builder $builder = new ViewBuilder; $builder->layout = false; $builder->setClassName('CsvView.Csv'); // Then the view $view = $builder->build($data); $view->set(compact('data', '_serialize', '_delimiter', '_enclosure', '_newline')); // And Save the file $file = new File('/full/path/to/file.csv', true, 0644); $file->write($view->render());

License

The MIT License (MIT), (*33)

Copyright (c) 2012 Jose Diaz-Gonzalez, (*34)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*35)

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*36)

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*37)

The Versions

13/07 2018

dev-master

9999999-dev http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

13/07 2018

3.2.3

3.2.3.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

08/06 2018

dev-fix-tests

dev-fix-tests http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

The Development Requires

csv cakephp export view

08/05 2018
16/03 2017

3.2.2

3.2.2.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

07/11 2016

3.2.1

3.2.1.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

20/07 2016

3.2.0

3.2.0.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

18/07 2016

3.1.1

3.1.1.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

18/07 2016

3.1.0

3.1.0.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

03/02 2016

3.0.0

3.0.0.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

20/01 2016

2.3.0

2.3.0.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

16/01 2016

2.2.0

2.2.0.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 3.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

02/12 2015

2.1.0

2.1.0.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 2.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

02/08 2015

2.0.0

2.0.0.0 http://github.com/friendsofcake/cakephp-csvview

A CSV View class for CakePHP 2.x

  Sources   Download

MIT

The Requires

 

csv cakephp export view

19/09 2014
20/03 2014
20/09 2013