<commit_before><?php

/**
 * This file is part of the Grido (http://grido.bugyik.cz)
 *
 * Copyright (c) 2011 <NAME> (http://petr.bugyik.cz)
 *
 * For the full copyright and license information, please view
 * the file license.md that was distributed with this source code.
 */

namespace Grido\Components;

/**
 * Exporting data to CSV.
 *
 * @package     Grido
 * @subpackage  Components
 * @author      <NAME>
 */
class Export extends Base implements \Nette\Application\IResponse
{
    const ID = 'export';

    /** @var Grido\Grid */
    protected $grid;

    /** @var string */
    protected $name;

    /**
     * @param \Grido\Grid $grid
     * @param string $name
     */
    public function __construct(\Grido\Grid $grid, $name)
    {
        $this->grid = $grid;
        $this->name = $name;

        $grid->addComponent($this, self::ID);
    }

    /**
     * @param array $data
     * @param \Nette\ComponentModel\RecursiveComponentIterator $columns
     * @return string
     */
    protected function generateCsv($data, $columns)
    {
        $newLine = "\n";
        $delimiter = "\t";

        $head = array();
        foreach ($columns as $column) {
            $head[] = $column->label;
        }

        $a = FALSE;
        $source = implode($delimiter, $head) . $newLine;
        foreach ($data as $item) {
            if ($a) {
                $source .= $newLine;
            }

            $b = FALSE;
            foreach ($columns as $column) {
                if ($b) {
                    $source .= $delimiter;
                }

                $source .= $column->renderExport($item);
                $b = TRUE;
            }
            $a = TRUE;
        }

        return $source;
    }

    /**
     * @internal
     */
    public function handleExport()
    {
        $this->grid->presenter->sendResponse($this);
        $this->grid->presenter->terminate();
    }

    /*************************** interface \Nette\Application\IResponse ***************************/

    /**
     * Sends response to output.
     * @return void
     */
    public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse)
    {
        $file = $this->name . '.csv';
        $data = $this->grid->getData(FALSE);
        $columns = $this->grid[\Grido\Components\Columns\Column::ID]->getComponents();
        $source = $this->generateCsv($data, $columns);

        $charset = 'UTF-16LE';
        $source = mb_convert_encoding($source, $charset, 'UTF-8');
        $source = "\xFF\xFE" . $source; //add BOM

        $httpResponse->setHeader('Content-Encoding', $charset);
        $httpResponse->setHeader('Content-Length', strlen($source));
        $httpResponse->setHeader('Content-Type', "text/csv; charset=$charset");
        $httpResponse->setHeader('Content-Disposition', "attachment; filename=\"$file\"; filename*=utf-8''$file");

        print $source;
    }
}<commit_msg>Export: Typo but possible BC BREAK!<commit_after><?php

/**
 * This file is part of the Grido (http://grido.bugyik.cz)
 *
 * Copyright (c) 2011 <NAME> (http://petr.bugyik.cz)
 *
 * For the full copyright and license information, please view
 * the file license.md that was distributed with this source code.
 */

namespace Grido\Components;

/**
 * Exporting data to CSV.
 *
 * @package     Grido
 * @subpackage  Components
 * @author      <NAME>
 *
 * @property-read string $label
 */
class Export extends Base implements \Nette\Application\IResponse
{
    const ID = 'export';

    /** @var Grido\Grid */
    protected $grid;

    /** @var string */
    protected $label;

    /**
     * @param \Grido\Grid $grid
     * @param string $label
     */
    public function __construct(\Grido\Grid $grid, $label = NULL)
    {
        $this->grid = $grid;

        if ($label === NULL) {
            $label = ucfirst($this->grid->name);
        }

        $this->label = $label;

        $grid->addComponent($this, self::ID);
    }

    /**
     * @param array $data
     * @param \Nette\ComponentModel\RecursiveComponentIterator $columns
     * @return string
     */
    protected function generateCsv($data, $columns)
    {
        $newLine = "\n";
        $delimiter = "\t";

        $head = array();
        foreach ($columns as $column) {
            $head[] = $column->label;
        }

        $a = FALSE;
        $source = implode($delimiter, $head) . $newLine;
        foreach ($data as $item) {
            if ($a) {
                $source .= $newLine;
            }

            $b = FALSE;
            foreach ($columns as $column) {
                if ($b) {
                    $source .= $delimiter;
                }

                $source .= $column->renderExport($item);
                $b = TRUE;
            }
            $a = TRUE;
        }

        return $source;
    }

    /**
     * @return string
     */
    public function getLabel()
    {
        return $this->label;
    }
    /**
     * @internal
     */
    public function handleExport()
    {
        $this->grid->presenter->sendResponse($this);
        $this->grid->presenter->terminate();
    }

    /*************************** interface \Nette\Application\IResponse ***************************/

    /**
     * Sends response to output.
     * @return void
     */
    public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse)
    {
        $file = $this->label . '.csv';
        $data = $this->grid->getData(FALSE);
        $columns = $this->grid[\Grido\Components\Columns\Column::ID]->getComponents();
        $source = $this->generateCsv($data, $columns);

        $charset = 'UTF-16LE';
        $source = mb_convert_encoding($source, $charset, 'UTF-8');
        $source = "\xFF\xFE" . $source; //add BOM

        $httpResponse->setHeader('Content-Encoding', $charset);
        $httpResponse->setHeader('Content-Length', strlen($source));
        $httpResponse->setHeader('Content-Type', "text/csv; charset=$charset");
        $httpResponse->setHeader('Content-Disposition', "attachment; filename=\"$file\"; filename*=utf-8''$file");

        print $source;
    }
}
{'fraction_non_alphanumeric': 0.17157343801112201, 'fraction_numerical': 0.0026169447170428526, 'mean_word_length': 2.0212450592885376, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2731245', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1776, 'n_words': 399}