Completed
Pull Request — master (#123)
by Jakub
36:02
created

helper::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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
 * Helper
15
 */
16
class 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\ad\manager */
34
	protected $manager;
35
36
	/** @var \phpbb\ads\location\manager */
37
	protected $location_manager;
38
39
	/** @var \phpbb\group\helper */
40
	protected $group_helper;
41
42
	/** @var string root_path */
43
	protected $root_path;
44
45
	/** @var string php_ext */
46
	protected $php_ext;
47
48
	/**
49
	 * Constructor
50
	 *
51
	 * @param \phpbb\user                 $user             User object
52
	 * @param \phpbb\user_loader          $user_loader      User loader object
53
	 * @param \phpbb\language\language    $language         Language object
54
	 * @param \phpbb\template\template    $template         Template object
55
	 * @param \phpbb\log\log              $log              The phpBB log system
56
	 * @param \phpbb\ads\ad\manager 	  $manager 			Ad manager object
57
	 * @param \phpbb\ads\location\manager $location_manager Template location manager object
58
	 * @param \phpbb\group\helper         $group_helper     Group helper object
59
	 * @param string                      $root_path        phpBB root path
60
	 * @param string                      $php_ext          PHP extension
61
	 */
62 17 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\ad\manager $manager, \phpbb\ads\location\manager $location_manager, \phpbb\group\helper $group_helper, $root_path, $php_ext)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
63
	{
64 17
		$this->user = $user;
65 17
		$this->user_loader = $user_loader;
66 17
		$this->language = $language;
67 17
		$this->template = $template;
68 17
		$this->log = $log;
69 17
		$this->location_manager = $location_manager;
70 17
		$this->manager = $manager;
71 17
		$this->group_helper = $group_helper;
72 17
		$this->root_path = $root_path;
73 17
		$this->php_ext = $php_ext;
74 17
	}
75
76
	/**
77
	 * Assign ad data for ACP form template.
78
	 *
79
	 * @param	array	$data	Ad data
80
	 * @param	array	$errors	Validation errors
81
	 */
82 5
	public function assign_data($data, $errors)
83
	{
84 5
		$this->assign_locations($data['ad_locations']);
85 5
		$this->assign_groups(isset($data['ad_id']) ? $data['ad_id'] : 0);
86
87 5
		$errors = array_map(array($this->language, 'lang'), $errors);
88 5
		$this->template->assign_vars(array(
89 5
			'S_ERROR'   => (bool) count($errors),
90 5
			'ERROR_MSG' => count($errors) ? implode('<br />', $errors) : '',
91
92 5
			'AD_NAME'         	=> $data['ad_name'],
93 5
			'AD_NOTE'         	=> $data['ad_note'],
94 5
			'AD_CODE'         	=> $data['ad_code'],
95 5
			'AD_ENABLED'      	=> $data['ad_enabled'],
96 5
			'AD_END_DATE'     	=> $data['ad_end_date'],
97 5
			'AD_PRIORITY'     	=> $data['ad_priority'],
98 5
			'AD_CONTENT_ONLY'	=> $data['ad_content_only'],
99 5
			'AD_VIEWS_LIMIT'  	=> $data['ad_views_limit'],
100 5
			'AD_CLICKS_LIMIT' 	=> $data['ad_clicks_limit'],
101 5
			'AD_OWNER'        	=> $this->get_username($data['ad_owner']),
102 5
		));
103 5
	}
104
105
	/**
106
	 * Assign template locations data to the template.
107
	 *
108
	 * @param	mixed	$ad_locations	The form data or nothing.
109
	 * @return	void
110
	 */
111 7
	public function assign_locations($ad_locations = false)
112
	{
113 7
		foreach ($this->location_manager->get_all_locations() as $location_category_id => $location_category)
114
		{
115 2
			$this->template->assign_block_vars('ad_locations', array(
116 2
				'CATEGORY_NAME' => $this->language->lang($location_category_id),
117 2
			));
118
119 2
			foreach ($location_category as $location_id => $location_data)
120
			{
121 2
				$this->template->assign_block_vars('ad_locations', array(
122 2
					'LOCATION_ID'   => $location_id,
123 2
					'LOCATION_DESC' => $location_data['desc'],
124 2
					'LOCATION_NAME' => $location_data['name'],
125 2
					'S_SELECTED'    => $ad_locations ? in_array($location_id, $ad_locations) : false,
126 2
				));
127 2
			}
128 7
		}
129 7
	}
130
131 6
	public function assign_groups($ad_id = 0)
132
	{
133 6
		$groups = $this->manager->load_groups($ad_id);
134 6
		foreach ($groups as $group)
135
		{
136 1
			$this->template->assign_block_vars('groups', array(
137 1
				'ID'         => $group['group_id'],
138 1
				'NAME'       => $this->group_helper->get_name($group['group_name']),
139 1
				'S_SELECTED' => (bool) $group['group_selected'],
140 1
			));
141 6
		}
142 6
	}
143
144
	/**
145
	 * Log action
146
	 *
147
	 * @param	string	$action		Performed action in uppercase
148
	 * @param	string	$ad_name	Advertisement name
149
	 * @return	void
150
	 */
151 1
	public function log($action, $ad_name)
152
	{
153 1
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
154 1
	}
155
156
	/**
157
	 * Get "Find username" URL to easily look for ad owner.
158
	 *
159
	 * @return	string	Find username URL
160
	 */
161 1
	public function get_find_username_link()
162
	{
163 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');
164
	}
165
166
	/**
167
	 * Is an ad expired?
168
	 *
169
	 * @param	array	$row	Advertisement data
170
	 * @return	bool	True if expired, false otherwise
171
	 */
172 7
	public function is_expired($row)
173
	{
174 7
		if ((int) $row['ad_end_date'] > 0 && (int) $row['ad_end_date'] < time())
175 7
		{
176 1
			return true;
177
		}
178
179 6
		if ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit'])
180 6
		{
181 1
			return true;
182
		}
183
184 5
		if ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit'])
185 5
		{
186 1
			return true;
187
		}
188
189 4
		return false;
190
	}
191
192
	/**
193
	 * Prepare ad owner for display. Method takes user_id
194
	 * of the ad owner and returns username.
195
	 *
196
	 * @param	int		$user_id	User ID
197
	 * @return	string	Username belonging to $user_id.
198
	 */
199 5
	protected function get_username($user_id)
200
	{
201 5
		if (!$user_id)
202 5
		{
203 2
			return '';
204
		}
205
206 3
		$this->user_loader->load_users(array($user_id));
207 3
		return $this->user_loader->get_username($user_id, 'username');
208
	}
209
}
210