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
180
181
182
183
184
185
186
187
188
189
190
191
192
|
<?php
/**
* RoomSearch.php - A search type for rooms.
*
* 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 Moritz Strohm <strohm@data-quest.de>
* @copyright 2018
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
class RoomSearch extends ResourceSearch
{
//Setup and search configuration methods:
public $with_seats = 0;
//SearchType interface implementations:
public function getTitle()
{
return _('Raumsuche');
}
/**
* @param string $keyword The name of the room or a part of it.
* @param array $contextual_data This parameter is not used at the moment.
*/
public function getResults(
$keyword,
$contextual_data = [],
$limit = PHP_INT_MAX,
$offset = 0
)
{
$user = User::findCurrent();
if (!$user) {
return [];
}
if ($this->with_seats) {
$prop_id_seats = ResourcePropertyDefinition::findOneByName('seats')->id;
$sql = "INNER JOIN resource_categories rc
ON resources.category_id = rc.id
INNER JOIN resource_properties rp ON rp.resource_id = resources.id
WHERE
rc.class_name IN ( :room_class_names )
AND
rp.property_id = :prop_id_seats
AND
rp.state > :seats
AND
resources.name LIKE CONCAT('%', :keyword, '%') ";
$sql_params = [
'keyword' => $keyword,
'seats' => $this->with_seats,
'prop_id_seats' => $prop_id_seats
];
} else {
$sql = "INNER JOIN resource_categories rc
ON resources.category_id = rc.id
WHERE
rc.class_name IN ( :room_class_names )
AND
resources.name LIKE CONCAT('%', :keyword, '%') ";
$sql_params = [
'keyword' => $keyword
];
}
$sql_params['room_class_names'] = RoomManager::getAllRoomClassNames();
//Root users can access everything so that we don't need
//to check for permissions in case the current user is root.
if (!$GLOBALS['perm']->have_perm('root')) {
$global_permission = null;
if ($this->use_global_permissions) {
//Get the global permission level:
$global_permission = ResourceManager::getGlobalResourcePermission(
$user
);
}
if (in_array($global_permission, $this->accepted_permission_levels)) {
//Retrieve all resources where the user has sufficient permissions.
//There may be resources where the user has explicitly lower
//permissions and we must therefore exclude these resources.
$lower_permission_levels = ResourceManager::getLowerPermissionLevels(
$global_permission
);
$sql .= "AND resources.id NOT IN (
SELECT resource_id
FROM
resource_permissions
WHERE
user_id = :user_id
AND
perms IN ( :lower_perms )
UNION
SELECT resource_id
FROM
resource_temporary_permissions
WHERE
user_id = :user_id
AND
perms IN ( :lower_perms )
AND
begin <= :now
AND
end >= :now
)";
$sql_params = array_merge(
$sql_params,
[
'user_id' => $user->id,
'lower_perms' => $lower_permission_levels,
'now' => time()
]
);
} else {
//Only retrieve those resources where the user has direct
//sufficient permissions.
$sql .= "AND resources.id IN (
SELECT resource_id
FROM
resource_permissions
WHERE
user_id = :user_id
AND
perms IN ( :perms )
UNION
SELECT resource_id
FROM
resource_temporary_permissions
WHERE
user_id = :user_id
AND
perms IN ( :perms )
AND
begin <= :now
AND
end >= :now
)";
$sql_params = array_merge(
$sql_params,
[
'user_id' => $user->id,
'perms' => $this->accepted_permission_levels,
'now' => time()
]
);
}
}
$sql .= " ORDER BY resources.name ASC
LIMIT :limit OFFSET :offset ";
$sql_params['limit'] = $limit;
$sql_params['offset'] = $offset;
$rooms = Room::findBySql(
$sql,
$sql_params
);
$results = [];
foreach ($rooms as $room) {
$additional_text = '';
if (count($this->additional_display_properties)) {
$additional_values = [];
foreach ($this->additional_display_properties as $ap) {
$additional_values[] = $room->getProperty($ap);
}
$additional_text = implode(', ', $additional_values);
}
$name = $room->name;
if($additional_text) {
$name .= sprintf(
$this->additional_property_format,
$additional_text
);
}
$results[] = [
$room->id,
$name
];
}
return $results;
}
}
|