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
|
<?php
namespace FilesSearch;
/**
* Simple class to hold everything about files search's filtering.
*
* @license GPL2 or any later version
*
* @since Stud.IP 4.1
*/
class Filter
{
protected $category;
protected $semester;
/**
* Returns all filter categories.
*
* @return array an associative array containing `id` => `label` pairs
*/
public static function getCategories()
{
return [
'course' => _('Veranstaltungen'),
'institute' => _('Einrichtungen'),
'message' => _('Nachrichten'),
'user' => _('Person'),
];
}
/**
* Is this filter really filtering?
*
* @return bool true if filtering, false otherwise
*/
public function isFiltering()
{
return $this->hasCategory() || $this->hasSemester();
}
/**
* Return the active filter category.
*
* @return string the active filter category
*/
public function getCategory()
{
return $this->category;
}
/**
* Set the active filter category.
*
* @param string $category the active filter category
*
* @return Filter return `$this` for chaining
*/
public function setCategory($category)
{
$this->category = $category;
return $this;
}
/**
* Is a an active filter category set?
*
* @return bool true if there is an active filter category;
* false otherwise
*/
public function hasCategory()
{
return $this->getCategory() != '';
}
/**
* Return the active filter semester.
*
* @return Semester the active filter semester
*/
public function getSemester()
{
return $this->semester;
}
/**
* Set the active filter semester.
*
* @param Semester $semester the active filter semester
*
* @return Filter return `$this` for chaining
*/
public function setSemester(?\Semester $semester = null)
{
$this->semester = $semester;
return $this;
}
/**
* Is a an active filter semester set?
*
* @return bool true if there is an active filter semester;
* false otherwise
*/
public function hasSemester()
{
return $this->getSemester() !== null;
}
/**
* Validate the filter.
*
* @return bool true if this filter is valid, false otherwise
*/
public function validate()
{
if (isset($this->category)
&& !in_array($this->category, ['', 'course', 'institute', 'user', 'message'])) {
return false;
}
return true;
}
/**
* Returns a representation of this filter as an array.
*
* @return array the representation of this filter
*/
public function toArray()
{
return [
'category' => $this->category ?: '',
'semester' => $this->semester ? $this->semester->id : '',
];
}
}
|