From: Christian Weiske Date: Wed, 3 Feb 2016 16:23:06 +0000 (+0100) Subject: filtering works X-Git-Tag: v0.1.0~27 X-Git-Url: https://git.cweiske.de/phinde.git/commitdiff_plain/e5c7780adb59e8038a040a1e534a4cee1fac8cd8 filtering works --- diff --git a/data/templates/search/hit.htm b/data/templates/search/hit.htm index 3d8f633..4981017 100644 --- a/data/templates/search/hit.htm +++ b/data/templates/search/hit.htm @@ -1,11 +1,23 @@ {% set doc = hit._source %}
  • - {{doc.title}} - {% if doc.author.name %} - by {{doc.author.name}} - {% endif %} -
    {{doc.extra.cleanUrl}} - {% if doc.modate %} -
    Changed: {{doc.extra.day}} - {% endif %} + + {{doc.title}} + {% if doc.author.name %} + by + + {% if doc.author.url %} + {{doc.author.name}} + {% else %} + {{doc.author.name}} + {% endif %} + + {% endif %} + +
    {{ellipsis(doc.extra.cleanUrl, 60)}} + +
    + {% if doc.modate %} + {{doc.extra.day|date("Y-m-d")}} + {% endif %} +
  • diff --git a/data/templates/search/sidebar-filter.htm b/data/templates/search/sidebar-filter.htm new file mode 100644 index 0000000..6c4bc81 --- /dev/null +++ b/data/templates/search/sidebar-filter.htm @@ -0,0 +1,21 @@ +{% if attribute(activeFilters, type) %} + {{title}} + +{% elseif attribute(aggregations, type).buckets|length > 1 %} +{{title}} + +{% endif %} diff --git a/data/templates/search/sidebar.htm b/data/templates/search/sidebar.htm index 619301e..eaddaeb 100644 --- a/data/templates/search/sidebar.htm +++ b/data/templates/search/sidebar.htm @@ -1,48 +1,7 @@

    Filter results by:

    -{% if aggregations.tags.buckets|length > 1 %} -Tag - -{% endif %} - -{% if aggregations.domain.buckets|length > 1 %} - -Domain - -{% endif %} - -{% if aggregations.language.buckets|length > 1 %} -Language - -{% endif %} - -{% if aggregations.type.buckets|length > 1 %} -Type - -{% endif %} - +{% include 'search/sidebar-filter.htm' with {'type': 'tags', 'title': 'Tag'} %} +{% include 'search/sidebar-filter.htm' with {'type': 'domain', 'title': 'Domain'} %} +{% include 'search/sidebar-filter.htm' with {'type': 'language', 'title': 'Language'} %} +{% include 'search/sidebar-filter.htm' with {'type': 'type', 'title': 'Type'} %} diff --git a/www/css/phinde.css b/www/css/phinde.css index 1e34fcf..a3d593e 100644 --- a/www/css/phinde.css +++ b/www/css/phinde.css @@ -12,4 +12,16 @@ } .hit { margin-bottom: 2ex; +} +.hit .title { + font-size: 120%; +} +.hit .url { + color: #070; +} +.hit .extract { + font-size: 95%; +} +.hit .date { + color: #666; } \ No newline at end of file diff --git a/www/index.php b/www/index.php index 8cdca41..257ad87 100644 --- a/www/index.php +++ b/www/index.php @@ -18,6 +18,8 @@ if (isset($_GET['page'])) { } $perPage = 10;//$GLOBALS['phinde']['perPage']; +$baseLink = '?q=' . urlencode($query); + $filters = array(); if (isset($_GET['filter'])) { $allowedFilter = array('domain', 'language', 'tags', 'term'); @@ -27,13 +29,37 @@ if (isset($_GET['filter'])) { } } } +$activeFilters = array(); +foreach ($filters as $type => $value) { + $activeFilters[$type] = array( + 'label' => $value, + 'removeUrl' => buildLink($baseLink, $filters, $type, null), + ); +} + +function buildLink($baseLink, $filters, $addFilterType, $addFilterValue) +{ + if ($addFilterValue === null) { + if (array_key_exists($addFilterType, $filters)) { + unset($filters[$addFilterType]); + } + } else { + $filters[$addFilterType] = $addFilterValue; + } + + $params = http_build_query(array('filter' => $filters)); + if (strlen($params)) { + return $baseLink . '&' . $params; + } + return $baseLink; +} $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']); $res = $es->search($query, $filters, $page, $perPage); $pager = new Html_Pager( $res->hits->total, $perPage, $page + 1, - '?q=' . $query + $baseLink ); foreach ($res->hits->hits as &$hit) { @@ -48,11 +74,9 @@ foreach ($res->hits->hits as &$hit) { } } -$baseLink = '?q=' . urlencode($query); foreach ($res->aggregations as $key => &$aggregation) { foreach ($aggregation->buckets as &$bucket) { - $bucket->url = $baseLink - . '&filter[' . urlencode($key) . ']=' . urlencode($bucket->key); + $bucket->url = buildLink($baseLink, $filters, $key, $bucket->key); } } //var_dump($res->aggregations); @@ -64,6 +88,7 @@ render( 'hitcount' => $res->hits->total, 'hits' => $res->hits->hits, 'aggregations' => $res->aggregations, + 'activeFilters' => $activeFilters, 'pager' => $pager ) ); diff --git a/www/www-header.php b/www/www-header.php index 60adacf..2ec8c2f 100644 --- a/www/www-header.php +++ b/www/www-header.php @@ -12,6 +12,14 @@ $GLOBALS['twig'] = new \Twig_Environment( ); $GLOBALS['twig']->addExtension(new \Twig_Extension_Debug()); +$twig->addFunction('ellipsis', new \Twig_Function_Function('\phinde\ellipsis')); +function ellipsis($text, $maxlength) +{ + if (strlen($text) > $maxlength) { + $text = substr($text, 0, $maxlength - 1) . '…'; + } + return $text; +} function render($tplname, $vars = array(), $return = false) {