Code Duplication    Length = 45-45 lines in 2 locations

code/model/ImageStyle.php 1 location

@@ 179-223 (lines=45) @@
176
177
178
179
    public function validate()
180
    {
181
        $result = parent::validate();
182
        $fieldLabels = $this->FieldLabels();
183
        $indexes = $this->Config()->get('indexes');
184
        $requiredFields = $this->Config()->get('required_fields');
185
        if(is_array($requiredFields)) {
186
            foreach($requiredFields as $field) {
187
                $value = $this->$field;
188
                if(! $value) {
189
                    $fieldWithoutID = $field;
190
                    if(substr($fieldWithoutID, -2) === 'ID') {
191
                        $fieldWithoutID = substr($fieldWithoutID, 0, -2);
192
                    }
193
                    $myName = isset($fieldLabels[$fieldWithoutID]) ? $fieldLabels[$fieldWithoutID] : $fieldWithoutID;
194
                    $result->error(
195
                        _t(
196
                            'ImageStyle.'.$field.'_REQUIRED',
197
                            $myName.' is required'
198
                        ),
199
                        'REQUIRED_ImageStyle_'.$field
200
                    );
201
                }
202
                if (isset($indexes[$field]) && isset($indexes[$field]['type']) && $indexes[$field]['type'] === 'unique') {
203
                    $id = (empty($this->ID) ? 0 : $this->ID);
204
                    $count = ImageStyle::get()
205
                        ->filter(array($field => $value))
206
                        ->exclude(array('ID' => $id))
207
                        ->count();
208
                    if($count > 0) {
209
                        $myName = $fieldLabels['$field'];
210
                        $result->error(
211
                            _t(
212
                                'ImageStyle.'.$field.'_UNIQUE',
213
                                $myName.' needs to be unique'
214
                            ),
215
                            'UNIQUE_ImageStyle_'.$field
216
                        );
217
                    }
218
                }
219
            }
220
        }
221
222
        return $result;
223
    }
224
225
    public function onBeforeWrite()
226
    {

code/model/ImagesWithStyleSelection.php 1 location

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