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

admin_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
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 View Code Duplication
		if (!check_link_hash($this->request->variable('hash', ''), 'edit' . $prefix_id))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
		if (!check_link_hash($this->request->variable('hash', ''), $direction . $prefix_id))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
	 * Return a common message and back link for trigger error dialogs
224
	 *
225
	 * @param string $message A language key
226
	 * @return string
227
	 */
228
	protected function get_error_message($message = '')
229
	{
230
		return $this->user->lang($message) . adm_back_link("{$this->u_action}&amp;forum_id={$this->forum_id}");
231
	}
232
}
233