public function buildCSV($file = '', $timestamp = '', $sanitize = true, $del = array()) { $this->limit = null; parent::build(); $segments = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); $filename = $file != '' ? basename($file, '.csv') : end($segments); $filename = preg_replace('/[^0-9a-z\\._-]/i', '', $filename); $filename .= $timestamp != "" ? date($timestamp) . ".csv" : ".csv"; $save = (bool) strpos($file, "/"); //Delimiter $delimiter = array(); $delimiter['delimiter'] = isset($del['delimiter']) ? $del['delimiter'] : ';'; $delimiter['enclosure'] = isset($del['enclosure']) ? $del['enclosure'] : '"'; $delimiter['line_ending'] = isset($del['line_ending']) ? $del['line_ending'] : "\n"; if ($save) { $handle = fopen(public_path() . '/' . dirname($file) . "/" . $filename, 'w'); } else { $headers = array('Content-Type' => 'text/csv', 'Pragma' => 'no-cache', '"Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', 'Content-Disposition' => 'attachment; filename="' . $filename . '"'); $handle = fopen('php://output', 'w'); ob_start(); } fputs($handle, $delimiter['enclosure'] . implode($delimiter['enclosure'] . $delimiter['delimiter'] . $delimiter['enclosure'], $this->headers) . $delimiter['enclosure'] . $delimiter['line_ending']); foreach ($this->data as $tablerow) { $row = new Row($tablerow); foreach ($this->columns as $column) { if (in_array($column->name, array("_edit"))) { continue; } $cell = new Cell($column->name); $value = str_replace('"', '""', str_replace(PHP_EOL, '', strip_tags($this->getCellValue($column, $tablerow, $sanitize)))); $cell->value($value); $row->add($cell); } if (count($this->row_callable)) { foreach ($this->row_callable as $callable) { $callable($row); } } fputs($handle, $delimiter['enclosure'] . implode($delimiter['enclosure'] . $delimiter['delimiter'] . $delimiter['enclosure'], $row->toArray()) . $delimiter['enclosure'] . $delimiter['line_ending']); } fclose($handle); if ($save) { //redirect, boolean or filename? } else { $output = ob_get_clean(); //return \Response::make(rtrim($output, "\n"), 200, $headers); } }
Deficient::boot("./"); ## burp, move it somewhere route_get('page/(\\d+)', array('as' => 'page', function ($page) { Zofe\Burp\BurpEvent::queue('dataset.page', array($page)); })); //define some general purpose events on query-string route_query('ord=(-?)(\\w+)', array('as' => 'orderby', function ($direction, $field) { $direction = $direction == '-' ? "DESC" : "ASC"; Zofe\Burp\BurpEvent::queue('dataset.sort', array($direction, $field)); }))->remove('page'); ## test routes route_get('^/{page?}$', array('as' => 'datagrid', function () { $grid = DataGrid::source(new User()); $grid->add('id', 'ID', true)->style('width:100px'); $grid->add('name', 'Name', true); $grid->paginate(5); echo blade('datagrid.tests.datagrid', compact('grid')); die; })); route_get('^/dataset/{page?}$', array('as' => 'dataset', function () { $ds = DataSet::source(new User()); $ds->paginate(5); $ds->build(); echo blade('datagrid.tests.dataset', compact('ds')); die; })); route_missing(function () { echo blade('datagrid.tests.error', array(), 404); die; }); route_dispatch();