<?php

// Do we have a search query?
if (!isset ($_GET['q']))
    exit ();

// Truncate
$query = substr (trim ($_GET['q']), 0, 2048);

// Empty search?
if (strlen ($query) == 0)
    exit ();

// Search SQLite
$nodes_db = new PDO ('sqlite:nodes.sqlite');

$stmt = $nodes_db->prepare (
    'SELECT * ' .
    'FROM `node`' .
    'WHERE ' .
        'LOWER(`name`) LIKE LOWER(:query) OR ' .
        'LOWER(`disambiguatingDescription`) LIKE LOWER(:query)' .
    'LIMIT 20');

$stmt->execute ([ ':query' => '%' . $query . '%' ]);

$result = $stmt->fetchAll (PDO::FETCH_ASSOC);

// Return results as formatted HTML
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        
        <link href="css/templates.css" rel="stylesheet">
        
        <title><?php echo htmlspecialchars ($query); ?> - DOKK Search</title>
    </head>
    
    <body class="fullsearch">
        <header>
            
            <div class="logo">
                <a href="https://dokk.org">
                    DOKK
                </a>
            </div>
            
            <div class="searchbox">
                <form action="search" id="search" class="search">
                    <input
                        autocomplete="off"
                        autofocus="true"
                        maxlength="2048"
                        name="q"
                        placeholder="Search topics"
                        required="true"
                        spellcheck="false"
                        type="text" />
                    
                    <input title="Search" value="" type="submit" />
                    
                    <div class="suggestions"></div>
                </form>
            </div>
            
        </header>
        
        <div class="results">
            
            <?php
                foreach ($result as $a_result)
                {
                    echo    '<div>' .
                                
                                '<a href="' . $a_result['uri'] . '">' .
                                    htmlspecialchars ($a_result['name']) .
                                '</a>' .
                                
                                '<div>' .
                                    htmlspecialchars ($a_result['disambiguatingDescription']) .
                                '</div>' .
                            '</div>';
                }
            ?>
            
        </div>
        
        <footer>
            
        </footer>
        
        <script src="js/dokk.js"></script>
    </body>
</html>




