Code Duplication    Length = 45-45 lines in 2 locations

code/model/ImagesWithStyleSelection.php 1 location

@@ 115-159 (lines=45) @@
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
162
    public function onBeforeWrite()

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
    {