-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathModelAdmin.php
817 lines (724 loc) · 28.1 KB
/
ModelAdmin.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
<?php
namespace SilverStripe\Admin;
use InvalidArgumentException;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\BulkLoader;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FileField;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
use SilverStripe\Forms\GridField\GridFieldImportButton;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\GridField\GridFieldPrintButton;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
/**
* Generates a three-pane UI for editing model classes, tabular results and edit forms.
*
* Relies on data such as {@link DataObject::$db} and {@link DataObject::getCMSFields()}
* to scaffold interfaces "out of the box", while at the same time providing
* flexibility to customize the default output.
*/
abstract class ModelAdmin extends LeftAndMain
{
/**
* @inheritdoc
*/
private static $url_rule = '/$ModelClass/$Action';
/**
* List of all managed {@link DataObject}s in this interface.
*
* Simple notation with class names only:
* <code>
* array('MyObjectClass','MyOtherObjectClass')
* </code>
*
* Extended notation with options (e.g. custom titles):
* <code>
* array(
* 'MyObjectClass' => ['title' => "Custom title"]
* 'urlslug' => ['title' => "Another title", 'dataClass' => MyNamespacedClass::class]
* )
* </code>
*
* Available options:
* - 'title': Set custom titles for the tabs or dropdown names
* - 'dataClass': The class name being managed. Defaults to the key. Useful for making shorter URLs or placing the same class in multiple tabs
*
* @config
* @var array|string
*/
private static $managed_models = null;
/**
* Override menu_priority so that ModelAdmin CMSMenu objects
* are grouped together directly above the Help menu item.
* @var float
*/
private static $menu_priority = -0.5;
/**
* @var string
*/
private static $menu_icon_class = 'font-icon-database';
private static $allowed_actions = [
'ImportForm',
'SearchForm'
];
private static $url_handlers = [
'$ModelClass/$Action' => 'handleAction'
];
/**
* @var string The {@link \SilverStripe\ORM\DataObject} sub-class being managed during this object's lifetime.
*/
protected $modelClass;
/**
* @var string The {@link \SilverStripe\ORM\DataObject} the currently active model tab, and key of managed_models.
*/
protected $modelTab;
/**
* Change this variable if you don't want the Import from CSV form to appear.
* This variable can be a boolean or an array.
* If array, you can list className you want the form to appear on. i.e. array('myClassOne','myClassTwo')
*/
public $showImportForm = true;
/**
* Change this variable if you don't want the gridfield search to appear.
* This variable can be a boolean or an array.
* If array, you can list className you want the form to appear on. i.e. array('myClassOne','myClassTwo')
*/
public $showSearchForm = true;
/**
* List of all {@link DataObject}s which can be imported through
* a subclass of {@link BulkLoader} (mostly CSV data).
* By default {@link CsvBulkLoader} is used, assuming a standard mapping
* of column names to {@link DataObject} properties/relations.
*
* e.g. "BlogEntry" => "BlogEntryCsvBulkLoader"
*
* @config
* @var array
*/
private static $model_importers = null;
/**
* @config
* @var int Amount of results to show per page
*/
private static $page_length = 30;
/**
* Initialize the model admin interface. Sets up embedded jquery libraries and requisite plugins.
*
* Sets the `modelClass` field which determines which of the {@link DataObject} objects will have visible data. This
* is determined by the URL (with the first slug being the name of the DataObject class to represent. If this class
* is loaded without any URL, we pick the first DataObject from the list of {@link self::$managed_models}.
*/
protected function init()
{
parent::init();
$models = $this->getManagedModels();
$this->modelTab = $this->getRequest()->param('ModelClass');
// if we've hit the "landing" page
if ($this->modelTab === null) {
reset($models);
$this->modelTab = key($models ?? []);
}
// security check for valid models
if (!$this->isManagedModel($this->modelTab)) {
// if it fails to match the string exactly, try reverse-engineering a classname
$this->modelTab = $this->unsanitiseClassName($this->modelTab);
if (!$this->isManagedModel($this->modelTab)) {
throw new \RuntimeException(sprintf('ModelAdmin::init(): Invalid Model class %s', $this->modelTab));
}
}
$this->modelClass = isset($models[$this->modelTab]['dataClass'])
? $models[$this->modelTab]['dataClass']
: $this->modelTab;
}
/**
* Overrides {@link \SilverStripe\Admin\LeftAndMain} to ensure the active model class (the DataObject we are
* currently viewing) is included in the URL.
*
* @inheritdoc
*/
public function Link($action = null)
{
if (!$action) {
$action = $this->sanitiseClassName($this->modelTab);
}
return parent::Link($action);
}
/**
* Get the link for the tab of a DataObject subclass managed by this ModelAdmin.
*
* Note that if the modelClass is managed on multiple tabs and you want a link for a
* specific tab you should use {@link getLinkForModelTab} instead.
*
* @throws InvalidArgumentException if $modelClass is not managed by this ModelAdmin.
*/
public function getLinkForModelClass(string $modelClass): string
{
if (!$this->isManagedModel($modelClass)) {
throw new InvalidArgumentException("$modelClass isn't managed by this ModelAdmin.");
}
return $this->getLinkForModelTab($this->getModelTabForModelClass($modelClass));
}
/**
* Get the link for a specific tab on this ModelAdmin.
*
* If you do not have multiple tabs for a given class you should use {@link getLinkForModelClass}
* instead.
*
* @param string $modelTab This is either the custom url segment for the tab or, if that
* was not not defined, it is the fully qualified class name of the managed model.
* In the latter case, you should prefer to use {@link getLinkForModelClass}.
*
* @throws InvalidArgumentException if $modelTab is not a tab on this ModelAdmin.
*/
public function getLinkForModelTab(string $modelTab): string
{
// Don't use isManagedModel here because a subclass of a managed model may not have its own tab.
if (!array_key_exists($modelTab, $this->getManagedModels())) {
throw new InvalidArgumentException("$modelTab isn't a tab on this ModelAdmin.");
}
return $this->Link($this->sanitiseClassName($modelTab));
}
/**
* Get the link for editing an object inside this ModelAdmin.
*
* @throws InvalidArgumentException if $obj is not managed by this ModelAdmin.
*/
public function getCMSEditLinkForManagedDataObject(DataObject $obj): string
{
$modelTab = $this->getModelTabForModelClass($obj->ClassName);
if ($modelTab === null) {
throw new InvalidArgumentException("$obj->ClassName isn't managed by this ModelAdmin");
}
$link = static::join_links(
$this->getLinkForModelClass($obj->ClassName),
'EditForm/field/',
$this->sanitiseClassName($modelTab),
'item',
$obj->ID
);
$this->extend('updateEditLinkForDataObject', $link, $obj);
return $link;
}
/**
* Produces an edit form that includes a default {@link \SilverStripe\Forms\GridField\GridField} for the currently
* active {@link \SilverStripe\ORM\DataObject}. The GridField will show data from the currently active `modelClass`
* only (see {@link self::init()}).
*
* @param int|null $id
* @param \SilverStripe\Forms\FieldList $fields
* @return \SilverStripe\Forms\Form A Form object with one tab per {@link \SilverStripe\Forms\GridField\GridField}
*/
public function getEditForm($id = null, $fields = null)
{
$form = Form::create(
$this,
'EditForm',
new FieldList($this->getGridField()),
new FieldList()
)->setHTMLID('Form_EditForm');
$form->addExtraClass('cms-edit-form cms-panel-padded center flexbox-area-grow');
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
$editFormAction = Controller::join_links($this->getLinkForModelTab($this->modelTab), 'EditForm');
$form->setFormAction($editFormAction);
$form->setAttribute('data-pjax-fragment', 'CurrentForm');
$this->extend('updateEditForm', $form);
return $form;
}
/**
* Generate the GridField field that will be used for this ModelAdmin.
*
* Developers may override this method in their ModelAdmin class to customise their GridField. Extensions can use
* the `updateGridField` hook for the same purpose.
*
* @see {@link getGridFieldConfig()}
* @return GridField
*/
protected function getGridField(): GridField
{
$field = GridField::create(
$this->sanitiseClassName($this->modelTab),
false,
$this->getList(),
$this->getGridFieldConfig()
);
$this->extend('updateGridField', $field);
return $field;
}
/**
* Generate the GridField Configuration that will use for the ModelAdmin Gridfield.
*
* Developers may override this method in their ModelAdmin class to customise their GridFieldConfiguration.
* Extensions can use the `updateGridFieldConfig` hook for the same purpose.
*
* @return GridFieldConfig
*/
protected function getGridFieldConfig(): GridFieldConfig
{
$config = GridFieldConfig_RecordEditor::create($this->config()->get('page_length'));
$exportButton = Injector::inst()->createWithArgs(GridFieldExportButton::class, ['buttons-before-left']);
$exportButton->setExportColumns($this->getExportFields());
$config
->addComponent($exportButton)
->addComponents(Injector::inst()->createWithArgs(GridFieldPrintButton::class, ['buttons-before-left']));
// Remove default and add our own filter header with extension points
// to retain API until deprecation in 5.0
$config->removeComponentsByType(GridFieldFilterHeader::class);
$config->addComponent(Injector::inst()->createWithArgs(GridFieldFilterHeader::class, [
false,
function ($context) {
$this->extend('updateSearchContext', $context);
},
function ($form) {
$this->extend('updateSearchForm', $form);
}
]));
if (!$this->showSearchForm ||
(is_array($this->showSearchForm) && !in_array($this->modelClass, $this->showSearchForm ?? []))
) {
$config->removeComponentsByType(GridFieldFilterHeader::class);
}
// GridFieldPaginator has to be added after filter header for it to function correctly
$paginator = $config->getComponentByType(GridFieldPaginator::class);
if ($paginator) {
$config
->removeComponent($paginator)
->addComponent($paginator);
}
// Validation
if (singleton($this->modelClass)->hasMethod('getCMSCompositeValidator')) {
$detailValidator = singleton($this->modelClass)->getCMSCompositeValidator();
/** @var GridFieldDetailForm $detailform */
$detailform = $config->getComponentByType(GridFieldDetailForm::class);
$detailform->setValidator($detailValidator);
}
if ($this->showImportForm) {
$config->addComponent(
GridFieldImportButton::create('buttons-before-left')
->setImportForm($this->ImportForm())
->setModalTitle(_t(__CLASS__ . '.IMPORT', 'Import from CSV'))
);
}
$this->extend('updateGridFieldConfig', $config);
return $config;
}
/**
* Define which fields are used in the {@link getEditForm} GridField export.
* By default, it uses the summary fields from the model definition.
*
* @return array
*/
public function getExportFields()
{
return singleton($this->modelClass)->summaryFields();
}
/**
* @deprecated 1.3.0 Use GridFieldFilterHeader instead
* @return \SilverStripe\ORM\Search\SearchContext
*/
public function getSearchContext()
{
Deprecation::notice('1.3.0', 'Use GridFieldFilterHeader instead');
$gridField = $this->getEditForm()->fields()
->fieldByName($this->sanitiseClassName($this->modelClass));
$filterHeader = $gridField->getConfig()
->getComponentByType(GridFieldFilterHeader::class);
$context = $filterHeader
->getSearchContext($gridField);
return $context;
}
/**
* Gets a list of fields that have been searched
*
* @deprecated 1.3.0 Use GridFieldFilterHeader instead
* @return ArrayList
*/
public function SearchSummary()
{
Deprecation::notice('1.3.0', 'Use GridFieldFilterHeader instead');
$context = Deprecation::withNoReplacement(function () {
return $this->getSearchContext();
});
return $context->getSummary();
}
/**
* Returns the search form
*
* @deprecated 1.3.0 Use GridFieldFilterHeader instead
* @return Form|bool
*/
public function SearchForm()
{
Deprecation::notice('1.3.0', 'Use GridFieldFilterHeader instead');
if (!$this->showSearchForm
|| (is_array($this->showSearchForm) && !in_array($this->modelClass, $this->showSearchForm ?? []))
) {
return false;
}
$gridField = $this->getEditForm()->fields()
->fieldByName($this->sanitiseClassName($this->modelClass));
$filterHeader = $gridField->getConfig()
->getComponentByType(GridFieldFilterHeader::class);
$form = $filterHeader->getSearchForm($gridField);
return $form;
}
/**
* You can override how ModelAdmin returns DataObjects by either overloading this method, or defining an extension
* to ModelAdmin that implements the `updateList` method (and takes a {@link \SilverStripe\ORM\DataList} as the
* first argument).
*
* For example, you might want to do this if this particular ModelAdmin should only ever show objects where an
* Archived flag is set to false. That would be best done as an extension, for example:
*
* <code>
* public function updateList(\SilverStripe\ORM\DataList $list)
* {
* return $list->filter('Archived', false);
* }
* </code>
*
* Note: If you override this method you may also need to override getCMSEditLinkForManagedDataObject()
*
* @return \SilverStripe\ORM\DataList
*/
public function getList()
{
$list = DataObject::singleton($this->modelClass)->get();
$this->extend('updateList', $list);
return $list;
}
/**
* The model managed by this instance.
* See $managed_models for potential values.
*
* @return string
*/
public function getModelClass()
{
return $this->modelClass;
}
/**
* @return \SilverStripe\ORM\ArrayList An ArrayList of all managed models to build the tabs for this ModelAdmin
*/
protected function getManagedModelTabs()
{
$models = $this->getManagedModels();
$forms = new ArrayList();
foreach ($models as $tab => $options) {
$forms->push(new ArrayData([
'Title' => $options['title'],
'Tab' => $tab,
// `getManagedModels` did not always return a `dataClass` attribute
// Legacy behaviour is for `ClassName` to map to `$tab`
'ClassName' => isset($options['dataClass']) ? $options['dataClass'] : $tab,
'Link' => $this->getLinkForModelTab($tab),
'LinkOrCurrent' => ($tab == $this->modelTab) ? 'current' : 'link'
]));
}
return $forms;
}
/**
* Sanitise a model class' name for inclusion in a link
*
* @param string $class
* @return string
*/
protected function sanitiseClassName($class)
{
return str_replace('\\', '-', $class ?? '');
}
/**
* Unsanitise a model class' name from a URL param
*
* @param string $class
* @return string
*/
protected function unsanitiseClassName($class)
{
return str_replace('-', '\\', $class ?? '');
}
/**
* @return array Map of class name to an array of 'title' (see {@link $managed_models})
*/
public function getManagedModels()
{
$models = $this->config()->get('managed_models');
if (is_string($models)) {
$models = [$models];
}
if (!count($models ?? [])) {
throw new \RuntimeException(
'ModelAdmin::getManagedModels():
You need to specify at least one DataObject subclass in private static $managed_models.
Make sure that this property is defined, and that its visibility is set to "private"'
);
}
// Normalize models to have their model class in array key
foreach ($models as $k => $v) {
// No custom tab url segment
if (is_numeric($k)) {
$models[$v] = ['dataClass' => $v, 'title' => singleton($v)->i18n_plural_name()];
unset($models[$k]);
// Custom title but no custom tab url segment
} elseif (is_array($v) && !isset($v['dataClass'])) {
$models[$k]['dataClass'] = $k;
// Custom tab url segment but no custom title
} elseif (is_a($v, DataObject::class, true)) {
$models[$k] = ['dataClass' => $v, 'title' => singleton($v)->i18n_plural_name()];
}
}
return $models;
}
/**
* Get the model tab name for a given model class
*
* If there are multiple tabs for a given model class, the first one defined will
* be returned.
* If you want a specific tab to be returned for a given model class you should
* override this method.
*
* @throws InvalidArgumentException if $modelClass isn't a DataObject subclass
*/
protected function getModelTabForModelClass(string $modelClass): ?string
{
if (!is_subclass_of($modelClass, DataObject::class)) {
throw new InvalidArgumentException('$modelClass must be a subclass of DataObject.');
}
$managed = $this->getManagedModels();
// Check the superclasses as well as the specifically passed-in class
$classes = array_reverse(ClassInfo::ancestry($modelClass));
foreach ($classes as $class) {
foreach ($managed as $tab => $spec) {
if (!isset($spec['dataClass'])) {
$spec['dataClass'] = $tab;
}
if ($spec['dataClass'] === $class) {
return $tab;
}
}
}
return null;
}
/**
* Check whether a model is managed by this ModelAdmin class
*/
public function isManagedModel(string $modelClassOrModelTab): bool
{
if (is_subclass_of($modelClassOrModelTab, DataObject::class)) {
return $this->getModelTabForModelClass($modelClassOrModelTab) !== null;
}
return array_key_exists($modelClassOrModelTab, $this->getManagedModels());
}
/**
* Returns all importers defined in {@link self::$model_importers}.
* If none are defined, we fall back to {@link self::managed_models}
* with a default {@link CsvBulkLoader} class. In this case the column names of the first row
* in the CSV file are assumed to have direct mappings to properties on the object.
*
* @return array Map of model class names to importer instances
*/
public function getModelImporters()
{
$importerClasses = $this->config()->get('model_importers');
$models = $this->getManagedModels();
// fallback to all defined models if not explicitly defined
if (is_null($importerClasses)) {
foreach ($models as $modelName => $options) {
$importerClasses[$modelName] = 'SilverStripe\\Dev\\CsvBulkLoader';
}
}
$importers = [];
foreach ($importerClasses as $modelClass => $importerClass) {
$tab = $modelClass;
if (isset($models[$modelClass]['dataClass'])) {
$modelClass = $models[$modelClass]['dataClass'];
}
$importer = new $importerClass($modelClass);
if (ClassInfo::hasMethod($importer, 'setCheckPermissions')) {
$importer->setCheckPermissions(true);
}
$importers[$tab] = $importer;
}
return $importers;
}
/**
* Generate a CSV import form for a single {@link DataObject} subclass.
*
* @return Form|false
*/
public function ImportForm()
{
$modelSNG = singleton($this->modelClass);
$modelName = $modelSNG->i18n_singular_name();
// check if a import form should be generated
if (!$this->showImportForm ||
(is_array($this->showImportForm) && !in_array($this->modelTab, $this->showImportForm ?? []))
) {
return false;
}
$importers = $this->getModelImporters();
if (!$importers || !isset($importers[$this->modelTab])) {
return false;
}
$fields = new FieldList(
new HiddenField('ClassName', false, $this->modelClass),
new FileField('_CsvFile', false)
);
// get HTML specification for each import (column names etc.)
/** @var BulkLoader $importer */
$importer = $importers[$this->modelTab];
$spec = $importer->getImportSpec();
$specFields = new ArrayList();
foreach ($spec['fields'] as $name => $desc) {
$specFields->push(new ArrayData(['Name' => $name, 'Description' => $desc]));
}
$specRelations = new ArrayList();
foreach ($spec['relations'] as $name => $desc) {
$specRelations->push(new ArrayData(['Name' => $name, 'Description' => $desc]));
}
$specHTML = $this->customise([
'ClassName' => $this->sanitiseClassName($this->modelClass),
'ModelName' => Convert::raw2att($modelName),
'Fields' => $specFields,
'Relations' => $specRelations,
])->renderWith($this->getTemplatesWithSuffix('_ImportSpec'));
$fields->push(new LiteralField("SpecFor{$modelName}", $specHTML));
$fields->push(
new CheckboxField(
'EmptyBeforeImport',
_t(__CLASS__ . '.EMPTYBEFOREIMPORT', 'Replace data'),
false
)
);
$actions = new FieldList(
FormAction::create('import', _t(__CLASS__ . '.IMPORT', 'Import from CSV'))
->addExtraClass('btn btn-outline-secondary font-icon-upload')
);
$form = new Form(
$this,
"ImportForm",
$fields,
$actions
);
$form->setFormAction(
Controller::join_links($this->getLinkForModelTab($this->modelTab), 'ImportForm')
);
$this->extend('updateImportForm', $form);
return $form;
}
/**
* Imports the submitted CSV file based on specifications given in
* {@link self::model_importers}.
* Redirects back with a success/failure message.
*
* @todo Figure out ajax submission of files via jQuery.form plugin
*
* @param array $data
* @param Form $form
* @param HTTPRequest $request
* @return bool|HTTPResponse
*/
public function import($data, $form, $request)
{
if (!$this->showImportForm || (is_array($this->showImportForm)
&& !in_array($this->modelClass, $this->showImportForm ?? []))
) {
return false;
}
$importers = $this->getModelImporters();
/** @var BulkLoader $loader */
$loader = $importers[$this->modelClass];
// File wasn't properly uploaded, show a reminder to the user
if (empty($_FILES['_CsvFile']['tmp_name']) ||
file_get_contents($_FILES['_CsvFile']['tmp_name'] ?? '') == ''
) {
$form->sessionMessage(
_t(__CLASS__ . '.NOCSVFILE', 'Please browse for a CSV file to import'),
ValidationResult::TYPE_ERROR
);
$this->redirectBack();
return false;
}
if (!empty($data['EmptyBeforeImport']) && $data['EmptyBeforeImport']) { //clear database before import
$loader->deleteExistingRecords = true;
}
try {
$results = $loader->load($_FILES['_CsvFile']['tmp_name']);
} catch (HTTPResponse_Exception $e) {
$form->sessionMessage($e->getMessage(), ValidationResult::TYPE_ERROR);
$this->redirectBack();
return false;
}
$message = '';
if ($results) {
if ($results->CreatedCount()) {
$message .= _t(
__CLASS__ . '.IMPORTEDRECORDS',
"Imported {count} records.",
['count' => $results->CreatedCount()]
);
}
if ($results && $results->UpdatedCount()) {
$message .= _t(
__CLASS__ . '.UPDATEDRECORDS',
"Updated {count} records.",
['count' => $results->UpdatedCount()]
);
}
if ($results->DeletedCount()) {
$message .= _t(
__CLASS__ . '.DELETEDRECORDS',
"Deleted {count} records.",
['count' => $results->DeletedCount()]
);
}
if (!$results->CreatedCount() && !$results->UpdatedCount()) {
$message .= _t(__CLASS__ . '.NOIMPORT', "Nothing to import");
}
} else {
$message .= _t(__CLASS__ . '.NOIMPORT', "Nothing to import");
}
$form->sessionMessage($message, 'good');
return $this->redirectBack();
}
/**
* @param bool $unlinked
* @return ArrayList
*/
public function Breadcrumbs($unlinked = false)
{
$items = parent::Breadcrumbs($unlinked);
// Show the class name rather than ModelAdmin title as root node
$models = $this->getManagedModels();
$params = $this->getRequest()->getVars();
if (isset($params['url'])) {
unset($params['url']);
}
$items[0]->Title = $models[$this->modelTab]['title'];
$items[0]->Link = Controller::join_links(
$this->getLinkForModelTab($this->modelTab),
'?' . http_build_query($params ?? [])
);
return $items;
}
}