aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/quicksearch.php
blob: c08649871018643650f88b4121d56a4a31436467 (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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
 * quicksearch.php - trails-controller for delivering search-suggestions
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Rasmus <fuhse@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */

/**
 * Controller for the ajax-response of the QuickSearch class found in
 * lib/classes/QuickSearch.php
 */
class QuicksearchController extends AuthenticatedController
{
    /**
     * the one action which is called by the QuickSearch-form when typed in
     * by user.
     *
     * @param string query_id first argument of url -> id of query in session
     */
    public function response_action($query_id)
    {
        try {
            $needle    = Request::get('request');
            $form_data = iterator_to_array(Request::getInstance());

            $this->search = QuickSearch::getFromSession($query_id);
            $results      = $this->getResults($needle, $form_data);

            $this->render_json($results);
        } catch (Exception $e) {
            $this->set_status(500);
            $this->render_text($e->getMessage());
        }
    }

    /**
     * Highlights the given needle in given subject with the given format.
     *
     * @param String $needle  Search for this string...
     * @param String $subject ...inside this string...
     * @param String $format  ...and replace it with this string (regexp
     *                        syntax)
     *
     * @return String containing the subject with highlighted needle
     */
    private function highlight($needle, $subject, $format = '<b>$0</b>')
    {
        $needle  = htmlReady($needle);
        $subject = htmlReady($subject);
        $regexp  = '/' . preg_quote($needle, '/') . '/i';

        return preg_replace($regexp, $format, $subject);
    }

    /**
     * private method to get a result-array in the way of array(array(item_id, item-name)).
     *
     * @param String $needle   The search keyword typed by the user.
     * @param array $form_data Additonal form data
     *
     * @return array of matched records
     */
    private function getResults($needle, $form_data)
    {
        if (!$this->search instanceof SearchType) {
            throw new Exception(_('Session abgelaufen oder unbekannter Suchtyp'));
        }

        $limit = 10;
        if ($this->search instanceof StandardSearch && $this->search->search === 'username') {
            $limit = 50;
        }

        $results = $this->search->getResults($needle, $form_data, $limit);

        $output = [];
        foreach ($results as $result) {
            $formatted = [
                'item_id'          => $result[0],
                'item_name'        => $this->highlight($needle, $result[1]),
                'item_description' => '',
                'item_search_name' => $result[1],
            ];

            if ($this->search instanceof StandardSearch && $this->search->extendedLayout) {
                $formatted['item_name'] = $this->search->getAvatarImageTag($result[0], Avatar::MEDIUM, ['title' => '']) . $formatted['item_name'];
                if (!empty($result[3])) {
                    $formatted['item_description'] = sprintf('%s (%s)', $result[2], $result[3]);
                } else {
                    $formatted['item_description'] = $result[2] ?? '';
                }
            } else if ($this->search instanceof SearchType) {
                 $formatted['item_name'] = $this->search->getAvatarImageTag($result[0], Avatar::SMALL, ['title' => '']) . $formatted['item_name'];
            }
            $output[] = $formatted;
        }
        return $output;
    }
}