Completed
Pull Request — master (#4)
by Matt
02:43
created

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 10
rs 9.4285
cc 1
eloc 8
nc 1
nop 7
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 Form action string */
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 $action The action (move_up|move_down)
0 ignored issues
show
Bug introduced by
There is no parameter named $action. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
217
	 * @return null
218
	 */
219
	public function move_prefix($prefix_id, $direction, $amount = 1)
220
	{
221
		if (!$this->check_hash($direction . $prefix_id))
222
		{
223
			$this->trigger_message('FORM_INVALID', E_USER_WARNING);
224
		}
225
226
		try
227
		{
228
			$this->manager->move_prefix($prefix_id, $direction, $amount);
229
		}
230
		catch (\OutOfBoundsException $e)
231
		{
232
			$this->trigger_message($e->getMessage(), E_USER_WARNING);
233
		}
234
235
		if ($this->request->is_ajax())
236
		{
237
			$json_response = new \phpbb\json_response;
238
			$json_response->send(['success' => true]);
239
		}
240
	}
241
242
	/**
243
	 * Set u_action
244
	 *
245
	 * @param string $u_action Custom form action
246
	 * @return null
0 ignored issues
show
Documentation introduced by
Should the return type not be admin_controller?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
247
	 */
248
	public function set_u_action($u_action)
249
	{
250
		$this->u_action = $u_action;
251
		return $this;
252
	}
253
254
	/**
255
	 * Set forum ID
256
	 *
257
	 * @param int $forum_id Forum identifier
258
	 */
259
	public function set_forum_id($forum_id)
260
	{
261
		$this->forum_id = $forum_id;
262
		return $this;
263
	}
264
265
	/**
266
	 * Check link hash helper
267
	 *
268
	 * @param string $hash A hashed string
269
	 * @return bool True if hash matches, false if not
270
	 */
271
	protected function check_hash($hash)
272
	{
273
		return check_link_hash($this->request->variable('hash', ''), $hash);
274
	}
275
276
	/**
277
	 * Trigger a message and back link for error/success dialogs
278
	 *
279
	 * @param string $message A language key
280
	 * @param int    $error   Error type constant, optional
281
	 * @return null
282
	 */
283
	protected function trigger_message($message = '', $error = E_USER_NOTICE)
284
	{
285
		trigger_error($this->user->lang($message) . adm_back_link("{$this->u_action}&amp;forum_id={$this->forum_id}"), $error);
286
	}
287
288
	/**
289
	 * Helper for logging topic prefix admin actions
290
	 *
291
	 * @param string $tag     The topic prefix tag
292
	 * @param string $message The log action language key
293
	 */
294
	protected function log($tag, $message)
295
	{
296
		$forum_data = $this->get_forum_info($this->forum_id);
297
298
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $message, time(), array($tag, $forum_data['forum_name']));
299
	}
300
301
	/**
302
	 * Get a forum's information
303
	 *
304
	 * @param int $forum_id
305
	 * @return mixed Array with the current row, false, if the row does not exist
306
	 */
307
	protected function get_forum_info($forum_id)
308
	{
309
		if (!class_exists('acp_forums'))
310
		{
311
			include $this->root_path . 'includes/acp/acp_forums.' . $this->php_ext;
312
		}
313
314
		$acp_forums = new \acp_forums();
315
316
		return $acp_forums->get_forum_info($forum_id);
317
	}
318
}
319