Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Becker committed Sep 5, 2024
0 parents commit 37413a2
Show file tree
Hide file tree
Showing 19 changed files with 1,212 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) wjbecker

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Filament Report Builder

### Dynamic Report Builder using Laravel Eloquent Models

---

## Installation

Install package via composer:
```bash
composer require wjbecker/filament-report-builder
```

Publish & migrate migration files
```bash
php artisan vendor:publish --tag=filament-report-builder-migrations
php artisan migrate
```

Filament Export actions
```bash
# Laravel 11 and higher
php artisan make:queue-batches-table
php artisan make:notifications-table

# Laravel 10
php artisan queue:batches-table
php artisan notifications:table

php artisan vendor:publish --tag=filament-actions-migrations
php artisan migrate
```
---

## Panel Configuration

Include this plugin in your panel configuration and enable database notifications:

```php
use Wjbecker\FilamentReportBuilder\FilamentReportBuilderPlugin;

return $panel
// ...
->databaseNotifications()
->plugins([
// ... Other Plugins
FilamentReportBuilderPlugin::make()
])
```
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "wjbecker/filament-report-builder",
"description": "Report builder for Filament using laravel models",
"keywords": [
"filament", "laravel", "reports", "report builder"
],
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Wjbecker\\FilamentReportBuilder\\": "src/"
}
},
"extra" : {
"laravel" : {
"providers" : [
"Wjbecker\\FilamentReportBuilder\\FilamentReportBuilderServiceProvider"
]
}
},
"authors": [
{
"name": "Josh Becker",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php" : "^8.0",
"spatie/laravel-package-tools": "^1.0|^1.13"
}
}
25 changes: 25 additions & 0 deletions database/migrations/create_reports_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

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

