Completed
Push — master ( 435892...45f10c )
by Matt
20:55
created

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