Plugin CSV – instalacja w katalogu aplikacji
# composer require friendsofcake/cakephp-csvview
# bin/cake plugin load CsvView
Plugin added successfully to `CONFIG/plugins.php`
public function index()
{
if ($this->request->is('csv')) {
$exports = [];
$header = ['Lp', 'Nazwa towaru', 'Ilość', 'Netto', 'Brutto'];
$i=1;
foreach($products as $product_id => $name)
{
$summary = isset($data[$product_id]) ? $data[$product_id] : null ;
if (!$summary) continue;
$exports[$i]['lp'] = $i;
$exports[$i]['name'] = $name;
$exports[$i]['amount'] = str_replace(".", ",", $summary->amount);
$exports[$i]['netto'] = str_replace(".", ",", $summary->netto);
$exports[$i]['brutto'] = str_replace(".", ",", $summary->brutto);
$i++;
}
$this->set(compact('exports'));
$this->viewBuilder()
->setClassName('CsvView.Csv')
->setOptions([
'serialize' => 'exports',
'header' => $header,
'dataEncoding' => 'UTF-8',
'csvEncoding' => 'CP1250',
'delimiter' => ';'
]);
$new_response = $this->getResponse()->withDownload('Magazyn-'.$s_month."/".$s_year.'.csv');
$this->setResponse($new_response);
} // csv - export
} // index