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

admin_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
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
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
use \phpbb\ads\controller\admin_input as input;
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
		$errors = array_map(array($this->user, 'lang'), $errors);
101
102 3
		$this->template->assign_vars(array(
103 3
			'S_ERROR'   => (bool) count($errors),
104 3
			'ERROR_MSG' => count($errors) ? implode('<br />', $errors) : '',
105 3
		));
106 3
	}
107
108
	/**
109
	 * Log action
110
	 *
111
	 * @param	string	$action		Performed action in uppercase
112
	 * @param	string	$ad_name	Advertisement name
113
	 * @return	void
114
	 */
115 1
	public function log($action, $ad_name)
116
	{
117 1
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
118 1
	}
119
120 1
	public function get_find_username_link()
121
	{
122 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');
123
	}
124
125
126
	/**
127
	 * Prepare end date for display
128
	 *
129
	 * @param	mixed	$end_date	End date.
130
	 * @return	string	End date prepared for display.
131
	 */
132 5
	protected function prepare_end_date($end_date)
133
	{
134 5
		if (empty($end_date))
135 5
		{
136 2
			return '';
137
		}
138
139 3
		if (is_numeric($end_date))
140 3
		{
141 2
			return $this->user->format_date($end_date, input::DATE_FORMAT);
142
		}
143
144 1
		return (string) $end_date;
145
	}
146
147
	/**
148
	 * Prepare ad owner for display. Method takes user_id
149
	 * of the ad owner and returns his/her username.
150
	 *
151
	 * @param	int		$ad_owner	User ID
152
	 * @return	string	Username belonging to $ad_owner.
153
	 */
154 5
	protected function prepare_ad_owner($ad_owner)
155
	{
156
		// Returns false when no errors occur trying to find the user
157 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...
158 5
		{
159 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...
160 1
			{
161
				return $ad_owner[0];
162
			}
163 1
			return $ad_owner_name[(int) $ad_owner[0]];
164
		}
165 4
		return '';
166
	}
167
}
168