Completed
Pull Request — master (#54)
by Jakub
09:22
created

admin_input::validate_ad_end_date()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 14
cts 14
cp 1
rs 9.2
cc 4
eloc 7
nc 4
nop 1
crap 4
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\request\request */
26
	protected $request;
27
28
	/** @var \phpbb\ads\banner\banner */
29
	protected $banner;
30
31
	/** @var array Form validation errors */
32
	protected $errors = array();
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\user								$user			User object
38
	 * @param \phpbb\request\request					$request		Request object
39
	 * @param \phpbb\ads\banner\banner					$banner			Banner upload object
40
	 */
41 16
	public function __construct(\phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\banner\banner $banner)
42
	{
43 16
		$this->user = $user;
44 16
		$this->request = $request;
45 16
		$this->banner = $banner;
46 16
	}
47
48
	/**
49
	 * Gets all errors
50
	 *
51
	 * @return	array	Errors
52
	 */
53 13
	public function get_errors()
54
	{
55 13
		return $this->errors;
56
	}
57
58
	/**
59
	 * Returns number of errors.
60
	 *
61
	 * @return	int	Number of errors
62
	 */
63 13
	public function has_errors()
64
	{
65 13
		return count($this->errors);
66
	}
67
68
	/**
69
	 * Get admin form data.
70
	 *
71
	 * @param	string	$form_name	The form name.
72
	 * @return	array	Form data
73
	 */
74 11
	public function get_form_data($form_name)
75
	{
76
		$data = array(
77 11
			'ad_name'         => $this->request->variable('ad_name', '', true),
78 11
			'ad_note'         => $this->request->variable('ad_note', '', true),
79 11
			'ad_code'         => $this->request->variable('ad_code', '', true),
80 11
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
81 11
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
82 11
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
83 11
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
84 11
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
85 11
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
86 11
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
87 11
		);
88
89
		// Validate form key
90 11
		if (!check_form_key($form_name))
91 11
		{
92 2
			$this->errors[] = $this->user->lang('FORM_INVALID');
93 2
		}
94
95
		// Validate each property. Every method adds errors directly to $this->errors.
96 11
		foreach ($data as $prop_name => $prop_val)
97
		{
98 11
			if (method_exists($this, 'validate_' . $prop_name))
99 11
			{
100 11
				$this->{'validate_' . $prop_name}($prop_val);
101 11
			}
102 11
		}
103
104
		// Replace end date and owner with IDs that will be stored in the DB
105 11
		$data['ad_end_date'] = $this->end_date_to_timestamp($data['ad_end_date']);
106 11
		if (!in_array('AD_OWNER_INVALID', $this->errors))
107 11
		{
108 9
			$data['ad_owner'] = $this->owner_to_id($data['ad_owner']);
109 9
		}
110
111 11
		return $data;
112
	}
113
114
	/**
115
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
116
	 *
117
	 * @param	 string	 $ad_code	 Current ad code
118
	 * @return	 mixed	 \phpbb\json_response when request is ajax or updated ad code otherwise.
119
	 */
120 5
	public function banner_upload($ad_code)
121
	{
122
		try
123
		{
124 5
			$this->banner->create_storage_dir();
125 3
			$realname = $this->banner->upload();
126
127 2
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
128
129 2
			if ($this->request->is_ajax())
130 2
			{
131
				$this->send_ajax_response(true, $banner_html);
132
			}
133
134 2
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
135
		}
136 5
		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...
137
		{
138 3
			$this->banner->remove();
139
140 3
			if ($this->request->is_ajax())
141 3
			{
142
				$this->send_ajax_response(false, $this->user->lang($e->getMessage()));
143
			}
144
145 3
			$this->errors[] = $this->user->lang($e->getMessage());
146
		}
147
148 5
		return $ad_code;
149
	}
150
151 11
	protected function validate_ad_name($ad_name)
152
	{
153 11
		if ($ad_name === '')
154 11
		{
155 2
			$this->errors[] = 'AD_NAME_REQUIRED';
156 2
		}
157 11
		if (truncate_string($ad_name, self::MAX_NAME_LENGTH) !== $ad_name)
158 11
		{
159
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
160
		}
161 11
	}
162
163 11
	protected function validate_ad_end_date($end_date)
164
	{
165 11
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
166 11
		{
167 2
			$end_date = (int) $this->end_date_to_timestamp($end_date);
168
169 2
			if ($end_date < time())
170 2
			{
171 1
				$this->errors[] = 'AD_END_DATE_INVALID';
172 1
			}
173 2
		}
174 9
		else if ($end_date !== '')
175 9
		{
176 2
			$this->errors[] = 'AD_END_DATE_INVALID';
177 2
		}
178 11
	}
179
180 11
	protected function validate_ad_priority($ad_priority)
181
	{
182 11
		if ($ad_priority < 1 || $ad_priority > 10)
183 11
		{
184 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
185 3
		}
186 11
	}
187
188 11
	protected function validate_ad_views_limit($ad_views_limit)
189
	{
190 11
		if ($ad_views_limit < 0)
191 11
		{
192 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
193 2
		}
194 11
	}
195
196 11
	protected function validate_ad_clicks_limit($ad_clicks_limit)
197
	{
198 11
		if ($ad_clicks_limit < 0)
199 11
		{
200 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
201 2
		}
202 11
	}
203
204 11
	protected function validate_ad_owner($ad_owner)
205
	{
206
		// user_get_id_name function returns false if everything is OK.
207 11
		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...
208 11
		{
209 2
			$this->errors[] = 'AD_OWNER_INVALID';
210 2
		}
211 11
	}
212
213 11
	protected function end_date_to_timestamp($end_date)
214
	{
215 11
		return (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $end_date);
216
	}
217
218 9
	protected function owner_to_id($ad_owner)
219
	{
220 9
		if (empty($ad_owner))
221 9
		{
222 8
			return 0;
223
		}
224
225 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...
226 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...
227
	}
228
229
	protected function send_ajax_response($success, $text)
230
	{
231
		$json_response = new \phpbb\json_response;
232
		$json_response->send(array(
233
			'success'	=> $success,
234
			'title'		=> $this->user->lang('INFORMATION'),
235
			'text'		=> $text,
236
		));
237
	}
238
}
239