Completed
Pull Request — master (#52)
by Jakub
09:16
created

admin_controller::action_edit()   C

Complexity

Conditions 9
Paths 7

Size

Total Lines 62
Code Lines 34

Duplication

Lines 20
Ratio 32.26 %

Code Coverage

Tests 38
CRAP Score 9.2055

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 20
loc 62
ccs 38
cts 44
cp 0.8636
rs 6.6867
cc 9
eloc 34
nc 7
nop 0
crap 9.2055

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 controller
15
*/
16
class admin_controller
17
{
18
	const MAX_NAME_LENGTH = 255;
19
	const DATE_FORMAT = 'Y-m-d';
20
	const DEFAULT_PRIORITY = 5;
21
22
	/** @var \phpbb\template\template */
23
	protected $template;
24
25
	/** @var \phpbb\user */
26
	protected $user;
27
28
	/** @var \phpbb\request\request */
29
	protected $request;
30
31
	/** @var \phpbb\ads\ad\manager */
32
	protected $manager;
33
34
	/** @var \phpbb\ads\location\manager */
35
	protected $location_manager;
36
37
	/** @var \phpbb\log\log */
38
	protected $log;
39
40
	/** @var \phpbb\config\db_text */
41
	protected $config_text;
42
43
	/** @var \phpbb\config\config */
44
	protected $config;
45
46
	/** @var \phpbb\files\upload */
47
	protected $files_upload;
48
49
	/** @var string root_path */
50
	protected $root_path;
51
52
	/** @var string php_ext */
53
	protected $php_ext;
54
55
	/** @var string ext_path */
56
	protected $ext_path;
57
58
	/** @var string Custom form action */
59
	protected $u_action;
60
61
	/** @var array Form validation errors */
62
	protected $errors = array();
63
64
	/**
65
	* Constructor
66
	*
67
	* @param \phpbb\template\template		$template			Template object
68
	* @param \phpbb\user					$user				User object
69
	* @param \phpbb\request\request			$request			Request object
70
	* @param \phpbb\ads\ad\manager			$manager			Advertisement manager object
71
	* @param \phpbb\ads\location\manager	$location_manager	Template location manager object
72
	* @param \phpbb\log\log					$log				The phpBB log system
73
	* @param \phpbb\config\db_text			$config_text		Config text object
74
	* @param \phpbb\config\config			$config				Config object
75
	* @param \phpbb\files\upload			$files_upload		Files upload object
76
	* @param string                      	$root_path			phpBB root path
77
	* @param string							$php_ext			PHP extension
78
	* @param string							$ext_path			Path to this extension
79
	*/
80 48
	public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\log\log $log, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\files\upload $files_upload, $root_path, $php_ext, $ext_path)
81
	{
82 48
		$this->template = $template;
83 48
		$this->user = $user;
84 48
		$this->request = $request;
85 48
		$this->manager = $manager;
86 48
		$this->location_manager = $location_manager;
87 48
		$this->log = $log;
88 48
		$this->config_text = $config_text;
89 48
		$this->config = $config;
90 48
		$this->files_upload = $files_upload;
91 48
		$this->root_path = $root_path;
92 48
		$this->php_ext = $php_ext;
93 48
		$this->ext_path = $ext_path;
94 48
	}
95
96
	/**
97
	 * Process user request for manage mode
98
	 *
99
	 * @return void
100
	 */
101 6
	public function mode_manage()
102
	{
103 6
		$this->setup();
104
105 6
		if (!function_exists('user_get_id_name'))
106 6
		{
107
			include($this->root_path . 'includes/functions_user.' . $this->php_ext);
108
		}
109
110
		// Trigger specific action
111 6
		$action = $this->request->variable('action', '');
112 6
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
113 6
		{
114 5
			$this->{'action_' . $action}();
115 5
		}
116
117
		// Otherwise default to this
118 6
		$this->list_ads();
119 6
	}
120
121
	/**
122
	 * Process user request for settings mode
123
	 *
124
	 * @return void
125
	 */
126 3
	public function mode_settings()
127
	{
128 3
		$this->setup();
129
130 3
		add_form_key('phpbb/ads/settings');
131 3
		if ($this->request->is_set_post('submit'))
132 3
		{
133
			// Validate form key
134 2
			if (!check_form_key('phpbb/ads/settings'))
135 2
			{
136 1
				$this->errors[] = $this->user->lang('FORM_INVALID');
137 1
			}
138
139 2
			if (empty($this->errors))
140 2
			{
141 1
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
142 1
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
143 1
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
144 1
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
145
146 1
				$this->success('ACP_AD_SETTINGS_SAVED');
147
			}
148
149 1
			$this->template->assign_vars(array(
150 1
				'S_ERROR'   => (bool) count($this->errors),
151 1
				'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '',
152 1
			));
153 1
		}
154
155 2
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
156 2
		$groups = $this->manager->load_groups();
157 2
		foreach ($groups as $group)
158
		{
159 2
			$group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name'];
160
161 2
			$this->template->assign_block_vars('groups', array(
162 2
				'ID'         => $group['group_id'],
163 2
				'NAME'       => $group_name,
164 2
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
165 2
			));
166 2
		}
167
168 2
		$this->template->assign_vars(array(
169 2
			'U_ACTION'          => $this->u_action,
170 2
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
171 2
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
172 2
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
173 2
		));
174 2
	}
175
176
	/**
177
	 * Set page url
178
	 *
179
	 * @param string $u_action Custom form action
180
	 * @return void
181
	 */
182 42
	public function set_page_url($u_action)
183
	{
184 42
		$this->u_action = $u_action;
185 42
	}
186
187
	/**
188
	 * Get ACP page title for Ads module
189
	 *
190
	 * @return string    Language string for Ads ACP module
191
	 */
192 1
	public function get_page_title()
193
	{
194 1
		return $this->user->lang('ACP_PHPBB_ADS_TITLE');
195
	}
196
197
	/**
198
	 * Add an advertisement
199
	 *
200
	 * @return void
201
	 */
202 15
	public function action_add()
203
	{
204 15
		$preview = $this->request->is_set_post('preview');
205 15
		$submit = $this->request->is_set_post('submit');
206 15
		$upload_banner = $this->request->is_set_post('upload_banner');
207
208 15
		add_form_key('phpbb/ads/add');
209 15
		if ($preview || $submit || $upload_banner)
210 15
		{
211 14
			$data = $this->get_form_data('phpbb/ads/add');
212
213 View Code Duplication
			if ($preview)
214 14
			{
215 1
				$this->ad_preview($data['ad_code']);
216 1
			}
217
			else if ($upload_banner)
218 13
			{
219 2
				$data['ad_code'] = $this->process_banner_upload($data['ad_code']);
220 2
			}
221
			else if (empty($this->errors))
222 11
			{
223 1
				$ad_id = $this->manager->insert_ad($data);
224 1
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
225
226 1
				$this->log('ADD', $data['ad_name']);
227
228 1
				$this->success('ACP_AD_ADD_SUCCESS');
229
			}
230
231 13
			$this->assign_locations($data);
232 13
			$this->assign_form_data($data);
233 13
		}
234
		else
235
		{
236 1
			$this->assign_locations();
237
		}
238
239
		// Set output vars for display in the template
240 14
		$this->template->assign_vars(array(
241 14
			'S_ADD_AD'           => true,
242 14
			'U_BACK'             => $this->u_action,
243 14
			'U_ACTION'           => "{$this->u_action}&amp;action=add",
244 14
			'PICKER_DATE_FORMAT' => self::DATE_FORMAT,
245 14
			'U_FIND_USERNAME'    => $this->get_find_username_link(),
246 14
		));
247 14
	}
248
249
	/**
250
	 * Edit an advertisement
251
	 *
252
	 * @return void
253
	 */
254 15
	public function action_edit()
255
	{
256 15
		$ad_id = $this->request->variable('id', 0);
257 15
		$preview = $this->request->is_set_post('preview');
258 15
		$submit = $this->request->is_set_post('submit');
259 15
		$upload_banner = $this->request->is_set_post('upload_banner');
260
261 15
		add_form_key('phpbb/ads/edit/' . $ad_id);
262 15
		if ($preview || $submit || $upload_banner)
263 15
		{
264 13
			$data = $this->get_form_data('phpbb/ads/edit/' . $ad_id);
265
266
			if ($preview)
267 13
			{
268 1
				$this->ad_preview($data['ad_code']);
269 1
			}
270 View Code Duplication
			else if ($upload_banner)
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
271 12
			{
272
				$data['ad_code'] = $this->process_banner_upload($data['ad_code']);
273
			}
274
			else if (empty($this->errors))
275 12
			{
276 2
				$success = $this->manager->update_ad($ad_id, $data);
277
278
				if ($success)
279 2
				{
280
					// Only insert new ad locations to DB when ad exists
281 1
					$this->manager->delete_ad_locations($ad_id);
282 1
					$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
283
284 1
					$this->log('EDIT', $data['ad_name']);
285
286 1
					$this->success('ACP_AD_EDIT_SUCCESS');
287
				}
288
289 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
290
			}
291 11
		}
292
		else
293
		{
294 2
			$data = $this->manager->get_ad($ad_id);
295 2
			if (empty($data))
296 2
			{
297 1
				$this->error('ACP_AD_DOES_NOT_EXIST');
298
			}
299
300
			// Load ad template locations
301 1
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
302
		}
303
304
		// Set output vars for display in the template
305 12
		$this->template->assign_vars(array(
306 12
			'S_EDIT_AD'          => true,
307 12
			'EDIT_ID'            => $ad_id,
308 12
			'U_BACK'             => $this->u_action,
309 12
			'U_ACTION'           => "{$this->u_action}&amp;action=edit&amp;id=" . $ad_id,
310 12
			'PICKER_DATE_FORMAT' => self::DATE_FORMAT,
311 12
			'U_FIND_USERNAME'    => $this->get_find_username_link(),
312 12
		));
313 12
		$this->assign_locations($data);
314 12
		$this->assign_form_data($data);
315 12
	}
316
317
	/**
318
	 * Enable an advertisement
319
	 *
320
	 * @return void
321
	 */
322 3
	public function action_enable()
323
	{
324 3
		$this->ad_enable(true);
325 1
	}
326
327
	/**
328
	 * Disable an advertisement
329
	 *
330
	 * @return void
331
	 */
332 3
	public function action_disable()
333
	{
334 3
		$this->ad_enable(false);
335 1
	}
336
337
	/**
338
	 * Delete an advertisement
339
	 *
340
	 * @return void
341
	 */
342 3
	public function action_delete()
343
	{
344 3
		$ad_id = $this->request->variable('id', 0);
345
		if ($ad_id)
346 3
		{
347 3
			if (confirm_box(true))
348 3
			{
349
				// Get ad data so that we can log ad name
350 2
				$ad_data = $this->manager->get_ad($ad_id);
351
352
				// Delete ad and it's template locations
353 2
				$this->manager->delete_ad_locations($ad_id);
354 2
				$success = $this->manager->delete_ad($ad_id);
355
356
				// Only notify user on error or if not ajax
357 2
				if (!$success)
358 2
				{
359 1
					$this->error('ACP_AD_DELETE_ERRORED');
360
				}
361
				else
362
				{
363 1
					$this->log('DELETE', $ad_data['ad_name']);
364
365 1
					if (!$this->request->is_ajax())
366 1
					{
367 1
						$this->success('ACP_AD_DELETE_SUCCESS');
368
					}
369
				}
370
			}
371
			else
372
			{
373 1
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
374 1
					'id'     => $ad_id,
375 1
					'i'      => $this->request->variable('i', ''),
376 1
					'mode'   => $this->request->variable('mode', ''),
377
					'action' => 'delete'
378 1
				)));
379
			}
