Completed
Pull Request — master (#2)
by Jakub
05:11
created

admin_controller::set_page_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\admanagement\controller;
12
13
/**
14
* Admin controller
15
*/
16
class admin_controller
17
{
18
	/** @var \phpbb\db\driver\driver_interface */
19
	protected $db;
20
21
	/** @var \phpbb\template\template */
22
	protected $template;
23
24
	/** @var \phpbb\user */
25
	protected $user;
26
27
	/** @var \phpbb\request\request */
28
	protected $request;
29
30
	/** @var string ads_table */
31
	protected $ads_table;
32
33
	/** @var string Custom form action */
34
	protected $u_action;
35
36
	/**
37
	* Constructor
38
	*
39
	* @param \phpbb\db\driver\driver_interface	$db			DB driver interface
40
	* @param \phpbb\template\template			$template	Template object
41
	* @param \phpbb\user						$user		User object
42
	* @param \phpbb\request\request				$request	Request object
43
	* @param string								$ads_table	Ads table
44
	*/
45
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, $ads_table)
46
	{
47
		$this->db = $db;
48
		$this->template = $template;
49
		$this->user = $user;
50
		$this->request = $request;
51
		$this->ads_table = $ads_table;
52
	}
53
54
	/**
55
	* Set page url
56
	*
57
	* @param string $u_action Custom form action
58
	* @return void
59
	*/
60
	public function set_page_url($u_action)
61
	{
62
		$this->u_action = $u_action;
63
	}
64
65
	/**
66
	* Load module-specific language
67
	*
68
	* @return void
69
	*/
70
	public function load_lang()
71
	{
72
		$this->user->add_lang_ext('phpbb/admanagement', 'acp');
73
	}
74
75
	/**
76
	* Get ACP page title for Ads module
77
	*
78
	* @return string	Language string for Ads ACP module
79
	*/
80
	public function get_page_title()
81
	{
82
		return $this->user->lang('ACP_ADMANAGEMENT_TITLE');
83
	}
84
85
	/**
86
	* Get action
87
	*
88
	* @return string	Ads module action
89
	*/
90
	public function get_action()
91
	{
92
		return $this->request->variable('action', '');
93
	}
94
95
	/**
96
	* Process 'add' action
97
	*
98
	* @return void
99
	*/
100
	public function action_add()
101
	{
102
		$errors = array();
103
104
		add_form_key('phpbb/admanagement/add');
105
		if ($this->request->is_set_post('submit'))
106
		{
107
			if (!check_form_key('phpbb/admanagement/add'))
108
			{
109
				$errors[] = $this->user->lang('FORM_INVALID');
110
			}
111
112
			$data = array(
113
				'ad_name'		=> $this->request->variable('ad_name', '', true),
114
				'ad_note'		=> $this->request->variable('ad_note', '', true),
115
				'ad_code'		=> $this->request->variable('ad_code', '', true),
116
				'ad_enabled'	=> $this->request->variable('ad_code', false),
117
			);
118
119
			// Validate data
120
			if ($data['ad_name'] === '')
121
			{
122
				$errors[] = $this->user->lang('AD_NAME_REQUIRED');
123
			}
124
			if (truncate_string($data['ad_name'], 255) !== $data['ad_name'])
125
			{
126
				$errors[] = $this->user->lang('AD_NAME_TOO_LONG');
127
			}
128
129
			if (empty($errors))
130
			{
131
				// Insert the ad data to the database
132
				$sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data);
133
				$this->db->sql_query($sql);
134
135
				trigger_error($this->user->lang('ACP_AD_ADD_SUCCESS') . adm_back_link($this->u_action));
136
			}
137
			else
138
			{
139
				$this->template->assign_vars(array(
140
					'S_ERROR'			=> (bool) count($errors),
141
					'ERROR_MSG'			=> count($errors) ? implode('<br />', $errors) : '',
142
143
					'AD_NAME'		=> $data['ad_name'],
144
					'AD_NOTE'		=> $data['ad_note'],
145
					'AD_CODE'		=> $data['ad_code'],
146
					'AD_ENABLED'	=> $data['ad_enabled'],
147
				));
148
			}
149
		}
150
151
		// Set output vars for display in the template
152
		$this->template->assign_vars(array(
153
			'S_ADD_AD'	=> true,
154
			'U_BACK'	=> $this->u_action,
155
		));
156
	}
157
158
159
	/**
160
	* Enable/disable ad
161
	*
162
	* @return void
163
	*/
164
	public function ad_enable($enable)
165
	{
166
		$sql = 'UPDATE ' . $this->ads_table . '
167
			SET ad_enabled = ' . (int) $enable . '
168
			WHERE ad_id = ' . (int) $this->request->variable('id', 0);
169
		$this->db->sql_query($sql);
170
		$success = (bool) $this->db->sql_affectedrows();
171
172
		// If AJAX was used, show user a result message
173
		if ($this->request->is_ajax())
174
		{
175
			$json_response = new \phpbb\json_response;
176
			$json_response->send(array(
177
				'text'	=> $enable ? $this->user->lang('ENABLED') : $this->user->lang('DISABLED'),
178
				'title'	=> $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
179
			));
180
		}
181
182
		// Otherwise, show traditional infobox
183
		if ($success)
184
		{
185
			trigger_error($this->user->lang($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS') . adm_back_link($this->u_action));
186
		}
187
		else
188
		{
189
			trigger_error($this->user->lang($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED') . adm_back_link($this->u_action), E_USER_WARNING);
190
		}
191
	}
192
193
	/**
194
	* Process 'delete' action
195
	*
196
	* @return void
197
	*/
198
	public function action_delete()
199
	{
200
		$sql = 'DELETE FROM ' . $this->ads_table . '
201
			WHERE ad_id = ' . (int) $this->request->variable('id', 0);
202
		$this->db->sql_query($sql);
203
204
		if ($this->db->sql_affectedrows())
205
		{
206
			trigger_error($this->user->lang('ACP_AD_DELETE_SUCCESS') . adm_back_link($this->u_action));
207
		}
208
		else
209
		{
210
			trigger_error($this->user->lang('ACP_AD_DELETE_ERRORED') . adm_back_link($this->u_action), E_USER_WARNING);
211
		}
212
	}
213
214
215
	/**
216
	* Display the ads
217
	*
218
	* @return void
219
	*/
220
	public function list_ads()
221
	{
222
		$sql = 'SELECT ad_id, ad_name, ad_enabled
223
			FROM ' . $this->ads_table;
224
		$result = $this->db->sql_query($sql);
225
		while ($row = $this->db->sql_fetchrow($result))
226
		{
227
			$ad_enabled = (bool) $row['ad_enabled'];
228
229
			$this->template->assign_block_vars('ads', array( // TODO: convert back to original notation (3.1 does not support this)
230
				'NAME'		=> $row['ad_name'],
231
				'S_ENABLED'	=> $ad_enabled,
232
				'ENABLED'	=> (int) $ad_enabled,
233
				'U_ENABLE'	=> $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'], // TODO: ACP method
234
				'U_PREVIEW'	=> '', // TODO: frontend logic
235
				'U_EDIT'	=> $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'], // TODO: ACP method
236
				'U_DELETE'	=> $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'], // TODO: ACP method
237
			));
238
		}
239
		$this->db->sql_freeresult($result);
240
241
		// Set output vars for display in the template
242
		$this->template->assign_vars(array(
243
			'U_ACTION_ADD'	=> $this->u_action . '&amp;action=add',
244
		));
245
	}
246
}
247