Skip to content

Commit

Permalink
Merge pull request #1 from aryehraber/master
Browse files Browse the repository at this point in the history
Download CSV file directly without temporarily saving in storage
  • Loading branch information
goellner authored Apr 22, 2018
2 parents b40d450 + a457397 commit 6af364e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 48 deletions.
57 changes: 22 additions & 35 deletions Exportit/ExportitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Statamic\Addons\Exportit;

use Statamic\Extend\Controller;
use Statamic\API\Collection;
use Statamic\API\Fieldset;
use Statamic\API\Entry;
use SplTempFileObject;
use Carbon\Carbon;
use League\Csv\Writer;
use Statamic\API\File;
use SplTempFileObject;
use Statamic\API\Entry;
use Statamic\API\Fieldset;
use Statamic\API\Collection;
use Illuminate\Http\Response;
use Statamic\Extend\Controller;

class ExportitController extends Controller
{
Expand All @@ -20,36 +20,25 @@ class ExportitController extends Controller
*
* @return mixed
*/
public function index() {


public function index()
{
return $this->view('index');
}

public function exportdata() {
$handle = $_POST['selectedcollection'];
public function exportdata()
{
$handle = request('selectedcollection');
$filename = slugify("{$handle}-".Carbon::now()->timestamp);

$this->writer = Writer::createFromFileObject(new SplTempFileObject);

$this->insertHeaders($handle);
$this->insertData($handle);

// TODO: Don't save to storage, start immediate download instead.
$document = File::disk('storage');
$document->put('exportit.csv', $this->writer);

$data = [
'csv' => $this->writer
];
return $this->view('exportdata');


}

public function download() {
$document = File::disk('storage');
$file = $document->get('exportit.csv');
return response($file)->header('Content-Type', 'text/csv')->header('Content-Disposition', 'attachment; filename="exportit.csv"');
return response((string) $this->writer, 200, [
'Content-type' => 'text/csv',
'Content-Disposition' => "attachment; filename={$filename}.csv",
]);
}

// Creates and inserts CSV Header based on collection fieldset
Expand All @@ -63,7 +52,7 @@ private function insertHeaders($handle)
$header_data = array_keys($fieldset_content);

// Adding title field, since it is not defined in fieldset
array_unshift($header_data, 'title' );
array_unshift($header_data, 'title');

$this->csv_header = $header_data;

Expand All @@ -78,30 +67,28 @@ private function insertData($handle)
$collectiondata = Entry::whereCollection($handle);

$data = collect($collectiondata)->map(function ($entry) use ($header_data) {

$ret = array();
$ret = [];
$entry = $entry->toArray();

foreach ($header_data as $key => $value) {
if(array_key_exists($value, $entry)) {
if (array_key_exists($value, $entry)) {
// convert entry array to pipe delimited string
$entry_value = '';
if(is_array($entry[$value])) {
if (is_array($entry[$value])) {
$entry_value = implode('|', $entry[$value]);
} else {
$entry_value = $entry[$value];
}

$ret[] = $entry_value;
}
else {
} else {
$ret[] = '';
}
}

return $ret;
})->toArray();

$this->writer->insertAll($data);

}
}
2 changes: 1 addition & 1 deletion Exportit/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: exportit
version: "1.0"
version: 1.1
description: Exports Data from a Collection to CSV
developer: Stefan Göllner
developer_url: https://www.goellner.io/
7 changes: 0 additions & 7 deletions Exportit/resources/views/exportdata.blade.php

This file was deleted.

2 changes: 1 addition & 1 deletion Exportit/resources/views/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@extends('layout')

@section('content')
<form action="{{ route('exportit.exportdata') }}" method="post">
<form method="POST">
{{ csrf_field() }}

<div class="card flush flat-bottom">
Expand Down
5 changes: 1 addition & 4 deletions Exportit/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ routes:
/:
as: addons.exportit
uses: index
post@exportdata:
post@/:
uses: exportdata
as: exportit.exportdata
download:
uses: download
as: exportit.download

0 comments on commit 6af364e

Please sign in to comment.