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