File size: 2,479 Bytes
b16040e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

declare(strict_types=1);

class SearchController
{
    /**
     * Full-text search with optional collection filter and pagination.
     */
    public function index(): void
    {
        $config = require __DIR__ . '/../config.php';

        $query      = Security::sanitizeSearch($_GET['q'] ?? '');
        $collection = isset($_GET['collection']) && $_GET['collection'] !== ''
            ? Security::sanitize($_GET['collection'])
            : null;
        $topicFilter = isset($_GET['topic']) && $_GET['topic'] !== ''
            ? Security::sanitize($_GET['topic'])
            : null;
        $stampFilter = isset($_GET['stamp']) && $_GET['stamp'] !== ''
            ? Security::sanitize($_GET['stamp'])
            : null;

        $perPage = $config['app']['per_page'];
        $limit   = min($perPage, $config['app']['search_limit']);
        $page    = max(1, (int) ($_GET['page'] ?? 1));
        $offset  = ($page - 1) * $limit;

        $results     = [];
        $total       = 0;
        $totalPages  = 0;

        if ($query !== '') {
            $searchModel = new Search();
            $results     = $searchModel->fullText($query, $collection, $limit, $offset, $topicFilter, $stampFilter);
            $total       = $searchModel->countResults($query, $collection, $topicFilter, $stampFilter);
            $totalPages  = (int) ceil($total / $limit);
        }

        // Gather collection list for the filter dropdown
        $docModel    = new Document();
        $collections = $docModel->getCollections();

        // Available topics and stamps for filter dropdowns
        $db = Database::getInstance();
        $availableTopics = [];
        $availableStamps = [];
        try {
            $availableTopics = [
                'intelligence operations', 'national security', 'military operations',
                'surveillance', 'assassination', 'congressional legislation',
                'government oversight', 'civil rights', 'foreign policy',
                'law enforcement', 'financial regulation', 'public health',
                'human experimentation', 'scientific research', 'judicial proceedings',
            ];
            $availableStamps = [
                'TOP SECRET', 'SECRET', 'CONFIDENTIAL', 'CLASSIFIED', 'UNCLASSIFIED',
                'NOFORN', 'EYES ONLY', 'REDACTED', 'DECLASSIFIED', 'SEALED',
            ];
        } catch (\PDOException $e) {}

        require __DIR__ . '/../views/search-results.php';
    }
}