Skip to content

Instantly share code, notes, and snippets.

@scoumbourdis
Last active July 25, 2020 07:10
Show Gist options
  • Save scoumbourdis/d546da74a57858786114fa2b668ef40f to your computer and use it in GitHub Desktop.
Save scoumbourdis/d546da74a57858786114fa2b668ef40f to your computer and use it in GitHub Desktop.
A PHP library for replacing the default Export State of Grocery CRUD Enterprise
<?php
namespace MyCustomStates;
use GroceryCrud\Core\GroceryCrud as GCrud;
use GroceryCrud\Core\State\StateInterface;
use GroceryCrud\Core\State\ExportState as GcExportState;
class ExportState implements StateInterface {
protected $exportStateObject;
protected $gCrud;
/**
* DatagridState constructor.
* @param \GroceryCrud\Core\GroceryCrud $gCrud
* @throws \GroceryCrud\Core\Exceptions\Exception
*/
function __construct(GCrud $gCrud)
{
$this->gCrud = $gCrud;
$this->exportStateObject = new GcExportState($gCrud);
$this->exportStateObject->setPage(1);
// Please have in mind that our new limitation is 10.000 as we've tested and checked that the database
// memory has some limitations with Zend db when we are calling more than 10.000 rows and send all of
// our results.
$this->exportStateObject->setPerPage(10000);
}
public function render() {
// As this is a custom file and you can literaly do whatever you wish, if the queries are not optimized for your
// needs (e.g. the 10.000 rows are too little) then you can use the function:
// $this->exportStateObject->getStateParameters(); and create your own queries with your way to export data to
// excel (e.g. you don't have to use the getFinalData if it doesn't fit your needs)
$output = $this->exportStateObject->getFinalData();
$results = $output->data;
$columns = $output->columns;
// Use your own folder that you have all the exported spreadsheets. In our example we are
// using "export/" (have in mind that I am not adding the slash at the end)
$temporaryFolderPath = 'export';
$this->exportToExcelWithXlsWriter($results, $columns, $temporaryFolderPath);
exit;
}
public function exportToExcelWithXlsWriter($results, $columns, $temporaryFolderPath) {
$header = [];
foreach ($columns as $column) {
$header[] = $column->displayAs;
}
$data = [
$header
];
foreach ($results as $row) {
$tmp_row = [];
foreach ($columns as $column) {
$tmp_row[] = $row[$column->name];
}
$data[] = $tmp_row;
}
$subjectPlural = $this->gCrud->getSubjectPlural();
$filename = !empty($subjectPlural) ? $subjectPlural : 'Spreadsheet';
$filename .= '_' . date('Y-m-d');
$filePath = $temporaryFolderPath. '/' . $filename . '_' . uniqid() .'.xlsx';
$writer = new \XLSXWriter();
$writer->writeSheet($data);
$writer->writeToFile($filePath);
// WARNING! This is just an example of a way to export an excel file without running out of memory
// Before using this redirection with a public access please make sure that you understand the security risks
// and that you have cronjobs to remove older spreadsheets or else this could drive you to a security and
// privacy breach
header('Location:' . $filePath);
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment