aboutsummaryrefslogtreecommitdiff
path: root/src/Handlers/Search.php
blob: 30311ea9d747b3c0a9bd07d136903677919f80af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php


namespace Micropoly\Handlers;

use Micropoly\Env;
use Micropoly\Handler;
use Micropoly\Models\Note;
use Micropoly\Search\ParseError;
use Micropoly\Search\Parser;
use Micropoly\Search\SearchResult;
use Micropoly\Search\TrueExpr;

class Search implements Handler
{
    public function handle(Env $env, array $variables)
    {
        $vars = ["query" => $_GET["q"] ?? ""];

        try {
            $expr = isset($_GET["q"])
                ? (Parser::parse($_GET["q"]) ?? new TrueExpr())
                : new TrueExpr();

            $results = SearchResult::search($env->db(), $expr);
            $vars["results"] = $results;
        } catch (ParseError $e) {
            $vars["error"] = $e->getMessage();
        }

        echo $env->twig()->render("/search.twig", $vars);
    }
}