/**
  * Build search results.
  */
 public function onPagesInitialized()
 {
     /** @var Taxonomy $taxonomy_map */
     $taxonomy_map = $this->grav['taxonomy'];
     $filters = (array) $this->config->get('plugins.simplesearch.filters');
     $operator = $this->config->get('plugins.simplesearch.filter_combinator', 'and');
     $this->collection = new Collection();
     $this->collection->append($taxonomy_map->findTaxonomy($filters, $operator)->toArray());
     /** @var Page $page */
     foreach ($this->collection as $page) {
         foreach ($this->query as $query) {
             $query = trim($query);
             if (stripos($page->content(), $query) === false && stripos($page->title(), $query) === false) {
                 $this->collection->remove($page);
             }
         }
     }
 }
 /**
  * Build search results.
  */
 public function onPagesInitialized()
 {
     /** @var Taxonomy $taxonomy_map */
     $taxonomy_map = $this->grav['taxonomy'];
     $filters = (array) $this->config->get('plugins.simplesearch.filters');
     $operator = $this->config->get('plugins.simplesearch.filter_combinator', 'and');
     $this->collection = new Collection();
     $this->collection->append($taxonomy_map->findTaxonomy($filters, $operator)->toArray());
     $extras = [];
     /** @var Page $page */
     foreach ($this->collection as $page) {
         foreach ($this->query as $query) {
             $query = trim($query);
             if (stripos($page->content(), $query) === false && stripos($page->title(), $query) === false) {
                 $this->collection->remove($page);
                 continue;
             }
             if ($page->modular()) {
                 $this->collection->remove($page);
                 $parent = $page->parent();
                 $extras[$parent->path()] = ['slug' => $parent->slug()];
             }
         }
     }
     if (!empty($extras)) {
         $this->collection->append($extras);
     }
     // use a configured sorting order
     $this->collection = $this->collection->order($this->config->get('order.by'), $this->config->get('order.dir'));
     // create the search page
     $page = new Page();
     $page->init(new \SplFileInfo(__DIR__ . '/pages/simplesearch.md'));
     // override the template is set in the config
     $template_override = $this->config->get('plugins.simplesearch.template');
     if ($template_override) {
         $page->template($template_override);
     }
     // allows us to redefine the page service without triggering RuntimeException: Cannot override frozen service
     // "page" issue
     unset($this->grav['page']);
     $this->grav['page'] = $page;
 }
