| Total Complexity | 45 |
| Total Lines | 418 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TagField often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TagField, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class TagField extends DropdownField |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private static $allowed_actions = array( |
||
| 31 | 'suggest' |
||
| 32 | ); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var bool |
||
| 36 | */ |
||
| 37 | protected $shouldLazyLoad = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | protected $lazyLoadItemLimit = 10; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $canCreate = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $titleField = 'Title'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var DataList |
||
| 56 | */ |
||
| 57 | protected $sourceList; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $isMultiple = true; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string $name |
||
| 66 | * @param string $title |
||
| 67 | * @param null|DataList $source |
||
| 68 | * @param null|DataList $value |
||
| 69 | * @param string $titleField |
||
| 70 | */ |
||
| 71 | public function __construct($name, $title = '', $source = [], $value = null, $titleField = 'Title') |
||
| 72 | { |
||
| 73 | $this->setSourceList($source); |
||
| 74 | $this->setTitleField($titleField); |
||
| 75 | parent::__construct($name, $title, $source, $value); |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public function getShouldLazyLoad() |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param bool $shouldLazyLoad |
||
| 88 | * |
||
| 89 | * @return static |
||
| 90 | */ |
||
| 91 | public function setShouldLazyLoad($shouldLazyLoad) |
||
| 92 | { |
||
| 93 | $this->shouldLazyLoad = $shouldLazyLoad; |
||
| 94 | |||
| 95 | return $this; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return int |
||
| 100 | */ |
||
| 101 | public function getLazyLoadItemLimit() |
||
| 102 | { |
||
| 103 | return $this->lazyLoadItemLimit; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param int $lazyLoadItemLimit |
||
| 108 | * |
||
| 109 | * @return static |
||
| 110 | */ |
||
| 111 | public function setLazyLoadItemLimit($lazyLoadItemLimit) |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public function getIsMultiple() |
||
| 122 | { |
||
| 123 | return $this->isMultiple; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param bool $isMultiple |
||
| 128 | * |
||
| 129 | * @return static |
||
| 130 | */ |
||
| 131 | public function setIsMultiple($isMultiple) |
||
| 132 | { |
||
| 133 | $this->isMultiple = $isMultiple; |
||
| 134 | |||
| 135 | return $this; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @return bool |
||
| 140 | */ |
||
| 141 | public function getCanCreate() |
||
| 142 | { |
||
| 143 | return $this->canCreate; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param bool $canCreate |
||
| 148 | * |
||
| 149 | * @return static |
||
| 150 | */ |
||
| 151 | public function setCanCreate($canCreate) |
||
| 152 | { |
||
| 153 | $this->canCreate = $canCreate; |
||
| 154 | |||
| 155 | return $this; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getTitleField() |
||
| 162 | { |
||
| 163 | return $this->titleField; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $titleField |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function setTitleField($titleField) |
||
| 172 | { |
||
| 173 | $this->titleField = $titleField; |
||
| 174 | |||
| 175 | return $this; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the DataList source. The 4.x upgrade for SelectField::setSource starts to convert this to an array |
||
| 180 | * @return DataList |
||
| 181 | */ |
||
| 182 | public function getSourceList() |
||
| 183 | { |
||
| 184 | return $this->sourceList; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Set the model class name for tags |
||
| 189 | * @param DataList $className |
||
| 190 | * @return self |
||
| 191 | */ |
||
| 192 | public function setSourceList($sourceList) |
||
| 193 | { |
||
| 194 | $this->sourceList = $sourceList; |
||
| 195 | return $this; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | public function Field($properties = array()) |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | protected function getSuggestURL() |
||
| 236 | { |
||
| 237 | return Controller::join_links($this->Link(), 'suggest'); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param bool $onlySelected Only return options that are selected |
||
| 242 | * @return ArrayList |
||
| 243 | */ |
||
| 244 | protected function getOptions($onlySelected = false) |
||
| 245 | { |
||
| 246 | $source = $this->getSourceList(); |
||
| 247 | |||
| 248 | if (!$source) { |
||
|
|
|||
| 249 | $source = ArrayList::create(); |
||
| 250 | } |
||
| 251 | |||
| 252 | $dataClass = $source->dataClass(); |
||
| 253 | $titleField = $this->getTitleField(); |
||
| 254 | $values = $this->Value(); |
||
| 255 | |||
| 256 | if ($values) { |
||
| 257 | if (is_array($values)) { |
||
| 258 | $values = DataList::create($dataClass)->filter($titleField, $values); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | if ($onlySelected) { |
||
| 262 | $source = $values; |
||
| 263 | } |
||
| 264 | |||
| 265 | return $source instanceof DataList ? $this->formatOptions($source) : ArrayList::create(); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param DataList $source |
||
| 270 | * @return ArrayList |
||
| 271 | */ |
||
| 272 | protected function formatOptions(DataList $source) |
||
| 273 | { |
||
| 274 | $options = ArrayList::create(); |
||
| 275 | $titleField = $this->getTitleField(); |
||
| 276 | |||
| 277 | foreach ($source as $object) { |
||
| 278 | $options->push( |
||
| 279 | ArrayData::create(array( |
||
| 280 | 'Title' => $object->$titleField, |
||
| 281 | 'Value' => $object->Title, |
||
| 282 | )) |
||
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | return $options; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | */ |
||
| 292 | public function setValue($value, $source = null) |
||
| 293 | { |
||
| 294 | if ($source instanceof DataObject) { |
||
| 295 | $name = $this->getName(); |
||
| 296 | |||
| 297 | if ($source->hasMethod($name)) { |
||
| 298 | $value = $source->$name()->column($this->getTitleField()); |
||
| 299 | } |
||
| 300 | } elseif ($value instanceof SS_List) { |
||
| 301 | $value = $value->column($this->getTitleField()); |
||
| 302 | } |
||
| 303 | |||
| 304 | if (!is_array($value)) { |
||
| 305 | return parent::setValue($value); |
||
| 306 | } |
||
| 307 | |||
| 308 | return parent::setValue(array_filter($value)); |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * {@inheritdoc} |
||
| 313 | */ |
||
| 314 | public function saveInto(DataObjectInterface $record) |
||
| 315 | { |
||
| 316 | parent::saveInto($record); |
||
| 317 | |||
| 318 | $name = $this->getName(); |
||
| 319 | $titleField = $this->getTitleField(); |
||
| 320 | $source = $this->getSource(); |
||
| 321 | $values = $this->Value(); |
||
| 322 | $relation = $record->$name(); |
||
| 323 | $ids = array(); |
||
| 324 | |||
| 325 | if (!$values) { |
||
| 326 | $values = array(); |
||
| 327 | } |
||
| 328 | if (empty($record) || empty($titleField)) { |
||
| 329 | return; |
||
| 330 | } |
||
| 331 | |||
| 332 | if (!$record->hasMethod($name)) { |
||
| 333 | throw new Exception( |
||
| 334 | sprintf("%s does not have a %s method", get_class($record), $name) |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | |||
| 338 | foreach ($values as $key => $value) { |
||
| 339 | // Get or create record |
||
| 340 | $record = $this->getOrCreateTag($value); |
||
| 341 | if ($record) { |
||
| 342 | $ids[] = $record->ID; |
||
| 343 | $values[$key] = $record->Title; |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | $relation->setByIDList(array_filter($ids)); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get or create tag with the given value |
||
| 352 | * |
||
| 353 | * @param string $term |
||
| 354 | * @return DataObject |
||
| 355 | */ |
||
| 356 | protected function getOrCreateTag($term) |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Returns a JSON string of tags, for lazy loading. |
||
| 383 | * |
||
| 384 | * @param HTTPRequest $request |
||
| 385 | * @return HTTPResponse |
||
| 386 | */ |
||
| 387 | public function suggest(HTTPRequest $request) |
||
| 388 | { |
||
| 389 | $tags = $this->getTags($request->getVar('term')); |
||
| 390 | |||
| 391 | $response = new HTTPResponse(); |
||
| 392 | $response->addHeader('Content-Type', 'application/json'); |
||
| 393 | $response->setBody(json_encode(array('items' => $tags))); |
||
| 394 | |||
| 395 | return $response; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns array of arrays representing tags. |
||
| 400 | * |
||
| 401 | * @param string $term |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | protected function getTags($term) |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * DropdownField assumes value will be a scalar so we must |
||
| 423 | * override validate. This only applies to Silverstripe 3.2+ |
||
| 424 | * |
||
| 425 | * @param Validator $validator |
||
| 426 | * @return bool |
||
| 427 | */ |
||
| 428 | public function validate($validator) |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Converts the field to a readonly variant. |
||
| 435 | * |
||
| 436 | * @return TagField_Readonly |
||
| 437 | */ |
||
| 438 | public function performReadonlyTransformation() |
||
| 443 | } |
||
| 444 | } |
||
| 445 |