2012-01-25 05:31:27 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* This class is is responsible for adding objects to another object's has_many
|
|
|
|
* and many_many relation, as defined by the {@link RelationList} passed to the
|
2013-05-20 12:18:07 +02:00
|
|
|
* {@link GridField} constructor.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Objects can be searched through an input field (partially matching one or
|
2013-05-20 12:18:07 +02:00
|
|
|
* more fields).
|
|
|
|
*
|
2012-03-01 14:43:42 +01:00
|
|
|
* Selecting from the results will add the object to the relation.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* Often used alongside {@link GridFieldDeleteAction} for detaching existing
|
2013-05-20 12:18:07 +02:00
|
|
|
* records from a relationship.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* For easier setup, have a look at a sample configuration in
|
2013-05-20 12:18:07 +02:00
|
|
|
* {@link GridFieldConfig_RelationEditor}.
|
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
2014-08-15 08:53:05 +02:00
|
|
|
class GridFieldAddExistingAutocompleter
|
2013-05-20 12:18:07 +02:00
|
|
|
implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator, GridField_URLHandler {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
* Which template to use for rendering
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-01-25 05:31:27 +01:00
|
|
|
* @var string $itemClass
|
|
|
|
*/
|
2012-03-09 00:54:02 +01:00
|
|
|
protected $itemClass = 'GridFieldAddExistingAutocompleter';
|
2012-03-09 04:50:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The HTML fragment to write this component into
|
|
|
|
*/
|
|
|
|
protected $targetFragment;
|
|
|
|
|
2012-08-01 09:15:31 +02:00
|
|
|
/**
|
|
|
|
* @var SS_List
|
|
|
|
*/
|
|
|
|
protected $searchList;
|
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
2012-10-04 15:59:36 +02:00
|
|
|
* Define column names which should be included in the search.
|
|
|
|
* By default, they're searched with a {@link StartsWithFilter}.
|
|
|
|
* To define custom filters, use the same notation as {@link DataList->filter()},
|
|
|
|
* e.g. "Name:EndsWith".
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-01 14:46:43 +01:00
|
|
|
* If multiple fields are provided, the filtering is performed non-exclusive.
|
2012-10-04 15:59:36 +02:00
|
|
|
* If no fields are provided, tries to auto-detect fields from
|
|
|
|
* {@link DataObject->searchableFields()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-10-04 15:59:36 +02:00
|
|
|
* The fields support "dot-notation" for relationships, e.g.
|
|
|
|
* a entry called "Team.Name" will search through the names of
|
|
|
|
* a "Team" relationship.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* array(
|
|
|
|
* 'Name',
|
|
|
|
* 'Email:StartsWith',
|
|
|
|
* 'Team.Name'
|
|
|
|
* )
|
2012-01-25 05:31:27 +01:00
|
|
|
*
|
2016-08-19 00:40:29 +02:00
|
|
|
* @var array
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
2012-03-01 14:46:43 +01:00
|
|
|
protected $searchFields = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string SSViewer template to render the results presentation
|
|
|
|
*/
|
|
|
|
protected $resultsFormat = '$Title';
|
2012-03-01 16:48:44 +01:00
|
|
|
|
|
|
|
/**
|
2016-08-19 00:40:29 +02:00
|
|
|
* @var string Text shown on the search field, instructing what to search for.
|
2012-03-01 16:48:44 +01:00
|
|
|
*/
|
|
|
|
protected $placeholderText;
|
2012-07-17 11:56:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $resultsLimit = 20;
|
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
2012-03-01 14:46:43 +01:00
|
|
|
* @param array $searchFields Which fields on the object in the list should be searched
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
2012-03-09 04:50:05 +01:00
|
|
|
public function __construct($targetFragment = 'before', $searchFields = null) {
|
|
|
|
$this->targetFragment = $targetFragment;
|
2012-03-01 14:46:43 +01:00
|
|
|
$this->searchFields = (array)$searchFields;
|
2012-01-25 05:31:27 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-01-25 05:31:27 +01:00
|
|
|
* @param GridField $gridField
|
2016-08-19 00:40:29 +02:00
|
|
|
* @return string[] - HTML
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField) {
|
2012-03-05 12:27:00 +01:00
|
|
|
$dataClass = $gridField->getList()->dataClass();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
$forTemplate = new ArrayData(array());
|
2016-05-02 07:37:03 +02:00
|
|
|
$forTemplate->Fields = new FieldList();
|
2012-03-05 12:27:00 +01:00
|
|
|
|
2013-10-10 23:08:59 +02:00
|
|
|
$searchField = new TextField('gridfield_relationsearch', _t('GridField.RelationSearch', "Relation search"));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-09-27 19:51:32 +02:00
|
|
|
$searchField->setAttribute('data-search-url', Controller::join_links($gridField->Link('search')));
|
2012-03-05 12:27:00 +01:00
|
|
|
$searchField->setAttribute('placeholder', $this->getPlaceholderText($dataClass));
|
2016-05-02 07:37:03 +02:00
|
|
|
$searchField->addExtraClass('relation-search no-change-track action_gridfield_relationsearch');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$findAction = new GridField_FormAction($gridField, 'gridfield_relationfind',
|
|
|
|
_t('GridField.Find', "Find"), 'find', 'find');
|
2012-03-06 16:58:13 +01:00
|
|
|
$findAction->setAttribute('data-icon', 'relationfind');
|
2016-05-02 07:37:03 +02:00
|
|
|
$findAction->addExtraClass('action_gridfield_relationfind');
|
2012-09-26 23:34:00 +02:00
|
|
|
|
|
|
|
$addAction = new GridField_FormAction($gridField, 'gridfield_relationadd',
|
|
|
|
_t('GridField.LinkExisting', "Link Existing"), 'addto', 'addto');
|
2012-03-08 02:55:23 +01:00
|
|
|
$addAction->setAttribute('data-icon', 'chain--plus');
|
2016-05-02 07:37:03 +02:00
|
|
|
$addAction->addExtraClass('action_gridfield_relationadd');
|
2012-01-25 05:31:27 +01:00
|
|
|
|
|
|
|
// If an object is not found, disable the action
|
2013-10-10 23:08:59 +02:00
|
|
|
if(!is_int($gridField->State->GridFieldAddRelation(null))) {
|
2012-01-25 05:31:27 +01:00
|
|
|
$addAction->setReadonly(true);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
$forTemplate->Fields->push($searchField);
|
|
|
|
$forTemplate->Fields->push($findAction);
|
|
|
|
$forTemplate->Fields->push($addAction);
|
2016-05-02 07:37:03 +02:00
|
|
|
if($form = $gridField->getForm()) {
|
|
|
|
$forTemplate->Fields->setForm($form);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 04:50:05 +01:00
|
|
|
return array(
|
|
|
|
$this->targetFragment => $forTemplate->renderWith($this->itemClass)
|
|
|
|
);
|
2012-01-25 05:31:27 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getActions($gridField) {
|
2013-10-10 23:08:59 +02:00
|
|
|
return array('addto');
|
2012-01-25 05:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-10 23:08:59 +02:00
|
|
|
* Manipulate the state to add a new relation
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-01-25 05:31:27 +01:00
|
|
|
* @param GridField $gridField
|
2013-10-10 23:08:59 +02:00
|
|
|
* @param string $actionName Action identifier, see {@link getActions()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param array $arguments Arguments relevant for this
|
2013-10-10 23:08:59 +02:00
|
|
|
* @param array $data All form data
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
|
|
|
|
switch($actionName) {
|
|
|
|
case 'addto':
|
|
|
|
if(isset($data['relationID']) && $data['relationID']){
|
|
|
|
$gridField->State->GridFieldAddRelation = $data['relationID'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
* If an object ID is set, add the object to the list
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param SS_List $dataList
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return SS_List
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
|
|
|
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
|
2013-10-10 23:08:59 +02:00
|
|
|
$objectID = $gridField->State->GridFieldAddRelation(null);
|
|
|
|
if(empty($objectID)) {
|
2012-01-25 05:31:27 +01:00
|
|
|
return $dataList;
|
|
|
|
}
|
2013-10-10 23:08:59 +02:00
|
|
|
$object = DataObject::get_by_id($dataList->dataclass(), $objectID);
|
|
|
|
if($object) {
|
|
|
|
$dataList->add($object);
|
2012-01-25 05:31:27 +01:00
|
|
|
}
|
|
|
|
$gridField->State->GridFieldAddRelation = null;
|
|
|
|
return $dataList;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getURLHandlers($gridField) {
|
|
|
|
return array(
|
2012-07-17 13:26:33 +02:00
|
|
|
'search' => 'doSearch',
|
2012-01-25 05:31:27 +01:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
* Returns a json array of a search results that can be used by for example Jquery.ui.autosuggestion
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param SS_HTTPRequest $request
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
|
|
|
public function doSearch($gridField, $request) {
|
2012-08-09 05:04:02 +02:00
|
|
|
$dataClass = $gridField->getList()->dataClass();
|
|
|
|
$allList = $this->searchList ? $this->searchList : DataList::create($dataClass);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
$searchFields = ($this->getSearchFields())
|
|
|
|
? $this->getSearchFields()
|
|
|
|
: $this->scaffoldSearchFields($dataClass);
|
2012-03-05 12:27:00 +01:00
|
|
|
if(!$searchFields) {
|
|
|
|
throw new LogicException(
|
2012-09-26 23:34:00 +02:00
|
|
|
sprintf('GridFieldAddExistingAutocompleter: No searchable fields could be found for class "%s"',
|
|
|
|
$dataClass));
|
2012-03-05 12:27:00 +01:00
|
|
|
}
|
|
|
|
|
2012-10-04 15:59:36 +02:00
|
|
|
$params = array();
|
2012-03-05 12:27:00 +01:00
|
|
|
foreach($searchFields as $searchField) {
|
2012-10-04 15:59:36 +02:00
|
|
|
$name = (strpos($searchField, ':') !== FALSE) ? $searchField : "$searchField:StartsWith";
|
|
|
|
$params[$name] = $request->getVar('gridfield_relationsearch');
|
2012-03-01 14:46:43 +01:00
|
|
|
}
|
2012-10-04 15:59:36 +02:00
|
|
|
$results = $allList
|
|
|
|
->subtract($gridField->getList())
|
|
|
|
->filterAny($params)
|
|
|
|
->sort(strtok($searchFields[0], ':'), 'ASC')
|
|
|
|
->limit($this->getResultsLimit());
|
2012-06-15 06:08:54 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
$json = array();
|
2016-08-19 00:40:29 +02:00
|
|
|
Config::nest();
|
2013-11-08 09:34:22 +01:00
|
|
|
Config::inst()->update('SSViewer', 'source_file_comments', false);
|
2016-08-19 00:40:29 +02:00
|
|
|
$viewer = SSViewer::fromString($this->resultsFormat);
|
2012-01-25 05:31:27 +01:00
|
|
|
foreach($results as $result) {
|
2016-08-19 00:40:29 +02:00
|
|
|
$json[$result->ID] = html_entity_decode($viewer->process($result));
|
2012-01-25 05:31:27 +01:00
|
|
|
}
|
2016-08-19 00:40:29 +02:00
|
|
|
Config::unnest();
|
2012-01-25 05:31:27 +01:00
|
|
|
return Convert::array2json($json);
|
|
|
|
}
|
2012-03-01 14:46:43 +01:00
|
|
|
|
|
|
|
/**
|
2016-08-19 00:40:29 +02:00
|
|
|
* @param string $format
|
|
|
|
*
|
|
|
|
* @return $this
|
2012-03-01 14:46:43 +01:00
|
|
|
*/
|
|
|
|
public function setResultsFormat($format) {
|
|
|
|
$this->resultsFormat = $format;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 00:40:29 +02:00
|
|
|
* @return string
|
2012-03-01 14:46:43 +01:00
|
|
|
*/
|
|
|
|
public function getResultsFormat() {
|
|
|
|
return $this->resultsFormat;
|
|
|
|
}
|
|
|
|
|
2012-08-01 09:15:31 +02:00
|
|
|
/**
|
|
|
|
* Sets the base list instance which will be used for the autocomplete
|
|
|
|
* search.
|
|
|
|
*
|
|
|
|
* @param SS_List $list
|
|
|
|
*/
|
|
|
|
public function setSearchList(SS_List $list) {
|
|
|
|
$this->searchList = $list;
|
2016-08-19 00:40:29 +02:00
|
|
|
return $this;
|
2012-08-01 09:15:31 +02:00
|
|
|
}
|
|
|
|
|
2012-03-01 14:46:43 +01:00
|
|
|
/**
|
2016-08-19 00:40:29 +02:00
|
|
|
* @param array $fields
|
2012-03-01 14:46:43 +01:00
|
|
|
*/
|
|
|
|
public function setSearchFields($fields) {
|
|
|
|
$this->searchFields = $fields;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 00:40:29 +02:00
|
|
|
* @return array
|
2012-03-01 14:46:43 +01:00
|
|
|
*/
|
|
|
|
public function getSearchFields() {
|
|
|
|
return $this->searchFields;
|
|
|
|
}
|
2012-03-01 16:48:44 +01:00
|
|
|
|
2012-03-05 12:27:00 +01:00
|
|
|
/**
|
2012-10-04 15:59:36 +02:00
|
|
|
* Detect searchable fields and searchable relations.
|
|
|
|
* Falls back to {@link DataObject->summaryFields()} if
|
|
|
|
* no custom search fields are defined.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-08-19 00:40:29 +02:00
|
|
|
* @param string $dataClass the class name
|
|
|
|
* @return array|null names of the searchable fields
|
2012-03-05 12:27:00 +01:00
|
|
|
*/
|
2012-10-04 15:59:36 +02:00
|
|
|
public function scaffoldSearchFields($dataClass) {
|
2012-03-05 12:27:00 +01:00
|
|
|
$obj = singleton($dataClass);
|
2012-10-04 15:59:36 +02:00
|
|
|
$fields = null;
|
|
|
|
if($fieldSpecs = $obj->searchableFields()) {
|
|
|
|
$customSearchableFields = $obj->stat('searchable_fields');
|
|
|
|
foreach($fieldSpecs as $name => $spec) {
|
|
|
|
if(is_array($spec) && array_key_exists('filter', $spec)) {
|
|
|
|
// The searchableFields() spec defaults to PartialMatch,
|
|
|
|
// so we need to check the original setting.
|
|
|
|
// If the field is defined $searchable_fields = array('MyField'),
|
|
|
|
// then default to StartsWith filter, which makes more sense in this context.
|
|
|
|
if(!$customSearchableFields || array_search($name, $customSearchableFields)) {
|
|
|
|
$filter = 'StartsWith';
|
|
|
|
} else {
|
|
|
|
$filter = preg_replace('/Filter$/', '', $spec['filter']);
|
|
|
|
}
|
|
|
|
$fields[] = "{$name}:{$filter}";
|
|
|
|
} else {
|
|
|
|
$fields[] = $name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_null($fields)) {
|
|
|
|
if ($obj->hasDatabaseField('Title')) {
|
|
|
|
$fields = array('Title');
|
|
|
|
} elseif ($obj->hasDatabaseField('Name')) {
|
|
|
|
$fields = array('Name');
|
|
|
|
}
|
2012-03-05 12:27:00 +01:00
|
|
|
}
|
2012-10-04 15:59:36 +02:00
|
|
|
|
|
|
|
return $fields;
|
2012-03-05 12:27:00 +01:00
|
|
|
}
|
|
|
|
|
2012-03-01 16:48:44 +01:00
|
|
|
/**
|
2016-08-19 00:40:29 +02:00
|
|
|
* @param string $dataClass The class of the object being searched for
|
|
|
|
*
|
|
|
|
* @return string
|
2012-03-01 16:48:44 +01:00
|
|
|
*/
|
|
|
|
public function getPlaceholderText($dataClass) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$searchFields = ($this->getSearchFields())
|
|
|
|
? $this->getSearchFields()
|
|
|
|
: $this->scaffoldSearchFields($dataClass);
|
2012-03-05 12:27:00 +01:00
|
|
|
|
2012-03-01 16:48:44 +01:00
|
|
|
if($this->placeholderText) {
|
|
|
|
return $this->placeholderText;
|
|
|
|
} else {
|
|
|
|
$labels = array();
|
2012-03-08 23:56:12 +01:00
|
|
|
if($searchFields) foreach($searchFields as $searchField) {
|
2015-01-07 15:57:57 +01:00
|
|
|
$searchField = explode(':', $searchField);
|
|
|
|
$label = singleton($dataClass)->fieldLabel($searchField[0]);
|
2012-03-01 16:48:44 +01:00
|
|
|
if($label) $labels[] = $label;
|
|
|
|
}
|
|
|
|
if($labels) {
|
2012-05-01 21:44:54 +02:00
|
|
|
return _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'GridField.PlaceHolderWithLabels',
|
|
|
|
'Find {type} by {name}',
|
2015-01-07 15:57:57 +01:00
|
|
|
array('type' => singleton($dataClass)->i18n_plural_name(), 'name' => implode(', ', $labels))
|
2012-03-01 16:48:44 +01:00
|
|
|
);
|
|
|
|
} else {
|
2012-05-01 21:44:54 +02:00
|
|
|
return _t(
|
|
|
|
'GridField.PlaceHolder', 'Find {type}',
|
2015-01-07 15:57:57 +01:00
|
|
|
array('type' => singleton($dataClass)->i18n_plural_name())
|
2012-03-01 16:48:44 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 00:40:29 +02:00
|
|
|
* @param string $text
|
|
|
|
*
|
|
|
|
* @return $this
|
2012-03-01 16:48:44 +01:00
|
|
|
*/
|
|
|
|
public function setPlaceholderText($text) {
|
|
|
|
$this->placeholderText = $text;
|
2016-08-19 00:40:29 +02:00
|
|
|
return $this;
|
2012-03-01 16:48:44 +01:00
|
|
|
}
|
2012-07-17 11:56:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the maximum number of autocomplete results to display.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getResultsLimit() {
|
|
|
|
return $this->resultsLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $limit
|
2016-08-19 00:40:29 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
2012-07-17 11:56:18 +02:00
|
|
|
*/
|
|
|
|
public function setResultsLimit($limit) {
|
|
|
|
$this->resultsLimit = $limit;
|
2016-08-19 00:40:29 +02:00
|
|
|
return $this;
|
2012-07-17 11:56:18 +02:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|