Beispiel #3
0
 /**
  * Get a collection of pages in the current context.
  *
  * @param string|array $params
  * @param boolean $pagination
  * @return Collection
  * @throws \InvalidArgumentException
  */
 public function collection($params = 'content', $pagination = true)
 {
     if (is_string($params)) {
         $params = (array) $this->value('header.' . $params);
     } elseif (!is_array($params)) {
         throw new \InvalidArgumentException('Argument should be either header variable name or array of parameters');
     }
     if (!isset($params['items'])) {
         return array();
     }
     $collection = $this->evaluate($params['items']);
     if (!$collection instanceof Collection) {
         $collection = new Collection();
     }
     $collection->setParams($params);
     /** @var Uri $uri */
     $uri = self::getGrav()['uri'];
     /** @var Config $config */
     $config = self::getGrav()['config'];
     $process_taxonomy = isset($params['url_taxonomy_filters']) ? $params['url_taxonomy_filters'] : $config->get('system.pages.url_taxonomy_filters');
     if ($process_taxonomy) {
         foreach ((array) $config->get('site.taxonomies') as $taxonomy) {
             if ($uri->param($taxonomy)) {
                 $items = explode(',', $uri->param($taxonomy));
                 $collection->setParams(['taxonomies' => [$taxonomy => $items]]);
                 foreach ($collection as $page) {
                     // Don't filter modular pages
                     if ($page->modular()) {
                         continue;
                     }
                     foreach ($items as $item) {
                         if (empty($page->taxonomy[$taxonomy]) || !in_array(htmlspecialchars_decode($item, ENT_QUOTES), $page->taxonomy[$taxonomy])) {
                             $collection->remove();
                         }
                     }
                 }
             }
         }
     }
     if (isset($params['dateRange'])) {
         $start = isset($params['dateRange']['start']) ? $params['dateRange']['start'] : 0;
         $end = isset($params['dateRange']['end']) ? $params['dateRange']['end'] : false;
         $field = isset($params['dateRange']['field']) ? $params['dateRange']['field'] : false;
         $collection->dateRange($start, $end, $field);
     }
     if (isset($params['order'])) {
         $by = isset($params['order']['by']) ? $params['order']['by'] : 'default';
         $dir = isset($params['order']['dir']) ? $params['order']['dir'] : 'asc';
         $custom = isset($params['order']['custom']) ? $params['order']['custom'] : null;
         $collection->order($by, $dir, $custom);
     }
     /** @var Grav $grav */
     $grav = self::getGrav()['grav'];
     // New Custom event to handle things like pagination.
     $grav->fireEvent('onCollectionProcessed', new Event(['collection' => $collection]));
     // Slice and dice the collection if pagination is required
     if ($pagination) {
         $params = $collection->params();
         $limit = isset($params['limit']) ? $params['limit'] : 0;
         $start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0;
         if ($limit && $collection->count() > $limit) {
             $collection->slice($start, $limit);
         }
     }
     return $collection;
 }
 /**
  * Build search results.
  */
 public function onPagesInitialized()
 {
     $page = $this->grav['page'];
     // If a page exists merge the configs
     if ($page) {
         $this->config->set('plugins.simplesearch', $this->mergeConfig($page));
     }
     /** @var Uri $uri */
     $uri = $this->grav['uri'];
     $query = $uri->param('query') ?: $uri->query('query');
     $route = $this->config->get('plugins.simplesearch.route');
     // performance check
     if ($route && $query && $route == $uri->path()) {
         $this->enable(['onTwigSiteVariables' => ['onTwigSiteVariables', 0]]);
     } else {
         return;
     }
     $this->query = explode(',', $query);
     /** @var Taxonomy $taxonomy_map */
     $taxonomy_map = $this->grav['taxonomy'];
     $taxonomies = [];
     $filters = (array) $this->config->get('plugins.simplesearch.filters');
     $operator = $this->config->get('plugins.simplesearch.filter_combinator', 'and');
     // see if the filter uses the new 'items-type' syntax
     $new_approach = false;
     foreach ($filters as $filter) {
         $filter_saved = $filter;
         if (is_array($filter)) {
             $filter = key($filter);
         }
         if (Utils::startsWith($filter, '@')) {
             if ($filter == '@self') {
                 $new_approach = true;
             }
             if ($filter == '@taxonomy') {
                 $taxonomies = $filter_saved[$filter];
             }
         }
     }
     if ($new_approach) {
         $params = $page->header()->content;
         $params['query'] = $this->config->get('plugins.simplesearch.query');
         $this->collection = $page->collection($params, false);
     } else {
         $this->collection = new Collection();
         $this->collection->append($taxonomy_map->findTaxonomy($filters, $operator)->toArray());
     }
     $extras = [];
     /** @var Page $cpage */
     foreach ($this->collection as $cpage) {
         foreach ($this->query as $query) {
             $query = trim($query);
             $taxonomy_match = false;
             if (!empty($taxonomies)) {
                 $page_taxonomies = $cpage->taxonomy();
                 foreach ((array) $taxonomies as $taxonomy) {
                     if (array_key_exists($taxonomy, $page_taxonomies)) {
                         $taxonomy_values = implode('|', $page_taxonomies[$taxonomy]);
                         if (stripos($taxonomy_values, $query) !== false) {
                             $taxonomy_match = true;
                             break;
                         }
                     }
                 }
             }
             if ($taxonomy_match === false && stripos($cpage->content(), $query) === false && stripos($cpage->title(), $query) === false) {
                 $this->collection->remove($cpage);
                 continue;
             }
             if ($cpage->modular()) {
                 $this->collection->remove($cpage);
                 $parent = $cpage->parent();
                 $extras[$parent->path()] = ['slug' => $parent->slug()];
             }
         }
     }
     if (!empty($extras)) {
         $this->collection->append($extras);
     }
     // use a configured sorting order if not already done
     if (!$new_approach) {
         $this->collection = $this->collection->order($this->config->get('plugins.simplesearch.order.by'), $this->config->get('plugins.simplesearch.order.dir'));
     }
     // if page doesn't have settings set, create a page
     if (!isset($page->header()->simplesearch)) {
         // create the search page
         $page = new Page();
         $page->init(new \SplFileInfo(__DIR__ . '/pages/simplesearch.md'));
         // override the template is set in the config
         $template_override = $this->config->get('plugins.simplesearch.template');
         if ($template_override) {
             $page->template($template_override);
         }
         // fix RuntimeException: Cannot override frozen service "page" issue
         unset($this->grav['page']);
         $this->grav['page'] = $page;
     }
 }