380 1
		}
381 1
	}
382
383
	/**
384
	 * Display the ads
385
	 *
386
	 * @return void
387
	 */
388 1
	public function list_ads()
389
	{
390 1
		foreach ($this->manager->get_all_ads() as $row)
391
		{
392 1
			$ad_enabled = (int) $row['ad_enabled'];
393 1
			$ad_end_date = (int) $row['ad_end_date'];
394 1
			$ad_expired = $ad_end_date > 0 && $ad_end_date < time();
395 1
			if ($ad_expired && $ad_enabled)
396 1
			{
397 1
				$ad_enabled = 0;
398 1
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
399 1
			}
400
401 1
			$this->template->assign_block_vars('ads', array(
402 1
				'NAME'               => $row['ad_name'],
403 1
				'END_DATE'           => $ad_end_date ? $this->user->format_date($ad_end_date, self::DATE_FORMAT) : '',
404 1
				'VIEWS'              => $row['ad_views'],
405 1
				'CLICKS'             => $row['ad_clicks'],
406 1
				'VIEWS_LIMIT'        => $row['ad_views_limit'],
407 1
				'CLICKS_LIMIT'       => $row['ad_clicks_limit'],
408 1
				'S_END_DATE_EXPIRED' => $ad_expired,
409 1
				'S_ENABLED'          => $ad_enabled,
410 1
				'U_ENABLE'           => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
411 1
				'U_EDIT'             => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
412 1
				'U_DELETE'           => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
413 1
			));
414 1
		}
415
416
		// Set output vars for display in the template
417 1
		$this->template->assign_vars(array(
418 1
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
419 1
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
420 1
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
421 1
		));
422 1
	}
423
424
	/**
425
	 * Perform general tasks
426
	 *
427
	 * @return void
428
	 */
429 9
	protected function setup()
430
	{
431 9
		$this->user->add_lang('posting'); // Used by process_banner_upload() file errors
432 9
		$this->user->add_lang_ext('phpbb/ads', 'acp');
433
434 9
		$this->template->assign_var('S_PHPBB_ADS', true);
435 9
	}
436
437
	/**
438
	 * Enable/disable an advertisement
439
	 *
440
	 * @param    bool $enable Enable or disable the advertisement?
441
	 * @return void
442
	 */
443 4
	protected function ad_enable($enable)
444
	{
445 4
		$ad_id = $this->request->variable('id', 0);
446
447 4
		$success = $this->manager->update_ad($ad_id, array(
448 4
			'ad_enabled' => (int) $enable,
449 4
		));
450
451
		// If AJAX was used, show user a result message
452 4
		if ($this->request->is_ajax())
453 4
		{
454
			$json_response = new \phpbb\json_response;
455
			$json_response->send(array(
456
				'text'  => $this->user->lang($enable ? 'ENABLED' : 'DISABLED'),
457
				'title' => $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
458
			));
459
		}
460
461
		// Otherwise, show traditional infobox
462
		if ($success)
463 4
		{
464 2
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
465
		}
466
		else
467
		{
468 2
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
469
		}
470
	}
471
472
	/**
473
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
474
	 *
475
	 * @param	 string	 $ad_code	 Current ad code
476
	 * @return	 mixed	 \phpbb\json_response when request is ajax or updated ad code otherwise.
477
	 */
478 2
	protected function process_banner_upload($ad_code)
479
	{
480
		// Set file restrictions
481 2
		$this->files_upload->reset_vars();
482 2
		$this->files_upload->set_allowed_extensions(array('gif', 'jpg', 'jpeg', 'png'));
483
484
		// Upload file
485 2
		$file = $this->files_upload->handle_upload('files.types.form', 'banner');
486 2
		$file->clean_filename('unique_ext');
487 2
		if (!$file->move_file('images/phpbb_ads'))
488 2
		{
489 2
			$file->set_error($this->user->lang('NO_UPLOAD_DIRECTORY'));
490 2
		}
491 2
		if (!$file->is_image())
492 2
		{
493 2
			$file->set_error($this->user->lang('NOT_IMAGE'));
494 2
		}
495
496
		// Problem with uploading
497 2
		if (count($file->error))
498 2
		{
499 1
			$file->remove();
500 1
			if ($this->request->is_ajax())
501 1
			{
502
				$json_response = new \phpbb\json_response;
503
				$json_response->send(array(
504
					'success'	=> false,
505
					'title'		=> $this->user->lang('INFORMATION'),
506
					'text'		=> implode(', ', $file->error),
507
				));
508
			}
509
			else
510
			{
511 1
				$this->errors[] = implode(', ', $file->error);
512
			}
513 1
		}
514
		else
515
		{
516 1
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $file->get('realname') . '" />';
517
518 1
			if ($this->request->is_ajax())
519 1
			{
520
				$json_response = new \phpbb\json_response;
521
				$json_response->send(array(
522
					'success'	=> true,
523
					'text'		=> $banner_html,
524
				));
525
			}
526
527 1
			return ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
528
		}
529
530 1
		return $ad_code;
531
	}
