1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package phpBB Extension - RH Topic Tags |
5
|
|
|
* @copyright (c) 2014 Robet Heim |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace robertheim\topictags\acp; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @ignore |
14
|
|
|
*/ |
15
|
|
|
use \phpbb\json_response; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Handles the "manage-tags" page of the ACP. |
19
|
|
|
*/ |
20
|
|
|
class manage_tags_controller |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** @var \phpbb\config\config */ |
24
|
|
|
private $config; |
25
|
|
|
|
26
|
|
|
/** @var \phpbb\request\request */ |
27
|
|
|
private $request; |
28
|
|
|
|
29
|
|
|
/** @var \phpbb\user */ |
30
|
|
|
private $user; |
31
|
|
|
|
32
|
|
|
/** @var \phpbb\template\template */ |
33
|
|
|
private $template; |
34
|
|
|
|
35
|
|
|
/** @var \phpbb\pagination */ |
36
|
|
|
private $pagination; |
37
|
|
|
|
38
|
|
|
/** @var \robertheim\topictags\service\tags_manager */ |
39
|
|
|
private $tags_manager; |
40
|
|
|
|
41
|
|
|
const SORT_ASC = 0; |
42
|
|
|
const SORT_NAME_ASC = 0; |
43
|
|
|
const SORT_NAME_DESC = 1; |
44
|
|
|
const SORT_COUNT_ASC = 2; |
45
|
|
|
const SORT_COUNT_DESC = 3; |
46
|
|
|
|
47
|
|
View Code Duplication |
public function __construct( |
48
|
|
|
\phpbb\config\config $config, |
49
|
|
|
\phpbb\request\request $request, |
50
|
|
|
\phpbb\user $user, |
51
|
|
|
\phpbb\template\template $template, |
52
|
|
|
\phpbb\pagination $pagination, |
53
|
|
|
\robertheim\topictags\service\tags_manager $tags_manager) |
54
|
|
|
{ |
55
|
|
|
$this->config = $config; |
56
|
|
|
$this->request = $request; |
57
|
|
|
$this->user = $user; |
58
|
|
|
$this->template = $template; |
59
|
|
|
$this->pagination = $pagination; |
60
|
|
|
$this->tags_manager = $tags_manager; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $mode phpbb acp-mode |
65
|
|
|
* @param string $u_action phpbb acp-u_action |
66
|
|
|
* @param string $id the modules-id (url-param "i") |
67
|
|
|
*/ |
68
|
|
|
public function manage_tags($mode, $u_action, $id) |
69
|
|
|
{ |
70
|
|
|
$action = $this->request->variable('action', ''); |
71
|
|
|
switch ($action) |
72
|
|
|
{ |
73
|
|
|
case 'delete': |
74
|
|
|
$this->handle_delete($mode, $u_action, $id); |
75
|
|
|
break; |
76
|
|
|
case 'edit': |
77
|
|
|
$this->handle_edit($u_action); |
78
|
|
|
break; |
79
|
|
|
case 'edit_non_ajax': |
80
|
|
|
$this->handle_edit($u_action, false); |
81
|
|
|
break; |
82
|
|
|
default: |
83
|
|
|
// show all tags |
84
|
|
|
$sort_key = $this->request->variable('sort_key', self::SORT_NAME_ASC); |
85
|
|
|
$sort_field = 'tag'; |
86
|
|
|
switch ($sort_key) |
87
|
|
|
{ |
88
|
|
|
case self::SORT_COUNT_ASC: |
89
|
|
|
// no break |
90
|
|
|
case self::SORT_COUNT_DESC: |
91
|
|
|
$sort_field = 'count'; |
92
|
|
|
break; |
93
|
|
|
case self::SORT_NAME_ASC: |
94
|
|
|
// no break |
95
|
|
|
case self::SORT_NAME_DESC: |
96
|
|
|
// no break |
97
|
|
|
default: |
98
|
|
|
$sort_field = 'tag'; |
99
|
|
|
break; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$ordering = (($sort_key % 2) == self::SORT_ASC); |
103
|
|
|
$start = $this->request->variable('start', 0); |
104
|
|
|
$limit = $this->config['topics_per_page']; |
105
|
|
|
$tags_count = $this->tags_manager->count_tags(); |
106
|
|
|
$start = $this->pagination->validate_start($start, $limit, $tags_count); |
107
|
|
|
$base_url = $u_action . "&sort_key=$sort_key"; |
108
|
|
|
|
109
|
|
|
$tags = $this->tags_manager->get_all_tags($start, $limit, $sort_field, $ordering); |
110
|
|
|
|
111
|
|
|
$this->pagination->generate_template_pagination($base_url, 'pagination', 'start', $tags_count, $limit, $start); |
112
|
|
|
foreach ($tags as $tag) |
113
|
|
|
{ |
114
|
|
|
$this->template->assign_block_vars('tags', array( |
115
|
|
|
'NAME' => $tag['tag'], |
116
|
|
|
'ASSIGNMENTS' => $tag['count'], |
117
|
|
|
'U_DELETE_TAG' => $this->get_tag_link($u_action, $tag['id']) . '&action=delete', |
118
|
|
|
'U_EDIT_TAG' => $this->get_tag_link($u_action, $tag['id']) . '&action=edit_non_ajax', |
119
|
|
|
)); |
120
|
|
|
} |
121
|
|
|
$this->template->assign_vars(array( |
122
|
|
|
'SELECT_SORT_KEY' => $this->create_sort_selects($sort_key), |
123
|
|
|
'U_ACTION' => $u_action . "&sort_key=$sort_key&start=$start", |
124
|
|
|
)); |
125
|
|
|
break; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $mode phpbb acp-mode |
131
|
|
|
* @param string $u_action phpbb acp-u_action |
132
|
|
|
* @param string $id |
133
|
|
|
*/ |
134
|
|
|
private function handle_delete($mode, $u_action, $id) |
135
|
|
|
{ |
136
|
|
|
$tag_id = $this->request->variable('tag_id', -1); |
137
|
|
|
if ($tag_id < 1) |
138
|
|
|
{ |
139
|
|
|
if ($this->request->is_ajax()) |
140
|
|
|
{ |
141
|
|
|
trigger_error('TOPICTAGS_MISSING_TAG_ID', E_USER_WARNING); |
142
|
|
|
} |
143
|
|
|
trigger_error($this->user->lang('TOPICTAGS_MISSING_TAG_ID') . adm_back_link($u_action), E_USER_WARNING); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$tag = $this->tags_manager->get_tag_by_id($tag_id); |
147
|
|
|
|
148
|
|
|
if (confirm_box(true)) |
149
|
|
|
{ |
150
|
|
|
$this->tags_manager->delete_tag($tag_id); |
151
|
|
|
|
152
|
|
|
if ($this->request->is_ajax()) |
153
|
|
|
{ |
154
|
|
|
trigger_error('TOPICTAGS_TAG_DELETED'); |
155
|
|
|
} |
156
|
|
|
trigger_error($this->user->lang('TOPICTAGS_TAG_DELETED') . adm_back_link($u_action)); |
157
|
|
|
} |
158
|
|
|
else |
159
|
|
|
{ |
160
|
|
|
$confirm_text = $this->user->lang('TOPICTAGS_TAG_DELETE_CONFIRM', $tag); |
161
|
|
|
confirm_box(false, $confirm_text, build_hidden_fields(array( |
162
|
|
|
'i' => $id, |
163
|
|
|
'mode' => $mode, |
164
|
|
|
'action' => 'delete', |
165
|
|
|
'tag_id' => $tag_id, |
166
|
|
|
))); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param $is_ajax whether the edit is called by ajax or not |
172
|
|
|
* @param string $u_action phpbb acp-u_action |
173
|
|
|
*/ |
174
|
|
|
private function handle_edit($u_action, $is_ajax = true) |
175
|
|
|
{ |
176
|
|
|
if (!$is_ajax) |
177
|
|
|
{ |
178
|
|
|
$new_tag_name = $this->request->variable('new_tag_name', ''); |
179
|
|
|
if (empty($new_tag_name)) |
180
|
|
|
{ |
181
|
|
|
$tag_id = $this->request->variable('tag_id', 0); |
182
|
|
|
$old_tag_name = $this->tags_manager->get_tag_by_id($tag_id); |
183
|
|
|
$this->template->assign_vars(array( |
184
|
|
|
'U_ACTION' => $this->get_tag_link($u_action, $tag_id) . '&action=edit_non_ajax', |
185
|
|
|
'S_EDIT_TAG_NON_AJAX' => true, |
186
|
|
|
'OLD_TAG_NAME' => $old_tag_name, |
187
|
|
|
)); |
188
|
|
|
return; |
189
|
|
|
} |
190
|
|
|
// now new_tag_name and old_tag_name are set and the normal procedure can take place. |
191
|
|
|
} |
192
|
|
|
$old_tag_name = $this->request->variable('old_tag_name', ''); |
193
|
|
|
$new_tag_name = $this->request->variable('new_tag_name', ''); |
194
|
|
|
|
195
|
|
|
if (empty($old_tag_name) || empty($new_tag_name)) |
196
|
|
|
{ |
197
|
|
|
$error_msg = $this->user->lang('TOPICTAGS_MISSING_TAG_NAMES'); |
198
|
|
|
$this->simple_response($u_action, $error_msg, false); |
199
|
|
|
} |
200
|
|
|
else |
201
|
|
|
{ |
202
|
|
|
if ($is_ajax) |
203
|
|
|
{ |
204
|
|
|
$old_tag_name = rawurldecode(base64_decode($old_tag_name)); |
205
|
|
|
$new_tag_name = rawurldecode(base64_decode($new_tag_name)); |
206
|
|
|
} |
207
|
|
|
if ($old_tag_name == $new_tag_name) |
208
|
|
|
{ |
209
|
|
|
$error_msg = $this->user->lang('TOPICTAGS_NO_MODIFICATION', $old_tag_name); |
210
|
|
|
$this->simple_response($u_action, $error_msg, false); |
211
|
|
|
} |
212
|
|
|
$old_ids = $this->tags_manager->get_existing_tags(array($old_tag_name), true); |
213
|
|
|
if (empty($old_ids)) |
214
|
|
|
{ |
215
|
|
|
$error_msg = $this->user->lang('TOPICTAGS_TAG_DOES_NOT_EXIST', $old_tag_name); |
216
|
|
|
$this->simple_response($u_action, $error_msg, false); |
217
|
|
|
} |
218
|
|
|
// if we reach here, we know that we got a single valid old tag |
219
|
|
|
$old_id = $old_ids[0]; |
220
|
|
|
|
221
|
|
|
$new_tag_name_clean = $this->tags_manager->clean_tag($new_tag_name); |
222
|
|
|
$is_valid = $this->tags_manager->is_valid_tag($new_tag_name_clean, true); |
223
|
|
|
if (!$is_valid) |
224
|
|
|
{ |
225
|
|
|
$error_msg = $this->user->lang('TOPICTAGS_TAG_INVALID', $new_tag_name); |
226
|
|
|
$this->simple_response($u_action, $error_msg, false); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// old tag exist and new tag is valid |
230
|
|
|
$new_ids = $this->tags_manager->get_existing_tags(array($new_tag_name), true); |
231
|
|
|
if (!empty($new_ids)) |
232
|
|
|
{ |
233
|
|
|
// new tag exist -> merge |
234
|
|
|
$new_id = $new_ids[0]; |
235
|
|
|
$new_tag_count = $this->tags_manager->merge($old_id, $new_tag_name, $new_id); |
236
|
|
|
$msg = $this->user->lang('TOPICTAGS_TAG_MERGED', $new_tag_name_clean); |
237
|
|
|
$this->simple_response($u_action, $msg, true, array( |
238
|
|
|
'success' => true, |
239
|
|
|
'merged' => true, |
240
|
|
|
'new_tag_count' => $new_tag_count, |
241
|
|
|
'msg' => base64_encode(rawurlencode($msg)), |
242
|
|
|
)); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// old tag exist and new tag is valid and does not exist -> rename it |
246
|
|
|
$this->tags_manager->rename($old_id, $new_tag_name_clean); |
247
|
|
|
$msg = $this->user->lang('TOPICTAGS_TAG_CHANGED'); |
248
|
|
|
$this->simple_response($u_action, $msg); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Creates an ajax response or a normal response depending on the request. |
254
|
|
|
* |
255
|
|
|
* @param string $u_action phpbb acp-u_action |
256
|
|
|
* @param string $msg the message for the normal response |
257
|
|
|
* @param boolean $success whether the response is marked successful (default) or not |
258
|
|
|
* @param array $ajax_response optional values to response in ajax_response. If no values are |
259
|
|
|
* given the response will be for success==true: |
260
|
|
|
* <pre>array( |
261
|
|
|
* 'success' => true, |
262
|
|
|
* 'msg' => base64_encode(rawurlencode($msg)) |
263
|
|
|
* )</pre> |
264
|
|
|
* and for success==false: |
265
|
|
|
* <pre>array( |
266
|
|
|
* 'success' => false, |
267
|
|
|
* 'error_msg' => base64_encode(rawurlencode($msg)) |
268
|
|
|
* )</pre> |
269
|
|
|
*/ |
270
|
|
|
private function simple_response($u_action, $msg, $success = true, array $ajax_response = array()) |
271
|
|
|
{ |
272
|
|
|
if ($this->request->is_ajax()) |
273
|
|
|
{ |
274
|
|
|
if (empty($ajax_response)) |
275
|
|
|
{ |
276
|
|
|
$msg_key = $success ? 'msg' : 'error_msg'; |
277
|
|
|
$ajax_response = array( |
278
|
|
|
'success' => $success, |
279
|
|
|
$msg_key => base64_encode(rawurlencode($msg)), |
280
|
|
|
); |
281
|
|
|
} |
282
|
|
|
$response = new json_response(); |
283
|
|
|
$response->send($ajax_response); |
284
|
|
|
} |
285
|
|
|
if ($success) |
286
|
|
|
{ |
287
|
|
|
trigger_error($msg . adm_back_link($u_action)); |
288
|
|
|
} |
289
|
|
|
else |
290
|
|
|
{ |
291
|
|
|
trigger_error($msg . adm_back_link($u_action), E_USER_WARNING); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
private function get_tag_link($u_action, $tag_id) |
296
|
|
|
{ |
297
|
|
|
return $u_action . (($tag_id) ? '&tag_id=' . $tag_id : ''); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
private function create_sort_selects($selected_sort_key) |
301
|
|
|
{ |
302
|
|
|
$sort_selects = '<select name="sort_key" id="sort_key">'; |
303
|
|
|
|
304
|
|
|
$sort_keys = array( |
305
|
|
|
self::SORT_NAME_ASC => $this->user->lang('TOPICTAGS_SORT_NAME_ASC'), |
306
|
|
|
self::SORT_NAME_DESC => $this->user->lang('TOPICTAGS_SORT_NAME_DESC'), |
307
|
|
|
self::SORT_COUNT_ASC => $this->user->lang('TOPICTAGS_SORT_COUNT_ASC'), |
308
|
|
|
self::SORT_COUNT_DESC => $this->user->lang('TOPICTAGS_SORT_COUNT_DESC'), |
309
|
|
|
); |
310
|
|
|
|
311
|
|
|
foreach ($sort_keys as $key => $text) |
312
|
|
|
{ |
313
|
|
|
$sort_selects .= '<option value="' . $key . '"' . (($selected_sort_key == $key) ? ' selected="selected"' : '') . '>' . $text . '</option>'; |
314
|
|
|
} |
315
|
|
|
$sort_selects .= '</select>'; |
316
|
|
|
|
317
|
|
|
return $sort_selects; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|