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