2015-04-14 03:15:28 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* Provides a tagging interface, storing links between tag DataObjects and a parent DataObject.
|
2015-04-14 03:15:28 +02:00
|
|
|
*
|
2015-05-15 12:29:17 +02:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields
|
2015-04-14 03:15:28 +02:00
|
|
|
*/
|
|
|
|
class TagField extends DropdownField {
|
2015-04-23 21:19:10 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $allowed_actions = array(
|
|
|
|
'suggest',
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
protected $shouldLazyLoad = false;
|
2015-04-23 21:19:10 +02:00
|
|
|
|
2015-04-14 03:15:28 +02:00
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @var int
|
2015-04-23 21:19:10 +02:00
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
protected $lazyLoadItemLimit = 10;
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @var bool
|
2015-04-23 21:19:10 +02:00
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
protected $canCreate = true;
|
2015-04-14 03:15:28 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @var string
|
2015-04-14 03:15:28 +02:00
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
protected $titleField = 'Title';
|
2015-04-14 03:15:28 +02:00
|
|
|
|
|
|
|
/**
|
2015-04-23 21:19:10 +02:00
|
|
|
* @param string $name
|
2015-05-15 12:29:17 +02:00
|
|
|
* @param string $title
|
|
|
|
* @param null|DataList $source
|
|
|
|
* @param null|DataList $value
|
2015-04-14 03:15:28 +02:00
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function __construct($name, $title = '', $source = null, $value = null) {
|
2015-04-14 03:15:28 +02:00
|
|
|
parent::__construct($name, $title, $source, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-23 21:19:10 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function getShouldLazyLoad() {
|
|
|
|
return $this->shouldLazyLoad;
|
2015-04-23 21:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @param bool $shouldLazyLoad
|
2015-04-23 21:19:10 +02:00
|
|
|
*
|
|
|
|
* @return static
|
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function setShouldLazyLoad($shouldLazyLoad) {
|
|
|
|
$this->shouldLazyLoad = $shouldLazyLoad;
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @return int
|
2015-04-23 21:19:10 +02:00
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function getLazyLoadItemLimit() {
|
|
|
|
return $this->lazyLoadItemLimit;
|
2015-04-23 21:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @param int $lazyLoadItemLimit
|
2015-04-23 21:19:10 +02:00
|
|
|
*
|
|
|
|
* @return static
|
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function setLazyLoadItemLimit($lazyLoadItemLimit) {
|
|
|
|
$this->lazyLoadItemLimit = $lazyLoadItemLimit;
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @return bool
|
2015-04-23 21:19:10 +02:00
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function getCanCreate() {
|
|
|
|
return $this->canCreate;
|
2015-04-23 21:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @param bool $canCreate
|
2015-04-23 21:19:10 +02:00
|
|
|
*
|
|
|
|
* @return static
|
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function setCanCreate($canCreate) {
|
|
|
|
$this->canCreate = $canCreate;
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @return string
|
2015-04-23 21:19:10 +02:00
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function getTitleField() {
|
|
|
|
return $this->titleField;
|
2015-04-23 21:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* @param string $titleField
|
2015-04-23 21:19:10 +02:00
|
|
|
*
|
2015-05-15 12:29:17 +02:00
|
|
|
* @return $this
|
2015-04-23 21:19:10 +02:00
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function setTitleField($titleField) {
|
|
|
|
$this->titleField = $titleField;
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2015-04-14 03:15:28 +02:00
|
|
|
*/
|
|
|
|
public function Field($properties = array()) {
|
|
|
|
Requirements::css(TAG_FIELD_DIR . '/css/select2.min.css');
|
|
|
|
Requirements::css(TAG_FIELD_DIR . '/css/TagField.css');
|
|
|
|
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
|
|
|
|
Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
|
2015-05-15 12:29:17 +02:00
|
|
|
Requirements::javascript(TAG_FIELD_DIR . '/js/select2.js');
|
|
|
|
Requirements::javascript(TAG_FIELD_DIR . '/js/TagField.js');
|
2015-04-14 03:15:28 +02:00
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
$this->addExtraClass('ss-tag-field');
|
2015-04-14 03:15:28 +02:00
|
|
|
|
|
|
|
$this->setAttribute('multiple', 'multiple');
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
if($this->shouldLazyLoad) {
|
|
|
|
$this->setAttribute('data-ss-tag-field-suggest-url', $this->getSuggestURL());
|
|
|
|
} else {
|
|
|
|
$properties = array_merge($properties, array(
|
|
|
|
'Options' => $this->getOptions()
|
|
|
|
));
|
2015-04-23 21:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this
|
|
|
|
->customise($properties)
|
|
|
|
->renderWith(array("templates/TagField"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getSuggestURL() {
|
|
|
|
return Controller::join_links($this->Link(), 'suggest');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
protected function getOptions() {
|
2015-04-14 03:15:28 +02:00
|
|
|
$options = ArrayList::create();
|
|
|
|
|
2015-04-23 21:19:10 +02:00
|
|
|
$source = $this->getSource();
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
if(!$source) {
|
|
|
|
$source = new ArrayList();
|
2015-04-23 21:19:10 +02:00
|
|
|
}
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
$dataClass = $source->dataClass();
|
|
|
|
|
2015-04-14 03:15:28 +02:00
|
|
|
$values = $this->Value();
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
if(!$values) {
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_array($values)) {
|
|
|
|
$values = DataList::create($dataClass)->filter('ID', $values);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $values->column('ID');
|
|
|
|
|
|
|
|
$titleField = $this->getTitleField();
|
|
|
|
|
|
|
|
foreach($source as $object) {
|
2015-04-14 03:15:28 +02:00
|
|
|
$options->push(
|
|
|
|
ArrayData::create(array(
|
2015-05-15 12:29:17 +02:00
|
|
|
'Title' => $object->$titleField,
|
|
|
|
'Value' => $object->ID,
|
|
|
|
'Selected' => in_array($object->ID, $ids),
|
2015-04-14 03:15:28 +02:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-23 21:19:10 +02:00
|
|
|
return $options;
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-23 21:19:10 +02:00
|
|
|
* {@inheritdoc}
|
2015-04-14 03:15:28 +02:00
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
public function setValue($value, $source = null) {
|
|
|
|
if($source instanceof DataObject) {
|
|
|
|
$name = $this->getName();
|
2015-04-23 21:19:10 +02:00
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
if($source->hasMethod($name)) {
|
|
|
|
$value = $source->$name()->getIDList();
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
2015-05-15 12:29:17 +02:00
|
|
|
} elseif($value instanceof SS_List) {
|
|
|
|
$value = $value->column('ID');
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
return parent::setValue(array_filter($value));
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-23 21:19:10 +02:00
|
|
|
* {@inheritdoc}
|
2015-04-14 03:15:28 +02:00
|
|
|
*/
|
|
|
|
public function getAttributes() {
|
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
array('name' => $this->getName() . '[]')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-23 21:19:10 +02:00
|
|
|
* {@inheritdoc}
|
2015-04-14 03:15:28 +02:00
|
|
|
*/
|
|
|
|
public function saveInto(DataObjectInterface $record) {
|
|
|
|
parent::saveInto($record);
|
|
|
|
|
2015-04-23 21:19:10 +02:00
|
|
|
$name = $this->getName();
|
2015-05-15 12:29:17 +02:00
|
|
|
$titleField = $this->getTitleField();
|
|
|
|
|
|
|
|
$source = $this->getSource();
|
|
|
|
|
|
|
|
$dataClass = $source->dataClass();
|
2015-04-14 03:15:28 +02:00
|
|
|
|
|
|
|
$values = $this->Value();
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
if(!$values) {
|
|
|
|
$values = array();
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
if(empty($record) || empty($source) || empty($titleField)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-14 03:15:28 +02:00
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
if(!$record->hasMethod($name)) {
|
|
|
|
throw new Exception(
|
|
|
|
sprintf("%s does not have a %s method", get_class($record), $name)
|
|
|
|
);
|
|
|
|
}
|
2015-04-14 03:15:28 +02:00
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
$relation = $record->$name();
|
2015-04-14 03:15:28 +02:00
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
foreach($values as $i => $value) {
|
|
|
|
if(!is_numeric($value)) {
|
|
|
|
if(!$this->getCanCreate()) {
|
|
|
|
unset($values[$i]);
|
|
|
|
continue;
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
2015-05-15 12:29:17 +02:00
|
|
|
|
|
|
|
$record = new $dataClass();
|
|
|
|
$record->{$titleField} = $value;
|
|
|
|
$record->write();
|
|
|
|
|
|
|
|
$values[$i] = $record->ID;
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
2015-05-15 12:29:17 +02:00
|
|
|
}
|
2015-04-14 03:15:28 +02:00
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
if($values instanceof SS_List) {
|
|
|
|
$values = iterator_to_array($values);
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
2015-05-15 12:29:17 +02:00
|
|
|
|
|
|
|
$relation->setByIDList(array_filter($values));
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* Returns a JSON string of tags, for lazy loading.
|
2015-04-23 21:19:10 +02:00
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
*
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
|
|
|
public function suggest(SS_HTTPRequest $request) {
|
2015-05-15 12:29:17 +02:00
|
|
|
$tags = $this->getTags($request->getVar('term'));
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
$response = new SS_HTTPResponse();
|
|
|
|
$response->addHeader('Content-Type', 'application/json');
|
2015-05-15 12:29:17 +02:00
|
|
|
$response->setBody(json_encode(array('items' => $tags)));
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-15 12:29:17 +02:00
|
|
|
* Returns array of arrays representing tags.
|
2015-04-23 21:19:10 +02:00
|
|
|
*
|
|
|
|
* @param string $term
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-05-15 12:29:17 +02:00
|
|
|
protected function getTags($term) {
|
|
|
|
/**
|
|
|
|
* @var DataList $source
|
|
|
|
*/
|
|
|
|
$source = $this->getSource();
|
|
|
|
|
|
|
|
$dataClass = $source->dataClass();
|
2015-04-23 21:19:10 +02:00
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
$titleField = $this->getTitleField();
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
$term = Convert::raw2sql($term);
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
$query = $dataClass::get()
|
|
|
|
->filter($titleField . ':PartialMatch:nocase', $term)
|
|
|
|
->sort($titleField)
|
|
|
|
->limit($this->getLazyLoadItemLimit());
|
2015-04-23 21:19:10 +02:00
|
|
|
|
|
|
|
$items = array();
|
|
|
|
|
2015-05-15 12:29:17 +02:00
|
|
|
foreach($query->map('ID', $titleField) as $id => $title) {
|
2015-04-23 21:19:10 +02:00
|
|
|
if(!in_array($title, $items)) {
|
|
|
|
$items[] = array(
|
|
|
|
'id' => $id,
|
|
|
|
'text' => $title
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
2015-08-17 17:53:38 +02: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) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-14 03:15:28 +02:00
|
|
|
}
|