Completed
Pull Request — master (#79)
by Jakub
10:08
created

admin_input::validate_ad_views_limit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
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\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
52 19
		add_form_key('phpbb_ads');
53 19
	}
54
55
	/**
56
	 * Gets all errors
57
	 *
58
	 * @return	array	Errors
59
	 */
60 14
	public function get_errors()
61
	{
62 14
		return $this->errors;
63
	}
64
65
	/**
66
	 * Returns number of errors.
67
	 *
68
	 * @return	int	Number of errors
69
	 */
70 14
	public function has_errors()
71
	{
72 14
		return count($this->errors);
73
	}
74
75
	/**
76
	 * Get admin form data.
77
	 *
78
	 * @return	array	Form data
79
	 */
80 12
	public function get_form_data()
81
	{
82
		$data = array(
83 12
			'ad_name'         => $this->request->variable('ad_name', '', true),
84 12
			'ad_note'         => $this->request->variable('ad_note', '', true),
85 12
			'ad_code'         => $this->request->variable('ad_code', '', true),
86 12
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
87 12
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
88 12
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
89 12
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
90 12
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
91 12
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
92 12
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
93 12
		);
94
95
		// Validate form key
96 12
		if (!check_form_key('phpbb_ads'))
97 12
		{
98 2
			$this->errors[] = $this->language->lang('FORM_INVALID');
99 2
		}
100
101
		// Validate each property. Every method adds errors directly to $this->errors.
102 12
		foreach ($data as $prop_name => $prop_val)
103
		{
104 12
			if (method_exists($this, 'validate_' . $prop_name))
105 12
			{
106 12
				$this->{'validate_' . $prop_name}($prop_val);
107 12
			}
108 12
		}
109
110
		// Replace end date and owner with IDs that will be stored in the DB
111 12
		$data['ad_end_date'] = $this->end_date_to_timestamp($data['ad_end_date']);
112 12
		if (!in_array('AD_OWNER_INVALID', $this->errors))
113 12
		{
114 10
			$data['ad_owner'] = $this->owner_to_id($data['ad_owner']);
115 10
		}
116
117 12
		return $data;
118
	}
119
120
	/**
121
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
122
	 *
123
	 * @param	 string	 $ad_code	 Current ad code
124
	 * @return	 string	 \phpbb\json_response when request is ajax or updated ad code otherwise.
125
	 */
126 7
	public function banner_upload($ad_code)
127
	{
128
		try
129
		{
130 7
			$this->banner->create_storage_dir();
131 4
			$realname = $this->banner->upload();
132
133 3
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
134
135 3
			if ($this->request->is_ajax())
136 3
			{
137 1
				$this->send_ajax_response(true, $banner_html);
138
			}
139
140 2
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
141
		}
142 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...
143
		{
144 4
			$this->banner->remove();
145
146 4
			if ($this->request->is_ajax())
147 4
			{
148 1
				$this->send_ajax_response(false, $this->language->lang($e->getMessage()));
149
			}
150
151 3
			$this->errors[] = $this->language->lang($e->getMessage());
152
		}
153
154 5
		return $ad_code;
155
	}
156
157
	/**
158
	 * Validate advertisement name
159
	 *
160
	 * @param string $ad_name Advertisement name
161
	 */
162 12
	protected function validate_ad_name($ad_name)
163
	{
164 12
		if ($ad_name === '')
165 12
		{
166 2
			$this->errors[] = 'AD_NAME_REQUIRED';
167 2
		}
168 12
		if (truncate_string($ad_name, self::MAX_NAME_LENGTH) !== $ad_name)
169 12
		{
170 1
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
171 1
		}
172 12
	}
173
174
	/**
175
	 * Validate advertisement end date
176
	 *
177
	 * @param string $end_date Advertisement end date
178
	 */
179 12
	protected function validate_ad_end_date($end_date)
180
	{
181 12
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
182 12
		{
183 2
			$end_date = (int) $this->end_date_to_timestamp($end_date);
184
185 2
			if ($end_date < time())
186 2
			{
187 1
				$this->errors[] = 'AD_END_DATE_INVALID';
188 1
			}
189 2
		}
190 10
		else if ($end_date !== '')
191 10
		{
192 2
			$this->errors[] = 'AD_END_DATE_INVALID';
193 2
		}
194 12
	}
195
196
	/**
197
	 * Validate advertisement priority
198
	 *
199
	 * @param int $ad_priority Advertisement priority
200
	 */
201 12
	protected function validate_ad_priority($ad_priority)
202
	{
203 12
		if ($ad_priority < 1 || $ad_priority > 10)
204 12
		{
205 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
206 3
		}
207 12
	}
208
209
	/**
210
	 * Validate advertisement views limit
211
	 *
212
	 * @param int $ad_views_limit Advertisement views limit
213
	 */
214 12
	protected function validate_ad_views_limit($ad_views_limit)
215
	{
216 12
		if ($ad_views_limit < 0)
217 12
		{
218 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
219 2
		}
220 12
	}
221
222
	/**
223
	 * Validate advertisement clicks limit
224
	 *
225
	 * @param int $ad_clicks_limit Advertisement clicks limit
226
	 */
227 12
	protected function validate_ad_clicks_limit($ad_clicks_limit)
228
	{
229 12
		if ($ad_clicks_limit < 0)
230 12
		{
231 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
232 2
		}
233 12
	}
234
235
	/**
236
	 * Validate advertisement owner
237
	 *
238
	 * @param string $ad_owner Advertisement owner
239
	 */
240 12
	protected function validate_ad_owner($ad_owner)
241
	{
242
		// user_get_id_name function returns false if everything is OK.
243 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...
244 12
		{
245 2
			$this->errors[] = 'AD_OWNER_INVALID';
246 2
		}
247 12
	}
248
249
	/**
250
	 * Convert format of end date from string to unix timestamp
251
	 *
252
	 * @param string $end_date Advertisement end date in YYYY-MM-DD format
253
	 * @return int Advertisement end date in unix timestamp
254
	 */
255 12
	protected function end_date_to_timestamp($end_date)
256
	{
257 12
		return (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $end_date);
258
	}
259
260
	/**
261
	 * Convert advertisement owner username to ID
262
	 *
263
	 * @param string $ad_owner Advertisement owner username
264
	 * @return int Advertisement owner ID
265
	 */
266 10
	protected function owner_to_id($ad_owner)
267
	{
268 10
		if (empty($ad_owner))
269 10
		{
270 9
			return 0;
271
		}
272
273 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...
274 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...
275
	}
276
277
	/**
278
	 * Send ajax response
279
	 *
280
	 * @param bool $success Is request successful?
281
	 * @param string $text Text to return
282
	 */
283 2
	protected function send_ajax_response($success, $text)
284
	{
285 2
		$json_response = new \phpbb\json_response;
286 2
		$json_response->send(array(
287 2
			'success'	=> $success,
288 2
			'title'		=> $this->language->lang('INFORMATION'),
289 2
			'text'		=> $text,
290 2
		));
291
	}
292
}
293