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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
<?php
/**
* ForumCat.php - Class to handle categories for areas
*
* 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 3 of
* the License, or (at your option) any later version.
*
* @author Till Glöggler <tgloeggl@uos.de>
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL version 3
* @category Stud.IP
*
* @property string $id alias column for category_id
* @property string $category_id database column
* @property string $seminar_id database column
* @property string $entry_name database column
* @property int $pos database column
*/
class ForumCat extends SimpleORMap
{
/**
* Configures this model.
*
* @param array $config Configuration array
*/
protected static function configure($config = [])
{
$config['db_table'] = 'forum_categories';
parent::configure($config);
}
/**
* Return a list of all areas with their categories.
* Empty categories are excluded by default
*
* @param string $seminar_id the seminar_id the retrieve the categories for
* @param string $exclude_null if false, empty categories are returned as well
* @return array list of categories
*/
public static function getListWithAreas($seminar_id, $exclude_null = true)
{
$stmt = DBManager::get()->prepare("SELECT * FROM forum_categories AS fc
LEFT JOIN forum_categories_entries AS fce USING (category_id)
WHERE seminar_id = ? "
. ($exclude_null ? 'AND fce.topic_id IS NOT NULL ' : '')
. "ORDER BY fc.pos ASC, fce.pos ASC");
$stmt->execute([$seminar_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
/**
* Returns the name of the associated category for an area denoted by the
* passed topic_id
*
* @param string $topic_id
* @return string the name of the category
*/
public static function getCategoryNameForArea($topic_id)
{
$stmt = DBManager::get()->prepare("SELECT fc.entry_name FROM forum_categories AS fc
LEFT JOIN forum_categories_entries AS fce USING (category_id)
WHERE fce.topic_id = ?");
$stmt->execute([$topic_id]);
return $stmt->fetchColumn();
}
/**
* Adds a new category with the passed name to the passed seminar and
* returns the id of the newly created category
*
* @param string $seminar_id
* @param string $name the name of the new category
* @return string the id of the newly created category
*/
public static function add($seminar_id, $name)
{
$stmt = DBManager::get()->prepare("INSERT INTO forum_categories
(category_id, seminar_id, entry_name)
VALUES (?, ?, ?)");
$category_id = md5(uniqid(rand()));
$stmt->execute([$category_id, $seminar_id, $name]);
return $category_id;
}
/**
* Remove the category with the passed id. The seminar_id is used only
* to be certain.
*
* @param string $category_id The ID of the category to be deleted
* @param string $seminar_id Seminar-ID the category belongs to
*/
public static function remove($category_id, $seminar_id)
{
// delete the category itself
$stmt = DBManager::get()->prepare("DELETE FROM
forum_categories
WHERE category_id = ?");
$stmt->execute([$category_id]);
// set all entries to default category
$stmt = DBManager::get()->prepare("UPDATE
forum_categories_entries
SET category_id = ?, pos = 999
WHERE category_id = ?");
$stmt->execute([$seminar_id, $category_id]);
}
/**
* Set the position for the passed category to the passed value
*
* @param string $category_id the ID of the category to update
* @param int $pos the new position
*/
public static function setPosition($category_id, $pos)
{
$stmt = DBManager::get()->prepare("UPDATE
forum_categories
SET pos = ? WHERE category_id = ?");
$stmt->execute([$pos, $category_id]);
}
/**
* Add the passed area to the passed category and remove it from all
* other categories.
*
* @param string $category_id the ID of the category
* @param string $area_id the ID of the area to add the category to
*/
public static function addArea($category_id, $area_id)
{
// remove area from all other categories
$stmt = DBManager::get()->prepare("DELETE FROM
forum_categories_entries
WHERE topic_id = ?");
$stmt->execute([$area_id]);
// add area to this category, make sure it is at the end
$stmt = DBManager::get()->prepare("SELECT COUNT(*) FROM
forum_categories_entries
WHERE category_id = ?");
$stmt->execute([$category_id]);
$new_pos = $stmt->fetchColumn() + 1;
$stmt = DBManager::get()->prepare("REPLACE INTO
forum_categories_entries
(category_id, topic_id, pos) VALUES (?, ?, ?)");
$stmt->execute([$category_id, $area_id, $new_pos]);
}
/**
* Remove the passed area from all categories.
*
* @param string $area_id the ID of the area to be removed
*/
public static function removeArea($area_id)
{
$stmt = DBManager::get()->prepare("DELETE FROM
forum_categories_entries
WHERE topic_id = ?");
$stmt->execute([$area_id]);
}
/**
* Set the position for the passed category to the passed value
*
* @param string $area_id the ID of the area to update
* @param int $pos the new position
*/
public static function setAreaPosition($area_id, $pos)
{
$stmt = DBManager::get()->prepare("UPDATE
forum_categories_entries
SET pos = ? WHERE topic_id = ?");
$stmt->execute([$pos, $area_id]);
}
/**
* Set the name for the passed category
*
* @param string $category_id the ID of the category to update
* @param string $name the name to set
*/
public static function setName($category_id, $name)
{
$stmt = DBManager::get()->prepare("UPDATE
forum_categories
SET entry_name = ? WHERE category_id = ?");
$stmt->execute([$name, $category_id]);
}
/**
* Return the data for the passed category_id
*
* @param string $category_id
*
* @return array the data for the passed category_id
*/
public static function get($category_id)
{
$stmt = DBManager::get()->prepare("SELECT * FROM forum_categories
WHERE category_id = ?");
$stmt->execute([$category_id]);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
/**
* Return the areas for the passed category_id
*
* @param string $category_id
* @param int $start limit start (optional)
* @param int $num number of entries to fetch (optional, default is 20)
*
* @return array the data for the passed category_id
*/
public static function getAreas($category_id, $start = null, $num = 20)
{
$category = self::get($category_id);
$limit = '';
if ($start !== null && $num) {
$limit = " LIMIT $start, $num";
}
if ($category_id == $category['seminar_id']) {
$stmt = DBManager::get()->prepare("SELECT fe.* FROM forum_entries AS fe
LEFT JOIN forum_categories_entries AS fce USING (topic_id)
WHERE seminar_id = ? AND depth = 1 AND (
fce.category_id = ? OR fce.category_id IS NULL
) ORDER BY category_id DESC, pos ASC" . $limit);
$stmt->execute([$category_id, $category_id]);
} else {
$stmt = DBManager::get()->prepare("SELECT forum_entries.* FROM forum_categories_entries
LEFT JOIN forum_entries USING(topic_id)
WHERE category_id = ?
ORDER BY pos ASC" . $limit);
$stmt->execute([$category_id]);
}
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
|