Completed
Push — master ( de1386...435892 )
by Matt
12s
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.0116

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2.0116
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 16
	 * @param \phpbb\ads\location\manager $location_manager Template location manager object
51
	 * @param string                      $root_path        phpBB root path
52 16
	 * @param string                      $php_ext          PHP extension
53 16
	 */
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 16
	{
56 16
		$this->user = $user;
57 16
		$this->user_loader = $user_loader;
58 16
		$this->language = $language;
59 16
		$this->template = $template;
60
		$this->log = $log;
61
		$this->location_manager = $location_manager;
62
		$this->root_path = $root_path;
63
		$this->php_ext = $php_ext;
64
	}
65
66
	/**
67 5
	 * Assign ad data for ACP form template.
68
	 *
69 5
	 * @param	array	$data	Ad data
70
	 * @param	array	$errors	Validation errors
71 5
	 */
72 5
	public function assign_data($data, $errors)
73 5
	{
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 5
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
			'AD_VIEWS_LIMIT'  => $data['ad_views_limit'],
88
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
89
			'AD_OWNER'        => $this->get_username($data['ad_owner']),
90
		));
91
	}
92
93
	/**
94 7
	 * Assign template locations data to the template.
95
	 *
96 7
	 * @param	mixed	$ad_locations	The form data or nothing.
97
	 * @return	void
98 2
	 */
99 2
	public function assign_locations($ad_locations = false)
100 2
	{
101 2 View Code Duplication
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
102 2
		{
103 2
			$this->template->assign_block_vars('ad_locations', array(
104 7
				'LOCATION_ID'   => $location_id,
105 7
				'LOCATION_DESC' => $location_data['desc'],
106
				'LOCATION_NAME' => $location_data['name'],
107
				'S_SELECTED'    => $ad_locations ? in_array($location_id, $ad_locations) : false,
108
			));
109
		}
110
	}
111
112
	/**
113
	 * Log action
114 1
	 *
115
	 * @param	string	$action		Performed action in uppercase
116 1
	 * @param	string	$ad_name	Advertisement name
117 1
	 * @return	void
118
	 */
119
	public function log($action, $ad_name)
120
	{
121
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
122
	}
123
124 1
	/**
125
	 * Get "Find username" URL to easily look for ad owner.
126 1
	 *
127
	 * @return	string	Find username URL
128
	 */
129
	public function get_find_username_link()
130
	{
131
		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 7
	 * Is an ad expired?
136
	 *
137 7
	 * @param	array	$row	Advertisement data
138 7
	 * @return	bool	True if expired, false otherwise
139 1
	 */
140
	public function is_expired($row)
141
	{
142 6
		if ((int) $row['ad_end_date'] > 0 && (int) $row['ad_end_date'] < time())
143 6
		{
144 1
			return true;
145
		}
146
147 5
		if ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit'])
148 5
		{
149 1
			return true;
150
		}
151
152 4
		if ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit'])
153
		{
154
			return true;
155
		}
156
157
		return false;
158
	}
159
160
	/**
161
	 * Prepare ad owner for display. Method takes user_id
162 5
	 * of the ad owner and returns username.
163
	 *
164 5
	 * @param	int		$user_id	User ID
165 5
	 * @return	string	Username belonging to $user_id.
166
	 */
167
	protected function get_username($user_id)
168 5
	{
169 5
		if (!$user_id)
170 1
		{
171 1
			return '';
172
		}
173
174 1
		$this->user_loader->load_users(array($user_id));
175
		return $this->user_loader->get_username($user_id, 'username');
176 4
	}
177
}
178