public function buildCSV($file = '', $timestamp = '', $sanitize = true, $del = array()) { $this->limit = null; parent::build(); $segments = \Request::segments(); $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)))); // Excel for Mac is pretty stupid, and will break a cell containing \r, such as user input typed on a // old Mac. // On the other hand, PHP will not deal with the issue for use, see for instance: // http://stackoverflow.com/questions/12498337/php-preg-replace-replacing-line-break // We need to normalize \r and \r\n into \n, otherwise the CSV will break on Macs $value = preg_replace('/\\r\\n|\\n\\r|\\n|\\r/', "\n", $value); $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); } }
public function buildCSV($file = '', $timestamp = '', $sanitize = true, $del = array()) { $this->limit = null; parent::build(); $segments = \Request::segments(); $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) { $methodReflection = new \ReflectionFunction($callable); $argCount = $methodReflection->getNumberOfParameters(); if ($argCount === 1) { $callable($row); } elseif ($argCount === 2) { $callable($row, $tablerow); } } } 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); } }
public function build($view = '') { $view == '' and $view = 'rapyd::datagrid'; parent::build(); foreach ($this->data as $tablerow) { $row = new Row(); foreach ($this->columns as $column) { $cell = new Cell(); $value = $this->getCellValue($column, $tablerow); $cell->value($value); $row->add($cell); } if ($this->row_callable) { $callable = $this->row_callable; $callable($row); } $this->rows[] = $row; } return \View::make($view, array('dg' => $this, 'buttons' => $this->button_container, 'label' => $this->label)); }
/** * return a form with a nested action button * * @param $url * @param $method * @param $name * @param string $position * @param array $attributes * @return $this * @static */ public static function formButton($url, $method, $name, $position = 'BL', $attributes = array()) { //Method inherited from \Zofe\Rapyd\Widget return \Zofe\Rapyd\DataSet::formButton($url, $method, $name, $position, $attributes); }