Source of file ListFilterBase.php
Size: 6,029 Bytes - Last Modified: 2021-12-23T10:20:51+00:00
/var/www/docs.ssmods.com/process/src/code/model/ListFilterBase.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 | <?php class ListFilterBase extends DataObject { private static $db = array( 'Title' => 'Varchar(255)', 'Sort' => 'Int', ); private static $has_one = array( 'Parent' => 'ListFilterSet', ); private static $summary_fields = array( 'singular_name' => 'Type', 'Title' => 'Title', 'ContextSummaryField' => 'Context', ); /** * @var string */ private static $default_sort = 'Sort'; /** * Hide from 'GridFieldAddClassesButton' * * @var string */ private static $hide_ancestor = 'ListFilterBase'; /** * @var array */ protected static $shared_filter_instances = array(); /** * This is executed before 'getFilterFields' and allows you to set properties * based on the $_GET vars so that templates can utilize the logic. * * ie. The use case for this is to detect if the user has explicitly disabled * finding records relevant to their location, but only for that request. * * @return null */ public function init(array $data) { } /** * @return FieldList */ public function getCMSFields() { $self = &$this; $self->beforeUpdateCMSFields(function($fields) use ($self) { $fields->removeByName(array('Sort', 'ParentID')); $error = $self->getContextSummaryField(true); if ($error) { $fields->insertBefore(LiteralField::create('ConfigError_Literal', $self->getContextSummaryField(true)), 'Title'); } }); $fields = parent::getCMSFields(); return $fields; } /** * Get list class name * ie. 'SiteTree' * * @return string */ public function getListClass() { return $this->Parent()->ListClass; } /** * The fields to show on the frontend to manipulate the map and * listing content. * * @return FieldList */ public function getFilterFields() { return new FieldList(); } /** * @return FieldList */ final public function getFilterFieldsAll() { $fields = $this->getFilterFields(); $this->extend('updateFilterFields', $fields); return $fields; } /** * Get config variables to utilize with the JavaScript callback. * This data is attached to '.js-listfilter-filter' elements. * * ie. $(this).data('data-fieldgroup-config') * * @return array */ public function getFilterConfig(array $data) { return array(); } /** * When generating map pins / widget data, this function will * add additional data so that the pins can be filtered in JavaScript. * * @return mixed */ public function getFilterData(DataObject $record) { return array( 'value' => null ); } /** * The data to return for filtering based on the backend request. * ie. For Solr, you just want to return a map/associate array of IDs that matches the search * for the frontend filtering. * * @return string */ public function getFilterBackendData(SS_List $list, array $data) { return null; } /** * When the form hits the backend (for updating the listing), filter * the SS_List by the filter fields. * * @return SS_List|ListFilterShared|null */ public function applyFilter(SS_List $list, array $data) { throw new Exception('Missing "'.__FUNCTION__.'" implementation for "'.$this->class.'"'); } /** * Allow inspection of SS_List and shared filters to change current objects state. * ie. ListFilterSolrFacet gets facets and updates its CheckboxSetField * * @param SS_List $list */ public function finaliseFilter(SS_List $list) { } /** * Return a ListFilterShared object to share between ListFilterBase objects. * * @return ListFilterShared */ public function SharedFilter($class, $namespace = '') { if (isset(self::$shared_filter_instances[$namespace][$class])) { return self::$shared_filter_instances[$namespace][$class]; } $sharedFilter = $this->LocalFilter($class); self::$shared_filter_instances[$namespace][$class] = $sharedFilter; return $sharedFilter; } /** * Return a ListFilterShared object to use locally only. * * @return ListFilterShared */ public function LocalFilter($class) { $sharedFilter = $class::create($this->Parent()); return $sharedFilter; } /** * The name of the event to pass to * jQuery('.js-listfilter-filter').triggerHandler() * * For examples, check 'ListFilter.js' * * @return string */ public function getJavascriptCallback() { return $this->class; } /** * {@inheritdoc} */ public function singular_name() { if ($this->class === __CLASS__) { return parent::singular_name(); } $singularName = parent::singular_name(); return str_replace('List Filter', '', $singularName); } /** * Get the calling object of the filtering functions like 'applyFilter'. * * @return object */ public function getCaller() { return $this->Parent()->getCaller(); } /** * @return string */ public function getContextSummaryField($errorOnly = false) { $html = new HTMLText('ContextSummaryField'); // Get class name, if its a 'special case' type, blank it out. $class = ''; $parent = $this->Parent(); if ($parent && $parent->exists()) { $class = $parent->ListClassName; if ($class && $class[0] === '(') { $class = ''; } } // Set color if error $color = ''; $error = $this->getConfigError($class); if ($error) { if ($error === true || $error === 1) { $error = 'Error'; } $color = '#C00'; } $context = $this->getContext(); $text = $error; if (!$errorOnly) { if ($text && $context) { $text .= ' -- '.$context; } else { $text .= $context; } } $html->setValue(sprintf( '<span %s>%s</span>', $color ? 'style="color: '.$color.';"' : '', htmlentities($text) )); return $html; } /** * Return generalized information about the filter configuration * * @return string */ public function getContext() { return ''; } /** * Return an error message to show in CMS fields and in summary fields * * @return bool|string */ public function getConfigError($class) { return false; } } |