2015-05-15 12:29:17 +02:00
|
|
|
<?php
|
|
|
|
|
2017-01-13 20:11:59 +01:00
|
|
|
namespace SilverStripe\TagField;
|
|
|
|
|
2018-11-15 20:33:38 +01:00
|
|
|
use Iterator;
|
2017-01-13 20:11:59 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
2018-11-15 20:33:38 +01:00
|
|
|
use SilverStripe\Forms\Validator;
|
2017-01-13 20:11:59 +01:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DataObjectInterface;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
/**
|
|
|
|
* Provides a tagging interface, storing comma-delimited tags in a DataObject string field.
|
|
|
|
*
|
|
|
|
* This is intended bridge the gap between 1.x and 2.x, and when possible TagField should be used
|
|
|
|
* instead.
|
|
|
|
*
|
2017-01-13 20:11:59 +01:00
|
|
|
* @package tagfield
|
2015-05-15 12:29:17 +02:00
|
|
|
* @subpackage fields
|
|
|
|
*/
|
2015-11-18 05:05:38 +01:00
|
|
|
class StringTagField extends DropdownField
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-08-07 08:54:55 +02:00
|
|
|
private static $allowed_actions = [
|
2018-11-15 20:33:38 +01:00
|
|
|
'suggest',
|
2017-01-13 20:11:59 +01:00
|
|
|
];
|
2015-11-18 05:05:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $shouldLazyLoad = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $lazyLoadItemLimit = 10;
|
|
|
|
|
2016-08-21 12:34:19 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $canCreate = true;
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
|
|
|
* @var null|DataObject
|
|
|
|
*/
|
|
|
|
protected $record;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $isMultiple = true;
|
|
|
|
|
2021-11-24 15:41:10 +01:00
|
|
|
protected $schemaComponent = 'TagField';
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getShouldLazyLoad()
|
|
|
|
{
|
|
|
|
return $this->shouldLazyLoad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $shouldLazyLoad
|
2018-11-15 20:33:38 +01:00
|
|
|
* @return $this
|
2015-11-18 05:05:38 +01:00
|
|
|
*/
|
|
|
|
public function setShouldLazyLoad($shouldLazyLoad)
|
|
|
|
{
|
|
|
|
$this->shouldLazyLoad = $shouldLazyLoad;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLazyLoadItemLimit()
|
|
|
|
{
|
|
|
|
return $this->lazyLoadItemLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $lazyLoadItemLimit
|
2018-11-15 20:33:38 +01:00
|
|
|
* @return $this
|
2015-11-18 05:05:38 +01:00
|
|
|
*/
|
|
|
|
public function setLazyLoadItemLimit($lazyLoadItemLimit)
|
|
|
|
{
|
|
|
|
$this->lazyLoadItemLimit = $lazyLoadItemLimit;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2015-05-15 12:29:17 +02:00
|
|
|
|
2015-10-09 00:10:33 +02:00
|
|
|
/**
|
2015-11-18 05:05:38 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getIsMultiple()
|
|
|
|
{
|
|
|
|
return $this->isMultiple;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $isMultiple
|
2018-11-15 20:33:38 +01:00
|
|
|
* @return $this
|
2015-11-18 05:05:38 +01:00
|
|
|
*/
|
|
|
|
public function setIsMultiple($isMultiple)
|
|
|
|
{
|
|
|
|
$this->isMultiple = $isMultiple;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|DataObject
|
|
|
|
*/
|
|
|
|
public function getRecord()
|
|
|
|
{
|
|
|
|
if ($this->record) {
|
|
|
|
return $this->record;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form = $this->getForm()) {
|
|
|
|
return $form->getRecord();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param DataObject $record
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setRecord(DataObject $record)
|
|
|
|
{
|
|
|
|
$this->record = $record;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:33:38 +01:00
|
|
|
public function Field($properties = [])
|
2015-11-18 05:05:38 +01:00
|
|
|
{
|
2021-07-26 10:49:05 +02:00
|
|
|
$this->addExtraClass('ss-tag-field entwine');
|
2015-05-15 12:29:17 +02:00
|
|
|
|
2018-11-15 21:59:08 +01:00
|
|
|
return $this
|
|
|
|
->customise($properties)
|
|
|
|
->renderWith(TagField::class);
|
|
|
|
}
|
2015-11-18 05:05:38 +01:00
|
|
|
|
2018-11-15 21:59:08 +01:00
|
|
|
/**
|
|
|
|
* Provide TagField data to the JSON schema for the frontend component
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSchemaDataDefaults()
|
|
|
|
{
|
|
|
|
$schema = array_merge(
|
|
|
|
parent::getSchemaDataDefaults(),
|
|
|
|
[
|
|
|
|
'name' => $this->getName() . '[]',
|
|
|
|
'lazyLoad' => $this->getShouldLazyLoad(),
|
|
|
|
'creatable' => $this->getCanCreate(),
|
|
|
|
'multi' => $this->getIsMultiple(),
|
|
|
|
'value' => $this->formatOptions($this->Value()),
|
|
|
|
'disabled' => $this->isDisabled() || $this->isReadonly(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$this->getShouldLazyLoad()) {
|
|
|
|
$schema['options'] = $this->getOptions()->toNestedArray();
|
2015-11-18 05:05:38 +01:00
|
|
|
} else {
|
2018-11-15 21:59:08 +01:00
|
|
|
$schema['optionUrl'] = $this->getSuggestURL();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $schema;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function formatOptions($fieldValue)
|
|
|
|
{
|
|
|
|
if (empty($fieldValue)) {
|
|
|
|
return [];
|
2015-10-09 00:10:33 +02:00
|
|
|
}
|
|
|
|
|
2018-11-15 21:59:08 +01:00
|
|
|
$formattedValue = [];
|
|
|
|
foreach ($fieldValue as $value) {
|
|
|
|
$formattedValue[] = [
|
|
|
|
'Title' => $value,
|
|
|
|
'Value' => $value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return $formattedValue;
|
|
|
|
}
|
2016-07-21 22:52:17 +02:00
|
|
|
|
2018-11-15 21:59:08 +01:00
|
|
|
/**
|
|
|
|
* When not used in a React form factory context, this adds the schema data to SilverStripe template
|
|
|
|
* rendered attributes lists
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAttributes()
|
|
|
|
{
|
|
|
|
$attributes = parent::getAttributes();
|
|
|
|
$attributes['data-schema'] = json_encode($this->getSchemaData());
|
|
|
|
return $attributes;
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getSuggestURL()
|
|
|
|
{
|
|
|
|
return Controller::join_links($this->Link(), 'suggest');
|
|
|
|
}
|
2015-09-23 10:56:18 +02:00
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
protected function getOptions()
|
|
|
|
{
|
|
|
|
$options = ArrayList::create();
|
|
|
|
|
|
|
|
$source = $this->getSource();
|
|
|
|
|
|
|
|
if ($source instanceof Iterator) {
|
|
|
|
$source = iterator_to_array($source);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($source as $value) {
|
|
|
|
$options->push(
|
2018-11-15 20:33:38 +01:00
|
|
|
ArrayData::create([
|
2015-11-18 05:05:38 +01:00
|
|
|
'Title' => $value,
|
|
|
|
'Value' => $value,
|
2018-11-15 20:33:38 +01:00
|
|
|
])
|
2015-11-18 05:05:38 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setValue($value, $source = null)
|
|
|
|
{
|
|
|
|
if (is_string($value)) {
|
2022-04-13 03:50:19 +02:00
|
|
|
$value = explode(',', $value ?? '');
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($source instanceof DataObject) {
|
|
|
|
$name = $this->getName();
|
2022-04-13 03:50:19 +02:00
|
|
|
$value = explode(',', $source->$name ?? '');
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($source instanceof SS_List) {
|
|
|
|
$value = $source->column('ID');
|
|
|
|
}
|
|
|
|
|
2018-11-15 20:33:38 +01:00
|
|
|
if ($value === null) {
|
|
|
|
$value = [];
|
2015-11-23 02:57:59 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 03:50:19 +02:00
|
|
|
return parent::setValue(array_filter($value ?? []));
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function saveInto(DataObjectInterface $record)
|
|
|
|
{
|
|
|
|
parent::saveInto($record);
|
|
|
|
|
|
|
|
$name = $this->getName();
|
|
|
|
|
2019-02-19 05:01:58 +01:00
|
|
|
$record->$name = $this->dataValue();
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
2019-02-19 05:01:58 +01:00
|
|
|
/**
|
|
|
|
* Ensure that arrays are imploded before being saved
|
|
|
|
*
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
|
|
|
public function dataValue()
|
|
|
|
{
|
|
|
|
return implode(',', $this->value);
|
|
|
|
}
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
|
|
|
* Returns a JSON string of tags, for lazy loading.
|
|
|
|
*
|
2017-01-13 20:11:59 +01:00
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return HTTPResponse
|
2015-11-18 05:05:38 +01:00
|
|
|
*/
|
2017-01-13 20:11:59 +01:00
|
|
|
public function suggest(HTTPRequest $request)
|
2015-11-18 05:05:38 +01:00
|
|
|
{
|
2018-10-28 22:42:26 +01:00
|
|
|
$responseBody = json_encode(
|
2018-11-15 21:59:08 +01:00
|
|
|
['items' => $this->getTags($request->getVar('term'))]
|
2015-11-18 05:05:38 +01:00
|
|
|
);
|
|
|
|
|
2018-11-15 20:33:38 +01:00
|
|
|
$response = HTTPResponse::create();
|
2015-11-18 05:05:38 +01:00
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
|
|
|
$response->setBody($responseBody);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2022-06-07 03:00:12 +02:00
|
|
|
/**
|
|
|
|
* Get or create tag with the given value
|
|
|
|
*
|
|
|
|
* @param string $term
|
|
|
|
* @return DataObject|bool
|
|
|
|
*/
|
|
|
|
protected function getOrCreateTag($term)
|
|
|
|
{
|
|
|
|
// Check if existing record can be found
|
|
|
|
$source = $this->getSourceList();
|
|
|
|
if (!$source) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$titleField = $this->getTitleField();
|
|
|
|
$record = $source
|
|
|
|
->filter($titleField, $term)
|
|
|
|
->first();
|
|
|
|
if ($record) {
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new instance if not yet saved
|
|
|
|
if ($this->getCanCreate()) {
|
|
|
|
$dataClass = $source->dataClass();
|
|
|
|
$record = Injector::inst()->create($dataClass);
|
|
|
|
|
|
|
|
if (is_array($term)) {
|
|
|
|
$term = $term['Value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$record->{$titleField} = $term;
|
|
|
|
$record->write();
|
|
|
|
if ($source instanceof SS_List) {
|
|
|
|
$source->add($record);
|
|
|
|
}
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-18 05:05:38 +01:00
|
|
|
/**
|
2018-11-15 21:59:08 +01:00
|
|
|
* Returns array of arrays representing tags that partially match the given search term
|
2015-11-18 05:05:38 +01:00
|
|
|
*
|
|
|
|
* @param string $term
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getTags($term)
|
|
|
|
{
|
2018-11-15 21:59:08 +01:00
|
|
|
$items = [];
|
|
|
|
foreach ($this->getOptions() as $i => $tag) {
|
|
|
|
/** @var ArrayData $tag */
|
|
|
|
$tagValue = $tag->Value;
|
|
|
|
// Map into a distinct list (prevent duplicates)
|
2022-04-13 03:50:19 +02:00
|
|
|
if (stripos($tagValue ?? '', $term ?? '') !== false && !array_key_exists($tagValue, $items ?? [])) {
|
2018-11-15 21:59:08 +01:00
|
|
|
$items[$tagValue] = [
|
|
|
|
'id' => $tag->Title,
|
|
|
|
'text' => $tag->Value,
|
|
|
|
];
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 22:07:22 +02:00
|
|
|
|
2022-04-13 03:50:19 +02:00
|
|
|
return array_slice(array_values($items ?? []), 0, $this->getLazyLoadItemLimit());
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DropdownField assumes value will be a scalar so we must
|
|
|
|
* override validate. This only applies to Silverstripe 3.2+
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validate($validator)
|
|
|
|
{
|
2023-02-22 17:07:46 +01:00
|
|
|
return $this->extendValidationResult(true, $validator);
|
2015-11-18 05:05:38 +01:00
|
|
|
}
|
2016-08-21 12:34:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getCanCreate()
|
|
|
|
{
|
|
|
|
return $this->canCreate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $canCreate
|
2018-11-15 20:33:38 +01:00
|
|
|
* @return $this
|
2016-08-21 12:34:19 +02:00
|
|
|
*/
|
|
|
|
public function setCanCreate($canCreate)
|
|
|
|
{
|
|
|
|
$this->canCreate = $canCreate;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2023-04-17 03:45:22 +02:00
|
|
|
|
|
|
|
public function performReadonlyTransformation()
|
|
|
|
{
|
|
|
|
$field = parent::performReadonlyTransformation();
|
|
|
|
$field->setValue(implode(', ', $this->Value()));
|
|
|
|
return $field;
|
|
|
|
}
|
2015-05-15 12:29:17 +02:00
|
|
|
}
|