Completed
Pull Request — master (#56)
by Jakub
10:57
created

admin_helper::assign_locations()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 9
Ratio 75 %

Code Coverage

Tests 10
CRAP Score 3

Importance

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