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

admin_controller   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 29
c 5
b 1
f 2
lcom 1
cbo 0
dl 0
loc 272
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B main() 0 28 6
A display_settings() 0 20 2
A add_prefix() 0 14 3
A edit_prefix() 0 17 3
B delete_prefix() 0 25 3
B move_prefix() 0 22 4
A set_u_action() 0 5 1
A set_forum_id() 0 4 1
A check_hash() 0 4 1
A trigger_message() 0 4 1
A log() 0 6 1
A get_forum_info() 0 11 2
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 implements admin_controller_interface
23
{
24
	/** @var manager */
25
	protected $manager;
26
27
	/** @var log */
28
	protected $log;
29
30
	/** @var request */
31
	protected $request;
32
33
	/** @var template */
34
	protected $template;
35
36
	/** @var user */
37
	protected $user;
38
39
	/** @var string */
40
	protected $root_path;
41
42
	/** @var string */
43
	protected $php_ext;
44
45
	/** @var string */
46
	protected $form_key;
47
48
	/** @var int */
49
	protected $forum_id;
50
51
	/** @var string */
52
	protected $u_action;
53
54
	/**
55
	 * Constructor
56
	 *
57
	 * @param manager  $manager
58
	 * @param log      $log
59
	 * @param request  $request
60
	 * @param template $template
61
	 * @param user     $user
62
	 * @param string   $phpbb_root_path
63
	 * @param string   $phpEx
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
	public function main()
77
	{
78
		$this->form_key = 'acp_topic_prefixes';
79
		add_form_key($this->form_key);
80
81
		$action = $this->request->variable('action', '');
82
		$prefix_id = $this->request->variable('prefix_id', 0);
83
		$this->set_forum_id($this->request->variable('forum_id', 0));
84
85
		switch ($action)
86
		{
87
			case 'add':
88
				$this->add_prefix();
89
			break;
90
91
			case 'edit':
92
			case 'delete':
93
				$this->{$action . '_prefix'}($prefix_id);
94
			break;
95
96
			case 'move_up':
97
			case 'move_down':
98
				$this->move_prefix($prefix_id, str_replace('move_', '', $action));
99
			break;
100
		}
101
102
		$this->display_settings();
103
	}
104
105
	/**
106
	 * @inheritdoc
107
	 */
108
	public function display_settings()
109
	{
110
		foreach ($this->manager->get_prefixes($this->forum_id) as $prefix)
111
		{
112
			$this->template->assign_block_vars('prefixes', [
113
				'PREFIX_TAG'		=> $prefix['prefix_tag'],
114
				'PREFIX_ENABLED'	=> (int) $prefix['prefix_enabled'],
115
				'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']),
116
				'U_DELETE'			=> "{$this->u_action}&amp;action=delete&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id,
117
				'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']),
118
				'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']),
119
			]);
120
		}
121
122
		$this->template->assign_vars([
123
			'S_FORUM_OPTIONS'	=> make_forum_select($this->forum_id, false, false, true),
124
			'FORUM_ID'			=> $this->forum_id,
125
			'U_ACTION'			=> $this->u_action,
126
		]);
127
	}
128
129
	/**
130
	 * @inheritdoc
131
	 */
132
	public function add_prefix()
133
	{
134
		if ($this->request->is_set_post('submit'))
135
		{
136
			if (!check_form_key($this->form_key))
137
			{
138
				$this->trigger_message('FORM_INVALID', E_USER_WARNING);
139
			}
140
141
			$tag = $this->request->variable('prefix_tag', '', true);
142
			$prefix = $this->manager->add_prefix($tag, $this->forum_id);
143
			$this->log($prefix['prefix_tag'], 'ACP_LOG_PREFIX_ADDED');
144
		}
145
	}
146
147
	/**
148
	 * @inheritdoc
149
	 */
150
	public function edit_prefix($prefix_id)
151
	{
152
		if (!$this->check_hash('edit' . $prefix_id))
153
		{
154
			$this->trigger_message('FORM_INVALID', E_USER_WARNING);
155
		}
156
157
		try
158
		{
159
			$prefix = $this->manager->get_prefix($prefix_id);
160
			$this->manager->update_prefix($prefix['prefix_id'], ['prefix_enabled' => !$prefix['prefix_enabled']]);
161
		}
162
		catch (\OutOfBoundsException $e)
163
		{
164
			$this->trigger_message($e->getMessage(), E_USER_WARNING);
165
		}
166
	}
