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

admin_helper::assign_locations()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
crap 3
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\controller\admin_controller as controller;
14
15
/**
16
 * Admin helper
17
 */
18
class admin_helper
19
{
20
	/** @var \phpbb\user */
21
	protected $user;
22
23
	/** @var \phpbb\template\template */
24
	protected $template;
25
26
	/** @var \phpbb\log\log */
27
	protected $log;
28
29
	/** @var \phpbb\ads\location\manager */
30
	protected $location_manager;
31
32
	/** @var string root_path */
33
	protected $root_path;
34
35
	/** @var string php_ext */
36
	protected $php_ext;
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\user						$user				User object
42
	 * @param \phpbb\template\template			$template			Template object
43
	 * @param \phpbb\log\log					$log				The phpBB log system
44
	 * @param \phpbb\ads\location\manager		$location_manager	Template location manager object
45
	 * @param string							$root_path			phpBB root path
46
	 * @param string							$php_ext			PHP extension
47
	 */
48 12
	public function __construct(\phpbb\user $user, \phpbb\template\template $template, \phpbb\log\log $log, \phpbb\ads\location\manager $location_manager, $root_path, $php_ext)
49
	{
50 12
		$this->user = $user;
51 12
		$this->template = $template;
52 12
		$this->log = $log;
53 12
		$this->location_manager = $location_manager;
54 12
		$this->root_path = $root_path;
55 12
		$this->php_ext = $php_ext;
56 12
	}
57
58
	/**
59
	 * Assign template locations data to the template.
60
	 *
61
	 * @param	mixed	$ad_locations	The form data or nothing.
62
	 * @return	void
63
	 */
64 2
	public function assign_locations($ad_locations = false)
65
	{
66 2
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
67
		{
68 2
			$this->template->assign_block_vars('ad_locations', array(
69 2
				'LOCATION_ID'   => $location_id,
70 2
				'LOCATION_DESC' => $location_data['desc'],
71 2
				'LOCATION_NAME' => $location_data['name'],
72 2
				'S_SELECTED'    => $ad_locations ? in_array($location_id, $ad_locations) : false,
73 2
			));
74 2
		}
75 2
	}
76
77
	/**
78
	 * Assign form data to the template.
79
	 *
80
	 * @param	array	$data	The form data.
81
	 * @return	void
82
	 */
83 5
	public function assign_form_data($data)
84
	{
85 5
		$this->template->assign_vars(array(
86 5
			'AD_NAME'         => $data['ad_name'],
87 5
			'AD_NOTE'         => $data['ad_note'],
88 5
			'AD_CODE'         => $data['ad_code'],
89 5
			'AD_ENABLED'      => $data['ad_enabled'],
90 5
			'AD_END_DATE'     => $this->prepare_end_date($data['ad_end_date']),
91 5
			'AD_PRIORITY'     => $data['ad_priority'],
92 5
			'AD_VIEWS_LIMIT'  => $data['ad_views_limit'],
93 5
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
94 5
			'AD_OWNER'        => $this->prepare_ad_owner($data['ad_owner']),
95 5
		));
96 5
	}
97
98 3
	public function assign_errors(array $errors)
99
	{
100 3
		$this->template->assign_vars(array(
101 3
			'S_ERROR'   => (bool) count($errors),
102 3
			'ERROR_MSG' => count($errors) ? implode('<br />', $errors) : '',
103 3
		));
104 3
	}
105
106
	/**
107
	 * Log action
108
	 *
109
	 * @param	string	$action		Performed action in uppercase
110
	 * @param	string	$ad_name	Advertisement name
111
	 * @return	void
112
	 */
113 1
	public function log($action, $ad_name)
114
	{
115 1
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
116 1
	}
117
118 1
	public function get_find_username_link()
119
	{
120 1
		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');
121
	}
122
123
124
	/**
125
	 * Prepare end date for display
126
	 *
127
	 * @param	mixed	$end_date	End date.
128
	 * @return	string	End date prepared for display.
129
	 */
130 5
	protected function prepare_end_date($end_date)
131
	{
132 5
		if (empty($end_date))
133 5
		{
134 2
			return '';
135
		}
136
137 3
		if (is_numeric($end_date))
138 3
		{
139 2
			return $this->user->format_date($end_date, controller::DATE_FORMAT);
140
		}
141
142 1
		return (string) $end_date;
143
	}
144
145
	/**
146
	 * Prepare ad owner for display. Method takes user_id
147
	 * of the ad owner and returns his/her username.
148
	 *
149
	 * @param	int		$ad_owner	User ID
150
	 * @return	string	Username belonging to $ad_owner.
151
	 */
152 5
	protected function prepare_ad_owner($ad_owner)
153
	{
154
		// Returns false when no errors occur trying to find the user
155 5
		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...
156 5
		{
157 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...
158 1
			{
159
				return $ad_owner[0];
160
			}
161 1
			return $ad_owner_name[(int) $ad_owner[0]];
162
		}
163 4
		return '';
164
	}
165
}
166