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

admin_controller::main()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 28
rs 8.439
cc 6
eloc 19
nc 6
nop 0
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\request\request;
14
use phpbb\template\template;
15
use phpbb\topicprefixes\prefixes\manager;
16
use phpbb\user;
17
18
/**
19
 * Class admin_controller
20
 */
21
class admin_controller implements admin_controller_interface
22
{
23
	/** @var manager */
24
	protected $manager;
25
26
	/** @var request */
27
	protected $request;
28
29
	/** @var template */
30
	protected $template;
31
32
	/** @var user */
33
	protected $user;
34
35
	/** @var string */
36
	protected $form_key;
37
38
	/** @var int */
39
	protected $forum_id;
40
41
	/** @var string */
42
	protected $u_action;
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param manager  $manager
48
	 * @param request  $request
49
	 * @param template $template
50
	 * @param user     $user
51
	 */
52
	public function __construct(manager $manager, request $request, template $template, user $user)
53
	{
54
		$this->manager = $manager;
55
		$this->request = $request;
56
		$this->template = $template;
57
		$this->user = $user;
58
	}
59
60
	public function main()
61
	{
62
		$this->form_key = 'acp_topic_prefixes';
63
		add_form_key($this->form_key);
64
65
		$action = $this->request->variable('action', '');
66
		$prefix_id = $this->request->variable('prefix_id', 0);
67
		$this->set_forum_id($this->request->variable('forum_id', 0));
68
69
		switch ($action)
70
		{
71
			case 'add':
72
				$this->add_prefix();
73
			break;
74
75
			case 'edit':
76
			case 'delete':
77
				$this->{$action . '_prefix'}($prefix_id);
78
			break;
79
80
			case 'move_up':
81
			case 'move_down':
82
				$this->move_prefix($prefix_id, str_replace('move_', '', $action));
83
			break;
84
		}
85
86
		$this->display_settings();
87
	}
88
89
	/**
90
	 * @inheritdoc
91
	 */
92
	public function display_settings()
93
	{
94
		foreach ($this->manager->get_prefixes($this->forum_id) as $prefix)
95
		{
96
			$this->template->assign_block_vars('prefixes', [
97
				'PREFIX_TAG'		=> $prefix['prefix_tag'],
98
				'PREFIX_ENABLED'	=> (int) $prefix['prefix_enabled'],
99
				'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']),
100
				'U_DELETE'			=> "{$this->u_action}&amp;action=delete&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id,
101
				'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']),
102
				'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']),
103
			]);
104
		}
105
106
		$this->template->assign_vars([
107
			'S_FORUM_OPTIONS'	=> make_forum_select($this->forum_id, false, false, true),
108
			'FORUM_ID'			=> $this->forum_id,
109
			'U_ACTION'			=> $this->u_action,
110
		]);
111
	}
112
113
	/**
114
	 * @inheritdoc
115
	 */
116
	public function add_prefix()
117
	{
118
		if ($this->request->is_set_post('submit'))
119
		{
120
			if (!check_form_key($this->form_key))
121
			{
122
				$this->trigger_message('FORM_INVALID', E_USER_WARNING);
123
			}
124
125
			$prefix = $this->request->variable('prefix_tag', '', true);
126
			$this->manager->add_prefix($prefix, $this->forum_id);
127
		}
128
	}
129
130
	/**
131
	 * @inheritdoc
132
	 */
133
	public function edit_prefix($prefix_id)
134
	{
135
		if (!$this->check_hash('edit' . $prefix_id))
136
		{
137
			$this->trigger_message('FORM_INVALID', E_USER_WARNING);
138
		}
139
140
		try
141
		{
142
			$prefix = $this->manager->get_prefix($prefix_id);
143
			$this->manager->update_prefix($prefix['prefix_id'], ['prefix_enabled' => !$prefix['prefix_enabled']]);
144
		}
145
		catch (\OutOfBoundsException $e)
146
		{
147
			$this->trigger_message($e->getMessage(), E_USER_WARNING);
148
		}
149
	}
150
151
	/**
152
	 * @inheritdoc
153
	 */
154
	public function delete_prefix($prefix_id)
155
	{
156
		if (confirm_box(true))
157
		{
158
			try
159
			{
160
				$this->manager->delete_prefix($prefix_id);
161
			}
162
			catch (\OutOfBoundsException $e)
163
			{
164
				$this->trigger_message($e->getMessage(), E_USER_WARNING);
165
			}
166
167
			$this->trigger_message('TOPIC_PREFIX_DELETED');
168
		}
169
170
		confirm_box(false, $this->user->lang('DELETE_TOPIC_PREFIX_CONFIRM'), build_hidden_fields([
171
			'mode'		=> 'manage',
172
			'action'	=> 'delete',
173
			'prefix_id'	=> $prefix_id,
174
			'forum_id'	=> $this->forum_id,
175
		]));
176
	}
177
178
	/**
179
	 * @inheritdoc
180
	 */
181
	public function move_prefix($prefix_id, $direction, $amount = 1)
182
	{
183
		if (!$this->check_hash($direction . $prefix_id))
184
		{
185
			$this->trigger_message('FORM_INVALID', E_USER_WARNING);
186
		}
187
188
		try
189
		{
190
			$this->manager->move_prefix($prefix_id, $direction, $amount);
191
		}
192
		catch (\OutOfBoundsException $e)
193
		{
194
			$this->trigger_message($e->getMessage(), E_USER_WARNING);
195
		}
196
197
		if ($this->request->is_ajax())
198
		{
199
			$json_response = new \phpbb\json_response;
200
			$json_response->send(['success' => true]);
201
		}
202
	}
203
204
	/**
205
	 * @inheritdoc
206
	 */
207
	public function set_u_action($u_action)
208
	{
209
		$this->u_action = $u_action;
210
		return $this;
211
	}
212
213
	/**
214
	 * @param int $forum_id
215
	 */
216
	public function set_forum_id($forum_id)
217
	{
218
		$this->forum_id = $forum_id;
219
	}
220
221
	/**
222
	 * Check link hash helper
223
	 *
224
	 * @param string $hash A hashed string
225
	 * @return bool True if hash matches, false if not
226
	 */
227
	protected function check_hash($hash)
228
	{
229
		return check_link_hash($this->request->variable('hash', ''), $hash);
230
	}
231
232
	/**
233
	 * Trigger a message and back link for error/success dialogs
234
	 *
235
	 * @param string $message A language key
236
	 * @param string $error   Error type constant, optional
237
	 * @return null
238
	 */
239
	protected function trigger_message($message = '', $error = E_USER_NOTICE)
240
	{
241
		trigger_error($this->user->lang($message) . adm_back_link("{$this->u_action}&amp;forum_id={$this->forum_id}"), $error);
242
	}
243
}
244