532
533
	/**
534
	* Get admin form data.
535
	*
536
	* @param	string	$form_name	The form name.
537
	* @return	array	Form data
538
	*/
539 27
	protected function get_form_data($form_name)
540
	{
541
		$data = array(
542 27
			'ad_name'         => $this->request->variable('ad_name', '', true),
543 27
			'ad_note'         => $this->request->variable('ad_note', '', true),
544 27
			'ad_code'         => $this->request->variable('ad_code', '', true),
545 27
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
546 27
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
547 27
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
548 27
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
549 27
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
550 27
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
551 27
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
552 27
		);
553
554
		// Validate form key
555 27
		if (!check_form_key($form_name))
556 27
		{
557 2
			$this->errors[] = $this->user->lang('FORM_INVALID');
558 2
		}
559
560
		// Validate ad name
561 27
		if ($data['ad_name'] === '')
562 27
		{
563 2
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
564 2
		}
565 27
		if (truncate_string($data['ad_name'], self::MAX_NAME_LENGTH) !== $data['ad_name'])
566 27
		{
567 2
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
568 2
		}
569
570
		// Validate ad end date
571 27
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $data['ad_end_date']))
572 27
		{
573 4
			$data['ad_end_date'] = (int) $this->user->get_timestamp_from_format(self::DATE_FORMAT, $data['ad_end_date']);
574
575 4
			if ($data['ad_end_date'] < time())
576 4
			{
577 2
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
578 2
			}
579 4
		}
580 23
		else if ($data['ad_end_date'] !== '')
581 23
		{
582 2
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
583 2
		}
584
		else
585
		{
586 21
			$data['ad_end_date'] = 0;
587
		}
588
589
		// Validate ad priority
590 27
		if ($data['ad_priority'] < 1 || $data['ad_priority'] > 10)
591 27
		{
592 6
			$this->errors[] = $this->user->lang('AD_PRIORITY_INVALID');
593 6
		}
594
595
		// Validate ad views limit
596 27
		if ($data['ad_views_limit'] < 0)
597 27
		{
598 2
			$this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID');
599 2
		}
600
601
		// Validate ad clicks limit
602 27
		if ($data['ad_clicks_limit'] < 0)
603 27
		{
604 2
			$this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID');
605 2
		}
606
607
		// Validate ad owner. Username in $data['ad_owner'] will be replaced with user_id.
608 27
		if (!empty($data['ad_owner']))
609 27
		{
610
			// Function returns false if everything is OK.
611 4
			if (user_get_id_name($ad_owner_id, $data['ad_owner']))
612 4
			{
613 2
				$this->errors[] = $this->user->lang('AD_OWNER_INVALID');
614 2
			}
615
			else
616
			{
617 2
				$data['ad_owner'] = $ad_owner_id[0];
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
618 1
			}
619 4
		}
620
		else
621
		{
622 23
			$data['ad_owner'] = 0;
623
		}
624
625 27
		return $data;
626
	}
627
628
	/**
629
	 * Assign form data to the template.
630
	 *
631
	 * @param    array $data The form data.
632
	 * @return void
633
	 */
634 25
	protected function assign_form_data($data)
