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) |
|
|
|
|
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
|
|
|
/** |
132
|
|
|
* Assign groups data to the template. |
133
|
|
|
* |
134
|
|
|
* @param int $ad_id Advertisement ID |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
6 |
|
public function assign_groups($ad_id = 0) |
138
|
|
|
{ |
139
|
6 |
|
$groups = $this->manager->load_groups($ad_id); |
140
|
6 |
|
foreach ($groups as $group) |
141
|
|
|
{ |
142
|
1 |
|
$this->template->assign_block_vars('groups', array( |
143
|
1 |
|
'ID' => $group['group_id'], |
144
|
1 |
|
'NAME' => $this->group_helper->get_name($group['group_name']), |
145
|
1 |
|
'S_SELECTED' => (bool) $group['group_selected'], |
146
|
1 |
|
)); |
147
|
6 |
|
} |
148
|
6 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Log action |
152
|
|
|
* |
153
|
|
|
* @param string $action Performed action in uppercase |
154
|
|
|
* @param string $ad_name Advertisement name |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
1 |
|
public function log($action, $ad_name) |
158
|
|
|
{ |
159
|
1 |
|
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'ACP_PHPBB_ADS_' . $action . '_LOG', time(), array($ad_name)); |
160
|
1 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get "Find username" URL to easily look for ad owner. |
164
|
|
|
* |
165
|
|
|
* @return string Find username URL |
166
|
|
|
*/ |
167
|
1 |
|
public function get_find_username_link() |
168
|
|
|
{ |
169
|
1 |
|
return append_sid("{$this->root_path}memberlist.{$this->php_ext}", 'mode=searchuser&form=acp_admanagement_add&field=ad_owner&select_single=true'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Is an ad expired? |
174
|
|
|
* |
175
|
|
|
* @param array $row Advertisement data |
176
|
|
|
* @return bool True if expired, false otherwise |
177
|
|
|
*/ |
178
|
7 |
|
public function is_expired($row) |
179
|
|
|
{ |
180
|
7 |
|
if ((int) $row['ad_end_date'] > 0 && (int) $row['ad_end_date'] < time()) |
181
|
7 |
|
{ |
182
|
1 |
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
6 |
|
if ($row['ad_views_limit'] && $row['ad_views'] >= $row['ad_views_limit']) |
186
|
6 |
|
{ |
187
|
1 |
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
5 |
|
if ($row['ad_clicks_limit'] && $row['ad_clicks'] >= $row['ad_clicks_limit']) |
191
|
5 |
|
{ |
192
|
1 |
|
return true; |
193
|
|
|
} |
194
|
|
|
|
195
|
4 |
|
return false; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Prepare ad owner for display. Method takes user_id |
200
|
|
|
* of the ad owner and returns username. |
201
|
|
|
* |
202
|
|
|
* @param int $user_id User ID |
203
|
|
|
* @return string Username belonging to $user_id. |
204
|
|
|
*/ |
205
|
5 |
|
protected function get_username($user_id) |
206
|
|
|
{ |
207
|
5 |
|
if (!$user_id) |
208
|
5 |
|
{ |
209
|
2 |
|
return ''; |
210
|
|
|
} |
211
|
|
|
|
212
|
3 |
|
$this->user_loader->load_users(array($user_id)); |
213
|
3 |
|
return $this->user_loader->get_username($user_id, 'username'); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
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.