@@ -305,7 +305,7 @@ discard block |
||
| 305 | 305 | return array_merge( |
| 306 | 306 | parent::getAttributes(), |
| 307 | 307 | [ |
| 308 | - 'name' => $this->getName() . '[]', |
|
| 308 | + 'name' => $this->getName().'[]', |
|
| 309 | 309 | 'style'=> 'width: 100%' |
| 310 | 310 | ] |
| 311 | 311 | ); |
@@ -414,7 +414,7 @@ discard block |
||
| 414 | 414 | $titleField = $this->getTitleField(); |
| 415 | 415 | |
| 416 | 416 | $query = $source |
| 417 | - ->filter($titleField . ':PartialMatch:nocase', $term) |
|
| 417 | + ->filter($titleField.':PartialMatch:nocase', $term) |
|
| 418 | 418 | ->sort($titleField) |
| 419 | 419 | ->limit($this->getLazyLoadItemLimit()); |
| 420 | 420 | |
@@ -23,434 +23,434 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class TagField extends DropdownField |
| 25 | 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 | - /** |
|
| 64 | - * @param string $name |
|
| 65 | - * @param string $title |
|
| 66 | - * @param null|DataList $source |
|
| 67 | - * @param null|DataList $value |
|
| 68 | - */ |
|
| 69 | - public function __construct($name, $title = '', $source = [], $value = null) |
|
| 70 | - { |
|
| 71 | - $this->setSourceList($source); |
|
| 72 | - parent::__construct($name, $title, $source, $value); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @return bool |
|
| 77 | - */ |
|
| 78 | - public function getShouldLazyLoad() |
|
| 79 | - { |
|
| 80 | - return $this->shouldLazyLoad; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @param bool $shouldLazyLoad |
|
| 85 | - * |
|
| 86 | - * @return static |
|
| 87 | - */ |
|
| 88 | - public function setShouldLazyLoad($shouldLazyLoad) |
|
| 89 | - { |
|
| 90 | - $this->shouldLazyLoad = $shouldLazyLoad; |
|
| 91 | - |
|
| 92 | - return $this; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @return int |
|
| 97 | - */ |
|
| 98 | - public function getLazyLoadItemLimit() |
|
| 99 | - { |
|
| 100 | - return $this->lazyLoadItemLimit; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param int $lazyLoadItemLimit |
|
| 105 | - * |
|
| 106 | - * @return static |
|
| 107 | - */ |
|
| 108 | - public function setLazyLoadItemLimit($lazyLoadItemLimit) |
|
| 109 | - { |
|
| 110 | - $this->lazyLoadItemLimit = $lazyLoadItemLimit; |
|
| 111 | - |
|
| 112 | - return $this; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * @return bool |
|
| 117 | - */ |
|
| 118 | - public function getIsMultiple() |
|
| 119 | - { |
|
| 120 | - return $this->isMultiple; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @param bool $isMultiple |
|
| 125 | - * |
|
| 126 | - * @return static |
|
| 127 | - */ |
|
| 128 | - public function setIsMultiple($isMultiple) |
|
| 129 | - { |
|
| 130 | - $this->isMultiple = $isMultiple; |
|
| 131 | - |
|
| 132 | - return $this; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @return bool |
|
| 137 | - */ |
|
| 138 | - public function getCanCreate() |
|
| 139 | - { |
|
| 140 | - return $this->canCreate; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @param bool $canCreate |
|
| 145 | - * |
|
| 146 | - * @return static |
|
| 147 | - */ |
|
| 148 | - public function setCanCreate($canCreate) |
|
| 149 | - { |
|
| 150 | - $this->canCreate = $canCreate; |
|
| 151 | - |
|
| 152 | - return $this; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @return string |
|
| 157 | - */ |
|
| 158 | - public function getTitleField() |
|
| 159 | - { |
|
| 160 | - return $this->titleField; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @param string $titleField |
|
| 165 | - * |
|
| 166 | - * @return $this |
|
| 167 | - */ |
|
| 168 | - public function setTitleField($titleField) |
|
| 169 | - { |
|
| 170 | - $this->titleField = $titleField; |
|
| 171 | - |
|
| 172 | - return $this; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Get the DataList source. The 4.x upgrade for SelectField::setSource starts to convert this to an array |
|
| 177 | - * @return DataList |
|
| 178 | - */ |
|
| 179 | - public function getSourceList() |
|
| 180 | - { |
|
| 181 | - return $this->sourceList; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Set the model class name for tags |
|
| 186 | - * @param DataList $className |
|
| 187 | - * @return self |
|
| 188 | - */ |
|
| 189 | - public function setSourceList($sourceList) |
|
| 190 | - { |
|
| 191 | - $this->sourceList = $sourceList; |
|
| 192 | - return $this; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * {@inheritdoc} |
|
| 197 | - */ |
|
| 198 | - public function Field($properties = array()) |
|
| 199 | - { |
|
| 200 | - Requirements::css('silverstripe/tagfield:css/select2.min.css'); |
|
| 201 | - Requirements::css('silverstripe/tagfield:css/TagField.css'); |
|
| 202 | - |
|
| 203 | - Requirements::javascript('silverstripe/tagfield:js/select2.js'); |
|
| 204 | - Requirements::javascript('silverstripe/tagfield:js/TagField.js'); |
|
| 205 | - |
|
| 206 | - $this->addExtraClass('ss-tag-field'); |
|
| 207 | - |
|
| 208 | - if ($this->getIsMultiple()) { |
|
| 209 | - $this->setAttribute('multiple', 'multiple'); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - if ($this->shouldLazyLoad) { |
|
| 213 | - $this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL()); |
|
| 214 | - } else { |
|
| 215 | - $properties = array_merge($properties, array( |
|
| 216 | - 'Options' => $this->getOptions() |
|
| 217 | - )); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - $this->setAttribute('data-can-create', (int) $this->getCanCreate()); |
|
| 221 | - |
|
| 222 | - return $this |
|
| 223 | - ->customise($properties) |
|
| 224 | - ->renderWith(self::class); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * @return string |
|
| 229 | - */ |
|
| 230 | - protected function getSuggestURL() |
|
| 231 | - { |
|
| 232 | - return Controller::join_links($this->Link(), 'suggest'); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @return ArrayList |
|
| 237 | - */ |
|
| 238 | - protected function getOptions() |
|
| 239 | - { |
|
| 240 | - $options = ArrayList::create(); |
|
| 241 | - |
|
| 242 | - $source = $this->getSourceList(); |
|
| 243 | - |
|
| 244 | - if (!$source) { |
|
| 245 | - $source = ArrayList::create(); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - $dataClass = $source->dataClass(); |
|
| 249 | - |
|
| 250 | - $values = $this->Value(); |
|
| 251 | - |
|
| 252 | - if (!$values) { |
|
| 253 | - return $options; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - if (is_array($values)) { |
|
| 257 | - $values = DataList::create($dataClass)->filter('Title', $values); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - $ids = $values->column('Title'); |
|
| 261 | - |
|
| 262 | - $titleField = $this->getTitleField(); |
|
| 263 | - |
|
| 264 | - foreach ($source as $object) { |
|
| 265 | - $options->push( |
|
| 266 | - ArrayData::create(array( |
|
| 267 | - 'Title' => $object->$titleField, |
|
| 268 | - 'Value' => $object->Title, |
|
| 269 | - 'Selected' => in_array($object->Title, $ids), |
|
| 270 | - )) |
|
| 271 | - ); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - return $options; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * {@inheritdoc} |
|
| 279 | - */ |
|
| 280 | - public function setValue($value, $source = null) |
|
| 281 | - { |
|
| 282 | - if ($source instanceof DataObject) { |
|
| 283 | - $name = $this->getName(); |
|
| 284 | - |
|
| 285 | - if ($source->hasMethod($name)) { |
|
| 286 | - $value = $source->$name()->column('Title'); |
|
| 287 | - } |
|
| 288 | - } elseif ($value instanceof SS_List) { |
|
| 289 | - $value = $value->column('Title'); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - if (!is_array($value)) { |
|
| 293 | - return parent::setValue($value); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - return parent::setValue(array_filter($value)); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * {@inheritdoc} |
|
| 301 | - */ |
|
| 302 | - public function getAttributes() |
|
| 303 | - { |
|
| 304 | - return array_merge( |
|
| 305 | - parent::getAttributes(), |
|
| 306 | - [ |
|
| 307 | - 'name' => $this->getName() . '[]', |
|
| 308 | - 'style'=> 'width: 100%' |
|
| 309 | - ] |
|
| 310 | - ); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * {@inheritdoc} |
|
| 315 | - */ |
|
| 316 | - public function saveInto(DataObjectInterface $record) |
|
| 317 | - { |
|
| 318 | - parent::saveInto($record); |
|
| 319 | - |
|
| 320 | - $name = $this->getName(); |
|
| 321 | - $titleField = $this->getTitleField(); |
|
| 322 | - $source = $this->getSource(); |
|
| 323 | - $values = $this->Value(); |
|
| 324 | - $relation = $record->$name(); |
|
| 325 | - $ids = array(); |
|
| 326 | - |
|
| 327 | - if (!$values) { |
|
| 328 | - $values = array(); |
|
| 329 | - } |
|
| 330 | - if (empty($record) || empty($titleField)) { |
|
| 331 | - return; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - if (!$record->hasMethod($name)) { |
|
| 335 | - throw new Exception( |
|
| 336 | - sprintf("%s does not have a %s method", get_class($record), $name) |
|
| 337 | - ); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - foreach ($values as $key => $value) { |
|
| 341 | - // Get or create record |
|
| 342 | - $record = $this->getOrCreateTag($value); |
|
| 343 | - if ($record) { |
|
| 344 | - $ids[] = $record->ID; |
|
| 345 | - $values[$key] = $record->Title; |
|
| 346 | - } |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - $relation->setByIDList(array_filter($ids)); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Get or create tag with the given value |
|
| 354 | - * |
|
| 355 | - * @param string $term |
|
| 356 | - * @return DataObject |
|
| 357 | - */ |
|
| 358 | - protected function getOrCreateTag($term) |
|
| 359 | - { |
|
| 360 | - // Check if existing record can be found |
|
| 361 | - /** @var DataList $source */ |
|
| 362 | - $source = $this->getSourceList(); |
|
| 363 | - $titleField = $this->getTitleField(); |
|
| 364 | - $record = $source |
|
| 365 | - ->filter($titleField, $term) |
|
| 366 | - ->first(); |
|
| 367 | - if ($record) { |
|
| 368 | - return $record; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - // Create new instance if not yet saved |
|
| 372 | - if ($this->getCanCreate()) { |
|
| 373 | - $dataClass = $source->dataClass(); |
|
| 374 | - $record = Injector::inst()->create($dataClass); |
|
| 375 | - $record->{$titleField} = $term; |
|
| 376 | - $record->write(); |
|
| 377 | - return $record; |
|
| 378 | - } else { |
|
| 379 | - return false; |
|
| 380 | - } |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Returns a JSON string of tags, for lazy loading. |
|
| 385 | - * |
|
| 386 | - * @param HTTPRequest $request |
|
| 387 | - * @return HTTPResponse |
|
| 388 | - */ |
|
| 389 | - public function suggest(HTTPRequest $request) |
|
| 390 | - { |
|
| 391 | - $tags = $this->getTags($request->getVar('term')); |
|
| 392 | - |
|
| 393 | - $response = new HTTPResponse(); |
|
| 394 | - $response->addHeader('Content-Type', 'application/json'); |
|
| 395 | - $response->setBody(json_encode(array('items' => $tags))); |
|
| 396 | - |
|
| 397 | - return $response; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * Returns array of arrays representing tags. |
|
| 402 | - * |
|
| 403 | - * @param string $term |
|
| 404 | - * @return array |
|
| 405 | - */ |
|
| 406 | - protected function getTags($term) |
|
| 407 | - { |
|
| 408 | - /** |
|
| 409 | - * @var array $source |
|
| 410 | - */ |
|
| 411 | - $source = $this->getSourceList(); |
|
| 412 | - |
|
| 413 | - $titleField = $this->getTitleField(); |
|
| 414 | - |
|
| 415 | - $query = $source |
|
| 416 | - ->filter($titleField . ':PartialMatch:nocase', $term) |
|
| 417 | - ->sort($titleField) |
|
| 418 | - ->limit($this->getLazyLoadItemLimit()); |
|
| 419 | - |
|
| 420 | - // Map into a distinct list |
|
| 421 | - $items = array(); |
|
| 422 | - $titleField = $this->getTitleField(); |
|
| 423 | - foreach ($query->map('ID', $titleField) as $id => $title) { |
|
| 424 | - $items[$title] = array( |
|
| 425 | - 'id' => $title, |
|
| 426 | - 'text' => $title |
|
| 427 | - ); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - return array_values($items); |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * DropdownField assumes value will be a scalar so we must |
|
| 435 | - * override validate. This only applies to Silverstripe 3.2+ |
|
| 436 | - * |
|
| 437 | - * @param Validator $validator |
|
| 438 | - * @return bool |
|
| 439 | - */ |
|
| 440 | - public function validate($validator) |
|
| 441 | - { |
|
| 442 | - return true; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Converts the field to a readonly variant. |
|
| 447 | - * |
|
| 448 | - * @return TagField_Readonly |
|
| 449 | - */ |
|
| 450 | - public function performReadonlyTransformation() |
|
| 451 | - { |
|
| 452 | - $copy = $this->castedCopy(ReadonlyTagField::class); |
|
| 453 | - $copy->setSourceList($this->getSourceList()); |
|
| 454 | - return $copy; |
|
| 455 | - } |
|
| 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 | + /** |
|
| 64 | + * @param string $name |
|
| 65 | + * @param string $title |
|
| 66 | + * @param null|DataList $source |
|
| 67 | + * @param null|DataList $value |
|
| 68 | + */ |
|
| 69 | + public function __construct($name, $title = '', $source = [], $value = null) |
|
| 70 | + { |
|
| 71 | + $this->setSourceList($source); |
|
| 72 | + parent::__construct($name, $title, $source, $value); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @return bool |
|
| 77 | + */ |
|
| 78 | + public function getShouldLazyLoad() |
|
| 79 | + { |
|
| 80 | + return $this->shouldLazyLoad; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @param bool $shouldLazyLoad |
|
| 85 | + * |
|
| 86 | + * @return static |
|
| 87 | + */ |
|
| 88 | + public function setShouldLazyLoad($shouldLazyLoad) |
|
| 89 | + { |
|
| 90 | + $this->shouldLazyLoad = $shouldLazyLoad; |
|
| 91 | + |
|
| 92 | + return $this; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @return int |
|
| 97 | + */ |
|
| 98 | + public function getLazyLoadItemLimit() |
|
| 99 | + { |
|
| 100 | + return $this->lazyLoadItemLimit; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param int $lazyLoadItemLimit |
|
| 105 | + * |
|
| 106 | + * @return static |
|
| 107 | + */ |
|
| 108 | + public function setLazyLoadItemLimit($lazyLoadItemLimit) |
|
| 109 | + { |
|
| 110 | + $this->lazyLoadItemLimit = $lazyLoadItemLimit; |
|
| 111 | + |
|
| 112 | + return $this; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * @return bool |
|
| 117 | + */ |
|
| 118 | + public function getIsMultiple() |
|
| 119 | + { |
|
| 120 | + return $this->isMultiple; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param bool $isMultiple |
|
| 125 | + * |
|
| 126 | + * @return static |
|
| 127 | + */ |
|
| 128 | + public function setIsMultiple($isMultiple) |
|
| 129 | + { |
|
| 130 | + $this->isMultiple = $isMultiple; |
|
| 131 | + |
|
| 132 | + return $this; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @return bool |
|
| 137 | + */ |
|
| 138 | + public function getCanCreate() |
|
| 139 | + { |
|
| 140 | + return $this->canCreate; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @param bool $canCreate |
|
| 145 | + * |
|
| 146 | + * @return static |
|
| 147 | + */ |
|
| 148 | + public function setCanCreate($canCreate) |
|
| 149 | + { |
|
| 150 | + $this->canCreate = $canCreate; |
|
| 151 | + |
|
| 152 | + return $this; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @return string |
|
| 157 | + */ |
|
| 158 | + public function getTitleField() |
|
| 159 | + { |
|
| 160 | + return $this->titleField; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @param string $titleField |
|
| 165 | + * |
|
| 166 | + * @return $this |
|
| 167 | + */ |
|
| 168 | + public function setTitleField($titleField) |
|
| 169 | + { |
|
| 170 | + $this->titleField = $titleField; |
|
| 171 | + |
|
| 172 | + return $this; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Get the DataList source. The 4.x upgrade for SelectField::setSource starts to convert this to an array |
|
| 177 | + * @return DataList |
|
| 178 | + */ |
|
| 179 | + public function getSourceList() |
|
| 180 | + { |
|
| 181 | + return $this->sourceList; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Set the model class name for tags |
|
| 186 | + * @param DataList $className |
|
| 187 | + * @return self |
|
| 188 | + */ |
|
| 189 | + public function setSourceList($sourceList) |
|
| 190 | + { |
|
| 191 | + $this->sourceList = $sourceList; |
|
| 192 | + return $this; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * {@inheritdoc} |
|
| 197 | + */ |
|
| 198 | + public function Field($properties = array()) |
|
| 199 | + { |
|
| 200 | + Requirements::css('silverstripe/tagfield:css/select2.min.css'); |
|
| 201 | + Requirements::css('silverstripe/tagfield:css/TagField.css'); |
|
| 202 | + |
|
| 203 | + Requirements::javascript('silverstripe/tagfield:js/select2.js'); |
|
| 204 | + Requirements::javascript('silverstripe/tagfield:js/TagField.js'); |
|
| 205 | + |
|
| 206 | + $this->addExtraClass('ss-tag-field'); |
|
| 207 | + |
|
| 208 | + if ($this->getIsMultiple()) { |
|
| 209 | + $this->setAttribute('multiple', 'multiple'); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + if ($this->shouldLazyLoad) { |
|
| 213 | + $this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL()); |
|
| 214 | + } else { |
|
| 215 | + $properties = array_merge($properties, array( |
|
| 216 | + 'Options' => $this->getOptions() |
|
| 217 | + )); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + $this->setAttribute('data-can-create', (int) $this->getCanCreate()); |
|
| 221 | + |
|
| 222 | + return $this |
|
| 223 | + ->customise($properties) |
|
| 224 | + ->renderWith(self::class); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * @return string |
|
| 229 | + */ |
|
| 230 | + protected function getSuggestURL() |
|
| 231 | + { |
|
| 232 | + return Controller::join_links($this->Link(), 'suggest'); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @return ArrayList |
|
| 237 | + */ |
|
| 238 | + protected function getOptions() |
|
| 239 | + { |
|
| 240 | + $options = ArrayList::create(); |
|
| 241 | + |
|
| 242 | + $source = $this->getSourceList(); |
|
| 243 | + |
|
| 244 | + if (!$source) { |
|
| 245 | + $source = ArrayList::create(); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + $dataClass = $source->dataClass(); |
|
| 249 | + |
|
| 250 | + $values = $this->Value(); |
|
| 251 | + |
|
| 252 | + if (!$values) { |
|
| 253 | + return $options; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + if (is_array($values)) { |
|
| 257 | + $values = DataList::create($dataClass)->filter('Title', $values); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + $ids = $values->column('Title'); |
|
| 261 | + |
|
| 262 | + $titleField = $this->getTitleField(); |
|
| 263 | + |
|
| 264 | + foreach ($source as $object) { |
|
| 265 | + $options->push( |
|
| 266 | + ArrayData::create(array( |
|
| 267 | + 'Title' => $object->$titleField, |
|
| 268 | + 'Value' => $object->Title, |
|
| 269 | + 'Selected' => in_array($object->Title, $ids), |
|
| 270 | + )) |
|
| 271 | + ); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + return $options; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * {@inheritdoc} |
|
| 279 | + */ |
|
| 280 | + public function setValue($value, $source = null) |
|
| 281 | + { |
|
| 282 | + if ($source instanceof DataObject) { |
|
| 283 | + $name = $this->getName(); |
|
| 284 | + |
|
| 285 | + if ($source->hasMethod($name)) { |
|
| 286 | + $value = $source->$name()->column('Title'); |
|
| 287 | + } |
|
| 288 | + } elseif ($value instanceof SS_List) { |
|
| 289 | + $value = $value->column('Title'); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if (!is_array($value)) { |
|
| 293 | + return parent::setValue($value); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + return parent::setValue(array_filter($value)); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * {@inheritdoc} |
|
| 301 | + */ |
|
| 302 | + public function getAttributes() |
|
| 303 | + { |
|
| 304 | + return array_merge( |
|
| 305 | + parent::getAttributes(), |
|
| 306 | + [ |
|
| 307 | + 'name' => $this->getName() . '[]', |
|
| 308 | + 'style'=> 'width: 100%' |
|
| 309 | + ] |
|
| 310 | + ); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * {@inheritdoc} |
|
| 315 | + */ |
|
| 316 | + public function saveInto(DataObjectInterface $record) |
|
| 317 | + { |
|
| 318 | + parent::saveInto($record); |
|
| 319 | + |
|
| 320 | + $name = $this->getName(); |
|
| 321 | + $titleField = $this->getTitleField(); |
|
| 322 | + $source = $this->getSource(); |
|
| 323 | + $values = $this->Value(); |
|
| 324 | + $relation = $record->$name(); |
|
| 325 | + $ids = array(); |
|
| 326 | + |
|
| 327 | + if (!$values) { |
|
| 328 | + $values = array(); |
|
| 329 | + } |
|
| 330 | + if (empty($record) || empty($titleField)) { |
|
| 331 | + return; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + if (!$record->hasMethod($name)) { |
|
| 335 | + throw new Exception( |
|
| 336 | + sprintf("%s does not have a %s method", get_class($record), $name) |
|
| 337 | + ); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + foreach ($values as $key => $value) { |
|
| 341 | + // Get or create record |
|
| 342 | + $record = $this->getOrCreateTag($value); |
|
| 343 | + if ($record) { |
|
| 344 | + $ids[] = $record->ID; |
|
| 345 | + $values[$key] = $record->Title; |
|
| 346 | + } |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + $relation->setByIDList(array_filter($ids)); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Get or create tag with the given value |
|
| 354 | + * |
|
| 355 | + * @param string $term |
|
| 356 | + * @return DataObject |
|
| 357 | + */ |
|
| 358 | + protected function getOrCreateTag($term) |
|
| 359 | + { |
|
| 360 | + // Check if existing record can be found |
|
| 361 | + /** @var DataList $source */ |
|
| 362 | + $source = $this->getSourceList(); |
|
| 363 | + $titleField = $this->getTitleField(); |
|
| 364 | + $record = $source |
|
| 365 | + ->filter($titleField, $term) |
|
| 366 | + ->first(); |
|
| 367 | + if ($record) { |
|
| 368 | + return $record; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + // Create new instance if not yet saved |
|
| 372 | + if ($this->getCanCreate()) { |
|
| 373 | + $dataClass = $source->dataClass(); |
|
| 374 | + $record = Injector::inst()->create($dataClass); |
|
| 375 | + $record->{$titleField} = $term; |
|
| 376 | + $record->write(); |
|
| 377 | + return $record; |
|
| 378 | + } else { |
|
| 379 | + return false; |
|
| 380 | + } |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Returns a JSON string of tags, for lazy loading. |
|
| 385 | + * |
|
| 386 | + * @param HTTPRequest $request |
|
| 387 | + * @return HTTPResponse |
|
| 388 | + */ |
|
| 389 | + public function suggest(HTTPRequest $request) |
|
| 390 | + { |
|
| 391 | + $tags = $this->getTags($request->getVar('term')); |
|
| 392 | + |
|
| 393 | + $response = new HTTPResponse(); |
|
| 394 | + $response->addHeader('Content-Type', 'application/json'); |
|
| 395 | + $response->setBody(json_encode(array('items' => $tags))); |
|
| 396 | + |
|
| 397 | + return $response; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * Returns array of arrays representing tags. |
|
| 402 | + * |
|
| 403 | + * @param string $term |
|
| 404 | + * @return array |
|
| 405 | + */ |
|
| 406 | + protected function getTags($term) |
|
| 407 | + { |
|
| 408 | + /** |
|
| 409 | + * @var array $source |
|
| 410 | + */ |
|
| 411 | + $source = $this->getSourceList(); |
|
| 412 | + |
|
| 413 | + $titleField = $this->getTitleField(); |
|
| 414 | + |
|
| 415 | + $query = $source |
|
| 416 | + ->filter($titleField . ':PartialMatch:nocase', $term) |
|
| 417 | + ->sort($titleField) |
|
| 418 | + ->limit($this->getLazyLoadItemLimit()); |
|
| 419 | + |
|
| 420 | + // Map into a distinct list |
|
| 421 | + $items = array(); |
|
| 422 | + $titleField = $this->getTitleField(); |
|
| 423 | + foreach ($query->map('ID', $titleField) as $id => $title) { |
|
| 424 | + $items[$title] = array( |
|
| 425 | + 'id' => $title, |
|
| 426 | + 'text' => $title |
|
| 427 | + ); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + return array_values($items); |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * DropdownField assumes value will be a scalar so we must |
|
| 435 | + * override validate. This only applies to Silverstripe 3.2+ |
|
| 436 | + * |
|
| 437 | + * @param Validator $validator |
|
| 438 | + * @return bool |
|
| 439 | + */ |
|
| 440 | + public function validate($validator) |
|
| 441 | + { |
|
| 442 | + return true; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Converts the field to a readonly variant. |
|
| 447 | + * |
|
| 448 | + * @return TagField_Readonly |
|
| 449 | + */ |
|
| 450 | + public function performReadonlyTransformation() |
|
| 451 | + { |
|
| 452 | + $copy = $this->castedCopy(ReadonlyTagField::class); |
|
| 453 | + $copy->setSourceList($this->getSourceList()); |
|
| 454 | + return $copy; |
|
| 455 | + } |
|
| 456 | 456 | } |