Source of file ListFilterWidget.php
Size: 5,172 Bytes - Last Modified: 2021-12-23T10:20:51+00:00
/var/www/docs.ssmods.com/process/src/code/widget/ListFilterWidget.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 | <?php abstract class ListFilterWidget extends Controller { private static $hide_ancestor = 'ListFilterWidget'; /** * A custom set list to use for the widget */ protected $list = null; /** * @var ListFilterForm */ protected $form; /** * @var DataObject */ protected $record = null; /** * Extra CSS classes * * @var array */ protected $extraClasses; public function __construct() { parent::__construct(); $this->addExtraClass('js-listfilter-widget'); } /** * @return void */ public function onBeforeRender() { } /** * @return ListFilterSet */ public function getListFilterSet() { $form = $this->getForm(); return ($form) ? $form->getRecord() : null; } /** * @return ListFilterWidget */ public function setForm(ListFilterForm $form) { $this->form = $form; return $this; } /** * @return ListFilterForm */ public function getForm() { return $this->form; } /** * Get current page * * @return SiteTree */ public function getPage() { $form = $this->getForm(); if (!$form) { return null; } return $form->getPage(); } /** * @return DataObject */ public function setRecord(DataObjectInterface $record) { $this->record = $record; return $this; } /** * @return DataObject */ public function getRecord() { return $this->record; } /** * @return SS_List */ public function BaseList() { $list = $this->getList(); if (!$list) { $filterSetRecord = $this->getListFilterSet(); $list = $filterSetRecord->BaseList(); } return $list; } /** * @return SS_List */ public function FilteredList($data = array()) { $filterSetRecord = $this->getListFilterSet(); $list = $this->BaseList(); $list = $filterSetRecord->applyFilterToList($list, $data, $this); return $list; } /** * Override the list used for the widget. * * @return ListFilterWidget */ public function setList(SS_List $list) { $this->list = $list; return $this; } /** * @return SS_List */ public function getList() { return $this->list; } /** * @return array */ public function getDataAttributes() { $attributes = array(); $listFilterSet = $this->getListFilterSet(); if ($listFilterSet) { // NOTE(Jake): This is required to link the <form> and map <div> together (2016-08-23) $attributes['listfilter-id'] = $listFilterSet->ID; } return $attributes; } /** * @return array */ final public function getDataAttributesAll() { $attributes = $this->getDataAttributes(); $this->extend('updateDataAttributes', $attributes); return $attributes; } /** * @return string */ protected $_cache_data_attributes_html = null; public function DataAttributesHTML() { if ($this->_cache_data_attributes_html !== null) { return $this->_cache_data_attributes_html; } $result = ''; $attributes = $this->getDataAttributesAll(); foreach ($attributes as $attribute => $value) { if ($value !== null) { if (is_array($value)) { $value = Convert::raw2att(json_encode($value)); } $result .= 'data-'.$attribute.'="'.$value.'" '; } } return $this->_cache_data_attributes_html = rtrim($result); } /** * If visiting the page with GET parameterss. * * @return array */ public function getVarData() { $data = $this->getRequest()->getVars(); if (!$data) { // Fallback to form $data = $this->getForm()->getVarData(); } return $data; } /** * @return string */ public function Link($action = null) { return Controller::join_links($this->getForm()->Link('doWidget'), $action); } /** * Compiles all CSS-classes set on this. * * @return string */ public function extraClass() { $classes = array(); if($this->extraClasses) { $classes = array_merge( $classes, array_values($this->extraClasses) ); } return implode(' ', $classes); } /** * Add one or more CSS-classes * * Multiple class names should be space delimited. * * @param string $class * * @return $this */ public function addExtraClass($class) { $classes = preg_split('/\s+/', $class); foreach ($classes as $class) { $this->extraClasses[$class] = $class; } return $this; } /** * Remove one or more CSS-classes * * @param string $class * * @return $this */ public function removeExtraClass($class) { $classes = preg_split('/\s+/', $class); foreach ($classes as $class) { unset($this->extraClasses[$class]); } return $this; } /** * @return array */ public function getTemplates($templateName, $recordOrClasses = null) { if ($recordOrClasses === null) { $recordOrClasses = $this->getRecord(); if (!$recordOrClasses) { $recordOrClasses = $this->getPage(); } } $result = ListFilterUtility::get_templates($templateName, $recordOrClasses); // todo(Jake): Add and test // $this->extend('updateTemplates', $result, $templateName, $recordOrClasses); return $result; } /** * @return HTMLText */ public function forTemplate() { $this->onBeforeRender(); $this->extend('onBeforeRender', $this); $result = $this->renderWith(array($this->class, __CLASS__)); return $result; } } |