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

admin_controller::add_prefix()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 9.4285
cc 3
eloc 6
nc 3
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
				$method = $action . '_prefix';
78
				$this->$method($prefix_id);
79
			break;
80
81
			case 'move_up':
82
			case 'move_down':
83
				$this->move_prefix($prefix_id, str_replace('move_', '', $action));
84
			break;
85
		}
86
87
		$this->display_settings();
88
	}
89
90
	/**
91
	 * @inheritdoc
92
	 */
93
	public function display_settings()
94
	{
95
		foreach ($this->manager->get_prefixes($this->forum_id) as $prefix)
96
		{
97
			$this->template->assign_block_vars('prefixes', [
98
				'PREFIX_TAG'		=> $prefix['prefix_tag'],
99
				'PREFIX_ENABLED'	=> (int) $prefix['prefix_enabled'],
100
				'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']),
101
				'U_DELETE'			=> "{$this->u_action}&amp;action=delete&amp;prefix_id=" . $prefix['prefix_id'] . '&amp;forum_id=' . $this->forum_id,
102
				'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']),
103
				'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']),
104
			]);
105
		}
106
107
		$this->template->assign_vars([
108
			'S_FORUM_OPTIONS'	=> make_forum_select($this->forum_id, false, false, true),
109
			'FORUM_ID'			=> $this->forum_id,
110
			'U_ACTION'			=> $this->u_action,
111
		]);
112
	}
113
114
	/**
115
	 * @inheritdoc
116
	 */
117
	public function add_prefix()
118
	{
119
		if ($this->request->is_set_post('submit'))
120
		{
121
			if (!check_form_key($this->form_key))
122
			{
123
				trigger_error($this->get_error_message('FORM_INVALID'), E_USER_WARNING);
124
			}
125
126
			$prefix = $this->request->variable('prefix_tag', '', true);
127
			$this->manager->add_prefix($prefix, $this->forum_id);
128
		}
129
	}
130
131
	/**
132
	 * @inheritdoc
133
	 */
134
	public function edit_prefix($prefix_id)
135
	{
136
		if (!$this->check_hash('edit' . $prefix_id))
137
		{
138
			trigger_error($this->get_error_message('FORM_INVALID'), E_USER_WARNING);
139
		}
140
141
		try
142
		{
143
			$prefix = $this->manager->get_prefix($prefix_id);
144
			$this->manager->update_prefix($prefix['prefix_id'], ['prefix_enabled' => !$prefix['prefix_enabled']]);
145
		}
146
		catch (\OutOfBoundsException $e)
147
		{
148
			trigger_error($e->getMessage() . $this->get_error_message(), E_USER_WARNING);
149
		}
150
	}
151
152
	/**
153
	 * @inheritdoc
154
	 */
155
	public function delete_prefix($prefix_id)
156
	{
157
		if (confirm_box(true))
158
		{
159
			try
160
			{
161
				$this->manager->delete_prefix($prefix_id);
162
			}
163
			catch (\OutOfBoundsException $e)
164
			{
165
				trigger_error($e->getMessage() . $this->get_error_message(), E_USER_WARNING);
166
			}
167
168
			trigger_error($this->get_error_message('TOPIC_PREFIX_DELETED'));
169
		}
170
171
		confirm_box(false, $this->user->lang('DELETE_TOPIC_PREFIX_CONFIRM'), build_hidden_fields([
172
			'mode'		=> 'manage',
173
			'action'	=> 'delete',
174
			'prefix_id'	=> $prefix_id,
175
			'forum_id'	=> $this->forum_id,
176
		]));
177
	}
178
179
	/**
180
	 * @inheritdoc
181
	 */
182
	public function move_prefix($prefix_id, $direction, $amount = 1)
183
	{
184
		if (!$this->check_hash($direction . $prefix_id))
185
		{
186
			trigger_error($this->get_error_message('FORM_INVALID'), E_USER_WARNING);
187
		}
188
189
		try
190
		{
191
			$this->manager->move_prefix($prefix_id, $direction, $amount);
192
		}
193
		catch (\OutOfBoundsException $e)
194
		{
195
			trigger_error($e->getMessage() . $this->get_error_message(), E_USER_WARNING);
196
		}
197
198
		if ($this->request->is_ajax())
199
		{
200
			$json_response = new \phpbb\json_response;
201
			$json_response->send(['success' => true]);
202
		}
203
	}
204
205
	/**
206
	 * @inheritdoc
207
	 */
208
	public function set_u_action($u_action)
209
	{
210
		$this->u_action = $u_action;
211
		return $this;
212
	}
213
214
	/**
215
	 * @param int $forum_id
216
	 */
217
	public function set_forum_id($forum_id)
218
	{
219
		$this->forum_id = $forum_id;
220
	}
221
222
	/**
223
	 * Check link hash helper
224
	 *
225
	 * @param string $hash A hashed string
226
	 * @return bool True if hash matches, false if not
227
	 */
228
	protected function check_hash($hash)
229
	{
230
		return check_link_hash($this->request->variable('hash', ''), $hash);
231
	}
232
233
	/**
234
	 * Return a common message and back link for trigger error dialogs
235
	 *
236
	 * @param string $message A language key
237
	 * @return string
238
	 */
239
	protected function get_error_message($message = '')
240
	{
241
		return $this->user->lang($message) . adm_back_link("{$this->u_action}&amp;forum_id={$this->forum_id}");
242
	}
243
}
244