Beispiel #5
0
 /**
  * Build search results.
  */
 public function onPagesInitialized()
 {
     $page = $this->grav['page'];
     // If a page exists merge the configs
     if ($page) {
         $this->config->set('plugins.simplesearch', $this->mergeConfig($page));
     }
     /** @var Uri $uri */
     $uri = $this->grav['uri'];
     $query = $uri->param('query') ?: $uri->query('query');
     $route = $this->config->get('plugins.simplesearch.route');
     // performance check for query
     if (empty($query)) {
         return;
     }
     // Support `route: '@self'` syntax
     if ($route === '@self') {
         $route = $page . route();
         $this->config->set('plugins.simplesearch.route', $route);
     }
     // performance check for route
     if (!($route && $route == $uri->path())) {
         return;
     }
     // Explode query into multiple strings
     $this->query = explode(',', $query);
     /** @var Taxonomy $taxonomy_map */
     $taxonomy_map = $this->grav['taxonomy'];
     $taxonomies = [];
     $find_taxonomy = [];
     $filters = (array) $this->config->get('plugins.simplesearch.filters');
     $operator = $this->config->get('plugins.simplesearch.filter_combinator', 'and');
     $new_approach = false;
     if (!$filters || $query === false || count($filters) == 1 && !reset($filters)) {
         /** @var \Grav\Common\Page\Pages $pages */
         $pages = $this->grav['pages'];
         $this->collection = $pages->all();
     } else {
         foreach ($filters as $key => $filter) {
             // flatten item if it's wrapped in an array
             if (is_int($key)) {
                 if (is_array($filter)) {
                     $key = key($filter);
                     $filter = $filter[$key];
                 } else {
                     $key = $filter;
                 }
             }
             // see if the filter uses the new 'items-type' syntax
             if ($key === '@self' || $key === 'self@') {
                 $new_approach = true;
             } elseif ($key === '@taxonomy' || $key === 'taxonomy@') {
                 $taxonomies = $filter === false ? false : array_merge($taxonomies, (array) $filter);
             } else {
                 $find_taxonomy[$key] = $filter;
             }
         }
         if ($new_approach) {
             $params = $page->header()->content;
             $params['query'] = $this->config->get('plugins.simplesearch.query');
             $this->collection = $page->collection($params, false);
         } else {
             $this->collection = new Collection();
             $this->collection->append($taxonomy_map->findTaxonomy($find_taxonomy, $operator)->toArray());
         }
     }
     $extras = [];
     if ($query) {
         foreach ($this->collection as $cpage) {
             foreach ($this->query as $query) {
                 $query = trim($query);
                 if ($this->notFound($query, $cpage, $taxonomies)) {
                     $this->collection->remove($cpage);
                     continue;
                 }
                 if ($cpage->modular()) {
                     $this->collection->remove($cpage);
                     $parent = $cpage->parent();
                     $extras[$parent->path()] = ['slug' => $parent->slug()];
                 }
             }
         }
     }
     if (!empty($extras)) {
         $this->collection->append($extras);
     }
     // use a configured sorting order if not already done
     if (!$new_approach) {
         $this->collection = $this->collection->order($this->config->get('plugins.simplesearch.order.by'), $this->config->get('plugins.simplesearch.order.dir'));
     }
     // if page doesn't have settings set, create a page
     if (!isset($page->header()->simplesearch)) {
         // create the search page
         $page = new Page();
         $page->init(new \SplFileInfo(__DIR__ . '/pages/simplesearch.md'));
         // override the template is set in the config
         $template_override = $this->config->get('plugins.simplesearch.template');
         if ($template_override) {
             $page->template($template_override);
         }
         // fix RuntimeException: Cannot override frozen service "page" issue
         unset($this->grav['page']);
         $this->grav['page'] = $page;
     }
 }
Beispiel #6
0
 /**
  * Get a collection of pages in the current context.
  *
  * @param string|array $params
  * @return Collection
  * @throws \InvalidArgumentException
  */
 public function collection($params = 'content')
 {
     if (is_string($params)) {
         $params = (array) $this->value('header.' . $params);
     } elseif (!is_array($params)) {
         throw new \InvalidArgumentException('Argument should be either header variable name or array of parameters');
     }
     if (!isset($params['items'])) {
         return array();
     }
     $collection = $this->evaluate($params['items']);
     if (!$collection instanceof Collection) {
         $collection = new Collection();
     }
     $collection->setParams($params);
     // TODO: MOVE THIS INTO SOMEWHERE ELSE?
     /** @var Uri $uri */
     $uri = self::$grav['uri'];
     /** @var Config $config */
     $config = self::$grav['config'];
     foreach ((array) $config->get('site.taxonomies') as $taxonomy) {
         if ($uri->param($taxonomy)) {
             $items = explode(',', $uri->param($taxonomy));
             $collection->setParams(['taxonomies' => [$taxonomy => $items]]);
             foreach ($collection as $page) {
                 if ($page->modular()) {
                     continue;
                 }
                 foreach ($items as $item) {
                     if (empty($page->taxonomy[$taxonomy]) || !in_array($item, $page->taxonomy[$taxonomy])) {
                         $collection->remove();
                     }
                 }
             }
         }
     }
     // TODO: END OF MOVE
     if (isset($params['order'])) {
         $by = isset($params['order']['by']) ? $params['order']['by'] : 'default';
         $dir = isset($params['order']['dir']) ? $params['order']['dir'] : 'asc';
         $custom = isset($params['order']['custom']) ? $params['order']['custom'] : null;
         $collection->order($by, $dir, $custom);
     }
     /** @var Grav $grav */
     $grav = self::$grav['grav'];
     // New Custom event to handle things like pagination.
     $grav->fireEvent('onCollectionProcessed', new Event(['collection' => $collection]));
     $params = $collection->params();
     $limit = isset($params['limit']) ? $params['limit'] : 0;
     $start = !empty($params['pagination']) ? ($uri->currentPage() - 1) * $limit : 0;
     if ($limit && $collection->count() > $limit) {
         $collection->slice($start, $limit);
     }
     return $collection;
 }