Skip to content

Commit

Permalink
Pulling in the generic exporter as a named route
Browse files Browse the repository at this point in the history
  • Loading branch information
coopers98 committed Oct 9, 2015
1 parent b0819dc commit 2548c42
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 34 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
},
"require": {
"php": ">=5.5.9",
"illuminate/http": "5.1.*"
"league/csv": "~7.0"
}
}
4 changes: 2 additions & 2 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
<a href="{{ route( $resource_link . '.create' ) }}" class="btn btn-info btn-sm"><i
class="fa fa-plus"></i>&nbsp;
New {{ str_singular( $table_name ) }}</a>&nbsp;
<div class="btn-group"><a href="{{ link_to_route( $resource_link . '.create' ) }}"
<div class="btn-group dropdown"><a href="#"
data-toggle="dropdown"
class="btn btn-warning btn-sm dropdown-toggle"><i
class="fa fa-wrench"></i>&nbsp;
Tools</a>
<ul class="dropdown-menu pull-right">
<li><a href="{{ route( 'admin.generic.export', [ 't' => $table_name ] ) }}">Export
<li><a href="{{ route( 'coopers98.genericcrud.export', [ 't' => $table_name ] ) }}">Export
to CSV</a></li>

@foreach( $tool_entries as $entry )
Expand Down
40 changes: 9 additions & 31 deletions resources/views/master.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,9 @@
<head>
<title>GenericCRUD</title>

<link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">

<style>
html, body {
height: 100%;
}
body {
margin: 0;
padding: 0;
width: 100%;
display: table;
font-weight: 100;
font-family: 'Lato';
}
.container {
text-align: center;
display: table-cell;
vertical-align: middle;
}
.content {
text-align: center;
display: inline-block;
}
.title {
font-size: 96px;
}
</style>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"
integrity="sha256-MfvZlkHCEqatNoGiOXveE8FIwMzZg4W85qfrfIFBfYc= sha512-dTfge/zgoMYpP7QbHy4gWMEGsbsdZeCXz7irItjcC3sPUFtf0kuFbDz/ixG7ArTxmDjLXDmezHubeNikyKGVyQ=="
crossorigin="anonymous">
</head>
<body>
<div class="container">
Expand All @@ -51,4 +23,10 @@
</div>
</div>
</body>
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"
integrity="sha256-Sk3nkD6mLTMOF0EOpNtsIry+s1CsaqQC1rVLTAy+0yc= sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ=="
crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-hover-dropdown/2.0.10/bootstrap-hover-dropdown.min.js"
crossorigin="anonymous"></script>
</html>
22 changes: 22 additions & 0 deletions src/Exporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace coopers98\GenericCRUD;

use DB;

class Exporter {

public static function export( $table ) {
$data = DB::table( $table )->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' );
}

}
17 changes: 17 additions & 0 deletions src/GenericCRUDServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

namespace coopers98\GenericCRUD;

use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Route;

class GenericCRUDServiceProvider extends ServiceProvider {

Expand All @@ -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() {
Expand Down

0 comments on commit 2548c42

Please sign in to comment.