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