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