kolber / stacey

Cheap & easy content management
http://staceyapp.com
MIT License
1.04k stars 131 forks source link

Limiting search scope? #123

Open eroonkang opened 10 years ago

eroonkang commented 10 years ago

Using the most recent Stacey version 3.0.0:

I'm curious whether it's possible to limit the search scope to a specific content folder. Say a user have "1.about" and "2.works" as content directories, it'd be great to be able to exclude "1.about" from the search and only get matching results from "Works".

My hunch is that it could be done by making some edits on twig-extensions.inc.php, but it's beyond my capacity. Any thoughts?

M4rtine commented 10 years ago

Hello I think you can do that :

{% block context %}
{% set page = get('/works') %}
    {% for child in page.children|reverse %}
        {% include 'partials/list-projects.html' %}
    {% endfor %}
{% endblock %}

and you don't need to name the folder "2.works". I think "works" is fine.

eroonkang commented 10 years ago

Thanks! I guess I should've had explained a bit clearer -- by search, I mean the search function with a user text input.

I did somehow solve the problem by adding an if statement to the function itself, but it feels like a hack to me.

    $results = array();
    foreach ($json as $page) {
      foreach ($page as $key => $value) {
        if (preg_match('/\/404\//', $page['url'])) continue;
        if ($key == 'file_path' || $key == 'url') continue;
        $clean_value = (is_string($value)) ? strip_tags($value) : '';
        if (preg_match('/.{0,30}'.$search.'.{0,30}/i', $clean_value, $matches)) {
          if (isset($matches[0])) {
            $page['search_match'] = ''.preg_replace('/('.$search.')/ui', '<mark>$1</mark>', $matches[0]).'...';

            #to exclude /about/
            if (strpos($page[url],'/about/') !== false) {
              #$page is /about/
            }
            else {
              #$page is not /about/
              $results[] = $page;
              if ($limit && count($results) >= $limit) {
                return $results;
              }
            }

            break;
          }
        }
      }
    }
kolber commented 10 years ago

You can add a variable hide_from_search: true to any pages you want to exclude from the search.

You can see the code implementation of how that works in 'app/cache.inc.php' here: https://github.com/kolber/stacey/blob/master/app/cache.inc.php#L78

M4rtine commented 10 years ago

@eroonkang Sorry ;)

eroonkang commented 10 years ago

@kolber That is exactly what I was looking for. Thanks a ton, and thank you for the great CMS. @M4rtine No worries, thanks for help :)