167
168
	/**
169
	 * @inheritdoc
170
	 */
171
	public function delete_prefix($prefix_id)
172
	{
173
		if (confirm_box(true))
174
		{
175
			try
176
			{
177
				$prefix = $this->manager->get_prefix($prefix_id);
178
				$this->manager->delete_prefix($prefix['prefix_id']);
179
				$this->log($prefix['prefix_tag'], 'ACP_LOG_PREFIX_DELETED');
180
			}
181
			catch (\OutOfBoundsException $e)
182
			{
183
				$this->trigger_message($e->getMessage(), E_USER_WARNING);
184
			}
185
186
			$this->trigger_message('TOPIC_PREFIX_DELETED');
187
		}
188
189
		confirm_box(false, $this->user->lang('DELETE_TOPIC_PREFIX_CONFIRM'), build_hidden_fields([
190
			'mode'		=> 'manage',
191
			'action'	=> 'delete',
192
			'prefix_id'	=> $prefix_id,
193
			'forum_id'	=> $this->forum_id,
194
		]));
195
	}
196
197
	/**
198
	 * @inheritdoc
199
	 */
200
	public function move_prefix($prefix_id, $direction, $amount = 1)
201
	{
202
		if (!$this->check_hash($direction . $prefix_id))
203
		{
204
			$this->trigger_message('FORM_INVALID', E_USER_WARNING);
205
		}
206
207
		try
208
		{
209
			$this->manager->move_prefix($prefix_id, $direction, $amount);
210
		}
211
		catch (\OutOfBoundsException $e)
212
		{
213
			$this->trigger_message($e->getMessage(), E_USER_WARNING);
214
		}
215
216
		if ($this->request->is_ajax())
217
		{
218
			$json_response = new \phpbb\json_response;
219
			$json_response->send(['success' => true]);
220
		}
221
	}
222
223
	/**
224
	 * @inheritdoc
225
	 */
226
	public function set_u_action($u_action)
227
	{
228
		$this->u_action = $u_action;
229
		return $this;
230
	}
231
232
	/**
233
	 * @param int $forum_id
234
	 */
235
	public function set_forum_id($forum_id)
236
	{
237
		$this->forum_id = $forum_id;
238
	}
239
240
	/**
241
	 * Check link hash helper
242
	 *
243
	 * @param string $hash A hashed string
244
	 * @return bool True if hash matches, false if not
245
	 */
246
	protected function check_hash($hash)
247
	{
248
		return check_link_hash($this->request->variable('hash', ''), $hash);
249
	}
250
251
	/**
252
	 * Trigger a message and back link for error/success dialogs
253
	 *
254
	 * @param string $message A language key
255
	 * @param string $error   Error type constant, optional
256
	 * @return null
257
	 */
258
	protected function trigger_message($message = '', $error = E_USER_NOTICE)
259
	{
260
		trigger_error($this->user->lang($message) . adm_back_link("{$this->u_action}&amp;forum_id={$this->forum_id}"), $error);
261
	}
262
263
	/**
264
	 * Helper for logging topic prefix admin actions
265
	 *
266
	 * @param string $tag     The topic prefix tag
267
	 * @param string $message The log action language key
268
	 */
269
	protected function log($tag, $message)
270
	{
271
		$forum_data = $this->get_forum_info($this->forum_id);
272
273
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $message, time(), array($tag, $forum_data['forum_name']));
274
	}
275
276
	/**
277
	 * Get a forum's information
278
	 *
279
	 * @param int $forum_id
280
	 * @return mixed Array with the current row, false, if the row does not exist
281
	 */
282
	protected function get_forum_info($forum_id)
283
	{
284
		if (!class_exists('acp_forums'))
285
		{
286
			include $this->root_path . 'includes/acp/acp_forums.' . $this->php_ext;
287
		}
288
289
		$acp_forums = new \acp_forums();
290
291
		return $acp_forums->get_forum_info($forum_id);
292
	}
293
}
294