Completed
Push — master ( a1b06d...27fa1b )
by Matt
11:42
created

admin_helper::prepare_end_date()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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