-
Notifications
You must be signed in to change notification settings - Fork 585
/
Copy pathModuleController.php
2752 lines (2373 loc) · 88.5 KB
/
ModuleController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace A17\Twill\Http\Controllers\Admin;
use A17\Twill\Exceptions\NoCapsuleFoundException;
use A17\Twill\Facades\TwillCapsules;
use A17\Twill\Facades\TwillPermissions;
use A17\Twill\Helpers\FlashLevel;
use A17\Twill\Models\Behaviors\HasSlug;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\Contracts\TwillSchedulableModel;
use A17\Twill\Repositories\ModuleRepository;
use A17\Twill\Services\Breadcrumbs\Breadcrumbs;
use A17\Twill\Services\Forms\Fields\BaseFormField;
use A17\Twill\Services\Forms\Fields\BlockEditor;
use A17\Twill\Services\Forms\Fields\Repeater;
use A17\Twill\Services\Forms\Form;
use A17\Twill\Services\Listings\Columns\Browser;
use A17\Twill\Services\Listings\Columns\FeaturedStatus;
use A17\Twill\Services\Listings\Columns\Image;
use A17\Twill\Services\Listings\Columns\Languages;
use A17\Twill\Services\Listings\Columns\NestedData;
use A17\Twill\Services\Listings\Columns\Presenter;
use A17\Twill\Services\Listings\Columns\PublishStatus;
use A17\Twill\Services\Listings\Columns\Relation;
use A17\Twill\Services\Listings\Columns\ScheduledStatus;
use A17\Twill\Services\Listings\Columns\Text;
use A17\Twill\Services\Listings\Filters\BasicFilter;
use A17\Twill\Services\Listings\Filters\FreeTextSearch;
use A17\Twill\Services\Listings\Filters\QuickFilter;
use A17\Twill\Services\Listings\Filters\QuickFilters;
use A17\Twill\Services\Listings\Filters\TableFilters;
use A17\Twill\Services\Listings\Filters\TwillBaseFilter;
use A17\Twill\Services\Listings\TableColumn;
use A17\Twill\Services\Listings\TableColumns;
use A17\Twill\Services\Listings\TableDataContext;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\View as IlluminateView;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class ModuleController extends Controller
{
use Concerns\FormSubmitOptions;
/**
* @var Application
*/
protected $app;
/**
* @var Request
*/
protected $request;
/**
* @var string
*/
protected $namespace;
/**
* @var string
*/
protected $routePrefix;
/**
* @var string
*/
protected $moduleName;
/**
* @var string
*/
protected $modelName;
/**
* @var string
*/
protected $modelTitle;
protected ModuleRepository $repository;
/**
* @var \A17\Twill\Models\User
*/
protected $user;
/**
* Options of the index view.
*
* @var array
*/
protected $defaultIndexOptions = [
'create' => true,
'edit' => true,
'publish' => true,
'bulkPublish' => true,
'feature' => false,
'bulkFeature' => false,
'restore' => true,
'bulkRestore' => true,
'forceDelete' => true,
'bulkForceDelete' => true,
'delete' => true,
'duplicate' => false,
'bulkDelete' => true,
'reorder' => false,
'permalink' => true,
'bulkEdit' => true,
'editInModal' => false,
'skipCreateModal' => false,
'includeScheduledInList' => true,
'showImage' => false,
'sortable' => true,
];
/**
* Options of the index view and the corresponding auth gates.
*
* @var array
*/
protected $authorizableOptions = [
'list' => 'access-module-list',
'create' => 'edit-module',
'edit' => 'edit-item',
'permalink' => 'edit-item',
'publish' => 'edit-item',
'feature' => 'edit-item',
'reorder' => 'edit-module',
'delete' => 'edit-item',
'duplicate' => 'edit-item',
'restore' => 'edit-item',
'forceDelete' => 'edit-item',
'bulkForceDelete' => 'edit-module',
'bulkPublish' => 'edit-module',
'bulkRestore' => 'edit-module',
'bulkFeature' => 'edit-module',
'bulkDelete' => 'edit-module',
'bulkEdit' => 'edit-module',
'editInModal' => 'edit-module',
'skipCreateModal' => 'edit-module',
'includeScheduledInList' => 'edit-module',
'showImage' => 'edit-module',
'sortable' => 'edit-module',
];
/**
* Relations to eager load for the index view.
*
* @var array
*/
protected $indexWith = [];
/**
* Relations to eager load for the form view.
*
* @var array
*/
protected $formWith = [];
/**
* Relation count to eager load for the form view.
*
* @var array
*/
protected $formWithCount = [];
/**
* Additional filters for the index view.
*
* To automatically have your filter added to the index view use the following convention:
* suffix the key containing the list of items to show in the filter by 'List' and
* name it the same as the filter you defined in this array.
*
* Example: 'fCategory' => 'category_id' here and 'fCategoryList' in indexData()
* By default, this will run a where query on the category_id column with the value
* of fCategory if found in current request parameters. You can intercept this behavior
* from your repository in the filter() function.
*
* @var array
*
* @deprecated use the method `filters` instead.
*/
protected $filters = [];
/**
* Additional links to display in the listing filter.
*
* @var array
*/
protected $filterLinks = [];
/**
* Default orders for the index view for fields that are not part of the indexColumns.
*
* @var array
*
* @deprecated when possible use getIndexTableColumns instead.
*/
protected $defaultOrders = [
'created_at' => 'desc',
];
/**
* @var int
*/
protected $perPage = 20;
/**
* Name of the index column to use as name column.
*
* @var string
*/
protected $titleColumnKey = 'title';
/**
* Label of the index column to use as name column.
*
* @var string
*/
protected $titleColumnLabel = 'Title';
/**
* Name of the index column to use as identifier column.
*
* @var string
*/
protected $identifierColumnKey = 'id';
/**
* Attribute to use as title in forms.
*
* @var string
*/
protected $titleFormKey;
/**
* Label of the title field in forms.
*
* @var string
*/
protected $titleFormLabel = 'Title';
/**
* Feature field name if the controller is using the feature route (defaults to "featured").
*
* @var string
*/
protected $featureField = 'featured';
/**
* Indicates if this module is edited through a parent module.
*
* @var bool
*/
protected $submodule = false;
/**
* @var int|null
*/
protected $submoduleParentId = null;
/**
* Can be used in child classes to disable the content editor (full screen block editor).
*
* @var bool
*/
protected $disableEditor = false;
/**
* @var array
*/
protected $indexOptions;
/**
* @var array
* @deprecated please use the getIndexTableColumns method. Will be removed in Twill 4.0
*/
protected $indexColumns = [];
/**
* @var array
* @deprecated please use the getBrowserTableColumns method. Will be removed in Twill 4.0
*/
protected $browserColumns = [];
/**
* @var string
*/
protected $permalinkBase;
/**
* Filters that are selected by default in the index view.
*
* Example: 'filter_key' => 'default_filter_value'
*
* @var array
*
* @deprecated use the method `default` in `filters` instead.
*/
protected $filtersDefaultOptions = [];
/**
* @var array
*
* Can be something like ['search' => 'title|search']
*
* @deprecated use the method `default` in `filters` instead.
*/
protected $defaultFilters;
/**
* @var string
*/
protected $viewPrefix;
/**
* @var string
*/
protected $previewView;
/**
* List of permissions keyed by a request field. Can be used to prevent unauthorized field updates.
*
* @var array
*/
protected $fieldsPermissions = [];
/**
* Determines if draft revisions can be added on top of published content.
*
* @var bool
*/
protected $enableDraftRevisions = false;
/**
* Array of customizable label translation keys.
*
* @var array
*/
protected $labels = [];
/**
* When set to true and the model is translatable, the language prefix will not be shown in the permalink.
*/
private bool $withoutLanguageInPermalink = false;
/**
* The columns to search for when using the search field.
*
* Do not modify this directly but use the method setSearchColumns().
*/
protected ?array $searchColumns = null;
/**
* If you need more fine control over the search query
*
* Do not modify this directly but use the method setSearchQuery().
*/
protected mixed $searchQuery = null;
/**
* Default label translation keys that can be overridden in the labels array.
*
* @var array
*/
protected $defaultLabels = [
'published' => 'twill::lang.main.published',
'draft' => 'twill::lang.main.draft',
'listing' => [
'filter' => [
'published' => 'twill::lang.listing.filter.published',
'draft' => 'twill::lang.listing.filter.draft',
],
],
];
private ?Breadcrumbs $breadcrumbs = null;
public function __construct(Application $app, Request $request)
{
parent::__construct();
$this->app = $app;
$this->request = $request;
$this->setUpController();
$this->modelName = $this->modelName ?? $this->getModelName();
$this->routePrefix = $this->routePrefix ?? $this->getRoutePrefix();
$this->namespace = $this->namespace ?? $this->getNamespace();
$this->repository = $this->repository ?? $this->getRepository();
$this->viewPrefix = $this->viewPrefix ?? $this->getViewPrefix();
$this->modelTitle = $this->modelTitle ?? $this->getModelTitle();
$this->labels = array_merge($this->defaultLabels, $this->labels);
$this->middleware(function ($request, $next) {
$this->user = auth('twill_users')->user();
return $next($request);
});
if (!$this instanceof AppSettingsController) {
$this->getForm($this->repository->getBaseModel())->registerDynamicRepeaters();
$this->getSideFieldsets($this->repository->getBaseModel())->registerDynamicRepeaters();
}
// When no searchColumns are set we default to the title column key.
if ($this->searchColumns === null) {
$this->searchColumns = [$this->titleColumnKey];
}
}
/**
* The setup method that is called when the controller is booted.
*/
protected function setUpController(): void
{
}
/**
* Removes the "Create" button on the listing page.
*/
protected function disableCreate(): void
{
$this->indexOptions['create'] = false;
}
/**
* Disables table interaction and removes edit links.
*/
protected function disableEdit(): void
{
$this->indexOptions['edit'] = false;
}
/**
* Disables the ability to sort the table by clicking table headers.
*/
protected function disableSortable(): void
{
$this->indexOptions['sortable'] = false;
}
/**
* Removes the publish/un-publish icon on the content listing.
*/
protected function disablePublish(): void
{
$this->indexOptions['publish'] = false;
}
/**
* Removes the "publish" option from the bulk operations.
*/
protected function disableBulkPublish(): void
{
$this->indexOptions['bulkPublish'] = false;
}
/**
* Removes "restore" from the list item dropdown on the "Trash" content list.
*/
protected function disableRestore(): void
{
$this->indexOptions['restore'] = false;
}
/**
* Removes the "Trash" quick filter.
*/
protected function disableBulkRestore(): void
{
$this->indexOptions['bulkRestore'] = false;
}
/**
* Removes the "delete" option from the "Trash" content list.
*/
protected function disableForceDelete(): void
{
$this->indexOptions['forceDelete'] = false;
}
/**
* Removes "restore" from the bulk operations on the "Trash" content list.
*/
protected function disableBulkForceDelete(): void
{
$this->indexOptions['bulkForceDelete'] = false;
}
/**
* Removes the "delete" option from the content lists.
*/
protected function disableDelete(): void
{
$this->indexOptions['delete'] = false;
}
/**
* Removes the "delete" option from the bulk operations.
*/
protected function disableBulkDelete(): void
{
$this->indexOptions['bulkDelete'] = false;
}
/**
* Removes the permalink from the create/edit screens.
*/
protected function disablePermalink(): void
{
$this->indexOptions['permalink'] = false;
}
/**
* Disables the editor button.
*/
protected function disableEditor(): void
{
$this->disableEditor = true;
}
/**
* Disables bulk operations.
*/
protected function disableBulkEdit(): void
{
$this->indexOptions['bulkEdit'] = false;
}
/**
* Hides publish scheduling information from the content list.
*
* This does not affect custom table builders. Unless implemented.
*/
protected function disableIncludeScheduledInList(): void
{
$this->indexOptions['includeScheduledInList'] = false;
}
/**
* Disables the create modal and directly forwards you to the full edit page.
*/
protected function enableSkipCreateModal(): void
{
$this->indexOptions['skipCreateModal'] = true;
}
/**
* Allow to feature the content. This requires a 'featured' fillable boolean on the model.
*
* If you want to use a different column you can use the `setFeaturedField` method.
*/
protected function enableFeature(): void
{
// @todo: Also expand on the documentation about this.
// Also mention isUniqueFeature that only one can be featured + test this.
$this->indexOptions['feature'] = true;
}
/**
* Enables the "Feature" bulk operation.
*/
protected function enableBulkFeature(): void
{
$this->indexOptions['bulkFeature'] = true;
}
/**
* Enables the "Duplicate" option from the content lists.
*/
protected function enableDuplicate(): void
{
$this->indexOptions['duplicate'] = true;
}
/**
* Allows to reorder the items, if this was setup on the model.
*/
protected function enableReorder(): void
{
$this->indexOptions['reorder'] = true;
}
/**
* Enables the function that content is edited in the create modal.
*/
protected function enableEditInModal(): void
{
// @3xtodo: When this is enabled, the "link" to the model in the listing does not work (Redirects back).
$this->indexOptions['editInModal'] = true;
}
/**
* Shows the thumbnail of the content in the list.
*/
protected function enableShowImage(): void
{
$this->indexOptions['showImage'] = true;
}
/**
* Set the field to use for featuring content.
*/
protected function setFeatureField(string $field): void
{
$this->featureField = $field;
}
/**
* Set the columns to search in.
*
* SearchColumns are automatically prefixes/suffixed with %.
*/
protected function setSearchColumns(array $searchColumns): void
{
$this->searchColumns = $searchColumns;
}
/**
* If you need finer control over the search query, you may provide a callback
* @param callable $query With the following signature: fn (Builder $query, string $searchString, array $translatedAttributes): void => $query
*/
protected function setSearchQuery(callable $query): void
{
$this->searchQuery = $query;
}
/**
* Set the name of the module you are working with.
*/
protected function setModuleName(string $moduleName): void
{
$this->moduleName = $moduleName;
}
/**
* The static permalink base to your module. Defaults to `setModuleName` when empty.
*/
protected function setPermalinkBase(string $permalinkBase): void
{
$this->permalinkBase = $permalinkBase;
}
protected function withoutLanguageInPermalink(bool $without = true): void
{
$this->withoutLanguageInPermalink = $without;
}
/**
* Sets the field to use as title, defaults to `title`.
*/
protected function setTitleColumnKey(string $titleColumnKey): void
{
$this->titleColumnKey = $titleColumnKey;
}
/**
* Sets the label to use for title column, defaults to `Title`.
*/
protected function setTitleColumnLabel(string $titleColumnLabel): void
{
$this->titleColumnLabel = $titleColumnLabel;
}
/**
* Sets the field to use as title in forms, defaults to `title`.
*/
protected function setTitleFormKey(string $titleFormKey): void
{
$this->titleFormKey = $titleFormKey;
}
/**
* Sets the label to use for title field in forms, defaults to `Title`.
*/
protected function setTitleFormLabel(string $titleFormLabel): void
{
$this->titleFormLabel = $titleFormLabel;
}
/**
* Usually not required, but in case customization is needed you can use this method to set the name of the model
* this controller acts on.
*/
protected function setModelName(string $modelName): void
{
$this->modelName = $modelName;
}
/**
* Sets the amount of results to show per page, defaults to 20.
*/
protected function setResultsPerPage(int $resultsPerPage): void
{
$this->perPage = $resultsPerPage;
}
/**
* Relations to eager load for the index view.
*/
protected function eagerLoadListingRelations(array $relations): void
{
$this->indexWith = $relations;
}
/**
* Relations to eager load for the form view.
*
* Add relationship used in multiselect and resource form fields.
*/
protected function eagerLoadFormRelations(array $relations): void
{
$this->formWith = $relations;
}
/**
* Relation count to eager load for the form view.
*/
protected function eagerLoadFormRelationCounts(array $relations): void
{
$this->formWithCount = $relations;
}
/**
* Set the breadcrumbs.
*/
protected function setBreadcrumbs(Breadcrumbs $breadcrumbs): void
{
$this->breadcrumbs = $breadcrumbs;
}
/**
* $type can be index or browser.
*/
private function getTableColumns(string $type): TableColumns
{
if ($type === 'index') {
$tableColumns = $this->getIndexTableColumns();
} else {
$tableColumns = $this->getBrowserTableColumns();
}
return $tableColumns->each(function (TableColumn $column) {
if ($column instanceof NestedData) {
$column->linkCell(function (TwillModelContract $model, NestedData $column) {
$module = Str::singular(last(explode('.', $this->moduleName)));
return moduleRoute(
"$this->moduleName." . $column->getField(),
$this->routePrefix,
'index',
[$module => $this->getItemIdentifier($model)]
);
});
} elseif ($column->shouldLinkToEdit()) {
$column->linkCell(function (TwillModelContract $model) {
if ($model->trashed()) {
return null;
}
if ($this->getIndexOption('edit', $model)) {
return $this->getModuleRoute($model->id, 'edit');
}
});
}
});
}
protected function getBrowserTableColumns(): TableColumns
{
$columns = TableColumns::make();
if ($this->browserColumns !== []) {
$this->handleLegacyColumns($columns, $this->browserColumns);
} elseif ($this->moduleHas('medias')) {
$columns->add(
Image::make()
->field('thumbnail')
->rounded()
->title(twillTrans('Image'))
);
}
$columns = $columns->merge($this->additionalBrowserTableColumns());
return $columns;
}
protected function getIndexTableColumns(): TableColumns
{
$columns = TableColumns::make();
if ($this->getIndexOption('publish')) {
$columns->add(
PublishStatus::make()
->title(twillTrans('twill::lang.listing.columns.published'))
->sortable()
->optional()
);
}
if ($this->indexColumns === []) {
// Add default columns.
if ($this->getIndexOption('showImage')) {
$columns->add(
Image::make()
->field('thumbnail')
->title(twillTrans('Image'))
);
}
if ($this->getIndexOption('feature') && $this->repository->isFillable('featured')) {
$columns->add(
FeaturedStatus::make()
->title(twillTrans('twill::lang.listing.columns.featured'))
);
}
}
// Consume Deprecated data.
if ($this->indexColumns !== []) {
$this->handleLegacyColumns($columns, $this->indexColumns);
} else {
$columns->add(
Text::make()
->field($this->titleColumnKey)
->title($this->titleColumnKey === 'title' && $this->titleColumnLabel === 'Title' ? twillTrans('twill::lang.main.title') : $this->titleColumnLabel)
->sortable()
->linkToEdit()
);
}
$columns = $columns->merge($this->additionalIndexTableColumns());
if ($this->getIndexOption('includeScheduledInList') && $this->repository->isFillable('publish_start_date')) {
$columns->add(
ScheduledStatus::make()
->title(twillTrans('twill::lang.publisher.scheduled'))
->optional()
);
}
if ($this->moduleHas('translations') && count(getLocales()) > 1) {
$columns->add(
Languages::make()
->title(twillTrans('twill::lang.listing.languages'))
->optional()
);
}
return $columns;
}
/**
* Similar to @see getBrowserTableColumns but these will be added on top of the default columns.
*/
protected function additionalBrowserTableColumns(): TableColumns
{
return new TableColumns();
}
/**
* Similar to @see getIndexTableColumns but these will be added on top of the default columns.
*/
protected function additionalIndexTableColumns(): TableColumns
{
return new TableColumns();
}
private function handleLegacyColumns(TableColumns $columns, array $items): void
{
foreach ($items as $key => $indexColumn) {
if ($indexColumn['nested'] ?? false) {
$columns->add(
NestedData::make()
->title($indexColumn['title'] ?? null)
->field($indexColumn['nested'])
->sortKey($indexColumn['sortKey'] ?? null)
->sortable($indexColumn['sort'] ?? false)
->optional($indexColumn['optional'] ?? false)
->linkCell(function (TwillModelContract $model) use ($indexColumn) {
$module = Str::singular(last(explode('.', $this->moduleName)));
return moduleRoute(
"$this->moduleName.{$indexColumn['nested']}",
$this->routePrefix,
'index',
[$module => $this->getItemIdentifier($model)]
);
})
);
} elseif ($indexColumn['thumb'] ?? false) {
$columns->add(
Image::make()
->title($indexColumn['title'] ?? $key)
->role($indexColumn['variant']['role'] ?? null)
->crop($indexColumn['variant']['crop'] ?? null)
->field($indexColumn['field'] ?? $key)
->sortKey($indexColumn['sortKey'] ?? null)
->optional($indexColumn['optional'] ?? false)
);
} elseif ($indexColumn['relatedBrowser'] ?? false) {
$columns->add(
Browser::make()
->title($indexColumn['title'])
->field($indexColumn['field'] ?? $key)
->sortKey($indexColumn['sortKey'] ?? null)
->optional($indexColumn['optional'] ?? false)
->browser($indexColumn['relatedBrowser'])
);
} elseif ($indexColumn['relationship'] ?? false) {
$columns->add(
Relation::make()
->title($indexColumn['title'])
->field($indexColumn['field'] ?? $key)
->sortKey($indexColumn['sortKey'] ?? null)
->optional($indexColumn['optional'] ?? false)
->relation($indexColumn['relationship'])
->sortable($indexColumn['sort'] ?? false)
);
} elseif ($indexColumn['present'] ?? false) {
$columns->add(
Presenter::make()
->title($indexColumn['title'])
->field($indexColumn['field'] ?? $key)
->sortKey($indexColumn['sortKey'] ?? null)
->optional($indexColumn['optional'] ?? false)
->sortable($indexColumn['sort'] ?? false)
);
} else {
$textColumn = Text::make()
->title($indexColumn['title'] ?? null)
->field($indexColumn['field'] ?? $key)
->sortKey($indexColumn['sortKey'] ?? null)
->optional($indexColumn['optional'] ?? false)
->sortable($indexColumn['sort'] ?? false);
// If it is a the title, we always want to link it.
if ($this->titleColumnKey === ($indexColumn['field'] ?? $key)) {
$textColumn->linkCell(function (TwillModelContract $model) {
if ($this->getIndexOption('edit', $model)) {
return $this->getModuleRoute($model->id, 'edit');
}
});
}
$columns->add($textColumn);
}
}
}
/**
* Match an option name to a gate name if needed, then authorize it.
*
* @return void
*/
protected function authorizeOption($option, $arguments = [])
{
$gate = $this->authorizableOptions[$option] ?? $option;
$this->authorize($gate, $arguments);
}
/**
* @return void
* @deprecated To be removed in Twill 3.0
* @todo: Check this.
*/
protected function setMiddlewarePermission()
{
$this->middleware('can:list', ['only' => ['index', 'show']]);
$this->middleware('can:edit', ['only' => ['store', 'edit', 'update']]);
$this->middleware('can:duplicate', ['only' => ['duplicate']]);
$this->middleware('can:publish', ['only' => ['publish', 'feature', 'bulkPublish', 'bulkFeature']]);
$this->middleware('can:reorder', ['only' => ['reorder']]);
$this->middleware(
'can:delete',
[
'only' => [
'destroy',
'bulkDelete',
'restore',