Skip to content

Commit

Permalink
Merge pull request #98 from cheesegrits/v3
Browse files Browse the repository at this point in the history
V3 fixes
  • Loading branch information
pxlrbt authored Aug 7, 2023
2 parents 771952c + 20d1129 commit 98938cc
Show file tree
Hide file tree
Showing 26 changed files with 84 additions and 55 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
}
],
"require": {
"php": "^8.0",
"filament/filament": "^2.13.24|^3.0",
"php": "^8.1",
"filament/filament": "^3.0.0-stable",
"maatwebsite/excel": "^3.1",
"anourvalar/eloquent-serialize": "^1.2"
},
Expand All @@ -39,5 +39,6 @@
},
"scripts": {
"pint": "vendor/bin/pint"
}
},
"prefer-stable": true
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Actions/Concerns/ExportableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function setUp(): void
$this->modalWidth('md');

$this->label(__('filament-excel::actions.label'));
$this->icon('heroicon-o-download');
$this->icon('heroicon-o-arrow-down-tray');
$this->action(Closure::fromCallable([$this, 'handleExport']));

$this->exports = collect([ExcelExport::make('export')->fromTable()]);
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/Pages/ExportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function setUp(): void
$this->parentSetUp();

$this->button();
$this->icon('heroicon-o-download');
$this->icon('heroicon-o-arrow-down-tray');

