Completed
Pull Request — master (#54)
by Jakub
07:53
created

admin_helper::assign_locations()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
crap 12
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_controller as controller;
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
	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
		$this->user = $user;
51
		$this->template = $template;
52
		$this->log = $log;
53
		$this->location_manager = $location_manager;
54
		$this->root_path = $root_path;
55
		$this->php_ext = $php_ext;
56
	}
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
	public function assign_locations($ad_locations = false)
65
	{
66
		foreach ($this->location_manager->get_all_locations() as $location_id => $location_data)
67
		{
68
			$this->template->assign_block_vars('ad_locations', array(
69
				'LOCATION_ID'   => $location_id,
70
				'LOCATION_DESC' => $location_data['desc'],
71
				'LOCATION_NAME' => $location_data['name'],
72
				'S_SELECTED'    => $ad_locations ? in_array($location_id, $ad_locations) : false,
73
			));
74
		}
75
	}
76
77
	/**
78
	 * Assign form data to the template.
79
	 *
80
	 * @param	array	$data	The form data.
81
	 * @return	void
82
	 */
83
	public function assign_form_data($data)
84
	{
85
		$this->template->assign_vars(array(
86
			'AD_NAME'         => $data['ad_name'],
87
			'AD_NOTE'         => $data['ad_note'],
88
			'AD_CODE'         => $data['ad_code'],
89
			'AD_ENABLED'      => $data['ad_enabled'],
90
			'AD_END_DATE'     => $this->prepare_end_date($data['ad_end_date']),
91
			'AD_PRIORITY'     => $data['ad_priority'],
92
			'AD_VIEWS_LIMIT'  => $data['ad_views_limit'],
93
			'AD_CLICKS_LIMIT' => $data['ad_clicks_limit'],
94
			'AD_OWNER'        => $this->prepare_ad_owner($data['ad_owner']),
95
		));
96
	}
97
98
	public function assign_errors(array $errors)
99
	{
100
		$this->template->assign_vars(array(
101
			'S_ERROR'   => (bool) count($errors),
102
			'ERROR_MSG' => count($errors) ? implode('<br />', $errors) : '',
103
		));
104
	}
105
106
	/**
107
	 * Log action
108
	 *
109
	 * @param	string	$action		Performed action in uppercase
110
	 * @param	string	$ad_name	Advertisement name
111
	 * @return	void
112
	 */
113
	public function log($action, $ad_name)
114
	{
115
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name));
116
	}
117
118
	public function get_find_username_link()
119
	{
120
		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');
121
	}
122
123
124
	/**
125
	 * Prepare end date for display
126
	 *
127
	 * @param	mixed	$end_date	End date.
128
	 * @return	string	End date prepared for display.
129
	 */
130
	protected function prepare_end_date($end_date)
131
	{
132
		if (empty($end_date))
133
		{
134
			return '';
135
		}
136
137
		if (is_numeric($end_date))
138
		{
139
			return $this->user->format_date($end_date, controller::DATE_FORMAT);
140
		}
141
142
		return (string) $end_date;
143
	}
144
145
	/**
146
	 * Prepare ad owner for display. Method takes user_id
147
	 * of the ad owner and returns his/her username.
148
	 *
149
	 * @param	int		$ad_owner	User ID
150
	 * @return	string	Username belonging to $ad_owner.
151
	 */
152
	protected function prepare_ad_owner($ad_owner)
153
	{
154
		// Returns false when no errors occur trying to find the user
155
		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...
156
		{
157
			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...
158
			{
159
				return $ad_owner[0];
160
			}
161
			return $ad_owner_name[(int) $ad_owner[0]];
162
		}
163
		return '';
164
	}
165
}
166