Source of file ListFilterTags.php
Size: 2,609 Bytes - Last Modified: 2021-12-23T10:20:51+00:00
/var/www/docs.ssmods.com/process/src/code/model/ListFilterTags.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | <?php class ListFilterTags extends ListFilterBase { /** * Hide from 'GridFieldAddClassesButton' * * @var string */ private static $hide_ancestor = 'ListFilterTags'; /** * The tags available to the user on the frontend. * * @return SS_List */ public function SelectableTags() { throw new Exception($this->class.' must override '.__FUNCTION__); } /** * The tags available to the user on the frontend. * * @return SS_List */ protected $cache_selected_tags = null; final public function SelectableTagsAll() { if ($this->cache_selected_tags !== null) { return $this->cache_selected_tags; } $list = $this->SelectableTags(); $this->extend('updateSelectableTags', $list); return $this->cache_selected_tags = $list; } /** * {@inheritdoc} */ public function getComponentRelationName() { throw new Exception($this->class.' must override '.__FUNCTION__); } /** * {@inheritdoc} */ public function getFilterFields() { $fields = parent::getFilterFields(); $tags = $this->SelectableTagsAll(); if ($tags instanceof SS_List) { $tags = $tags->map('ID', 'Title'); } $fields->push($field = CheckboxSetField::create('Tags', '', $tags)); return $fields; } /** * Return tag ID(s) used by record * * @return array */ public function getFilterData(DataObject $record) { $relationName = $this->getComponentRelationName(); $tagsOrHasOne = $record->$relationName(); $tagIDs = array(); if ($tagsOrHasOne instanceof DataList || $tagsOrHasOne instanceof ArrayList) { // Handle has_many / many_many relationship $tagIDs = $tagsOrHasOne->map('ID', 'ID'); if ($tagIDs instanceof SS_Map) { $tagIDs = $tagIDs->toArray(); } $tagIDs = array_values($tagIDs); } else if ($tagsOrHasOne && $tagsOrHasOne->exists()) { // Handle has_one relationship $tagIDs[] = $tagsOrHasOne->ID; } return array( 'value' => $tagIDs ); } /** * {@inheritdoc} */ public function applyFilter(SS_List $list, array $data) { if (!isset($data['Tags'])) { return; } $list = ListFilterUtility::filter_by_relation_ids($list, $this->getComponentRelationName(), array_keys($data['Tags'])); return $list; } /** * {@inheritdoc} */ public function getJavascriptCallback() { return 'ListFilterTags'; } /** * {@inheritdoc} */ public function getContext() { if ($this->isInDB()) { $list = $this->SelectableTagsAll(); if ($list instanceof SS_List) { $list = $list->map('Title'); } if ($list instanceof SS_Map) { $list = $list->toArray(); } return 'Tags: '.implode(', ', $list); } } } |