$this->exports = collect([
ExcelExport::make()->fromForm(),
Expand Down
2 changes: 2 additions & 0 deletions src/Actions/Tables/ExportBulkAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static function getDefaultName(): ?string

protected function setUp(): void
{
parent::setUp();

$this->parentSetUp();

$this->deselectRecordsAfterCompletion();
Expand Down
12 changes: 7 additions & 5 deletions src/Columns/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,24 @@ public function format(Closure|string $format): static

public function tableColumn(TableColumn $tableColumn): static
{
$clone = clone($tableColumn);

// Try to remove all closures
foreach ((new ReflectionClass($tableColumn))->getProperties() as $property) {
foreach ((new ReflectionClass($clone))->getProperties() as $property) {
$property->setAccessible(true);
$type = (string) $property->getType();

if (strpos($type, 'Closure') !== false) {
if (strpos($type, 'null') !== false || strpos($type, '?') !== false) {
$property->setValue($tableColumn, null);
$property->setValue($clone, null);
}
}
}

// $tableColumn->getStateUsing(null);
// $tableColumn->formatStateUsing(null);
// Remove other unsafe properties
$clone->table(null);

$this->tableColumn = $tableColumn;
$this->tableColumn = $clone;

return $this;
}
Expand Down
Empty file removed src/Concerns/Except.php
Empty file.
14 changes: 8 additions & 6 deletions src/Exports/Concerns/WithColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Closure;
use Filament\Forms\Components\Field;
use Filament\Forms\Components\Repeater;
use Filament\Resources\Form;
use Filament\Forms\Form;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Contracts\HasTable;
Expand Down Expand Up @@ -91,8 +91,8 @@ public function fromModel(): static

protected function createFieldMappingFromForm(): Collection
{
$form = $this->getResourceClass()::form(new Form());
$components = collect($form->getSchema());
$form = $this->getResourceClass()::form(new Form($this->getLivewire()));
$components = collect($form->getComponents());
$extracted = collect();

while (($component = $components->shift()) !== null) {
Expand Down Expand Up @@ -129,27 +129,29 @@ protected function createFieldMappingFromTable(): Collection
$livewire = $this->getLivewire();

if ($livewire instanceof HasTable) {
$columns = collect(invade($this->getLivewire())->getTableColumns());
$columns = collect($livewire->getTable()->getColumns());
} else {
$table = $this->getResourceClass()::table(new Table());
$columns = collect($table->getColumns());
}

return $columns
->when(
$livewire->hasToggleableTableColumns(),
$livewire->getTable()->hasToggleableColumns(),
fn ($collection) => $collection->reject(
fn (Tables\Columns\Column $column) => $livewire->isTableColumnToggledHidden($column->getName())
)
)
->mapWithKeys(function (Tables\Columns\Column $column) {
$clonedCol = clone $column;

// Invade for protected properties
$invadedColumn = invade($clonedCol);

$exportColumn = Column::make($column->getName())
->heading($column->getLabel())
->getStateUsing($invadedColumn->getStateUsing)
->tableColumn($column);
->tableColumn($clonedCol);

rescue(fn () => $exportColumn->formatStateUsing($invadedColumn->formatStateUsing), report: false);

Expand Down
64 changes: 42 additions & 22 deletions src/Exports/Concerns/WithMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace pxlrbt\FilamentExcel\Exports\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use UnitEnum;

Expand All @@ -19,7 +20,7 @@ public function getMapping($row): Collection
// If user didn't specify a custom except array, use the hidden columns.
// User can override this by passing an empty array ->except([])
// When user specifies with only(), ignore if the column is hidden or not.
if ($except === null && (! is_array($only) || count($only) === 0)) {
if ($except === null && (!is_array($only) || count($only) === 0)) {
$except = $row->getHidden();
}
}
Expand All @@ -36,7 +37,7 @@ public function getMapping($row): Collection
}

/**
* @param Model|mixed $row
* @param Model|mixed $row
*/
public function map($record): array
{
Expand All @@ -49,35 +50,54 @@ public function map($record): array

foreach ($columns as $column) {
$key = $column->getName();
$state = data_get($record, $key);

$state = $column->getStateUsing === null
if ($this->columnsSource === 'table') {
$column->tableColumn->record($record);
$state = $column->tableColumn->getStateFromRecord();
} else {
$state = data_get($record, $key);
}

$arrayState = $column->getStateUsing === null
? $state
: $this->evaluate($column->getStateUsing->getClosure(), [
'column' => $column->tableColumn,
'column' => $column->tableColumn,
'livewire' => $this->getLivewire(),
'record' => $record,
'state' => $state,
'record' => $record,
]);

$state = $column->formatStateUsing === null
? $state
: $this->evaluate($column->formatStateUsing->getClosure(), [
'column' => $column->tableColumn,
'livewire' => $this->getLivewire(),
'record' => $record,
'state' => $state,
]);
if ($this->columnsSource === 'table' && is_string($arrayState) && ($separator = $column->tableColumn->getSeparator())) {
$arrayState = explode($separator, $arrayState);
$arrayState = (count($arrayState) === 1 && blank($arrayState[0])) ?
[] :
$arrayState;
}

if (is_object($state)) {
$state = match (true) {
method_exists($state, 'toString') => $state->toString(),
method_exists($state, '__toString') => $state->__toString(),
function_exists('enum_exists') && $state instanceof UnitEnum => $state->value,
};
$arrayState = Arr::wrap($arrayState);
$formattedArrayState = [];

foreach ($arrayState as $state) {
$state = $column->formatStateUsing === null
? $state
: $this->evaluate($column->formatStateUsing->getClosure(), [
'column' => $column->tableColumn,
'livewire' => $this->getLivewire(),
'record' => $record,
'state' => $state,
]);

if (is_object($state)) {
$state = match (true) {
method_exists($state, 'toString') => $state->toString(),
method_exists($state, '__toString') => $state->__toString(),
function_exists('enum_exists') && $state instanceof UnitEnum => $state->value,
};
}

$formattedArrayState[] = $state;
}

$result[$key] = $state;
$result[$key] = implode("\n", $formattedArrayState);
}

return $result;
Expand Down
4 changes: 2 additions & 2 deletions src/Exports/ExcelExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function getModelClass(): ?string
if (($resource = $this->getResourceClass()) !== null) {
$model = $resource::getModel();
} elseif (($livewire = $this->getLivewire()) instanceof HasTable) {
$model = $livewire->getTableModel();
$model = $livewire->getTable()->getModel();
}

return $this->model ??= $model;
Expand Down Expand Up @@ -220,7 +220,7 @@ public function export()
->body(__('filament-excel::notifications.queued.body'))
->success()
->seconds(5)
->icon('heroicon-o-inbox-in')
->icon('heroicon-o-arrow-down-tray')
->send();
}

Expand Down
32 changes: 17 additions & 15 deletions src/FilamentExcelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

namespace pxlrbt\FilamentExcel;

use Closure;
use Filament\Facades\Filament;
use Filament\Notifications\Actions\Action;
use Filament\Notifications\Notification;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Illuminate\Support\Str;
use pxlrbt\FilamentExcel\Commands\PruneExportsCommand;
use pxlrbt\FilamentExcel\Events\ExportFinishedEvent;

class FilamentExcelServiceProvider extends ServiceProvider
class FilamentExcelServiceProvider extends PackageServiceProvider
{
public function register()
public function register(): void
{
config()->set('filesystems.disks.filament-excel', [
'driver' => 'local',
Expand All @@ -28,24 +28,26 @@ public function register()
parent::register();
}

public function boot()
public function configurePackage(Package $package): void
{
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');

$this->loadTranslationsFrom(__DIR__.'/../lang', 'filament-excel');
$package->name('filament-excel')
->hasCommands([PruneExportsCommand::class])
->hasRoutes(['web'])
->hasTranslations();
}

$this->commands([PruneExportsCommand::class]);
public function bootingPackage()
{
Filament::serving($this->sendExportFinishedNotification(...));

$this->callAfterResolving(Schedule::class, function (Schedule $schedule) {
$schedule->command(PruneExportsCommand::class)->daily();
});

Event::listen(ExportFinishedEvent::class, [$this, 'cacheExportFinishedNotification']);

Filament::serving(Closure::fromCallable([$this, 'sendExportFinishedNotification']));
}

public function sendExportFinishedNotification()
public function sendExportFinishedNotification(): void
{
$exports = cache()->pull($this->getNotificationCacheKey(auth()->id()));

Expand All @@ -68,7 +70,7 @@ public function sendExportFinishedNotification()
->title(__('filament-excel::notifications.download_ready.title'))
->body(__('filament-excel::notifications.download_ready.body'))
->success()
->icon('heroicon-o-download')
->icon('heroicon-o-arrow-down-tray')
->actions([
Action::make('download')
->label(__('filament-excel::notifications.download_ready.download'))
Expand All @@ -81,7 +83,7 @@ public function sendExportFinishedNotification()
}
}

public function cacheExportFinishedNotification(ExportFinishedEvent $event)
public function cacheExportFinishedNotification(ExportFinishedEvent $event): void
{
if ($event->userId === null) {
return;
Expand All @@ -99,7 +101,7 @@ public function cacheExportFinishedNotification(ExportFinishedEvent $event)
cache()->put($key, $exports);
}

protected function getNotificationCacheKey($userId)
protected function getNotificationCacheKey($userId): string
{
return 'filament-excel:exports:'.$userId;
}
Expand Down

0 comments on commit 98938cc

Please sign in to comment.