Completed
Push — master ( 56db4e...d7c302 )
by Michael
8s
created

admin_controller::set_forum_id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 *
4
 * Topic Prefixes extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2016 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\topicprefixes\controller;
12
13
use phpbb\log\log;
14
use phpbb\request\request;
15
use phpbb\template\template;
16
use phpbb\topicprefixes\prefixes\manager;
17
use phpbb\user;
18
19
/**
20
 * Class admin_controller
21
 */
22
class admin_controller
23
{
24
	/** @var manager Topic prefixes manager object */
25
	protected $manager;
26
27
	/** @var log phpBB log object */
28
	protected $log;
29
30
	/** @var request phpBB request object */
31
	protected $request;
32
33
	/** @var template phpBB template object */
34
	protected $template;
35
36
	/** @var user phpBB user object */
37
	protected $user;
38
39
	/** @var string phpBB root path */
40
	protected $root_path;
41
42
	/** @var string PHP extension */
43
	protected $php_ext;
44
45
	/** @var string Form key used for form validation */
46
	protected $form_key;
47
48
	/** @var int Forum identifier */
49
	protected $forum_id;
50
51
	/** @var string Custom form action */
52
	protected $u_action;
53
54
	/**
55
	 * Constructor
56
	 *
57
	 * @param manager  $manager         Topic prefixes manager object
58
	 * @param log      $log             phpBB log object
59
	 * @param request  $request         phpBB request object
60
	 * @param template $template        phpBB template object
61
	 * @param user     $user            phpBB user object
62
	 * @param string   $phpbb_root_path phpBB root path
63
	 * @param string   $phpEx           PHP extension
64
	 */
65
	public function __construct(manager $manager, log $log, request $request, template $template, user $user, $phpbb_root_path, $phpEx)
66
	{
67
		$this->manager = $manager;
68
		$this->log = $log;
69
		$this->request = $request;
70
		$this->template = $template;
71
		$this->user = $user;
72
		$this->root_path = $phpbb_root_path;
73
		$this->php_ext = $phpEx;
74
	}
75
76
	/**
77
	 * Main handler, called by the ACP module
78
	 *
79
	 * @return null
80
	 */
81
	public function main()
82
	{
83
		$this->form_key = 'acp_topic_prefixes';
84
		add_form_key($this->form_key);
85
86
		$action = $this->request->variable('action', '');
87
		$prefix_id = $this->request->variable('prefix_id', 0);
88
		$this->set_forum_id($this->request->variable('forum_id', 0));
89
90
		switch ($action)
91
		{
92
			case 'add':
93
				$this->add_prefix();
94
			break;
95
96
			case 'edit':
97
			case 'delete':
98
				$this->{$action . '_prefix'}($prefix_id);
99
			break;
100
101
			case 'move_up':
102
			case 'move_down':
103
				$this->move_prefix($prefix_id, str_replace('move_', '', $action));
104
			break;
105
		}
106
107
		$this->display_settings();
108
	}
109
110
	/**
111
	 * Display topic prefix settings
112
	 *
113
	 * @return null
114
	 */
115
	public function display_settings()
116
	{
117
		foreach ($this->manager->get_prefixes($this->forum_id) as $prefix)
118
		{
119
			$this->template->assign_block_vars('prefixes', [
120
				'PREFIX_TAG'		=> $prefix['prefix_tag'],
121
				'PREFIX_ENABLED'	=> (int) $prefix['prefix_enabled'],
122
				'U_EDIT'			=> "{$this->u_action}&amp;action=edit&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id . '&amp;hash=' . generate_link_hash('edit' . $prefix['prefix_id']),
123
				'U_DELETE'			=> "{$this->u_action}&amp;action=delete&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id,
124
				'U_MOVE_UP'			=> "{$this->u_action}&amp;action=move_up&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id . '&amp;hash=' . generate_link_hash('up' . $prefix['prefix_id']),
125
				'U_MOVE_DOWN'		=> "{$this->u_action}&amp;action=move_down&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id . '&amp;hash=' . generate_link_hash('down' . $prefix['prefix_id']),
126
			]);
127
		}
128
129
		$this->template->assign_vars([
130
			'S_FORUM_OPTIONS'	=> make_forum_select($this->forum_id, false, false, true),
131
			'FORUM_ID'			=> $this->forum_id,
132
			'U_ACTION'			=> $this->u_action,
133
		]);
134
	}
135
136
	/**
137
	 * Add a prefix
138
	 *
139
	 * @return null
140
	 */
141
	public function add_prefix()
142
	{
143
		if ($this->request->is_set_post('submit'))
144
		{
145
			if (!check_form_key($this->form_key))
146
			{
147
				$this->trigger_message('FORM_INVALID', E_USER_WARNING);
148
			}
149
150
			$tag = $this->request->variable('prefix_tag', '', true);
151
			$prefix = $this->manager->add_prefix($tag, $this->forum_id);
152
			$this->log($prefix['prefix_tag'], 'ACP_LOG_PREFIX_ADDED');
153
		}
154
	}
155
156
	/**
157
	 * Edit a prefix
158
	 *
159
	 * @param int $prefix_id The prefix identifier to edit
160
	 * @return null
161
	 */
162
	public function edit_prefix($prefix_id)
163
	{
164
		if (!$this->check_hash('edit' . $prefix_id))
165
		{
166
			$this->trigger_message('FORM_INVALID', E_USER_WARNING);
167
		}
168
169
		try
170
		{
171
			$prefix = $this->manager->get_prefix($prefix_id);
172
			$this->manager->update_prefix($prefix['prefix_id'], ['prefix_enabled' => !$prefix['prefix_enabled']]);
173
		}
174
		catch (\OutOfBoundsException $e)
175
		{
176
			$this->trigger_message($e->getMessage(), E_USER_WARNING);
177
		}
178
	}
179
180
	/**
181
	 * Delete a prefix
182
	 *
183
	 * @param int $prefix_id The prefix identifier to delete
184
	 * @return null
185
	 */
186
	public function delete_prefix($prefix_id)
187
	{
188
		if (confirm_box(true))
189
		{
190
			try
191
			{
192
				$prefix = $this->manager->get_prefix($prefix_id);
193
				$this->manager->delete_prefix($prefix['prefix_id']);
194
				$this->log($prefix['prefix_tag'], 'ACP_LOG_PREFIX_DELETED');
195
			}
196
			catch (\OutOfBoundsException $e)
197
			{
198
				$this->trigger_message($e->getMessage(), E_USER_WARNING);
199
			}
200
201
			$this->trigger_message('TOPIC_PREFIX_DELETED');
202
		}
203
204
		confirm_box(false, $this->user->lang('DELETE_TOPIC_PREFIX_CONFIRM'), build_hidden_fields([
205
			'mode'		=> 'manage',
206
			'action'	=> 'delete',
207
			'prefix_id'	=> $prefix_id,
208
			'forum_id'	=> $this->forum_id,
209
		]));
210
	}
211
212
	/**
213
	 * Move a prefix up/down
214
	 *
215
	 * @param int    $prefix_id The prefix identifier to move
216
	 * @param string $direction The direction (up|down)
217
	 * @param int    $amount    The amount of places to move (default: 1)
218
	 * @return null
219
	 */
220
	public function move_prefix($prefix_id, $direction, $amount = 1)
221
	{
222
		if (!$this->check_hash($direction . $prefix_id))
223
		{
224
			$this->trigger_message('FORM_INVALID', E_USER_WARNING);
225
		}
226
227
		try
228
		{
229
			$this->manager->move_prefix($prefix_id, $direction, $amount);
230
		}
231
		catch (\OutOfBoundsException $e)
232
		{
233
			$this->trigger_message($e->getMessage(), E_USER_WARNING);
234
		}
235
236
		if ($this->request->is_ajax())
237
		{
238
			$json_response = new \phpbb\json_response;
239
			$json_response->send(['success' => true]);
240
		}
241
	}
242
243
	/**
244
	 * Set u_action
245
	 *
246
	 * @param string $u_action Custom form action
247
	 * @return admin_controller
248
	 */
249
	public function set_u_action($u_action)
250
	{
251
		$this->u_action = $u_action;
252
		return $this;
253
	}
254
255
	/**
256
	 * Set forum ID
257
	 *
258
	 * @param int $forum_id Forum identifier
259
	 * @return admin_controller
260
	 */
261
	public function set_forum_id($forum_id)
262
	{
263
		$this->forum_id = $forum_id;
264
		return $this;
265
	}
266
267
	/**
268
	 * Check link hash helper
269
	 *
270
	 * @param string $hash A hashed string
271
	 * @return bool True if hash matches, false if not
272
	 */
273
	protected function check_hash($hash)
274
	{
275
		return check_link_hash($this->request->variable('hash', ''), $hash);
276
	}
277
278
	/**
279
	 * Trigger a message and back link for error/success dialogs
280
	 *
281
	 * @param string $message A language key
282
	 * @param int    $error   Error type constant, optional
283
	 * @return null
284
	 */
285
	protected function trigger_message($message = '', $error = E_USER_NOTICE)
286
	{
287
		trigger_error($this->user->lang($message) . adm_back_link("{$this->u_action}&amp;forum_id={$this->forum_id}"), $error);
288
	}
289
290
	/**
291
	 * Helper for logging topic prefix admin actions
292
	 *
293
	 * @param string $tag     The topic prefix tag
294
	 * @param string $message The log action language key
295
	 * @return null
296
	 */
297
	protected function log($tag, $message)
298
	{
299
		$forum_data = $this->get_forum_info($this->forum_id);
300
301
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $message, time(), array($tag, $forum_data['forum_name']));
302
	}
303
304
	/**
305
	 * Get a forum's information
306
	 *
307
	 * @param int $forum_id
308
	 * @return mixed Array with the current row, false, if the row does not exist
309
	 */
310
	protected function get_forum_info($forum_id)
311
	{
312
		if (!class_exists('acp_forums'))
313
		{
314
			include $this->root_path . 'includes/acp/acp_forums.' . $this->php_ext;
315
		}
316
317
		$acp_forums = new \acp_forums();
318
319
		return $acp_forums->get_forum_info($forum_id);
320
	}
321
}
322