Completed
Push — master ( 1a9402...04d7ff )
by Nicolaas
02:23
created

ImagesWithStyleSelection::validate()   C

Complexity

Conditions 11
Paths 2

Size

Total Lines 45
Code Lines 32

Duplication

Lines 45
Ratio 100 %

Importance

Changes 0
Metric Value
dl 45
loc 45
c 0
b 0
f 0
rs 5.2653
cc 11
eloc 32
nc 2
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
4
5
class ImagesWithStyleSelection extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
8
9
    #######################
10
    ### Names Section
11
    #######################
12
13
    private static $singular_name = 'Selection of Images';
14
15
    function i18n_singular_name()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
16
    {
17
        return _t('ImagesWithStyleSelection.SINGULAR_NAME', 'Selection of Images');
18
    }
19
20
    private static $plural_name = 'Selections of Images';
21
22
    function i18n_plural_name()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
    {
24
        return _t('ImagesWithStyleSelection.PLURAL_NAME', 'Selections of Images');
25
    }
26
27
28
    #######################
29
    ### Model Section
30
    #######################
31
32
    private static $db = [
33
        'Title' => 'Varchar',
34
        'Description' => 'Text'
35
    ];
36
37
    private static $has_one = [
38
        'PlaceToStoreImages' => 'Folder'
39
    ];
40
41
    private static $many_many = [
42
        'StyledImages' => 'ImageWithStyle'
43
    ];
44
45
    private static $many_many_extraFields = [
46
        'StyledImages' => [
47
            'SortOrder' => 'Int',
48
        ]
49
    ];
50
51
52
53
    #######################
54
    ### Further DB Field Details
55
    #######################
56
57
    private static $indexes = [
58
        'Created' => true,
59
        'Title' => 'unique("Title")'
60
    ];
61
62
    private static $defaults = [
63
        'Title' => ''
64
    ];
65
66
    private static $default_sort = [
67
        'Created' => 'DESC'
68
    ];
69
70
    private static $required_fields = [
71
        'Title'
72
    ];
73
74
    private static $searchable_fields = [
75
        'Title' => 'PartialMatchFilter'
76
    ];
77
78
79
    #######################
80
    ### Field Names and Presentation Section
81
    #######################
82
83
    private static $field_labels = [
84
        'StyledImages' => 'Images to be included'
85
    ];
86
87
    private static $field_labels_right = [
88
        'StyledImages' => 'Select as many as you like and sort them in the right order'
89
    ];
90
91
    private static $summary_fields = [
92
        'Title' => 'Name',
93
        'StyledImages.Count' => 'Number of Images'
94
    ];
95
96
97
    #######################
98
    ### Casting Section
99
    #######################
100
101
102
    #######################
103
    ### can Section
104
    #######################
105
106
107
108
    #######################
109
    ### write Section
110
    #######################
111
112
113
114
115
    public function validate()
116
    {
117
        $result = parent::validate();
118
        $fieldLabels = $this->FieldLabels();
119
        $indexes = $this->Config()->get('indexes');
120
        $requiredFields = $this->Config()->get('required_fields');
121
        if(is_array($requiredFields)) {
122
            foreach($requiredFields as $field) {
123
                $value = $this->$field;
124
                if(! $value) {
125
                    $fieldWithoutID = $field;
126
                    if(substr($fieldWithoutID, -2) === 'ID') {
127
                        $fieldWithoutID = substr($fieldWithoutID, 0, -2);
128
                    }
129
                    $myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID;
130
                    $result->error(
131
                        _t(
132
                            'ImagesWithStyleSelection.'.$field.'_REQUIRED',
133
                            $myName.' is required'
134
                        ),
135
                        'REQUIRED_ImagesWithStyleSelection_'.$field
136
                    );
137
                }
138
                if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') {
139
                    $id = (empty($this->ID) ? 0 : $this->ID);
140
                    $count = ImagesWithStyleSelection::get()
141
                        ->filter(array($field => $value))
142
                        ->exclude(array('ID' => $id))
143
                        ->count();
144
                    if($count > 0) {
145
                        $myName = $fieldLabels['$field'];
146
                        $result->error(
147
                            _t(
148
                                'ImagesWithStyleSelection.'.$field.'_UNIQUE',
149
                                $myName.' needs to be unique'
150
                            ),
151
                            'UNIQUE_ImagesWithStyleSelection_'.$field
152
                        );
153
                    }
154
                }
155
            }
156
        }
157
158
        return $result;
159
    }
160
161
    public function onBeforeWrite()
162
    {
163
        parent::onBeforeWrite();
164
        //...
165
    }
166
167
    public function onAfterWrite()
168
    {
169
        parent::onAfterWrite();
170
        if($this->PlaceToStoreImagesID) {
171
            $folder = $this->PlaceToStoreImages();
172
            $files = Image::get()->filter(['ParentID' => $this->PlaceToStoreImagesID])
173
174
        }
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '}'
Loading history...
175
        //...
176
    }
177
178
    public function requireDefaultRecords()
179
    {
180
        parent::requireDefaultRecords();
181
        //...
182
    }
183
184
185
    #######################
186
    ### Import / Export Section
187
    #######################
188
189
    public function getExportFields()
190
    {
191
        //..
192
        return parent::getExportFields();
193
    }
194
195
196
197
    #######################
198
    ### CMS Edit Section
199
    #######################
200
201
202
    public function CMSEditLink()
203
    {
204
        $controller = singleton("ImageWithStyleAdmin");
205
206
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/".$this->ID."/edit";
207
    }
208
209
    public function CMSAddLink()
210
    {
211
        $controller = singleton("ImageWithStyleAdmin");
212
213
        return $controller->Link().$this->ClassName."/EditForm/field/".$this->ClassName."/item/new";
214
    }
215
216
217
    public function getCMSFields()
218
    {
219
        $fields = parent::getCMSFields();
220
221
        //do first??
222
        $rightFieldDescriptions = $this->Config()->get('field_labels_right');
223
        foreach($rightFieldDescriptions as $field => $desc) {
224
           $formField = $fields->DataFieldByName($field);
225
           if(! $formField) {
226
            $formField = $fields->DataFieldByName($field.'ID');
227
           }
228
           if($formField) {
229
               $formField->setDescription($desc);
230
           }
231
        }
232
        //...
233
        if($this->exists()) {
234
            $config = GridFieldConfig_RelationEditor::create();
235
            $config->addComponent(new GridFieldSortableRows('SortOrder'));
236
            $fields->removeByName('StyledImages');
237
            $fields->addFieldToTab(
238
                    'Root.Images',
239
                    GridField::create(
240
                        'StyledImages',
241
                        'Images',
242
                        $this->StyledImages(),
243
                        $config
244
                    )
245
                );
246
        }
247
        return $fields;
248
    }
249
250
251
    public function RawImages()
252
    {
253
        $array = [];
254
        foreach($this->StyledImages() as $styledImage) {
255
            $array[$styledImage->ImageID] = $styledImage->ImageID;
256
        }
257
    }
258
259
}
260