aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/searchtypes/RangeSearch.php
blob: caefae0bc0ee86247bc18e64af8fc564b208a00e (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
<?php
/**
 * @author      Jan-Hendrik Willms <tleilx+studip@gail.com>
 * @license     GPL2 or any later version
 * @category    Stud.IP
 */
class RangeSearch extends SearchType
{
    /**
     * returns the title/description of the searchfield
     *
     * @return string title/description
     */
    public function getTitle()
    {
        return _('Person, Veranstaltung oder Einrichtung suchen');
    }

    /**
     * returns a sql-string appropriate for the searchtype of the current class
     *
     * @return string
     */
    private function getSQL()
    {
        $this->extendedLayout = true;

        $queries = [];
        $queries[] = "SELECT user_id AS id,
                             TRIM(CONCAT(Nachname, ', ', Vorname, ' (', username, ')')) AS name,
                             'user' AS type
                      FROM auth_user_md5
                      LEFT JOIN user_info USING (user_id)
                      WHERE (
                          CONCAT(Nachname, ', ', Vorname, ' ', Nachname) LIKE REPLACE(:input, ' ', '% ')
                          OR username LIKE :input
                        )
                        AND " . get_vis_query();
        $queries[] = "SELECT Seminar_id AS id,
                             TRIM(CONCAT(VeranstaltungsNummer, ' ', Name)) AS name,
                             'course' AS type
                      FROM seminare
                      WHERE CONCAT(VeranstaltungsNummer, ' ', Name, ' ', Untertitel) LIKE REPLACE(:input, ' ', '% ')";
        $queries[] = "SELECT Institut_id AS id,
                             Name AS name,
                             'institute' AS type
                      FROM Institute
                      WHERE Name LIKE REPLACE(:input, ' ', '% ')";
        $queries = implode(" UNION ALL ", $queries);

        return "SELECT *
                FROM ({$queries}) AS tmp
                ORDER BY name ASC";
    }

    /**
     * {@inheritdoc}
     */
    public function getResults($input, $contextual_data = [], $limit = PHP_INT_MAX, $offset = 0)
    {
        $query = $this->getSQL();

        if ($offset || $limit != PHP_INT_MAX) {
            $query .= sprintf(' LIMIT %u, %u', $offset, $limit);
        }

        return DBManager::get()->fetchAll($query, [':input' => "%{$input}%"], function ($row) {
            $range = RangeFactory::createRange($row['type'], null);
            return [
                $row['id'],
                $range->describeRange() . ': ' . $row['name'],
            ];
        });

    }

    /**
     * A very simple overwrite of the same method from SearchType class.
     * returns the absolute path to this class for autoincluding this class.
     *
     * @return: path to this class
     */
    public function includePath()
    {
        return studip_relative_path(__FILE__);
    }
}