/**
  * If HideUnmatchedFilters is on then remove all filters which are not found in the items by their 'AssociatedFilters' relationship.
  *
  * @param \DataList $filters list of GridListFilter models
  * @param array     $parameters
  * @return \ArrayList
  */
 public function constrainGridListFilters(&$filters, &$parameters = [])
 {
     $out = new \ArrayList();
     if ($this()->{self::SingleFieldName}) {
         $ids = $filters->column('ID');
         if (count($ids)) {
             $items = $this()->GridListItems();
             // this is where we keep track of GridListFilters which have been found on items where ID is the key
             $foundFilters = array_combine($ids, array_fill(0, count($ids), false));
             foreach ($foundFilters as $filterID => &$found) {
                 /** @var \Page|Model $item */
                 foreach ($items as $item) {
                     if ($item->hasExtension(HasGridListFilters::class_name())) {
                         if ($itemFilters = $item->{HasGridListFilters::relationship_name()}()->column('ID')) {
                             if (in_array($filterID, $itemFilters)) {
                                 $found = true;
                                 break;
                             }
                         }
                     }
                 }
             }
             foreach ($filters as $filter) {
                 if (isset($foundFilters[$filter->ID])) {
                     $out->push($filter);
                 }
             }
             $filters = $out;
         }
     }
 }
 public function sequenceGridListFilters(&$filters, $items, &$parameters = [])
 {
     if ($allFilter = Application::get_current_page()->FilterAll()) {
         $allTag = $allFilter->Filter;
         $allItemCount = 0;
     } else {
         $allItemCount = $items->count();
         $allTag = '';
     }
     foreach ($filters as $filter) {
         $itemCount = 0;
         $tag = $filter->ModelTag;
         /** @var \DataObject|HasGridListFilters $item */
         foreach ($items as $item) {
             if ($item->hasExtension(HasGridListFilters::class_name())) {
                 if ($item->GridListFilters()->find('ModelTag', $tag)) {
                     $itemCount++;
                 } else {
                     if ($allTag == 'all' || $allTag && $item->GridListFilters()->find('ModelTag', $allTag)) {
                         $allItemCount++;
                     }
                 }
             }
         }
         // don't recount 'all filter' after first pass
         $allTag = false;
         $filter->ItemCount = max($itemCount - 1, 0);
     }
     $parameters[self::AllItemCountKey] = max($allItemCount - 1, 0);
 }
 /**
  * Expects parameters 'start' and 'limit' to be set, limits items by filter to page length
  *
  *
  * @param \SS_LIst $items
  * @param          $filters
  * @param array    $parameters
  */
 public function sequenceGridListItems(&$items, $filters, &$parameters = [])
 {
     $out = new \ArrayList();
     $start = $parameters[Constraints::StartIndexGetVar];
     $limit = $parameters[Constraints::PageLengthGetVar];
     if (!is_null($limit)) {
         $added = 0;
         if ($allFilter = Application::get_current_page()->FilterAll()) {
             // first add 'all filter' items
             if ($allTag = $allFilter->Filter) {
                 $index = 0;
                 $added = 0;
                 foreach ($items as $item) {
                     $index++;
                     if ($index < $start) {
                         continue;
                     }
                     if ($allTag == 'all' || $item->GridListFilters()->find('ModelTag', $allTag)) {
                         // we don't add all, we're just getting the count
                         // $out->push($item);
                         $added++;
                     }
                     if ($added >= $limit) {
                         break;
                     }
                 }
             }
         }
         // initial number of 'all filter' items loaded in page
         $parameters['AllLoadCount'] = $added;
         foreach ($filters as $filter) {
             if ($tag = $filter->ModelTag) {
                 $index = 0;
                 $added = 0;
                 foreach ($items as $item) {
                     $index++;
                     if ($index < $start) {
                         continue;
                     }
                     if ($item->hasExtension(HasGridListFilters::class_name())) {
                         if ($item->GridListFilters()->find('ModelTag', $tag)) {
                             $out->push($item);
                             $added++;
                         }
                     }
                     if ($added >= $limit) {
                         break;
                     }
                 }
                 // initial number of items loaded in page (may be less than page length)
                 $filter->LoadCount = $added;
             }
         }
         $out->removeDuplicates();
         $items = $out;
     }
 }
 /**
  * Provide pages which have
  *
  * @return \DataList
  */
 public function provideGridListItems($parameters = [])
 {
     if ($this()->{self::SingleFieldName}) {
         if ($this()->hasExtension(HasGridListFilters::class_name())) {
             $filterIDs = $this->filterIDs();
             // name of the field on Pages
             $filterField = HasGridListFilters::relationship_name('ID');
             $pages = \Page::get()->filter([$filterField => $filterIDs]);
             // debug help
             $count = $pages->count();
             return $pages;
         }
     }
 }
 /**
  * @return \ArrayList
  */
 protected function items($mode)
 {
     static $items;
     if (!$items) {
         $providers = $this->providers();
         $items = new \ArrayList();
         $service = $this->service();
         $currentFilterID = $service->Filters()->currentFilterID();
         foreach ($providers as $provider) {
             // first we get any items related to the GridList itself , e.g. curated blocks added by HasBlocks
             // this will return an array of SS_Lists
             $lists = $provider->extend('provideGridListItems');
             /** @var \ManyManyList $list */
             foreach ($lists as $itemList) {
                 // filter to current filter if set
                 if ($currentFilterID) {
                     $itemList = $itemList->filter([HasGridListFilters::relationship_name('ID') => $currentFilterID]);
                 }
                 $items->merge($itemList);
             }
         }
         $items->removeDuplicates();
     }
     return $items;
 }
 public function FilterTags()
 {
     return implode(' ', array_merge($this()->{HasGridListFilters::relationship_name()}()->column('ModelTag'), $this()->hasMethod('customFilterTags') ? $this()->customFilterTags() : []));
 }