aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/searchtypes/SQLSearch.php
blob: de4efa3aa45e02f12fd2fe229e08f86955bd1df8 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
# Lifter010: TODO
/**
 * SQLSearch.php - Class of type SearchType used for searches with QuickSearch
 *
 * Long description for file (if any)...
 *
 * 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
 */

/**
 * Class of type SearchType used for searches with QuickSearch
 * (lib/classes/QuickSearch.php). You can search with a sql-syntax in the
 * database. You just need to give in a query like for a PDB-prepare statement
 * and at least the variable ":input" in the query (the :input will be replaced
 * with the input of the QuickSearch userinput.
 *  [code]
 *  $search = new SQLSearch("SELECT username, Nachname "
 *      "FROM auth_user_md5 " .
 *      "WHERE Nachname LIKE :input ", _("Nachname suchen"), "username");
 *  [/code]
 *
 * @author Rasmus Fuhse
 *
 */

class SQLSearch extends SearchType
{

    protected $sql;
    protected $avatarLike;
    protected $title;

    /**
     *
     * @param string $query SQL with at least ":input" as parameter
     * @param string $title
     * @param string $avatarLike
     * in this search. array("input_name" => "placeholder_in_sql_query")
     *
     * @return void
     */
    public function __construct($query, $title = "", $avatarLike = "")
    {
        $this->sql = $query;
        $this->title = $title;
        $this->avatarLike = $avatarLike;
    }

    /**
     * returns an object of type SQLSearch with parameters to constructor
     *
     * @param string $query SQL with at least ":input" as parameter
     * @param string $title
     * @param string $avatarLike
     * in this search. array("input_name" => "placeholder_in_sql_query")
     *
     * @return SQLSearch
     */
    static public function get()
    {
        $class = get_called_class();
        $ref = new ReflectionClass($class);
        return $ref->newInstanceArgs(func_get_args());
    }

    /**
     * returns the title/description of the searchfield
     *
     * @return string title/description
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * returns an adress of the avatar of the searched item (if avatar enabled)
     *
     * @param string $id id of the item which can be username, user_id, Seminar_id or Institut_id
     *
     * @return string adress of an image
     */
    public function getAvatar($id)
    {
        switch ($this->avatarLike) {
            case "username":
                return Avatar::getAvatar(get_userid($id), $id)->getURL(Avatar::MEDIUM);
            case "user_id":
                return Avatar::getAvatar($id)->getURL(Avatar::MEDIUM);
            case "Seminar_id":
            case "Arbeitsgruppe_id":
                return CourseAvatar::getAvatar($id)->getURL(Avatar::SMALL);
            case "Institut_id":
                return InstituteAvatar::getAvatar($id)->getURL(Avatar::SMALL);
            default:
                return '';
        }
    }

    /**
     * returns an html tag of the image of the searched item (if avatar enabled)
     *
     * @param string $id id of the item which can be username, user_id, Seminar_id or Institut_id
     * @param string $size enum(NORMAL, SMALL, MEDIUM): size of the avatar
     * @param array $options
     *
     * @return string like "<img src="...avatar.jpg" ... >"
     */
    public function getAvatarImageTag($id, $size = Avatar::SMALL, $options = [])
    {
        switch ($this->avatarLike) {
            case "username":
                return Avatar::getAvatar(get_userid($id), $id)->getImageTag($size, $options);
            case "user_id":
                return Avatar::getAvatar($id)->getImageTag($size, $options);
            case "Seminar_id":
            case "Arbeitsgruppe_id":
                return CourseAvatar::getAvatar($id)->getImageTag($size, $options);
            case "Institut_id":
                return InstituteAvatar::getAvatar($id)->getImageTag($size, $options);
            default:
                return '';
        }
    }

    /**
     * returns the results of a search
     * Use the contextual_data variable to send more variables than just the input
     * to the SQL. QuickSearch for example sends all other variables of the same
     * <form>-tag here.
     *
     * @param string $input the search-word(s)
     * @param array $contextual_data an associative array with more variables
     * @param int $limit maximum number of results (default: all)
     * @param int $offset return results starting from this row (default: 0)
     *
     * @return array  array(array(), ...)
     */
    public function getResults($input, $contextual_data = [], $limit = PHP_INT_MAX, $offset = 0)
    {
        $db = DBManager::get();
        $sql = $this->sql;
        if ($offset || $limit != PHP_INT_MAX) {
            $sql .= sprintf(' LIMIT %d, %d', $offset, $limit);
        }
        $statement = $db->prepare($sql, [PDO::FETCH_NUM]);
        $data = [];
        if (is_array($contextual_data)) {
            foreach ($contextual_data as $name => $value) {
                if ($name !== "input" && mb_strpos($sql, ":".$name) !== false) {
                    $data[":".$name] = $value;
                }
            }
        }
        $data[":input"] = "%".$input."%";
        $statement->execute($data);
        $results = $statement->fetchAll();
        return $results;
    }

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