Skip to content

Commit 49ef348

Browse files
authored
Merge pull request #39 from adereksisusanto/new-feature
add new feature (sample file excel) and fix styling
2 parents d34f3de + c66cbb1 commit 49ef348

15 files changed

+123
-74
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ protected function getHeaderActions(): array
284284
->icon('heroicon-m-clipboard')
285285
->requiresConfirmation(),
286286
),
287+
// OR
288+
\EightyNine\ExcelImport\ExcelImportAction::make()
289+
->sampleFileExcel(
290+
url: url('excel/users.xlsx'),
291+
sampleButtonLabel: 'Download Sample',
292+
customiseActionUsing: fn(Action $action) => $action->color('secondary')
293+
->icon('heroicon-m-clipboard')
294+
->requiresConfirmation(),
295+
),
287296
Actions\CreateAction::make(),
288297
];
289298
}

config/excel-import.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
/**
77
* File upload path
8-
*
9-
* Customise the path where the file will be uploaded to,
8+
*
9+
* Customise the path where the file will be uploaded to,
1010
* if left empty, config('filesystems.default') will be used
1111
*/
1212
'upload_disk' => null,

src/Concerns/BelongsToTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ public function table(Table $table): static
1414

1515
return $this;
1616
}
17-
}
17+
}

src/Concerns/CanCustomiseActionSetup.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
trait CanCustomiseActionSetup
66
{
7-
87
protected array $acceptedFileTypes = ['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'text/csv'];
98

109
protected bool $storeFiles = false;

src/Concerns/HasCustomCollectionMethod.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ trait HasCustomCollectionMethod
1111
public function processCollectionUsing(Closure $closure): static
1212
{
1313
$this->collectionMethod = $closure;
14+
1415
return $this;
1516
}
1617
}

src/Concerns/HasExcelImportAction.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
trait HasExcelImportAction
1010
{
11-
use HasUploadForm,
12-
HasFormActionHooks,
13-
HasCustomCollectionMethod,
14-
CanCustomiseActionSetup,
15-
BelongsToTable,
16-
HasSampleExcelFile;
11+
use BelongsToTable;
12+
use CanCustomiseActionSetup;
13+
use HasCustomCollectionMethod;
14+
use HasFormActionHooks;
15+
use HasSampleExcelFile;
16+
use HasUploadForm;
1717

1818
protected array $importClassAttributes = [];
1919

20-
public function use(string $class = null, ...$attributes): static
20+
public function use(?string $class = null, ...$attributes): static
2121
{
2222
$this->importClass = $class ?: DefaultImport::class;
2323
$this->importClassAttributes = $attributes;
@@ -71,22 +71,24 @@ private function importData(): Closure
7171
$this->additionalData
7272
);
7373

74-
if(method_exists($importObject, 'setAdditionalData') && isset($this->additionalData)) {
74+
if (method_exists($importObject, 'setAdditionalData') && isset($this->additionalData)) {
7575
$importObject->setAdditionalData($this->additionalData);
7676
}
7777

78-
if(method_exists($importObject, 'setCustomImportData') && isset($this->customImportData)) {
78+
if (method_exists($importObject, 'setCustomImportData') && isset($this->customImportData)) {
7979
$importObject->setCustomImportData($this->customImportData);
8080
}
8181

82-
if(method_exists($importObject, 'setCollectionMethod') && isset($this->collectionMethod)) {
82+
if (method_exists($importObject, 'setCollectionMethod') && isset($this->collectionMethod)) {
8383
$importObject->setCollectionMethod($this->collectionMethod);
8484
}
8585

86-
if(method_exists($importObject, 'setAfterValidationMutator' &&
86+
if (method_exists(
87+
$importObject,
88+
'setAfterValidationMutator' &&
8789
(isset($this->afterValidationMutator) || $this->shouldRetainBeforeValidationMutation)
88-
)){
89-
$afterValidationMutator = $this->shouldRetainBeforeValidationMutation ?
90+
)) {
91+
$afterValidationMutator = $this->shouldRetainBeforeValidationMutation ?
9092
$this->beforeValidationMutator :
9193
$this->afterValidationMutator;
9294
$importObject->setAfterValidationMutator($afterValidationMutator);
@@ -97,6 +99,7 @@ private function importData(): Closure
9799
if (is_callable($this->afterImportClosure)) {
98100
call_user_func($this->afterImportClosure, $data, $livewire);
99101
}
102+
100103
return true;
101104
};
102105
}

src/Concerns/HasSampleExcelFile.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010
trait HasSampleExcelFile
1111
{
1212
protected string $sampleFileName = 'sample.xlsx';
13+
1314
protected $defaultExportClass = SampleExcelExport::class;
14-
protected ?array $sampleData = null;
15+
16+
protected string | array | null $sampleData = null;
17+
1518
protected ?string $sampleButtonLabel = null;
19+
1620
protected ?Closure $actionCustomisationClosure = null;
1721

1822
public function downloadSampleExcelFile()
@@ -33,15 +37,24 @@ public function setDefaultExportClass(string $class)
3337
$this->defaultExportClass = $class;
3438
}
3539

36-
public function setSampleData(array $data)
40+
public function setSampleData(string | array $data)
3741
{
38-
if (count($data) > 0 && isset($data[0]) && is_array($data[0])) {
42+
if (is_string($data)) {
3943
$this->sampleData = $data;
44+
4045
return;
4146
} else {
42-
$this->sampleData = [$data];
43-
return;
47+
if (count($data) > 0 && isset($data[0]) && is_array($data[0])) {
48+
$this->sampleData = $data;
49+
50+
return;
51+
} else {
52+
$this->sampleData = [$data];
53+
54+
return;
55+
}
4456
}
57+
4558
}
4659

4760
public function setSampleButtonLabel(?string $label)
@@ -51,15 +64,21 @@ public function setSampleButtonLabel(?string $label)
5164

5265
protected function getSampleExcelButton()
5366
{
54-
$action = Action::make($this->sampleButtonLabel ?: __('excel-import::excel-import.download_sample_excel_file'))
55-
->action(fn() => $this->downloadSampleExcelFile());
67+
$action = Action::make($this->sampleButtonLabel ?: __('excel-import::excel-import.download_sample_excel_file'));
68+
if (is_string($this->sampleData)) {
69+
$action->url($this->sampleData);
70+
} else {
71+
$action->action(fn () => $this->downloadSampleExcelFile());
72+
}
5673
if (isset($this->actionCustomisationClosure)) {
5774
return call_user_func($this->actionCustomisationClosure, $action);
5875
}
76+
5977
return $action;
6078
}
6179

62-
public function setActionCustomisationClosure(?Closure $customiseActionUsing){
80+
public function setActionCustomisationClosure(?Closure $customiseActionUsing)
81+
{
6382
$this->actionCustomisationClosure = $customiseActionUsing;
6483
}
6584

@@ -75,6 +94,19 @@ public function sampleExcel(
7594
$this->setDefaultExportClass($exportClass ?: $this->defaultExportClass);
7695
$this->setSampleButtonLabel($sampleButtonLabel ?: $this->sampleButtonLabel);
7796
$this->setActionCustomisationClosure($customiseActionUsing);
97+
98+
return $this;
99+
}
100+
101+
public function sampleFileExcel(
102+
string $url,
103+
?string $sampleButtonLabel = null,
104+
?Closure $customiseActionUsing = null
105+
): static {
106+
$this->setSampleData($url);
107+
$this->setSampleButtonLabel($sampleButtonLabel ?: $this->sampleButtonLabel);
108+
$this->setActionCustomisationClosure($customiseActionUsing);
109+
78110
return $this;
79111
}
80112
}

src/Concerns/HasUploadForm.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Closure;
66
use EightyNine\ExcelImport\ValidationImport;
7-
use Filament\Forms\Components\Actions\Action;
87
use Filament\Forms\Components\Field;
98
use Filament\Forms\Components\FileUpload;
109
use Maatwebsite\Excel\Facades\Excel;
@@ -35,12 +34,14 @@ public function mutateBeforeValidationUsing(Closure $closure, $shouldRetainBefor
3534
{
3635
$this->beforeValidationMutator = $closure;
3736
$this->shouldRetainBeforeValidationMutation = $shouldRetainBeforeValidationMutation;
37+
3838
return $this;
3939
}
4040

4141
public function mutateAfterValidationUsing(Closure $closure): static
4242
{
4343
$this->afterValidationMutator = $closure;
44+
4445
return $this;
4546
}
4647

@@ -55,6 +56,7 @@ public function validateUsing(array $rules): static
5556
public function uploadField(Closure $closure): static
5657
{
5758
$this->uploadField = $closure;
59+
5860
return $this;
5961
}
6062

@@ -65,6 +67,7 @@ private function addField(Field $field, bool $isAfterUpload = false)
6567
} else {
6668
$this->beforeUploadFieldFormFields[] = $field;
6769
}
70+
6871
return $this;
6972
}
7073

@@ -73,6 +76,7 @@ public function beforeUploadField(array $fields): static
7376
foreach ($fields as $field) {
7477
$this->addField($field, false);
7578
}
79+
7680
return $this;
7781
}
7882

@@ -81,6 +85,7 @@ public function afterUploadField(array $fields): static
8185
foreach ($fields as $field) {
8286
$this->addField($field, true);
8387
}
88+
8489
return $this;
8590
}
8691

@@ -96,6 +101,7 @@ protected function getDefaultForm(): array
96101
if ($this->afterUploadFieldFormFields) {
97102
$formFields = array_merge($formFields, $this->afterUploadFieldFormFields);
98103
}
104+
99105
return $formFields;
100106
}
101107

@@ -106,7 +112,6 @@ public function disk(string $disk): static
106112
return $this;
107113
}
108114

109-
110115
public function visibility(string | Closure | null $visibility): static
111116
{
112117
$this->visibility = $visibility;
@@ -119,15 +124,15 @@ protected function getUploadField()
119124
$fileUpload = FileUpload::make('upload')
120125
->acceptedFileTypes($this->acceptedFileTypes)
121126
->label(function ($livewire) {
122-
if (!method_exists($livewire, 'getTable')) {
127+
if (! method_exists($livewire, 'getTable')) {
123128
return __('Excel Data');
124129
}
125130

126131
return str($livewire->getTable()->getPluralModelLabel())->title() . ' ' . __('Excel Data');
127132
})
128133
->default(1)
129134
->storeFiles($this->storeFiles)
130-
->disk(fn() => $this->disk ?: (config('excel-import.upload_disk') ?:
135+
->disk(fn () => $this->disk ?: (config('excel-import.upload_disk') ?:
131136
config('filesystems.default')))
132137
->visibility($this->visibility)
133138
->rules($this->validationRules())
@@ -139,14 +144,15 @@ protected function getUploadField()
139144
$this->getSampleExcelButton()
140145
);
141146
}
147+
142148
return $fileUpload;
143149
}
144150

145151
public function validationRules(): array
146152
{
147153
$rules = [];
148154
if ($this->validate) {
149-
$rules[] = fn(): Closure => function (string $attribute, $value, Closure $fail) {
155+
$rules[] = fn (): Closure => function (string $attribute, $value, Closure $fail) {
150156
Excel::import(
151157
new ValidationImport(
152158
$fail,
@@ -157,6 +163,7 @@ public function validationRules(): array
157163
);
158164
};
159165
}
166+
160167
return $rules;
161168
}
162169
}

src/DefaultImport.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class DefaultImport implements ToCollection, WithHeadingRow
1111
{
1212
protected array $additionalData = [];
13-
13+
1414
protected array $customImportData = [];
1515

1616
protected ?Closure $collectionMethod = null;
@@ -20,14 +20,13 @@ class DefaultImport implements ToCollection, WithHeadingRow
2020
public function __construct(
2121
public string $model,
2222
public array $attributes = []
23-
) {
24-
}
23+
) {}
2524

2625
public function setAdditionalData(array $additionalData): void
2726
{
2827
$this->additionalData = $additionalData;
2928
}
30-
29+
3130
public function setCustomImportData(array $customImportData): void
3231
{
3332
$this->customImportData = $customImportData;
@@ -45,21 +44,21 @@ public function setAfterValidationMutator(Closure $closure): void
4544

4645
public function collection(Collection $collection)
4746
{
48-
if(is_callable($this->collectionMethod)) {
47+
if (is_callable($this->collectionMethod)) {
4948
$collection = call_user_func(
50-
$this->collectionMethod,
49+
$this->collectionMethod,
5150
$this->model,
5251
$collection,
5352
$this->additionalData,
5453
$this->afterValidationMutator
5554
);
56-
}else{
55+
} else {
5756
foreach ($collection as $row) {
5857
$data = $row->toArray();
59-
if(filled($this->additionalData)) {
58+
if (filled($this->additionalData)) {
6059
$data = array_merge($data, $this->additionalData);
6160
}
62-
if($this->afterValidationMutator){
61+
if ($this->afterValidationMutator) {
6362
$data = call_user_func(
6463
$this->afterValidationMutator,
6564
$data

0 commit comments

Comments
 (0)