Passed
Push — master ( d1836f...473017 )
by Thomas
03:01
created

getGridFieldExportButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace LeKoala\ExcelImportExport\Extensions;
4
5
use SilverStripe\Forms\Form;
6
use SilverStripe\Core\Extension;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridFieldConfig;
9
use LeKoala\ExcelImportExport\ExcelBulkLoader;
10
use LeKoala\ExcelImportExport\ExcelImportExport;
11
use SilverStripe\Forms\GridField\GridFieldExportButton;
12
use SilverStripe\Forms\GridField\GridFieldImportButton;
13
use LeKoala\ExcelImportExport\ExcelGridFieldExportButton;
14
use LeKoala\ExcelImportExport\ExcelGridFieldImportButton;
15
16
/**
17
 * Extends {@link ModelAdmin}. to bind new forms and features
18
 *
19
 * @author Koala
20
 */
21
class ModelAdminExcelExtension extends Extension
22
{
23
    private static $allowed_actions = array(
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
24
        'downloadsample'
25
    );
26
27
    public function onAfterInit()
28
    {
29
        $this->updateModelImporters();
30
    }
31
32
    /**
33
     * Replace default model import with a predefined value
34
     *
35
     * @return void
36
     */
37
    protected function updateModelImporters()
38
    {
39
        /* @var $owner ModelAdmin */
40
        $owner = $this->owner;
41
        $config = $this->owner->config();
42
43
        // Overwrite model imports
44
        $importerClasses = $config->get('model_importers');
45
46
        if (is_null($importerClasses)) {
47
            $models = $owner->getManagedModels();
48
            foreach (array_keys($models) as $modelName) {
49
                $importerClasses[$modelName] = ExcelBulkLoader::class;
50
            }
51
52
            $config->set('model_importers', $importerClasses);
53
        }
54
    }
55
56
    public function downloadsample()
57
    {
58
        ExcelImportExport::sampleFileForClass($this->owner->modelClass);
59
    }
60
61
    /**
62
     * Helper method to return a gridfield that your ide loves
63
     *
64
     * @param Form $form
65
     * @param string $sanitisedClass
66
     * @return GridField
67
     */
68
    protected function getGridFieldForClass($form, $sanitisedClass)
69
    {
70
        return $form->Fields()->dataFieldByName($sanitisedClass);
71
    }
72
73
    public function updateEditForm(Form $form)
74
    {
75
        /* @var $owner ModelAdmin */
76
        $owner       = $this->owner;
77
        $class       = $owner->modelClass;
78
        $sanitisedClass = str_replace('\\', '-', $class);
79
        $classConfig = $owner->config();
80
81
        $gridfield = $this->getGridFieldForClass($form, $sanitisedClass);
82
        if (!$gridfield) {
0 ignored issues
show
introduced by
$gridfield is of type SilverStripe\Forms\GridField\GridField, thus it always evaluated to true.
Loading history...
83
            return;
84
        }
85
        $config = $gridfield->getConfig();
86
87
        // Handle export buttons
88
        if ($classConfig->export_csv) {
89
            $GridFieldExportButton = $this->getGridFieldExportButton($config);
90
            $GridFieldExportButton->setExportColumns(ExcelImportExport::exportFieldsForClass($class));
91
        } else {
92
            $config->removeComponentsByType(GridFieldExportButton::class);
93
        }
94
        if ($classConfig->export_excel) {
95
            $ExcelGridFieldExportButton = new ExcelGridFieldExportButton('buttons-before-left');
96
            $config->addComponent($ExcelGridFieldExportButton);
97
        }
98
99
        // Rename import button
100
        $config->removeComponentsByType(GridFieldImportButton::class);
101
        if ($this->owner->showImportForm) {
102
            $ExcelGridFieldImportButton = new ExcelGridFieldImportButton('buttons-before-left');
103
            $ExcelGridFieldImportButton->setImportForm($this->owner->ImportForm());
104
            $ExcelGridFieldImportButton->setModalTitle(_t('ExcelImportExport.IMPORTFROMFILE', 'Import from a file'));
105
            $config->addComponent($ExcelGridFieldImportButton);
106
        }
107
    }
108
109
    /**
110
     * @param GridFieldConfig $config
111
     * @return GridFieldExportButton
112
     */
113
    protected function getGridFieldExportButton($config)
114
    {
115
        return $config->getComponentByType(GridFieldExportButton::class);
116
    }
117
118
    public function updateImportForm(Form $form)
119
    {
120
        /* @var $owner ModelAdmin */
121
        $owner = $this->owner;
122
        $class = $owner->modelClass;
123
        $modelSNG  = singleton($class);
124
        $modelName = $modelSNG->i18n_singular_name();
125
126
        $fields = $form->Fields();
127
128
        $downloadSampleLink = $owner->Link(str_replace('\\', '-', $class) . '/downloadsample');
129
        $downloadSample = '<a href="' . $downloadSampleLink . '" class="no-ajax" target="_blank">' . _t(
130
            'ExcelImportExport.DownloadSample',
131
            'Download sample file'
132
        ) . '</a>';
133
134
        $file = $fields->dataFieldByName('_CsvFile');
135
        if ($file) {
0 ignored issues
show
introduced by
$file is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
136
            $csvDescription = ExcelImportExport::getValidExtensionsText();
137
            $csvDescription .= '. ' . $downloadSample;
138
            $file->setDescription($csvDescription);
139
            $file->getValidator()->setAllowedExtensions(ExcelImportExport::getValidExtensions());
140
        }
141
142
        // We moved the specs into a nice to use download sample button
143
        $fields->removeByName("SpecFor{$modelName}");
144
145
        // If you cannot delete, you cannot empty
146
        if (!$modelSNG->canDelete()) {
147
            $fields->removeByName('EmptyBeforeImport');
148
        }
149
150
        $actions = $form->Actions();
151
152
        // Update import button
153
        $import = $actions->dataFieldByName('action_import');
154
        if ($import) {
0 ignored issues
show
introduced by
$import is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
155
            $import->setTitle(_t(
156
                'ExcelImportExport.ImportExcel',
157
                "Import from Excel"
158
            ));
159
            $import->removeExtraClass('btn-outline-secondary');
160
            $import->addExtraClass('btn-primary');
161
        }
162
    }
163
}
164