blob: 6a1b33e50e79386ecdb02f8d3a080cf2da5dee99 (
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
<?php
namespace FilesSearch;
/**
* Simple class to hold everything about a files search query.
*
* @license GPL2 or any later version
*
* @since Stud.IP 4.1
*/
class Query
{
// minimum length of a search term; this is a constraint of MYSQL too
const MIN_LENGTH = 4;
// symbol for sorting by relevance
const SORT_RELEVANCE = 'relevance';
// symbol for sorting by chdate
const SORT_CHDATE = 'chdate';
private $error;
private $filter;
private $page;
private $query;
private $resultsPerPage;
private $sort;
private $user;
/**
* Creates an empty search query.
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
public function __construct(\User $user = null)
{
$this->user = isset($user) ? $user : $GLOBALS['user'];
$this->error = false;
$this->resultsPerPage = \Config::get()->ENTRIES_PER_PAGE;
$this->sort = self::SORT_RELEVANCE;
}
/**
* Returns a possible error.
*
* @return string if there is an error, returns a string,
* otherwise null
*/
public function getError()
{
return $this->error;
}
/**
* Returns the associated filter.
*
* @return Filter the associated filter
*/
public function getFilter()
{
return $this->filter;
}
/**
* Returns the user performing the search.
*
* @return User the associated user
*/
public function getUser()
{
return $this->user;
}
/**
* Returns the offset of the first result using the current page
* and the setting of results per page.
*
* @return int the offset of the first result
*/
public function getOffset()
{
return $this->getResultsPerPage() * $this->getPage();
}
/**
* Returns the current page.
*
* @return int the current page
*/
public function getPage()
{
return $this->page;
}
/**
* Returns the search query term.
*
* @return string the search query term
*/
public function getQuery()
{
return $this->query;
}
/**
* Returns the current number of results per page.
*
* @return int the current number of results per page
*/
public function getResultsPerPage()
{
return $this->resultsPerPage;
}
/**
* Returns the sorting type.
*
* @return string the sorting type
*/
public function getSort()
{
return $this->sort;
}
/**
* Has the query an error?
*
* @return bool either true, if there is an error; false otherwise
*/
public function hasError()
{
return $this->error !== false;
}
/**
* Set the filter of this search query.
*
* @param Filter $filter the filter to associate
*
* @return Query returns `$this` for chaining
*/
public function setFilter(Filter $filter)
{
if (!$filter->validate()) {
$this->error = _('Ungültige Filter.');
} else {
$this->filter = $filter;
}
return $this;
}
/**
* Set the current page.
*
* @param int $page the current page
*
* @return Query returns `$this` for chaining
*/
public function setPage($page)
{
if ($page < 0) {
$this->error = _('Die Seitennummer muss positiv sein.');
} else {
$this->page = (int) $page;
}
return $this;
}
/**
* Set the search query term.
*
* @param string $query the search query term
*
* @return Query returns `$this` for chaining
*/
public function setQuery($query)
{
if ($query) {
if (strlen($query) < self::MIN_LENGTH) {
$this->error = _('Der eingegebene Suchbegriff ist zu kurz');
}
$this->query = $query;
}
return $this;
}
/**
* Set the number of results per page.
*
* @param int $resultsPerPage the number of results per page
*
* @return Query returns `$this` for chaining
*/
public function setResultsPerPage($resultsPerPage)
{
if ($resultsPerPage < 1) {
$this->error = _('Es kann nicht weniger als 1 Ergebnis pro Seite angezeigt werden.');
} else {
$this->resultsPerPage = (int) $resultsPerPage;
}
return $this;
}
/**
* Set the sort order.
*
* @param string $sort either one of the two symbols
* Query::SORT_CHDATE or Query:SORT_RELEVANCE
*
* @return Query returns `$this` for chaining
*/
public function setSort($sort)
{
if (!in_array($sort, [self::SORT_CHDATE, self::SORT_RELEVANCE])) {
$this->error = _('Ungültige Sortierung.');
} else {
$this->sort = $sort;
}
return $this;
}
}
|