return new class extends Migration {
public function up(): void
{
Schema::create('reports', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('description');
$table->boolean('is_built_in')->default(false);
$table->json('data');
$table->softDeletes();
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists('reports');
}
};
3 changes: 3 additions & 0 deletions resources/views/report-resource/pages/view-report.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<x-filament-panels::page>
{{ $this->table }}
</x-filament-panels::page>
68 changes: 68 additions & 0 deletions src/Enums/ReportConditionsEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Wjbecker\FilamentReportBuilder\Enums;

use Filament\Support\Contracts\HasLabel;

enum ReportConditionsEnum: string implements HasLabel
{
case EQUAL = 'equal';
case NOT_EQUAL = 'not_equal';
case BEGINS_WITH = 'begins_with';
case NOT_BEGINS_WITH = 'not_begins_with';
case CONTAINS = 'contains';
case NOT_CONTAINS = 'not_contains';
case ENDS_WITH = 'ends_with';
case NOT_ENDS_WITH = 'not_ends_with';
case IS_EMPTY = 'is_empty';
case IS_NOT_EMPTY = 'is_not_empty';
case IS_NULL = 'is_null';
case IS_NOT_NULL = 'is_not_null';
case GREATER = 'greater';
case GREATER_OR_EQUAL = 'greater_or_equal';
case LESS = 'less';
case LESS_OR_EQUAL = 'less_or_equal';
case BETWEEN = 'between';
case NOT_BETWEEN = 'not_between';
case SPECIAL_DATE = 'special_date';

public function getLabel(): ?string
{
return match ($this) {
self::EQUAL => 'Equal',
self::NOT_EQUAL => 'Not Equal',
self::BEGINS_WITH => 'Begins With',
self::NOT_BEGINS_WITH => 'Doesn\'t Begin With',
self::CONTAINS => 'Contains',
self::NOT_CONTAINS => 'Doesn\'t Contain',
self::ENDS_WITH => 'Ends With',
self::NOT_ENDS_WITH => 'Doesn\'t End With',
self::IS_EMPTY => 'Is Empty',
self::IS_NOT_EMPTY => 'Is Not Empty',
self::IS_NULL => 'Is Null',
self::IS_NOT_NULL => 'Is Not Null',
self::GREATER => 'Greater',
self::GREATER_OR_EQUAL => 'Greater Or Equal',
self::LESS => 'Less',
self::LESS_OR_EQUAL => 'Less Or Equal',
self::BETWEEN => 'Between',
self::NOT_BETWEEN => 'Not Between',
self::SPECIAL_DATE => 'Special Date',
};
}

public function getOperator(): ?string
{
return match ($this) {
self::EQUAL, self::IS_EMPTY => '=',
self::NOT_EQUAL, self::IS_NOT_EMPTY => '!=',
self::BEGINS_WITH, self::CONTAINS, self::ENDS_WITH => 'like',
self::NOT_BEGINS_WITH, self::NOT_CONTAINS, self::NOT_ENDS_WITH => 'not like',
self::GREATER => '>',
self::GREATER_OR_EQUAL => '>=',
self::LESS => '<',
self::LESS_OR_EQUAL => '<=',
default => ''
};
}
}
52 changes: 52 additions & 0 deletions src/Enums/ReportSpecialDateEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Wjbecker\FilamentReportBuilder\Enums;

use Carbon\Carbon;
use Filament\Support\Contracts\HasLabel;

enum ReportSpecialDateEnum: string implements HasLabel
{
case TODAY = 'today';
case AFTER_TODAY = 'after_today';
case BEFORE_TODAY = 'before_today';
case YESTERDAY = 'yesterday';
case TOMORROW = 'tomorrow';
case THIS_MONTH = 'this_month';
case THIS_YEAR = 'this_year';

public function getLabel(): ?string
{
return match ($this) {
self::TODAY => 'Today',
self::AFTER_TODAY => 'After Today',
self::BEFORE_TODAY => 'Before Today',
self::YESTERDAY => 'Yesterday',
self::TOMORROW => 'Tomorrow',
self::THIS_MONTH => 'This Month',
self::THIS_YEAR => 'This Year',
};
}

public function getCarbonDates(): Carbon|array|null
{
return match ($this) {
self::TODAY, self::AFTER_TODAY, self::BEFORE_TODAY => Carbon::now(),
self::YESTERDAY => Carbon::now()->subDay(),
self::TOMORROW => Carbon::now()->addDay(),
self::THIS_MONTH => [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()],
self::THIS_YEAR => [Carbon::now()->startOfYear(), Carbon::now()->endOfYear()],
default => null,
};
}

public function getCondition(): string
{
return match ($this) {
self::TODAY, self::YESTERDAY, self::TOMORROW => 'equal',
self::THIS_MONTH, self::THIS_YEAR => 'between',
self::AFTER_TODAY => '>',
self::BEFORE_TODAY => '<',
};
}
}
58 changes: 58 additions & 0 deletions src/Exports/ReportExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Wjbecker\FilamentReportBuilder\Exports;

use Wjbecker\FilamentReportBuilder\Models\Report;
use Carbon\Carbon;
use Filament\Actions\Exports\ExportColumn;
use Filament\Actions\Exports\Exporter;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Support\Str;

class ReportExporter extends Exporter
{
public static function getColumns(): array
{
$id = Str::of(url()->previous())->between('reports/', '/view')->toInteger();
$report = Report::find($id);

$columns = [];
foreach ($report->data['columns'] as $header) {
$data = json_decode($header['column_data']);
$columns[] = ExportColumn::make($data->item)
->label($header['column_title'])
->formatStateUsing(function ($state): string {
if ($state && gettype($state) === 'object' && class_exists(get_class($state))) {
$class = new \ReflectionClass(get_class($state));

if ($class->isSubclassOf(Carbon::class)) {
return $state->toDateString();
}

if ($class->isEnum()) {
return $state->getLabel();
}

return '';
} elseif(is_null($state)) {
return '';
}

return $state;
});
}

return $columns;
}

public static function getCompletedNotificationBody(Export $export): string
{
$body = 'Your report export has completed and ' . number_format($export->successful_rows) . ' ' . str('row')->plural($export->successful_rows) . ' exported.';

if ($failedRowsCount = $export->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to export.';
}

return $body;
}
}
31 changes: 31 additions & 0 deletions src/FilamentReportBuilderPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Wjbecker\FilamentReportBuilder;

use Filament\Contracts\Plugin;
use Filament\Panel;

class FilamentReportBuilderPlugin implements Plugin
{
public static function make(): static
{
return app(static::class);
}

public function getId(): string
{
return 'filament-report-builder';
}

public function register(Panel $panel): void
{
$panel->resources([
Resources\ReportResource::class
]);
}

public function boot(Panel $panel): void
{
//
}
}
17 changes: 17 additions & 0 deletions src/FilamentReportBuilderServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Wjbecker\FilamentReportBuilder;

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class FilamentReportBuilderServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package
->name('filament-report-builder')
->hasViews()
->hasMigrations(['create_reports_table']);
}
}
23 changes: 23 additions & 0 deletions src/Models/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Wjbecker\FilamentReportBuilder\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Report extends Model
{
use SoftDeletes;

protected $fillable = [
'name',
'is_built_in',
'category',
'description',
'data',
];

protected $casts = [
'data' => 'array',
];
}
Loading

0 comments on commit 37413a2

Please sign in to comment.