@@ -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 | } |
@@ -19,337 +19,337 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class TagFieldTest extends SapphireTest |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var string |
|
| 24 | - */ |
|
| 25 | - protected static $fixture_file = 'TagFieldTest.yml'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - protected static $extra_dataobjects = [ |
|
| 31 | - TagFieldTestBlogTag::class, |
|
| 32 | - TagFieldTestBlogPost::class, |
|
| 33 | - ]; |
|
| 34 | - |
|
| 35 | - public function testItSavesLinksToNewTagsOnNewRecords() |
|
| 36 | - { |
|
| 37 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 38 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 39 | - $field->setValue(array('Tag3', 'Tag4')); |
|
| 40 | - $field->saveInto($record); |
|
| 41 | - $record->write(); |
|
| 42 | - $this->compareExpectedAndActualTags( |
|
| 43 | - array('Tag3', 'Tag4'), |
|
| 44 | - $record |
|
| 45 | - ); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param string $name |
|
| 50 | - * |
|
| 51 | - * @return TagFieldTestBlogPost |
|
| 52 | - */ |
|
| 53 | - protected function getNewTagFieldTestBlogPost($name) |
|
| 54 | - { |
|
| 55 | - return $this->objFromFixture( |
|
| 56 | - TagFieldTestBlogPost::class, |
|
| 57 | - $name |
|
| 58 | - ); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @param array $expected |
|
| 63 | - * @param TagFieldTestBlogPost $record |
|
| 64 | - */ |
|
| 65 | - protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record) |
|
| 66 | - { |
|
| 67 | - $this->compareTagLists($expected, $record->Tags()); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Ensure a source of tags matches the given string tag names |
|
| 72 | - * |
|
| 73 | - * @param array $expected |
|
| 74 | - * @param DataList $actualSource |
|
| 75 | - */ |
|
| 76 | - protected function compareTagLists(array $expected, DataList $actualSource) |
|
| 77 | - { |
|
| 78 | - $actual = array_values($actualSource->map('ID', 'Title')->toArray()); |
|
| 79 | - sort($expected); |
|
| 80 | - sort($actual); |
|
| 81 | - |
|
| 82 | - $this->assertEquals( |
|
| 83 | - $expected, |
|
| 84 | - $actual |
|
| 85 | - ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - public function testItSavesLinksToNewTagsOnExistingRecords() |
|
| 89 | - { |
|
| 90 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 91 | - $record->write(); |
|
| 92 | - |
|
| 93 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 94 | - $field->setValue(array('Tag3', 'Tag4')); |
|
| 95 | - $field->saveInto($record); |
|
| 96 | - |
|
| 97 | - $this->compareExpectedAndActualTags( |
|
| 98 | - array('Tag3', 'Tag4'), |
|
| 99 | - $record |
|
| 100 | - ); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - public function testItSavesLinksToExistingTagsOnNewRecords() |
|
| 104 | - { |
|
| 105 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 106 | - |
|
| 107 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 108 | - $field->setValue(array('Tag1', 'Tag2')); |
|
| 109 | - $field->saveInto($record); |
|
| 110 | - |
|
| 111 | - $record->write(); |
|
| 112 | - |
|
| 113 | - $this->compareExpectedAndActualTags( |
|
| 114 | - array('Tag1', 'Tag2'), |
|
| 115 | - $record |
|
| 116 | - ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - public function testItSavesLinksToExistingTagsOnExistingRecords() |
|
| 120 | - { |
|
| 121 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 122 | - $record->write(); |
|
| 123 | - |
|
| 124 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 125 | - $field->setValue(array('Tag1', 'Tag2')); |
|
| 126 | - $field->saveInto($record); |
|
| 127 | - |
|
| 128 | - $this->compareExpectedAndActualTags( |
|
| 129 | - array('Tag1', 'Tag2'), |
|
| 130 | - $record |
|
| 131 | - ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Ensure that {@see TagField::saveInto} respects existing tags |
|
| 136 | - */ |
|
| 137 | - public function testSaveDuplicateTags() |
|
| 138 | - { |
|
| 139 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost2'); |
|
| 140 | - $record->write(); |
|
| 141 | - $tag2ID = $this->idFromFixture(TagFieldTestBlogTag::class, 'Tag2'); |
|
| 142 | - |
|
| 143 | - // Check tags before write |
|
| 144 | - $this->compareExpectedAndActualTags( |
|
| 145 | - array('Tag1', '222'), |
|
| 146 | - $record |
|
| 147 | - ); |
|
| 148 | - $this->compareTagLists( |
|
| 149 | - array('Tag1', '222'), |
|
| 150 | - TagFieldTestBlogTag::get() |
|
| 151 | - ); |
|
| 152 | - $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
| 153 | - |
|
| 154 | - // Write new tags |
|
| 155 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 156 | - $field->setValue(array('222', 'Tag3')); |
|
| 157 | - $field->saveInto($record); |
|
| 158 | - |
|
| 159 | - // Check only one new tag was added |
|
| 160 | - $this->compareExpectedAndActualTags( |
|
| 161 | - array('222', 'Tag3'), |
|
| 162 | - $record |
|
| 163 | - ); |
|
| 164 | - |
|
| 165 | - // Ensure that only one new dataobject was added and that tag2s id has not changed |
|
| 166 | - $this->compareTagLists( |
|
| 167 | - array('Tag1', '222', 'Tag3'), |
|
| 168 | - TagFieldTestBlogTag::get() |
|
| 169 | - ); |
|
| 170 | - $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - public function testItSuggestsTags() |
|
| 174 | - { |
|
| 175 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Partial tag title match. |
|
| 179 | - */ |
|
| 180 | - $request = $this->getNewRequest(array('term' => 'Tag')); |
|
| 181 | - |
|
| 182 | - $this->assertEquals( |
|
| 183 | - '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
| 184 | - $field->suggest($request)->getBody() |
|
| 185 | - ); |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Exact tag title match. |
|
| 189 | - */ |
|
| 190 | - $request = $this->getNewRequest(array('term' => '222')); |
|
| 191 | - |
|
| 192 | - $this->assertEquals( |
|
| 193 | - '{"items":[{"id":"222","text":"222"}]}', |
|
| 194 | - $field->suggest($request)->getBody() |
|
| 195 | - ); |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Case-insensitive tag title match. |
|
| 199 | - */ |
|
| 200 | - $request = $this->getNewRequest(array('term' => 'TAG1')); |
|
| 201 | - |
|
| 202 | - $this->assertEquals( |
|
| 203 | - '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
| 204 | - $field->suggest($request)->getBody() |
|
| 205 | - ); |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * No tag title match. |
|
| 209 | - */ |
|
| 210 | - $request = $this->getNewRequest(array('term' => 'unknown')); |
|
| 211 | - |
|
| 212 | - $this->assertEquals( |
|
| 213 | - '{"items":[]}', |
|
| 214 | - $field->suggest($request)->getBody() |
|
| 215 | - ); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Tests that TagField supports pre-filtered data sources |
|
| 220 | - */ |
|
| 221 | - public function testRestrictedSuggestions() |
|
| 222 | - { |
|
| 223 | - $source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2'); |
|
| 224 | - $field = new TagField('Tags', '', $source); |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Partial tag title match. |
|
| 228 | - */ |
|
| 229 | - $request = $this->getNewRequest(array('term' => 'Tag')); |
|
| 230 | - |
|
| 231 | - $this->assertEquals( |
|
| 232 | - '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
| 233 | - $field->suggest($request)->getBody() |
|
| 234 | - ); |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Exact tag title match. |
|
| 238 | - */ |
|
| 239 | - $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
| 240 | - |
|
| 241 | - $this->assertEquals( |
|
| 242 | - '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
| 243 | - $field->suggest($request)->getBody() |
|
| 244 | - ); |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Excluded item doesn't appear in matches |
|
| 248 | - */ |
|
| 249 | - $request = $this->getNewRequest(array('term' => 'Tag2')); |
|
| 250 | - |
|
| 251 | - $this->assertEquals( |
|
| 252 | - '{"items":[]}', |
|
| 253 | - $field->suggest($request)->getBody() |
|
| 254 | - ); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @param array $parameters |
|
| 259 | - * |
|
| 260 | - * @return HTTPRequest |
|
| 261 | - */ |
|
| 262 | - protected function getNewRequest(array $parameters) |
|
| 263 | - { |
|
| 264 | - return new HTTPRequest( |
|
| 265 | - 'get', |
|
| 266 | - 'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest', |
|
| 267 | - $parameters |
|
| 268 | - ); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - public function testItDisplaysValuesFromRelations() |
|
| 272 | - { |
|
| 273 | - $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 274 | - $record->write(); |
|
| 275 | - |
|
| 276 | - $form = new Form( |
|
| 277 | - new TagFieldTestController($record), |
|
| 278 | - 'Form', |
|
| 279 | - new FieldList( |
|
| 280 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)) |
|
| 281 | - ), |
|
| 282 | - new FieldList() |
|
| 283 | - ); |
|
| 284 | - |
|
| 285 | - $form->loadDataFrom( |
|
| 286 | - $this->objFromFixture(TagFieldTestBlogPost::class, 'BlogPost2') |
|
| 287 | - ); |
|
| 288 | - |
|
| 289 | - $ids = TagFieldTestBlogTag::get()->column('Title'); |
|
| 290 | - |
|
| 291 | - $this->assertEquals($field->Value(), $ids); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - public function testItIgnoresNewTagsIfCannotCreate() |
|
| 295 | - { |
|
| 296 | - $this->markTestSkipped( |
|
| 297 | - 'This test has not been updated yet.' |
|
| 298 | - ); |
|
| 299 | - |
|
| 300 | - $record = new TagFieldTestBlogPost(); |
|
| 301 | - $record->write(); |
|
| 302 | - |
|
| 303 | - $tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first(); |
|
| 304 | - |
|
| 305 | - $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class), array($tag->Title, 'Tag3')); |
|
| 306 | - $field->setCanCreate(false); |
|
| 307 | - $field->saveInto($record); |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * @var TagFieldTestBlogPost $record |
|
| 311 | - */ |
|
| 312 | - $record = DataObject::get_by_id(TagFieldTestBlogPost::class, $record->ID); |
|
| 313 | - |
|
| 314 | - $this->compareExpectedAndActualTags( |
|
| 315 | - array('Tag1'), |
|
| 316 | - $record |
|
| 317 | - ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Test you can save without a source set |
|
| 322 | - */ |
|
| 323 | - public function testSaveEmptySource() |
|
| 324 | - { |
|
| 325 | - $record = new TagFieldTestBlogPost(); |
|
| 326 | - $record->write(); |
|
| 327 | - |
|
| 328 | - // Clear database of tags |
|
| 329 | - TagFieldTestBlogTag::get()->removeAll(); |
|
| 330 | - |
|
| 331 | - $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); |
|
| 332 | - $field->setValue(['New Tag']); |
|
| 333 | - $field->setCanCreate(true); |
|
| 334 | - $field->saveInto($record); |
|
| 335 | - |
|
| 336 | - $tag = TagFieldTestBlogTag::get()->first(); |
|
| 337 | - $this->assertNotEmpty($tag); |
|
| 338 | - $this->assertEquals('New Tag', $tag->Title); |
|
| 339 | - $record = TagFieldTestBlogPost::get()->byID($record->ID); |
|
| 340 | - $this->assertEquals( |
|
| 341 | - $tag->ID, |
|
| 342 | - $record->Tags()->first()->ID |
|
| 343 | - ); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Test read only fields are returned |
|
| 348 | - */ |
|
| 349 | - public function testReadonlyTransformation() |
|
| 350 | - { |
|
| 351 | - $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); |
|
| 352 | - $readOnlyField = $field->performReadonlyTransformation(); |
|
| 353 | - $this->assertEquals(ReadonlyTagField::class, get_class($readOnlyField)); |
|
| 354 | - } |
|
| 22 | + /** |
|
| 23 | + * @var string |
|
| 24 | + */ |
|
| 25 | + protected static $fixture_file = 'TagFieldTest.yml'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + protected static $extra_dataobjects = [ |
|
| 31 | + TagFieldTestBlogTag::class, |
|
| 32 | + TagFieldTestBlogPost::class, |
|
| 33 | + ]; |
|
| 34 | + |
|
| 35 | + public function testItSavesLinksToNewTagsOnNewRecords() |
|
| 36 | + { |
|
| 37 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 38 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 39 | + $field->setValue(array('Tag3', 'Tag4')); |
|
| 40 | + $field->saveInto($record); |
|
| 41 | + $record->write(); |
|
| 42 | + $this->compareExpectedAndActualTags( |
|
| 43 | + array('Tag3', 'Tag4'), |
|
| 44 | + $record |
|
| 45 | + ); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param string $name |
|
| 50 | + * |
|
| 51 | + * @return TagFieldTestBlogPost |
|
| 52 | + */ |
|
| 53 | + protected function getNewTagFieldTestBlogPost($name) |
|
| 54 | + { |
|
| 55 | + return $this->objFromFixture( |
|
| 56 | + TagFieldTestBlogPost::class, |
|
| 57 | + $name |
|
| 58 | + ); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @param array $expected |
|
| 63 | + * @param TagFieldTestBlogPost $record |
|
| 64 | + */ |
|
| 65 | + protected function compareExpectedAndActualTags(array $expected, TagFieldTestBlogPost $record) |
|
| 66 | + { |
|
| 67 | + $this->compareTagLists($expected, $record->Tags()); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Ensure a source of tags matches the given string tag names |
|
| 72 | + * |
|
| 73 | + * @param array $expected |
|
| 74 | + * @param DataList $actualSource |
|
| 75 | + */ |
|
| 76 | + protected function compareTagLists(array $expected, DataList $actualSource) |
|
| 77 | + { |
|
| 78 | + $actual = array_values($actualSource->map('ID', 'Title')->toArray()); |
|
| 79 | + sort($expected); |
|
| 80 | + sort($actual); |
|
| 81 | + |
|
| 82 | + $this->assertEquals( |
|
| 83 | + $expected, |
|
| 84 | + $actual |
|
| 85 | + ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + public function testItSavesLinksToNewTagsOnExistingRecords() |
|
| 89 | + { |
|
| 90 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 91 | + $record->write(); |
|
| 92 | + |
|
| 93 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 94 | + $field->setValue(array('Tag3', 'Tag4')); |
|
| 95 | + $field->saveInto($record); |
|
| 96 | + |
|
| 97 | + $this->compareExpectedAndActualTags( |
|
| 98 | + array('Tag3', 'Tag4'), |
|
| 99 | + $record |
|
| 100 | + ); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + public function testItSavesLinksToExistingTagsOnNewRecords() |
|
| 104 | + { |
|
| 105 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 106 | + |
|
| 107 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 108 | + $field->setValue(array('Tag1', 'Tag2')); |
|
| 109 | + $field->saveInto($record); |
|
| 110 | + |
|
| 111 | + $record->write(); |
|
| 112 | + |
|
| 113 | + $this->compareExpectedAndActualTags( |
|
| 114 | + array('Tag1', 'Tag2'), |
|
| 115 | + $record |
|
| 116 | + ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + public function testItSavesLinksToExistingTagsOnExistingRecords() |
|
| 120 | + { |
|
| 121 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 122 | + $record->write(); |
|
| 123 | + |
|
| 124 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 125 | + $field->setValue(array('Tag1', 'Tag2')); |
|
| 126 | + $field->saveInto($record); |
|
| 127 | + |
|
| 128 | + $this->compareExpectedAndActualTags( |
|
| 129 | + array('Tag1', 'Tag2'), |
|
| 130 | + $record |
|
| 131 | + ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Ensure that {@see TagField::saveInto} respects existing tags |
|
| 136 | + */ |
|
| 137 | + public function testSaveDuplicateTags() |
|
| 138 | + { |
|
| 139 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost2'); |
|
| 140 | + $record->write(); |
|
| 141 | + $tag2ID = $this->idFromFixture(TagFieldTestBlogTag::class, 'Tag2'); |
|
| 142 | + |
|
| 143 | + // Check tags before write |
|
| 144 | + $this->compareExpectedAndActualTags( |
|
| 145 | + array('Tag1', '222'), |
|
| 146 | + $record |
|
| 147 | + ); |
|
| 148 | + $this->compareTagLists( |
|
| 149 | + array('Tag1', '222'), |
|
| 150 | + TagFieldTestBlogTag::get() |
|
| 151 | + ); |
|
| 152 | + $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
| 153 | + |
|
| 154 | + // Write new tags |
|
| 155 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 156 | + $field->setValue(array('222', 'Tag3')); |
|
| 157 | + $field->saveInto($record); |
|
| 158 | + |
|
| 159 | + // Check only one new tag was added |
|
| 160 | + $this->compareExpectedAndActualTags( |
|
| 161 | + array('222', 'Tag3'), |
|
| 162 | + $record |
|
| 163 | + ); |
|
| 164 | + |
|
| 165 | + // Ensure that only one new dataobject was added and that tag2s id has not changed |
|
| 166 | + $this->compareTagLists( |
|
| 167 | + array('Tag1', '222', 'Tag3'), |
|
| 168 | + TagFieldTestBlogTag::get() |
|
| 169 | + ); |
|
| 170 | + $this->assertContains($tag2ID, TagFieldTestBlogTag::get()->column('ID')); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + public function testItSuggestsTags() |
|
| 174 | + { |
|
| 175 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)); |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Partial tag title match. |
|
| 179 | + */ |
|
| 180 | + $request = $this->getNewRequest(array('term' => 'Tag')); |
|
| 181 | + |
|
| 182 | + $this->assertEquals( |
|
| 183 | + '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
| 184 | + $field->suggest($request)->getBody() |
|
| 185 | + ); |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Exact tag title match. |
|
| 189 | + */ |
|
| 190 | + $request = $this->getNewRequest(array('term' => '222')); |
|
| 191 | + |
|
| 192 | + $this->assertEquals( |
|
| 193 | + '{"items":[{"id":"222","text":"222"}]}', |
|
| 194 | + $field->suggest($request)->getBody() |
|
| 195 | + ); |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Case-insensitive tag title match. |
|
| 199 | + */ |
|
| 200 | + $request = $this->getNewRequest(array('term' => 'TAG1')); |
|
| 201 | + |
|
| 202 | + $this->assertEquals( |
|
| 203 | + '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
| 204 | + $field->suggest($request)->getBody() |
|
| 205 | + ); |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * No tag title match. |
|
| 209 | + */ |
|
| 210 | + $request = $this->getNewRequest(array('term' => 'unknown')); |
|
| 211 | + |
|
| 212 | + $this->assertEquals( |
|
| 213 | + '{"items":[]}', |
|
| 214 | + $field->suggest($request)->getBody() |
|
| 215 | + ); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Tests that TagField supports pre-filtered data sources |
|
| 220 | + */ |
|
| 221 | + public function testRestrictedSuggestions() |
|
| 222 | + { |
|
| 223 | + $source = TagFieldTestBlogTag::get()->exclude('Title', 'Tag2'); |
|
| 224 | + $field = new TagField('Tags', '', $source); |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Partial tag title match. |
|
| 228 | + */ |
|
| 229 | + $request = $this->getNewRequest(array('term' => 'Tag')); |
|
| 230 | + |
|
| 231 | + $this->assertEquals( |
|
| 232 | + '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
| 233 | + $field->suggest($request)->getBody() |
|
| 234 | + ); |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Exact tag title match. |
|
| 238 | + */ |
|
| 239 | + $request = $this->getNewRequest(array('term' => 'Tag1')); |
|
| 240 | + |
|
| 241 | + $this->assertEquals( |
|
| 242 | + '{"items":[{"id":"Tag1","text":"Tag1"}]}', |
|
| 243 | + $field->suggest($request)->getBody() |
|
| 244 | + ); |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Excluded item doesn't appear in matches |
|
| 248 | + */ |
|
| 249 | + $request = $this->getNewRequest(array('term' => 'Tag2')); |
|
| 250 | + |
|
| 251 | + $this->assertEquals( |
|
| 252 | + '{"items":[]}', |
|
| 253 | + $field->suggest($request)->getBody() |
|
| 254 | + ); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @param array $parameters |
|
| 259 | + * |
|
| 260 | + * @return HTTPRequest |
|
| 261 | + */ |
|
| 262 | + protected function getNewRequest(array $parameters) |
|
| 263 | + { |
|
| 264 | + return new HTTPRequest( |
|
| 265 | + 'get', |
|
| 266 | + 'TagFieldTestController/TagFieldTestForm/fields/Tags/suggest', |
|
| 267 | + $parameters |
|
| 268 | + ); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + public function testItDisplaysValuesFromRelations() |
|
| 272 | + { |
|
| 273 | + $record = $this->getNewTagFieldTestBlogPost('BlogPost1'); |
|
| 274 | + $record->write(); |
|
| 275 | + |
|
| 276 | + $form = new Form( |
|
| 277 | + new TagFieldTestController($record), |
|
| 278 | + 'Form', |
|
| 279 | + new FieldList( |
|
| 280 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class)) |
|
| 281 | + ), |
|
| 282 | + new FieldList() |
|
| 283 | + ); |
|
| 284 | + |
|
| 285 | + $form->loadDataFrom( |
|
| 286 | + $this->objFromFixture(TagFieldTestBlogPost::class, 'BlogPost2') |
|
| 287 | + ); |
|
| 288 | + |
|
| 289 | + $ids = TagFieldTestBlogTag::get()->column('Title'); |
|
| 290 | + |
|
| 291 | + $this->assertEquals($field->Value(), $ids); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + public function testItIgnoresNewTagsIfCannotCreate() |
|
| 295 | + { |
|
| 296 | + $this->markTestSkipped( |
|
| 297 | + 'This test has not been updated yet.' |
|
| 298 | + ); |
|
| 299 | + |
|
| 300 | + $record = new TagFieldTestBlogPost(); |
|
| 301 | + $record->write(); |
|
| 302 | + |
|
| 303 | + $tag = TagFieldTestBlogTag::get()->filter('Title', 'Tag1')->first(); |
|
| 304 | + |
|
| 305 | + $field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class), array($tag->Title, 'Tag3')); |
|
| 306 | + $field->setCanCreate(false); |
|
| 307 | + $field->saveInto($record); |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * @var TagFieldTestBlogPost $record |
|
| 311 | + */ |
|
| 312 | + $record = DataObject::get_by_id(TagFieldTestBlogPost::class, $record->ID); |
|
| 313 | + |
|
| 314 | + $this->compareExpectedAndActualTags( |
|
| 315 | + array('Tag1'), |
|
| 316 | + $record |
|
| 317 | + ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Test you can save without a source set |
|
| 322 | + */ |
|
| 323 | + public function testSaveEmptySource() |
|
| 324 | + { |
|
| 325 | + $record = new TagFieldTestBlogPost(); |
|
| 326 | + $record->write(); |
|
| 327 | + |
|
| 328 | + // Clear database of tags |
|
| 329 | + TagFieldTestBlogTag::get()->removeAll(); |
|
| 330 | + |
|
| 331 | + $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); |
|
| 332 | + $field->setValue(['New Tag']); |
|
| 333 | + $field->setCanCreate(true); |
|
| 334 | + $field->saveInto($record); |
|
| 335 | + |
|
| 336 | + $tag = TagFieldTestBlogTag::get()->first(); |
|
| 337 | + $this->assertNotEmpty($tag); |
|
| 338 | + $this->assertEquals('New Tag', $tag->Title); |
|
| 339 | + $record = TagFieldTestBlogPost::get()->byID($record->ID); |
|
| 340 | + $this->assertEquals( |
|
| 341 | + $tag->ID, |
|
| 342 | + $record->Tags()->first()->ID |
|
| 343 | + ); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Test read only fields are returned |
|
| 348 | + */ |
|
| 349 | + public function testReadonlyTransformation() |
|
| 350 | + { |
|
| 351 | + $field = new TagField('Tags', '', TagFieldTestBlogTag::get()); |
|
| 352 | + $readOnlyField = $field->performReadonlyTransformation(); |
|
| 353 | + $this->assertEquals(ReadonlyTagField::class, get_class($readOnlyField)); |
|
| 354 | + } |
|
| 355 | 355 | } |