635
	{
636 25
		$this->template->assign_vars(array(
637 25
			'S_ERROR'   => (bool) count($this->errors),
638 25
			'ERROR_MSG' => count($this->errors) ? implode('<br />', $this->errors) : '',
639
640 25
			'AD_NAME'         => $data['ad_name'],
641 25
			'AD_NOTE'         => $data['ad_note'],
642 25
			'AD_CODE'         => $data['ad_code'],
643 25
			'AD_ENABLED'      => $data['ad_enabled'],
644 25
			'AD_END_DATE'     => $this->prepare_end_date($data['ad_end_date']),
645 25
			'AD_PRIORITY'     => $data['ad_priority'],
646 25
			'AD_VIEWS_LIMIT'  => $data['ad_views_limit'],
647 25
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
648 25
			'AD_OWNER'        => $this->prepare_ad_owner($data['ad_owner']),
649 25
		));
650 25
	}
651
652
	/**
653
	 * Prepare end date for display
654
	 *
655
	 * @param    mixed $end_date End date.
656
	 * @return    string    End date prepared for display.
657
	 */
658 25
	protected function prepare_end_date($end_date)
659
	{
660 25
		if (empty($end_date))
661 25
		{
662 20
			return '';
663
		}
664
665 5
		if (is_numeric($end_date))
666 5
		{
667 3
			return $this->user->format_date($end_date, self::DATE_FORMAT);
668
		}
669
670 2
		return (string) $end_date;
671
	}
672
673
	/**
674
	 * Prepare ad owner for display. Method takes user_id
675
	 * of the ad owner and returns his/her username.
676
	 *
677
	 * @param	int		$ad_owner	User ID
678
	 * @return	string	Username belonging to $ad_owner.
679
	 */
680 25
	protected function prepare_ad_owner($ad_owner)
681
	{
682
		// Returns false when no errors occur trying to find the user
683 25
		if (false === user_get_id_name($ad_owner, $ad_owner_name))
0 ignored issues
show
Bug introduced by
The variable $ad_owner_name 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...
684 25
		{
685 1
			if (empty($ad_owner_name))
0 ignored issues
show
Bug introduced by
The variable $ad_owner_name 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...
686 1
			{
687
				return $ad_owner[0];
688
			}
689 1
			return $ad_owner_name[(int) $ad_owner[0]];
690
		}
691 24
		return '';
692
	}
693
	/**
694
	 * Assign template locations data to the template.
695
	 *
696
	 * @param    mixed $data The form data or nothing.
697
	 * @return    void
698
	 */
699 26
	protected function assign_locations($data = false)
700
	{
701 26
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
702
		{
703 26
			$this->template->assign_block_vars('ad_locations', array(
704 26
				'LOCATION_ID'   => $location_id,
705 26
				'LOCATION_DESC' => $location_data['desc'],
706 26
				'LOCATION_NAME' => $location_data['name'],
707 26
				'S_SELECTED'    => $data ? in_array($location_id, $data['ad_locations']) : false,
708 26
			));
709 26
		}
710 26
	}
711
712
	/**
713
	 * Prepare advertisement preview
714
	 *
715
	 * @param    string $code Ad code to preview
716
	 * @return    void
717
	 */
718 2
	protected function ad_preview($code)
719
	{
720 2
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
721 2
	}
722
723
	/**
724
	 * Print success message.
725
	 *
726
	 * It takes arguments in the form of a language key, followed by language substitution values.
727
	 */
728 6
	protected function success()
729
	{
730 6
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
731
	}
732
733
	/**
734
	 * Print error message.
735
	 *
736
	 * It takes arguments in the form of a language key, followed by language substitution values.
737
	 */
738 5
	protected function error()
739
	{
740 5
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
741
	}
742
743
	/**
744
	 * Log action
745
	 *
746
	 * @param    string $action  Performed action in uppercase
747
	 * @param    string $ad_name Advertisement name
748
	 * @return    void
749
	 */
750 3
	protected function log($action, $ad_name)
751
	{
752 3
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
753 3
	}
754
755 26
	protected function get_find_username_link()
756
	{
757 26
		return append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=searchuser&amp;form=acp_admanagement_add&amp;field=ad_owner&amp;select_single=true');
758
	}
759
}
760