Skip to content

Commit

Permalink
[TASK] Add initial code from internal project
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoex committed Jan 29, 2021
1 parent b126306 commit fc4d535
Show file tree
Hide file tree
Showing 10 changed files with 393 additions and 22 deletions.
31 changes: 9 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
/vendor/
node_modules/
npm-debug.log
yarn-error.log

# Laravel 4 specific
bootstrap/compiled.php
app/storage/

# Laravel 5 & Lumen specific
public/storage
public/hot

# Laravel 5 & Lumen specific with changed public path
public_html/storage
public_html/hot

storage/*.key
.env
Homestead.yaml
Homestead.json
/.vagrant
/.idea
/vendor
/node_modules
package-lock.json
composer.phar
composer.lock
phpunit.xml
.phpunit.result.cache
.DS_Store
Thumbs.db
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "eur-rsm/database-view-export",
"description": "A Laravel Nova package for exporting configured database views.",
"keywords": [
"laravel",
"nova",
"exports",
"database-views",
"actions"
],
"license": "MIT",
"require": {
"php": ">=7.3.0",
"laravel/nova": "*",
"maatwebsite/excel": "^3.1"
},
"autoload": {
"psr-4": {
"EUR\\RSM\\DatabaseViewExport\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"EUR\\RSM\\DatabaseViewExport\\DatabaseViewExportServiceProvider"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDatabaseViewExportsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('database_view_exports', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('view_name');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('database_view_exports');
}
}
37 changes: 37 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Laravel Nova: Export database views
> Export manually created database views
## Installation
1) Override the Nova layout through creating `resources/views/vendor/nova/layout.blade.php` in your project route.
2) Add `@include('database-view-export::exports-dropdown')` to your desired location
- Example:
```html
<!-- Content -->
<div class="content">
<div class="flex items-center relative shadow h-header bg-white z-20 px-view">
<a v-if="@json(\Laravel\Nova\Nova::name() !== null)" href="{{ \Illuminate\Support\Facades\Config::get('nova.url') }}" class="no-underline dim font-bold text-90 mr-6">
{{ \Laravel\Nova\Nova::name() }}
</a>

@if (count(\Laravel\Nova\Nova::globallySearchableResources(request())) > 0)
<global-search dusk="global-search-component"></global-search>
@endif

{{-- Add dropdown menu for exports package --}}
<div class="ml-auto flex items-center dropdown-right">
@include('database-view-export::exports-dropdown')
</div>

<dropdown class="ml-8 h-9 flex items-center dropdown-right">
@include('nova::partials.user')
</dropdown>
</div>

<div data-testid="content" class="px-view py-view mx-auto">
@yield('content')

@include('nova::partials.footer')
</div>
</div>
```
3) Add the displayed key & the specific name of the view database for it to render in the dropdown.
27 changes: 27 additions & 0 deletions resources/views/exports-dropdown.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php $exportService = app(\EUR\RSM\DatabaseViewExport\Services\BackofficeExportService::class); ?>

<dropdown>
<dropdown-trigger class="h-9 flex items-center">
<span class="text-90">Export</span>
</dropdown-trigger>

<dropdown-menu dusk="exports-list" slot="menu" width="200" direction="rtl">
<ul class="list-reset">
@if($exportService->all()->count() === 0)
<li>
<a href="javascript:void(0)" class="block no-underline text-70 p-3 cursor-default">
No exports available
</a>
</li>
@else
@foreach($exportService->all() as $export)
<li>
<a href="{{ $export->url() }}" class="block no-underline text-90 hover:bg-30 p-3">
{{ $export->label() }}
</a>
</li>
@endforeach
@endif
</ul>
</dropdown-menu>
</dropdown>
40 changes: 40 additions & 0 deletions src/DatabaseViewExportServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace EUR\RSM\DatabaseViewExport;

use EUR\RSM\DatabaseViewExport\Http\Controllers\ExportController;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class DatabaseViewExportServiceProvider extends ServiceProvider
{
/**
* @return void
*/
public function boot(): void
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'database-view-export');
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');

$this->app->booted(function (): void {
$this->routes();
});
}

/**
* @return void
*/
protected function routes(): void
{
if ($this->app->routesAreCached()) {
return;
}

Route::middleware('nova')
->prefix('nova-vendor/database-view-export')
->group(function (): void {
Route::get('/export/{key}', [ExportController::class, 'export'])
->name('database-view-export.export');
});
}
}
109 changes: 109 additions & 0 deletions src/Exports/ViewExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace EUR\RSM\DatabaseViewExport\Exports;

use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Excel;

/**
* Represents a database view that can be exported to Excel.
*/
final class ViewExport implements FromCollection, WithHeadings, WithStrictNullComparison
{
use Exportable;

/** @var string */
private $label;

/**
* Name of the view (table) to base the export on.
*
* @var string
*/
private $viewName;

/**
* @see \Maatwebsite\Excel\Excel::XLSX
* @see \Maatwebsite\Excel\Excel::CSV
* @var string
*/
private $exportType = Excel::XLSX;

/**
* @param string $label
* @param string $viewName
*/
public function __construct(string $label, string $viewName)
{
$this->label = $label;
$this->viewName = $viewName;
}

/**
* Key (unique) that identifies the export.
*
* @return string
*/
public function key(): string
{
return Str::snake($this->label(), '-');
}

/**
* Human-friendly label for this export, displayed in the dropdown menu.
*
* @return string
*/
public function label(): string
{
return $this->label;
}

/**
* URL to access this export, used in the dropdown menu.
*
* @return string
*/
public function url(): string
{
return route('database-view-export.export', $this->key());
}

/**
* Collect the data that need to be exported.
*
* @return \Illuminate\Support\Collection
*/
public function collection(): Collection
{
return DB::table($this->viewName)->select()->get();
}

/**
* Get the headings (columns) that are inserted as the first row.
*
* @return array
*/
public function headings(): array
{
return DB::getSchemaBuilder()->getColumnListing($this->viewName);
}

/**
* Determine the filename for the exported file.
*
* @param string $extension
* @return string
*/
public function filename(): string
{
return $this->key() . '_' . Carbon::now()->toDateTimeString() . '.' . strtolower($this->exportType);
}
}
28 changes: 28 additions & 0 deletions src/Http/Controllers/ExportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace EUR\RSM\DatabaseViewExport\Http\Controllers;

use EUR\RSM\DatabaseViewExport\Services\BackofficeExportService;
use Symfony\Component\HttpFoundation\Response;

final class ExportController
{
/**
* @param string $exportKey
* @param \EUR\RSM\DatabaseViewExport\Services\BackofficeExportService $exportService
* @return \Symfony\Component\HttpFoundation\Response
*/
public function export(string $exportKey, BackofficeExportService $exportService): Response
{
$export = $exportService->findByKey($exportKey);

return rescue(
function () use ($export, $exportService): Response {
return $exportService->runExport($export);
},
function (): void {
abort(500, 'Unable to run the requested export. An exception was thrown (see logs).');
}
);
}
}
23 changes: 23 additions & 0 deletions src/Models/Export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace EUR\RSM\DatabaseViewExport\Models;

use Illuminate\Database\Eloquent\Model;

final class Export extends Model
{
/** @var string */
protected $table = 'database_view_exports';

/** @var bool */
public $incrementing = false;

/** @var bool */
public $timestamps = false;

/** @var string */
protected $primaryKey = 'name';

/** @var string */
protected $keyType = 'string';
}
Loading

0 comments on commit fc4d535

Please sign in to comment.