Completed
Pull Request — master (#89)
by Matt
10:44
created

admin_input::owner_to_id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
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
use phpbb\ads\ext;
14
15
/**
16
 * Admin input
17
 */
18
class admin_input
19
{
20
	/** @var \phpbb\user */
21
	protected $user;
22
23
	/** @var \phpbb\user_loader */
24
	protected $user_loader;
25
26
	/** @var \phpbb\language\language */
27
	protected $language;
28
29
	/** @var \phpbb\request\request */
30
	protected $request;
31
32
	/** @var \phpbb\ads\banner\banner */
33
	protected $banner;
34
35
	/** @var array Form validation errors */
36
	protected $errors = array();
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\user              $user        User object
42
	 * @param \phpbb\user_loader       $user_loader User loader object
43
	 * @param \phpbb\language\language $language    Language object
44
	 * @param \phpbb\request\request   $request     Request object
45
	 * @param \phpbb\ads\banner\banner $banner      Banner upload object
46
	 */
47 19
	public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\banner\banner $banner)
48
	{
49 19
		$this->user = $user;
50 19
		$this->user_loader = $user_loader;
51 19
		$this->language = $language;
52 19
		$this->request = $request;
53 19
		$this->banner = $banner;
54
55 19
		add_form_key('phpbb_ads');
56 19
	}
57
58
	/**
59
	 * Gets all errors
60
	 *
61
	 * @return	array	Errors
62
	 */
63 14
	public function get_errors()
64
	{
65 14
		return $this->errors;
66
	}
67
68
	/**
69
	 * Returns number of errors.
70
	 *
71
	 * @return	int	Number of errors
72
	 */
73 14
	public function has_errors()
74
	{
75 14
		return count($this->errors);
76
	}
77
78
	/**
79
	 * Get admin form data.
80
	 *
81
	 * @return	array	Form data
82
	 */
83 12
	public function get_form_data()
84
	{
85
		$data = array(
86 12
			'ad_name'         => $this->request->variable('ad_name', '', true),
87 12
			'ad_note'         => $this->request->variable('ad_note', '', true),
88 12
			'ad_code'         => $this->request->variable('ad_code', '', true),
89 12
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
90 12
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
91 12
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
92 12
			'ad_priority'     => $this->request->variable('ad_priority', ext::DEFAULT_PRIORITY),
93 12
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
94 12
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
95 12
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
96 12
		);
97
98
		// Validate form key
99 12
		if (!check_form_key('phpbb_ads'))
100 12
		{
101 2
			$this->errors[] = $this->language->lang('FORM_INVALID');
102 2
		}
103
104
		// Validate each property. Some validators update the property value. Errors are added to $this->errors.
105 12
		foreach ($data as $prop_name => &$prop_val)
106
		{
107 12
			if (method_exists($this, 'validate_' . $prop_name))
108 12
			{
109 12
				$prop_val = $this->{'validate_' . $prop_name}($prop_val);
110 12
			}
111 12
		}
112
113 12
		unset($prop_val);
114
115 12
		return $data;
116
	}
117
118
	/**
119
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
120
	 *
121
	 * @param	 string	 $ad_code	 Current ad code
122
	 * @return	 string	 \phpbb\json_response when request is ajax or updated ad code otherwise.
123
	 */
124 7
	public function banner_upload($ad_code)
125
	{
126
		try
127
		{
128 7
			$this->banner->create_storage_dir();
129 4
			$realname = $this->banner->upload();
130
131 3
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
132
133 3
			if ($this->request->is_ajax())
134 3
			{
135 1
				$this->send_ajax_response(true, $banner_html);
136
			}
137
138 2
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
139
		}
140 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...
141
		{
142 4
			$this->banner->remove();
143
144 4
			if ($this->request->is_ajax())
145 4
			{
146 1
				$this->send_ajax_response(false, $this->language->lang($e->getMessage()));
147
			}
148
149 3
			$this->errors[] = $this->language->lang($e->getMessage());
150
		}
151
152 5
		return $ad_code;
153
	}
154
155
	/**
156
	 * Validate advertisement name
157
	 *
158
	 * @param string $ad_name Advertisement name
159
	 * @return string Advertisement name
160
	 */
161 12
	protected function validate_ad_name($ad_name)
162
	{
163 12
		if ($ad_name === '')
164 12
		{
165 2
			$this->errors[] = 'AD_NAME_REQUIRED';
166 2
		}
167
168 12
		if (truncate_string($ad_name, ext::MAX_NAME_LENGTH) !== $ad_name)
169 12
		{
170 1
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', ext::MAX_NAME_LENGTH);
171 1
		}
172
173 12
		return $ad_name;
174
	}
175
176
	/**
177
	 * Validate advertisement end date
178
	 *
179
	 * @param string $end_date Advertisement end date
180
	 * @return int The end date converted to timestamp if valid, otherwise 0.
181
	 */
182 12
	protected function validate_ad_end_date($end_date)
183
	{
184 12
		$timestamp = 0;
185 12
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
186 12
		{
187 2
			$timestamp = (int) $this->user->get_timestamp_from_format(ext::DATE_FORMAT, $end_date);
188
189 2
			if ($timestamp < time())
190 2
			{
191 1
				$this->errors[] = 'AD_END_DATE_INVALID';
192 1
			}
193 2
		}
194 10
		else if ($end_date !== '')
195 10
		{
196 2
			$this->errors[] = 'AD_END_DATE_INVALID';
197 2
		}
198
199 12
		return $timestamp;
200
	}
201
202
	/**
203
	 * Validate advertisement priority
204
	 *
205
	 * @param int $ad_priority Advertisement priority
206
	 * @return int Advertisement priority
207
	 */
208 12
	protected function validate_ad_priority($ad_priority)
209
	{
210 12
		if ($ad_priority < 1 || $ad_priority > 10)
211 12
		{
212 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
213 3
		}
214
215 12
		return $ad_priority;
216
	}
217
218
	/**
219
	 * Validate advertisement views limit
220
	 *
221
	 * @param int $ad_views_limit Advertisement views limit
222
	 * @return int Advertisement views limit
223
	 */
224 12
	protected function validate_ad_views_limit($ad_views_limit)
225
	{
226 12
		if ($ad_views_limit < 0)
227 12
		{
228 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
229 2
		}
230
231 12
		return $ad_views_limit;
232
	}
233
234
	/**
235
	 * Validate advertisement clicks limit
236
	 *
237
	 * @param int $ad_clicks_limit Advertisement clicks limit
238
	 * @return int Advertisement clicks limit
239
	 */
240 12
	protected function validate_ad_clicks_limit($ad_clicks_limit)
241
	{
242 12
		if ($ad_clicks_limit < 0)
243 12
		{
244 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
245 2
		}
246
247 12
		return $ad_clicks_limit;
248
	}
249
250
	/**
251
	 * Validate advertisement owner
252
	 *
253
	 * @param string $ad_owner User name
254
	 * @return int User id if user exists, otherwise 0.
255
	 */
256 12
	protected function validate_ad_owner($ad_owner)
257
	{
258 12
		if (!empty($ad_owner) && ANONYMOUS === ($ad_owner = $this->user_loader->load_user_by_username($ad_owner)))
259 12
		{
260 2
			$this->errors[] = 'AD_OWNER_INVALID';
261 2
		}
262
263 12
		return ANONYMOUS === $ad_owner ? 0 : (int) $ad_owner;
264
	}
265
266
	/**
267
	 * Send ajax response
268
	 *
269
	 * @param bool $success Is request successful?
270
	 * @param string $text Text to return
271
	 */
272 2
	protected function send_ajax_response($success, $text)
273
	{
274 2
		$json_response = new \phpbb\json_response;
275 2
		$json_response->send(array(
276 2
			'success'	=> $success,
277 2
			'title'		=> $this->language->lang('INFORMATION'),
278 2
			'text'		=> $text,
279 2
		));
280
	}
281
}
282