Completed
Pull Request — master (#61)
by Matt
09:57
created

admin_input   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 310
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.8%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 37
c 4
b 0
f 0
lcom 1
cbo 1
dl 0
loc 310
ccs 116
cts 125
cp 0.928
rs 8.6

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A get() 0 4 1
A is_set_post() 0 4 1
A is_ajax() 0 4 1
A get_errors() 0 4 1
A has_errors() 0 4 1
B get_form_data() 0 39 5
B banner_upload() 0 30 5
A validate_ad_name() 0 11 3
A validate_ad_end_date() 0 16 4
A validate_ad_priority() 0 7 3
A validate_ad_views_limit() 0 7 2
A validate_ad_clicks_limit() 0 7 2
A validate_ad_owner() 0 8 3
A end_date_to_timestamp() 0 4 1
A owner_to_id() 0 10 2
A send_ajax_response() 0 9 1
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\ads\controller;
12
13
/**
14
 * Admin input
15
 */
16
class admin_input
17
{
18
	const MAX_NAME_LENGTH = 255;
19
	const DATE_FORMAT = 'Y-m-d';
20
	const DEFAULT_PRIORITY = 5;
21
22
	/** @var \phpbb\user */
23
	protected $user;
24
25
	/** @var \phpbb\language\language */
26
	protected $language;
27
28
	/** @var \phpbb\request\request */
29
	protected $request;
30
31
	/** @var \phpbb\ads\banner\banner */
32
	protected $banner;
33
34
	/** @var array Form validation errors */
35
	protected $errors = array();
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param \phpbb\user								$user			User object
41
	 * @param \phpbb\language\language                  $language       Language object
42
	 * @param \phpbb\request\request					$request		Request object
43
	 * @param \phpbb\ads\banner\banner					$banner			Banner upload object
44
	 */
45 19
	public function __construct(\phpbb\user $user, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\banner\banner $banner)
46
	{
47 19
		$this->user = $user;
48 19
		$this->language = $language;
49 19
		$this->request = $request;
50 19
		$this->banner = $banner;
51 19
	}
52
53
	/**
54
	 * Get a variable from the form
55
	 *
56
	 * @param string $name    The form variable's name from which data shall be retrieved.
57
	 * @param mixed  $default A default value that is returned if the variable was not set.
58
	 * @param bool   $mbyte   If the variable may contain any UTF-8 characters.
59
	 * @return mixed The value of the form variable or the $default value.
60
	 */
61
	public function get($name, $default, $mbyte = false)
62
	{
63
		return $this->request->variable($name, $default, $mbyte);
64
	}
65
66
	/**
67
	 * Check if a variable is posted
68
	 *
69
	 * @param string $name The form variable's name from which data shall be retrieved.
70
	 * @return bool True if form variable is posted, false otherwise.
71
	 */
72
	public function is_set_post($name)
73
	{
74
		return $this->request->is_set_post($name);
75
	}
76
77
	/**
78
	 * Check if a request is AJAX
79
	 *
80
	 * @return bool True if the request is AJAX, false otherwise.
81
	 */
82
	public function is_ajax()
83
	{
84
		return $this->request->is_ajax();
85
	}
86
87
	/**
88
	 * Gets all errors
89
	 *
90
	 * @return	array	Errors
91
	 */
92 14
	public function get_errors()
93
	{
94 14
		return $this->errors;
95
	}
96
97
	/**
98
	 * Returns number of errors.
99
	 *
100
	 * @return	int	Number of errors
101
	 */
102 14
	public function has_errors()
103
	{
104 14
		return count($this->errors);
105
	}
106
107
	/**
108
	 * Get admin form data.
109
	 *
110
	 * @param	string	$form_name	The form name.
111
	 * @return	array	Form data
112
	 */
113 12
	public function get_form_data($form_name)
114
	{
115
		$data = array(
116 12
			'ad_name'         => $this->request->variable('ad_name', '', true),
117 12
			'ad_note'         => $this->request->variable('ad_note', '', true),
118 12
			'ad_code'         => $this->request->variable('ad_code', '', true),
119 12
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
120 12
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
121 12
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
122 12
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
123 12
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
124 12
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
125 12
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
126 12
		);
127
128
		// Validate form key
129 12
		if (!check_form_key($form_name))
130 12
		{
131 2
			$this->errors[] = $this->language->lang('FORM_INVALID');
132 2
		}
133
134
		// Validate each property. Every method adds errors directly to $this->errors.
135 12
		foreach ($data as $prop_name => $prop_val)
136
		{
137 12
			if (method_exists($this, 'validate_' . $prop_name))
138 12
			{
139 12
				$this->{'validate_' . $prop_name}($prop_val);
140 12
			}
141 12
		}
142
143
		// Replace end date and owner with IDs that will be stored in the DB
144 12
		$data['ad_end_date'] = $this->end_date_to_timestamp($data['ad_end_date']);
145 12
		if (!in_array('AD_OWNER_INVALID', $this->errors))
146 12
		{
147 10
			$data['ad_owner'] = $this->owner_to_id($data['ad_owner']);
148 10
		}
149
150 12
		return $data;
151
	}
152
153
	/**
154
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
155
	 *
156
	 * @param	 string	 $ad_code	 Current ad code
157
	 * @return	 mixed	 \phpbb\json_response when request is ajax or updated ad code otherwise.
158
	 */
159 7
	public function banner_upload($ad_code)
160
	{
161
		try
162
		{
163 7
			$this->banner->create_storage_dir();
164 4
			$realname = $this->banner->upload();
165
166 3
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
167
168 3
			if ($this->request->is_ajax())
169 3
			{
170 1
				$this->send_ajax_response(true, $banner_html);
171
			}
172
173 2
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
174
		}
175 7
		catch (\phpbb\exception\runtime_exception $e)
0 ignored issues
show
Bug introduced by
The class phpbb\exception\runtime_exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
176
		{
177 4
			$this->banner->remove();
178
179 4
			if ($this->request->is_ajax())
180 4
			{
181 1
				$this->send_ajax_response(false, $this->language->lang($e->getMessage()));
182
			}
183
184 3
			$this->errors[] = $this->language->lang($e->getMessage());
185
		}
186
187 5
		return $ad_code;
188
	}
189
190
	/**
191
	 * Validate advertisement name
192
	 *
193
	 * @param string $ad_name Advertisement name
194
	 */
195 12
	protected function validate_ad_name($ad_name)
196
	{
197 12
		if ($ad_name === '')
198 12
		{
199 2
			$this->errors[] = 'AD_NAME_REQUIRED';
200 2
		}
201 12
		if (truncate_string($ad_name, self::MAX_NAME_LENGTH) !== $ad_name)
202 12
		{
203 1
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
204 1
		}
205 12
	}
206
207
	/**
208
	 * Validate advertisement end date
209
	 *
210
	 * @param string $end_date Advertisement end date
211
	 */
212 12
	protected function validate_ad_end_date($end_date)
213
	{
214 12
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
215 12
		{
216 2
			$end_date = (int) $this->end_date_to_timestamp($end_date);
217
218 2
			if ($end_date < time())
219 2
			{
220 1
				$this->errors[] = 'AD_END_DATE_INVALID';
221 1
			}
222 2
		}
223 10
		else if ($end_date !== '')
224 10
		{
225 2
			$this->errors[] = 'AD_END_DATE_INVALID';
226 2
		}
227 12
	}
228
229
	/**
230
	 * Validate advertisement priority
231
	 *
232
	 * @param int $ad_priority Advertisement priority
233
	 */
234 12
	protected function validate_ad_priority($ad_priority)
235
	{
236 12
		if ($ad_priority < 1 || $ad_priority > 10)
237 12
		{
238 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
239 3
		}
240 12
	}
241
242
	/**
243
	 * Validate advertisement views limit
244
	 *
245
	 * @param int $ad_views_limit Advertisement views limit
246
	 */
247 12
	protected function validate_ad_views_limit($ad_views_limit)
248
	{
249 12
		if ($ad_views_limit < 0)
250 12
		{
251 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
252 2
		}
253 12
	}
254
255
	/**
256
	 * Validate advertisement clicks limit
257
	 *
258
	 * @param int $ad_clicks_limit Advertisement clicks limit
259
	 */
260 12
	protected function validate_ad_clicks_limit($ad_clicks_limit)
261
	{
262 12
		if ($ad_clicks_limit < 0)
263 12
		{
264 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
265 2
		}
266 12
	}
267
268
	/**
269
	 * Validate advertisement owner
270
	 *
271
	 * @param string $ad_owner Advertisement owner
272
	 */
273 12
	protected function validate_ad_owner($ad_owner)
274
	{
275
		// user_get_id_name function returns false if everything is OK.
276 12
		if (!empty($ad_owner) && user_get_id_name($ad_owner_id, $ad_owner))
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
277 12
		{
278 2
			$this->errors[] = 'AD_OWNER_INVALID';
279 2
		}
280 12
	}
281
282
	/**
283
	 * Convert format of end date from string to unix timestamp
284
	 *
285
	 * @param string $end_date Advertisement end date in YYYY-MM-DD format
286
	 * @return int Advertisement end date in unix timestamp
287
	 */
288 12
	protected function end_date_to_timestamp($end_date)
289
	{
290 12
		return (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $end_date);
291
	}
292
293
	/**
294
	 * Convert advertisement owner username to ID
295
	 *
296
	 * @param string $ad_owner Advertisement owner username
297
	 * @return int Advertisement owner ID
298
	 */
299 10
	protected function owner_to_id($ad_owner)
300
	{
301 10
		if (empty($ad_owner))
302 10
		{
303 9
			return 0;
304
		}
305
306 1
		user_get_id_name($ad_owner_id, $ad_owner);
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
307 1
		return $ad_owner_id[0];
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
308
	}
309
310
	/**
311
	 * Send ajax response
312
	 *
313
	 * @param bool $success Is request successful?
314
	 * @param string $text Text to return
315
	 */
316 2
	protected function send_ajax_response($success, $text)
317
	{
318 2
		$json_response = new \phpbb\json_response;
319 2
		$json_response->send(array(
320 2
			'success'	=> $success,
321 2
			'title'		=> $this->language->lang('INFORMATION'),
322 2
			'text'		=> $text,
323 2
		));
324
	}
325
}
326