| Total Complexity | 44 |
| Total Lines | 459 |
| 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 |
||
| 24 | class TagField extends MultiSelectField |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private static $allowed_actions = array( |
||
| 30 | 'suggest' |
||
| 31 | ); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | protected $shouldLazyLoad = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $lazyLoadItemLimit = 10; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $canCreate = true; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $titleField = 'Title'; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var DataList |
||
| 55 | */ |
||
| 56 | protected $sourceList; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | protected $isMultiple = true; |
||
| 62 | |||
| 63 | /** @skipUpgrade */ |
||
| 64 | protected $schemaComponent = 'TagField'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $name |
||
| 68 | * @param string $title |
||
| 69 | * @param null|DataList $source |
||
| 70 | * @param null|DataList $value |
||
| 71 | * @param string $titleField |
||
| 72 | */ |
||
| 73 | public function __construct($name, $title = '', $source = [], $value = null, $titleField = 'Title') |
||
| 74 | { |
||
| 75 | $this->setSourceList($source); |
||
| 76 | $this->setTitleField($titleField); |
||
| 77 | parent::__construct($name, $title, $source, $value); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @return bool |
||
| 82 | */ |
||
| 83 | public function getShouldLazyLoad() |
||
| 84 | { |
||
| 85 | return $this->shouldLazyLoad; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param bool $shouldLazyLoad |
||
| 90 | * |
||
| 91 | * @return static |
||
| 92 | */ |
||
| 93 | public function setShouldLazyLoad($shouldLazyLoad) |
||
| 94 | { |
||
| 95 | $this->shouldLazyLoad = $shouldLazyLoad; |
||
| 96 | |||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | public function getLazyLoadItemLimit() |
||
| 104 | { |
||
| 105 | return $this->lazyLoadItemLimit; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param int $lazyLoadItemLimit |
||
| 110 | * |
||
| 111 | * @return static |
||
| 112 | */ |
||
| 113 | public function setLazyLoadItemLimit($lazyLoadItemLimit) |
||
| 114 | { |
||
| 115 | $this->lazyLoadItemLimit = $lazyLoadItemLimit; |
||
| 116 | |||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function getIsMultiple() |
||
| 124 | { |
||
| 125 | return $this->isMultiple; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param bool $isMultiple |
||
| 130 | * |
||
| 131 | * @return static |
||
| 132 | */ |
||
| 133 | public function setIsMultiple($isMultiple) |
||
| 134 | { |
||
| 135 | $this->isMultiple = $isMultiple; |
||
| 136 | |||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function getCanCreate() |
||
| 144 | { |
||
| 145 | return $this->canCreate; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param bool $canCreate |
||
| 150 | * |
||
| 151 | * @return static |
||
| 152 | */ |
||
| 153 | public function setCanCreate($canCreate) |
||
| 154 | { |
||
| 155 | $this->canCreate = $canCreate; |
||
| 156 | |||
| 157 | return $this; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | public function getTitleField() |
||
| 164 | { |
||
| 165 | return $this->titleField; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $titleField |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | public function setTitleField($titleField) |
||
| 174 | { |
||
| 175 | $this->titleField = $titleField; |
||
| 176 | |||
| 177 | return $this; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get the DataList source. The 4.x upgrade for SelectField::setSource starts to convert this to an array |
||
| 182 | * @return DataList |
||
| 183 | */ |
||
| 184 | public function getSourceList() |
||
| 185 | { |
||
| 186 | return $this->sourceList; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set the model class name for tags |
||
| 191 | * @param DataList $className |
||
| 192 | * @return self |
||
| 193 | */ |
||
| 194 | public function setSourceList($sourceList) |
||
| 195 | { |
||
| 196 | $this->sourceList = $sourceList; |
||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * {@inheritdoc} |
||
| 202 | */ |
||
| 203 | public function Field($properties = array()) |
||
| 204 | { |
||
| 205 | Requirements::css('silverstripe/tagfield:css/select2.min.css'); |
||
| 206 | Requirements::css('silverstripe/tagfield:css/TagField.css'); |
||
| 207 | |||
| 208 | Requirements::javascript('silverstripe/tagfield:js/select2.js'); |
||
| 209 | Requirements::javascript('silverstripe/tagfield:js/TagField.js'); |
||
| 210 | |||
| 211 | $this->addExtraClass('ss-tag-field'); |
||
| 212 | |||
| 213 | if ($this->getIsMultiple()) { |
||
| 214 | $this->setAttribute('multiple', 'multiple'); |
||
| 215 | } |
||
| 216 | |||
| 217 | if ($this->shouldLazyLoad) { |
||
| 218 | $this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL()); |
||
| 219 | } else { |
||
| 220 | $properties = array_merge($properties, array( |
||
| 221 | 'Options' => $this->getOptions() |
||
| 222 | )); |
||
| 223 | } |
||
| 224 | |||
| 225 | $this->setAttribute('data-can-create', (int) $this->getCanCreate()); |
||
| 226 | |||
| 227 | return $this |
||
| 228 | ->customise($properties) |
||
| 229 | ->renderWith(self::class); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | protected function getSuggestURL() |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @return ArrayList |
||
| 242 | */ |
||
| 243 | protected function getOptions() |
||
| 244 | { |
||
| 245 | $options = ArrayList::create(); |
||
| 246 | |||
| 247 | $source = $this->getSourceList(); |
||
| 248 | |||
| 249 | if (!$source) { |
||
|
|
|||
| 250 | $source = ArrayList::create(); |
||
| 251 | } |
||
| 252 | |||
| 253 | $dataClass = $source->dataClass(); |
||
| 254 | |||
| 255 | $values = $this->Value(); |
||
| 256 | |||
| 257 | if (!$values) { |
||
| 258 | return $options; |
||
| 259 | } |
||
| 260 | |||
| 261 | if (is_array($values)) { |
||
| 262 | $values = DataList::create($dataClass)->filter($this->getTitleField(), $values); |
||
| 263 | } |
||
| 264 | |||
| 265 | $ids = $values->column($this->getTitleField()); |
||
| 266 | |||
| 267 | $titleField = $this->getTitleField(); |
||
| 268 | |||
| 269 | foreach ($source as $object) { |
||
| 270 | $options->push( |
||
| 271 | ArrayData::create(array( |
||
| 272 | 'Title' => $object->$titleField, |
||
| 273 | 'Value' => $object->ID, |
||
| 274 | 'Selected' => in_array($object->$titleField, $ids), |
||
| 275 | )) |
||
| 276 | ); |
||
| 277 | } |
||
| 278 | |||
| 279 | return $options; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritdoc} |
||
| 284 | */ |
||
| 285 | public function setValue($value, $source = null) |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * {@inheritdoc} |
||
| 315 | */ |
||
| 316 | public function getAttributes() |
||
| 323 | ] |
||
| 324 | ); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * {@inheritdoc} |
||
| 329 | */ |
||
| 330 | public function saveInto(DataObjectInterface $record) |
||
| 331 | { |
||
| 332 | $name = $this->getName(); |
||
| 333 | $titleField = $this->getTitleField(); |
||
| 334 | $source = $this->getSource(); |
||
| 335 | $values = $this->Value(); |
||
| 336 | $relation = $record->$name(); |
||
| 337 | $ids = array(); |
||
| 338 | |||
| 339 | if (!$values) { |
||
| 340 | $values = array(); |
||
| 341 | } |
||
| 342 | |||
| 343 | if (empty($record) || empty($titleField)) { |
||
| 344 | return; |
||
| 345 | } |
||
| 346 | |||
| 347 | if (!$record->hasMethod($name)) { |
||
| 348 | throw new Exception( |
||
| 349 | sprintf("%s does not have a %s method", get_class($record), $name) |
||
| 350 | ); |
||
| 351 | } |
||
| 352 | |||
| 353 | foreach ($values as $key => $value) { |
||
| 354 | // Get or create record |
||
| 355 | $record = $this->getOrCreateTag($value); |
||
| 356 | if ($record) { |
||
| 357 | $ids[] = $record->ID; |
||
| 358 | $values[$key] = $record->Title; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | $relation->setByIDList(array_filter($ids)); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Get or create tag with the given value |
||
| 367 | * |
||
| 368 | * @param string $term |
||
| 369 | * @return DataObject |
||
| 370 | */ |
||
| 371 | protected function getOrCreateTag($term) |
||
| 393 | } |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Returns a JSON string of tags, for lazy loading. |
||
| 398 | * |
||
| 399 | * @param HTTPRequest $request |
||
| 400 | * @return HTTPResponse |
||
| 401 | */ |
||
| 402 | public function suggest(HTTPRequest $request) |
||
| 403 | { |
||
| 404 | $tags = $this->getTags($request->getVar('term')); |
||
| 405 | |||
| 406 | $response = new HTTPResponse(); |
||
| 407 | $response->addHeader('Content-Type', 'application/json'); |
||
| 408 | $response->setBody(json_encode(array('items' => $tags))); |
||
| 409 | |||
| 410 | return $response; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Returns array of arrays representing tags. |
||
| 415 | * |
||
| 416 | * @param string $term |
||
| 417 | * @return array |
||
| 418 | */ |
||
| 419 | protected function getTags($term) |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * DropdownField assumes value will be a scalar so we must |
||
| 448 | * override validate. This only applies to Silverstripe 3.2+ |
||
| 449 | * |
||
| 450 | * @param Validator $validator |
||
| 451 | * @return bool |
||
| 452 | */ |
||
| 453 | public function validate($validator) |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Converts the field to a readonly variant. |
||
| 460 | * |
||
| 461 | * @return TagField_Readonly |
||
| 462 | */ |
||
| 463 | public function performReadonlyTransformation() |
||
| 468 | } |
||
| 469 | |||
| 470 | public function getSchemaStateDefaults() |
||
| 486 |