From 2548c42a7d692800f56a78dfa4952ace6ea50c86 Mon Sep 17 00:00:00 2001 From: Cooper Sellers Date: Fri, 9 Oct 2015 12:00:10 -0500 Subject: [PATCH] Pulling in the generic exporter as a named route --- README.md | 9 +++++++ composer.json | 2 +- resources/views/index.blade.php | 4 +-- resources/views/master.blade.php | 40 +++++++----------------------- src/Exporter.php | 22 ++++++++++++++++ src/GenericCRUDServiceProvider.php | 17 +++++++++++++ 6 files changed, 60 insertions(+), 34 deletions(-) create mode 100644 src/Exporter.php diff --git a/README.md b/README.md index b3d0c07..231eae2 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,11 @@ In your app config, add the `GenericCRUD` to the providers array. Usage ----- +Be warned that this isn't really a turn key package right now. You'll likely need to dive into the source a little to +understand how it is all going. Fear not, it is pretty straight forward and shouldn't be too hard to follow. + + + This is a trait so use it on a Controller that you want to have the Generic CRUD functionality on. In your routes.php file, add a resource @@ -104,6 +109,10 @@ There are also authorization checks called for each action. } ``` +---- + +This package also includes a generic table exporter that uses the League/CSV package to export the given +index view as a csv file. ### License diff --git a/composer.json b/composer.json index 0afe6e6..9e0a527 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,6 @@ }, "require": { "php": ">=5.5.9", - "illuminate/http": "5.1.*" + "league/csv": "~7.0" } } diff --git a/resources/views/index.blade.php b/resources/views/index.blade.php index 1825236..630a319 100644 --- a/resources/views/index.blade.php +++ b/resources/views/index.blade.php @@ -17,13 +17,13 @@   New {{ str_singular( $table_name ) }}  -
  Tools
+ + + \ No newline at end of file diff --git a/src/Exporter.php b/src/Exporter.php new file mode 100644 index 0000000..7960b72 --- /dev/null +++ b/src/Exporter.php @@ -0,0 +1,22 @@ +get(); + + $csv = \League\Csv\Writer::createFromFileObject( new \SplTempFileObject() ); + $csv->insertOne( \Schema::getColumnListing( $table ) ); + + foreach ( $data as $row ) { + $csv->insertOne( (array) $row ); + } + + $csv->output( $table . '-' . date( 'YmdHi' ) . '.csv' ); + } + +} \ No newline at end of file diff --git a/src/GenericCRUDServiceProvider.php b/src/GenericCRUDServiceProvider.php index 7b1b146..021b1c6 100644 --- a/src/GenericCRUDServiceProvider.php +++ b/src/GenericCRUDServiceProvider.php @@ -10,7 +10,9 @@ namespace coopers98\GenericCRUD; +use Illuminate\Http\Request; use Illuminate\Support\ServiceProvider; +use Route; class GenericCRUDServiceProvider extends ServiceProvider { @@ -29,6 +31,21 @@ class GenericCRUDServiceProvider extends ServiceProvider { public function boot() { $viewPath = __DIR__ . '/../resources/views'; $this->loadViewsFrom( $viewPath, 'genericcrud' ); + + if ( ! $this->app->routesAreCached() ) { + Route::get( '/coopers98/genericcrud/export', + [ + 'as' => 'coopers98.genericcrud.export', + function ( Request $request ) { + $table = $request->input( 't' ); + \coopers98\GenericCRUD\Exporter::export( $table ); + } + ] ); + + +// $router->get( '/genericImport', [ 'as' => 'admin.generic.import', 'uses' => 'Admin\DashboardController@genericImport' ] ); + + } } public function register() {