generated from filamentphp/plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds ability to download sample excel
- Loading branch information
1 parent
da759f0
commit 048155a
Showing
9 changed files
with
284 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace EightyNine\ExcelImport\Concerns; | ||
|
||
use Filament\Tables\Table; | ||
|
||
trait BelongsToTable | ||
{ | ||
protected Table $table; | ||
|
||
public function table(Table $table): static | ||
{ | ||
$this->table = $table; | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace EightyNine\ExcelImport\Concerns; | ||
|
||
use Closure; | ||
use EightyNine\ExcelImport\DefaultImport; | ||
use Maatwebsite\Excel\Facades\Excel; | ||
|
||
trait HasExcelImportAction | ||
{ | ||
use HasUploadForm, | ||
HasFormActionHooks, | ||
HasCustomCollectionMethod, | ||
CanCustomiseActionSetup, | ||
BelongsToTable, | ||
HasSampleExcelFile; | ||
|
||
protected string $importClass = DefaultImport::class; | ||
|
||
protected array $importClassAttributes = []; | ||
|
||
public function use(string $class = null, ...$attributes): static | ||
{ | ||
$this->importClass = $class ?: DefaultImport::class; | ||
$this->importClassAttributes = $attributes; | ||
|
||
return $this; | ||
} | ||
|
||
public static function getDefaultName(): ?string | ||
{ | ||
return 'import'; | ||
} | ||
|
||
public function action(Closure | string | null $action): static | ||
{ | ||
if ($action !== 'importData') { | ||
throw new \Exception('You\'re unable to override the action for this plugin'); | ||
} | ||
|
||
$this->action = $this->importData(); | ||
|
||
return $this; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->icon('heroicon-o-arrow-down-tray') | ||
->color('warning') | ||
->form(fn () => $this->getDefaultForm()) | ||
->modalIcon('heroicon-o-arrow-down-tray') | ||
->color('success') | ||
->modalWidth('md') | ||
->modalAlignment('center') | ||
->modalHeading(fn ($livewire) => __('Import Excel')) | ||
->modalDescription(__('Import data into database from excel file')) | ||
->modalFooterActionsAlignment('right') | ||
->closeModalByClickingAway(false) | ||
->action('importData'); | ||
} | ||
|
||
private function importData(): Closure | ||
{ | ||
return function (array $data, $livewire): bool { | ||
if (is_callable($this->beforeImportClosure)) { | ||
call_user_func($this->beforeImportClosure, $data, $livewire, $this); | ||
} | ||
$importObject = new $this->importClass( | ||
method_exists($livewire, 'getModel') ? $livewire->getModel() : null, | ||
$this->importClassAttributes, | ||
$this->additionalData | ||
); | ||
|
||
if(method_exists($importObject, 'setAdditionalData') && isset($this->additionalData)) { | ||
$importObject->setAdditionalData($this->additionalData); | ||
} | ||
|
||
if(method_exists($importObject, 'setCustomImportData') && isset($this->customImportData)) { | ||
$importObject->setCustomImportData($this->customImportData); | ||
} | ||
|
||
if(method_exists($importObject, 'setCollectionMethod') && isset($this->collectionMethod)) { | ||
$importObject->setCollectionMethod($this->collectionMethod); | ||
} | ||
|
||
if(method_exists($importObject, 'setAfterValidationMutator' && | ||
(isset($this->afterValidationMutator) || $this->shouldRetainBeforeValidationMutation) | ||
)){ | ||
$afterValidationMutator = $this->shouldRetainBeforeValidationMutation ? | ||
$this->beforeValidationMutator : | ||
$this->afterValidationMutator; | ||
$importObject->setAfterValidationMutator($afterValidationMutator); | ||
} | ||
|
||
Excel::import($importObject, $data['upload']); | ||
|
||
if (is_callable($this->afterImportClosure)) { | ||
call_user_func($this->afterImportClosure, $data, $livewire); | ||
} | ||
return true; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace EightyNine\ExcelImport\Concerns; | ||
|
||
use Closure; | ||
use EightyNine\ExcelImport\SampleExcelExport; | ||
use Filament\Forms\Components\Actions\Action; | ||
use Maatwebsite\Excel\Facades\Excel; | ||
|
||
trait HasSampleExcelFile | ||
{ | ||
protected string $sampleFileName = 'sample.xlsx'; | ||
protected $defaultExportClass = SampleExcelExport::class; | ||
protected ?array $sampleData = null; | ||
protected ?string $sampleButtonLabel = null; | ||
protected ?Closure $actionCustomisationClosure = null; | ||
|
||
public function downloadSampleExcelFile() | ||
{ | ||
return Excel::download( | ||
new $this->defaultExportClass($this->sampleData), | ||
$this->sampleFileName | ||
); | ||
} | ||
|
||
public function setSampleFileName(string $name) | ||
{ | ||
$this->sampleFileName = $name; | ||
} | ||
|
||
public function setDefaultExportClass(string $class) | ||
{ | ||
$this->defaultExportClass = $class; | ||
} | ||
|
||
public function setSampleData(array $data) | ||
{ | ||
if (count($data) > 0 && isset($data[0]) && is_array($data[0])) { | ||
$this->sampleData = $data; | ||
return; | ||
} else { | ||
$this->sampleData = [$data]; | ||
return; | ||
} | ||
} | ||
|
||
public function setSampleButtonLabel(?string $label) | ||
{ | ||
$this->sampleButtonLabel = $label; | ||
} | ||
|
||
protected function getSampleExcelButton() | ||
{ | ||
$action = Action::make($this->sampleButtonLabel ?: __('excel-import::excel-import.download_sample_excel_file')) | ||
->action(fn() => $this->downloadSampleExcelFile()); | ||
if (isset($this->actionCustomisationClosure)) { | ||
return call_user_func($this->actionCustomisationClosure, $action); | ||
} | ||
return $action; | ||
} | ||
|
||
public function setActionCustomisationClosure(?Closure $customiseActionUsing){ | ||
$this->actionCustomisationClosure = $customiseActionUsing; | ||
} | ||
|
||
public function sampleExcel( | ||
array $sampleData, | ||
?string $fileName = null, | ||
?string $exportClass = null, | ||
?string $sampleButtonLabel = null, | ||
?Closure $customiseActionUsing = null | ||
): static { | ||
$this->setSampleData($sampleData); | ||
$this->setSampleFileName($fileName ?: $this->sampleFileName); | ||
$this->setDefaultExportClass($exportClass ?: $this->defaultExportClass); | ||
$this->setSampleButtonLabel($sampleButtonLabel ?: $this->sampleButtonLabel); | ||
$this->setActionCustomisationClosure($customiseActionUsing); | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.