/** * Exports statements as CSV. * @param String $id Export's ID. * @return StreamedResponse. */ public function showCsv($id) { // should top timeouts on long queries set_time_limit(0); $opts = $this->getOptions(); $model = $this->repo->show($id, $opts); return IlluminateResponse::stream(function () use($model, $opts) { if (ob_get_level() == 0) { ob_start(); } // Outputs the field names (column headers). echo implode(',', array_map(function ($field) { return $field['to']; }, $model->fields)) . "\n"; // Outputs the statements (rows). $this->repo->export($model, array_merge($opts, ['next' => function () { echo "\n"; flush(); ob_flush(); }, 'stream' => function ($obj) { // Outputs the field values (column values). echo implode(',', array_map(function ($value) { return $this->quoteCSV($value); flush(); ob_flush(); }, $obj)); }])); }, 200, ['Content-Type' => 'text/csv']); }
/** * Gets an array of statements. * https://github.com/adlnet/xAPI-Spec/blob/master/xAPI.md#723-getstatements * @param [String => mixed] $options * @return Response */ public function index($options) { // Gets the acceptable languages. $langs = LockerRequest::header('Accept-Language', []); $langs = is_array($langs) ? $langs : explode(',', $langs); $langs = array_map(function ($lang) { return explode(';', $lang)[0]; }, $langs); // Gets the params. $params = LockerRequest::all(); if (isset($params['agent'])) { $decoded_agent = json_decode($params['agent']); if ($decoded_agent !== null) { $params['agent'] = $decoded_agent; } } // Gets an index of the statements with the given options. list($statements, $count, $opts) = $this->statements->index(array_merge(['langs' => $langs], $params, $options)); // Defines the content type and body of the response. if ($opts['attachments'] === true) { $content_type = 'multipart/mixed; boundary=' . static::BOUNDARY; $body = function () use($statements, $count, $opts) { return $this->makeAttachmentsResult($statements, $count, $opts); }; } else { $content_type = 'application/json;'; $body = function () use($statements, $count, $opts) { return $this->makeStatementsResult($statements, $count, $opts); }; } // Creates the response. return \Response::stream($body, 200, ['Content-Type' => $content_type, 'X-Experience-API-Consistent-Through' => Helpers::getCurrentDate()]); }
/** * @param array $attributes array( '中文描述' => 'key' ); * @param string $title * @param array $data * @param string $fromCharSet * @param string $toCharSet * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function export_csv(array $attributes, $title = 'csv-data-dump', $data = array(), $fromCharSet = 'UTF-8', $toCharSet = 'UTF-8') { $title .= '-' . date('Ymd-His'); $config = new ExporterConfig(); $config->setDelimiter(';')->setEnclosure("'")->setEscape("\\")->setFromCharset($fromCharSet)->setToCharset($toCharSet); $head = array([], []); foreach ($attributes as $key => $val) { $head[0][] = $key; $head[1][] = $val; } $data = array_merge($head, $data); $headers = array('Content-type' => "application/csv; filename=\"{$title}.csv\"", 'Content-Disposition' => "attachement; filename=\"{$title}.csv\"", 'Cache-Control' => "no-cache"); $response = \Response::stream(function () use($config, $data) { $exporter = new Exporter($config); $exporter->export('php://output', $data); }, 200, $headers); return $response; }
public function csv(ExportRequest $request, $crudeName) { $list = $request->crude->getFiltered(null, null, null, null, null, null); $setup = $request->crude->getCrudeSetup(); $column = $setup->getCsvColumn(); if (empty($column)) { $column = $setup->getColumnAttr(); } $list = $this->formatData($list, $column); $fileName = $this->fileName($setup, 'csv'); $headers = $this->csvHeaders; $headers['Content-Disposition'] = 'attachment; filename=' . $fileName; $callback = function () use($list, $column) { $file = fopen('php://output', 'w'); fputcsv($file, $column, "\t"); foreach ($list as $row) { fputcsv($file, $row, "\t"); } fclose($file); }; return \Response::stream($callback, 200, $headers); }
public function studentiPredmetaCsv() { // pretestiraj! $headers = ['Content-type' => 'application/csv', 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', 'Content-type' => 'text/csv', 'Content-Disposition' => 'attachment; filename=galleries.csv', 'Expires' => '0', 'Pragma' => 'public']; $predmet = Input::get('predmet'); $program = Input::get('program'); $letnik = Input::get('letnik'); $leto = Input::get('leto'); $naslov = Input::get('naslov'); $studenti = Vpisan_predmet::where('Vpisan_predmet.sifra_predmeta', $predmet)->where('Vpisan_predmet.sifra_studijskega_programa', $program)->where('Vpisan_predmet.sifra_letnika', $letnik)->where('Vpisan_predmet.sifra_studijskega_leta_izvedbe_predmeta', $leto)->join('student', 'Vpisan_predmet.vpisna_stevilka', '=', 'student.vpisna_stevilka')->join('vpis', function ($join) { $join->on('Vpisan_predmet.vpisna_stevilka', '=', 'Vpis.vpisna_stevilka')->on('Vpisan_predmet.sifra_studijskega_leta', '=', 'Vpis.sifra_studijskega_leta'); })->join('Vrsta_vpisa', 'Vpis.sifra_vrste_vpisa', '=', 'Vrsta_vpisa.sifra_vrste_vpisa')->select('student.vpisna_stevilka AS vpisna', 'student.priimek_studenta AS priimek', 'student.ime_studenta AS ime', 'Vrsta_vpisa.opis_vrste_vpisa AS vrsta_vpisa')->orderByRaw('priimek COLLATE utf8_slovenian_ci')->get(); # prva vrstica: imena stolpcev (preveri ce je izpis ok) array_unshift($studenti, array_keys($studenti[0])); $callback = function () use($studenti) { $outCsv = fopen('php://output', 'w'); foreach ($studenti as $row) { fputcsv($outCsv, $row); } fclose($outCsv); }; return Response::stream($callback, 200, $headers); }
/** * Generates content response. * @param mixed $data used to select the Document. * @return Response */ public function documentResponse($data) { $document = $this->document->find($this->getOptions(), $this->document_type, $data); if (!$document) { throw new Exceptions\NotFound($data[$this->identifier], $this->document_type); } else { $headers = ['Updated' => $document->updated_at->toISO8601String(), 'Content-Type' => $document->contentType, 'ETag' => '"' . $document->sha . '"']; if ($this->method === 'HEAD') { //Only return headers return \Response::make(null, 200, $headers); } else { switch ($document->contentType) { case "application/json": return \Response::json($document->content, 200, $headers); case "text/plain": return \Response::make($document->content, 200, $headers); default: $stream = FileFactory::create()->stream($document->getFilePath(), []); return \Response::stream(function () use($stream) { while (!feof($stream)) { echo fread($stream, 8192); flush(); ob_flush(); } fclose($stream); }, 200, $headers); } } } }
/** * Returns the generated PDF as as a Response. * * @param string|null $filename * @param bool $attachment * @return Response * @throws UnableToExecute */ public function download($filename = null, $attachment = false) { $data = $this->generate(); $this->reset(); $headers = ['Content-Type' => 'application/pdf']; if ($filename != null) { if ($attachment) { $headers['Content-Disposition'] = 'attachment; filename="' . $filename . '"'; } else { $headers['Content-Disposition'] = 'inline; filename="' . $filename . '"'; } } return ResponseFacade::stream(function () use($data) { $out = fopen('php://output', 'w'); fputs($out, $data); fclose($out); }, 200, $headers); }
/** * Return a new streamed response from the application. * * @param \Closure $callback * @param int $status * @param array $headers * @return \Symfony\Component\HttpFoundation\StreamedResponse */ public function stream($callback, $status = 200, array $headers = array()) { return \Response::stream($callback, $status, $headers); }
return Response::stream(function () { $sep = "<br/>"; echo "[ " . \Carbon\Carbon::create() . " ]" . "Generate Data Sample Begin"; echo "<br/> <hr>"; // User // printf("[ %s ] %s %s <br/>",date('d M Y'), // "Generate User Admin ...Warehouse and Category Warehouse ", // \Emayk\Ics\Repo\Users\Users::generateUserAdmin()); echo "Generate User Admin ...Warehouse and Category Warehouse"; echo \Emayk\Ics\Repo\Users\Users::generateUserAdmin(); sleep(1); echo $sep; //Legalitas echo "Generate Legality ..."; echo \Emayk\Ics\Repo\Legality\Legality::generateMassiveLegality(); sleep(1); echo $sep; //departement echo "Generate Departement ..."; echo \Emayk\Ics\Repo\Dept\Dept::generateMassive(); echo $sep; sleep(1); //Position echo "Generate Position ..."; echo \Emayk\Ics\Repo\Positions\Positions::generateMassiveDataDummy(false, 50); echo $sep; sleep(1); //Color echo "Generate Color "; echo \Emayk\Ics\Repo\Colors\Colors::generateColorSample(); sleep(1); echo $sep; //Location echo "Generate Default Location "; echo \Emayk\Ics\Repo\Locations\Locations::createRecordCountry(); sleep(1); echo $sep; //currency echo "Generate Currency "; echo \Emayk\Ics\Repo\Currencies\Currencies::generateMassive(); sleep(1); echo $sep; //Fabric Grade echo "Generate Fabric Grade..."; echo \Emayk\Ics\Repo\Fabricgrade\Fabricgrade::generateMassive(); echo $sep; sleep(1); // fabric type echo "Generate Fabric Type ..."; echo \Emayk\Ics\Repo\Fabrictype\Fabrictype::generateMassive(); sleep(1); echo $sep; //Bank echo "Generate Bank ..."; echo \Emayk\Ics\Repo\Bank\Bank::getIdsOrCreateBanks(); sleep(1); echo $sep; // Generate Account echo "Generate Type Account Bank ..."; echo \Emayk\Ics\Repo\Bankaccounttype\Bankaccounttype::getDefaultBankTypeIdsOrCreate(); sleep(1); echo $sep; // Generate Rekening Bank echo "Generate Account Bank ..."; echo \Emayk\Ics\Repo\Bankaccount\Bankaccount::createMassiveSampleRecord(false, 100); sleep(1); echo $sep; // Generate Buyer Supplier and Contact Person echo "Generate Buyers, Supplier, Contact Person ..."; echo \Emayk\Ics\Repo\Contactperson\Contactperson::generateMassive(false, 100); sleep(1); echo $sep; // Generate Phones echo "Generate Phone Buyers, Supplier ..."; echo \Emayk\Ics\Repo\Phones\Phones::generateMassivePhoneSample(false, 300); sleep(1); echo $sep; // Generate Product, Detail , Stock, Stock History echo "Generate Product , detail , Stock,stock detail ..."; echo \Emayk\Ics\Repo\Products\Products::generateSampleProducts(); sleep(1); echo $sep; // Generate Office echo "<br/> <hr>"; sleep(1); echo "[ " . \Carbon\Carbon::create() . " ]" . "Finish....,Generate Done"; });