Code Duplication    Length = 45-45 lines in 2 locations

code/model/ImageStyle.php 1 location

@@ 195-239 (lines=45) @@
192
193
194
195
    public function validate()
196
    {
197
        $result = parent::validate();
198
        $fieldLabels = $this->FieldLabels();
199
        $indexes = $this->Config()->get('indexes');
200
        $requiredFields = $this->Config()->get('required_fields');
201
        if (is_array($requiredFields)) {
202
            foreach ($requiredFields as $field) {
203
                $value = $this->$field;
204
                if (! $value) {
205
                    $fieldWithoutID = $field;
206
                    if (substr($fieldWithoutID, -2) === 'ID') {
207
                        $fieldWithoutID = substr($fieldWithoutID, 0, -2);
208
                    }
209
                    $myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID;
210
                    $result->error(
211
                        _t(
212
                            'ImageStyle.'.$field.'_REQUIRED',
213
                            $myName.' is required'
214
                        ),
215
                        'REQUIRED_ImageStyle_'.$field
216
                    );
217
                }
218
                if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') {
219
                    $id = (empty($this->ID) ? 0 : $this->ID);
220
                    $count = ImageStyle::get()
221
                        ->filter(array($field => $value))
222
                        ->exclude(array('ID' => $id))
223
                        ->count();
224
                    if ($count > 0) {
225
                        $myName = $fieldLabels['$field'];
226
                        $result->error(
227
                            _t(
228
                                'ImageStyle.'.$field.'_UNIQUE',
229
                                $myName.' needs to be unique'
230
                            ),
231
                            'UNIQUE_ImageStyle_'.$field
232
                        );
233
                    }
234
                }
235
            }
236
        }
237
238
        return $result;
239
    }
240
241
    public function onBeforeWrite()
242
    {

code/model/ImagesWithStyleSelection.php 1 location

@@ 118-162 (lines=45) @@
115
116
117
118
    public function validate()
119
    {
120
        $result = parent::validate();
121
        $fieldLabels = $this->FieldLabels();
122
        $indexes = $this->Config()->get('indexes');
123
        $requiredFields = $this->Config()->get('required_fields');
124
        if (is_array($requiredFields)) {
125
            foreach ($requiredFields as $field) {
126
                $value = $this->$field;
127
                if (! $value) {
128
                    $fieldWithoutID = $field;
129
                    if (substr($fieldWithoutID, -2) === 'ID') {
130
                        $fieldWithoutID = substr($fieldWithoutID, 0, -2);
131
                    }
132
                    $myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID;
133
                    $result->error(
134
                        _t(
135
                            'ImagesWithStyleSelection.'.$field.'_REQUIRED',
136
                            $myName.' is required'
137
                        ),
138
                        'REQUIRED_ImagesWithStyleSelection_'.$field
139
                    );
140
                }
141
                if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') {
142
                    $id = (empty($this->ID) ? 0 : $this->ID);
143
                    $count = ImagesWithStyleSelection::get()
144
                        ->filter(array($field => $value))
145
                        ->exclude(array('ID' => $id))
146
                        ->count();
147
                    if ($count > 0) {
148
                        $myName = $fieldLabels['$field'];
149
                        $result->error(
150
                            _t(
151
                                'ImagesWithStyleSelection.'.$field.'_UNIQUE',
152
                                $myName.' needs to be unique'
153
                            ),
154
                            'UNIQUE_ImagesWithStyleSelection_'.$field
155
                        );
156
                    }
157
                }
158
            }
159
        }
160
161
        return $result;
162
    }
163
164
165
    public function onBeforeWrite()