1 | <?php |
||
16 | class admin_controller |
||
17 | { |
||
18 | const MAX_NAME_LENGTH = 255; |
||
19 | |||
20 | /** @var \phpbb\db\driver\driver_interface */ |
||
21 | protected $db; |
||
22 | |||
23 | /** @var \phpbb\template\template */ |
||
24 | protected $template; |
||
25 | |||
26 | /** @var \phpbb\user */ |
||
27 | protected $user; |
||
28 | |||
29 | /** @var \phpbb\request\request */ |
||
30 | protected $request; |
||
31 | |||
32 | /** @var string ads_table */ |
||
33 | protected $ads_table; |
||
34 | |||
35 | /** @var string php_ext */ |
||
36 | protected $php_ext; |
||
37 | |||
38 | /** @var string ext_path */ |
||
39 | protected $ext_path; |
||
40 | |||
41 | /** @var string Custom form action */ |
||
42 | protected $u_action; |
||
43 | |||
44 | /** @var array Form validation errors */ |
||
45 | protected $errors = array(); |
||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | * |
||
50 | * @param \phpbb\db\driver\driver_interface $db DB driver interface |
||
51 | * @param \phpbb\template\template $template Template object |
||
52 | * @param \phpbb\user $user User object |
||
53 | * @param \phpbb\request\request $request Request object |
||
54 | * @param string $ads_table Ads table |
||
55 | * @param string $php_ext PHP extension |
||
56 | * @param string $ext_path Path to this extension |
||
57 | */ |
||
58 | 27 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, $ads_table, $php_ext, $ext_path) |
|
68 | |||
69 | /** |
||
70 | * Process user request |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | 6 | public function main() |
|
88 | |||
89 | /** |
||
90 | * Set page url |
||
91 | * |
||
92 | * @param string $u_action Custom form action |
||
93 | * @return void |
||
94 | */ |
||
95 | 21 | public function set_page_url($u_action) |
|
99 | |||
100 | /** |
||
101 | * Get ACP page title for Ads module |
||
102 | * |
||
103 | * @return string Language string for Ads ACP module |
||
104 | */ |
||
105 | 1 | public function get_page_title() |
|
109 | |||
110 | /** |
||
111 | * Add an advertisement |
||
112 | * |
||
113 | * @return void |
||
114 | */ |
||
115 | 5 | public function action_add() |
|
116 | { |
||
117 | 5 | add_form_key('phpbb/admanagement/add'); |
|
118 | 5 | if ($this->request->is_set_post('submit')) |
|
119 | 5 | { |
|
120 | 4 | $this->check_form_key('phpbb/admanagement/add'); |
|
121 | |||
122 | 4 | $data = $this->get_form_data(); |
|
123 | |||
124 | 4 | $this->validate($data); |
|
125 | |||
126 | 4 | if (empty($this->errors)) |
|
127 | 4 | { |
|
128 | // Insert the ad data to the database |
||
129 | 1 | $sql = 'INSERT INTO ' . $this->ads_table . ' ' . $this->db->sql_build_array('INSERT', $data); |
|
130 | 1 | $this->db->sql_query($sql); |
|
131 | |||
132 | 1 | $this->success('ACP_AD_ADD_SUCCESS'); |
|
133 | } |
||
134 | |||
135 | 3 | $this->assign_form_data($data); |
|
136 | 3 | } |
|
137 | |||
138 | // Set output vars for display in the template |
||
139 | 4 | $this->template->assign_vars(array( |
|
140 | 4 | 'S_ADD_AD' => true, |
|
141 | 4 | 'U_BACK' => $this->u_action, |
|
142 | 4 | )); |
|
143 | 4 | } |
|
144 | |||
145 | /** |
||
146 | * Edit an advertisement |
||
147 | * |
||
148 | * @return void |
||
149 | */ |
||
150 | 14 | public function action_edit() |
|
151 | { |
||
152 | 7 | $ad_id = $this->request->variable('id', 0); |
|
153 | |||
154 | 7 | add_form_key('phpbb/admanagement/edit/' . $ad_id); |
|
155 | 7 | if ($this->request->is_set_post('submit')) |
|
156 | 7 | { |
|
157 | 5 | $this->check_form_key('phpbb/admanagement/edit/' . $ad_id); |
|
158 | |||
159 | 5 | $data = $this->get_form_data(); |
|
160 | |||
161 | 5 | $this->validate($data); |
|
162 | |||
163 | 5 | if (empty($this->errors)) |
|
164 | 5 | { |
|
165 | // Insert the ad data to the database |
||
166 | 2 | $sql = 'UPDATE ' . $this->ads_table . ' |
|
167 | 2 | SET ' . $this->db->sql_build_array('UPDATE', $data) . ' |
|
168 | 2 | WHERE ad_id = ' . (int) $ad_id; |
|
169 | 2 | $this->db->sql_query($sql); |
|
170 | |||
171 | 2 | if ($this->db->sql_affectedrows()) |
|
172 | 2 | { |
|
173 | 1 | $this->success('ACP_AD_EDIT_SUCCESS'); |
|
174 | } |
||
175 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
176 | } |
||
177 | 3 | } |
|
178 | else |
||
179 | { |
||
180 | $sql = 'SELECT * |
||
181 | 2 | FROM ' . $this->ads_table . ' |
|
182 | 2 | WHERE ad_id = ' . (int) $ad_id; |
|
183 | 2 | $result = $this->db->sql_query($sql); |
|
184 | 2 | $data = $this->db->sql_fetchrow($result); |
|
185 | 2 | $this->db->sql_freeresult($result); |
|
186 | |||
187 | 2 | if (empty($data)) |
|
188 | 2 | { |
|
189 | 1 | $this->error('ACP_AD_DOES_NOT_EXIST'); |
|
190 | } |
||
191 | } |
||
192 | |||
193 | // Set output vars for display in the template |
||
194 | 4 | $this->template->assign_vars(array( |
|
195 | 4 | 'S_EDIT_AD' => true, |
|
196 | 14 | 'EDIT_ID' => $ad_id, |
|
197 | 4 | 'U_BACK' => $this->u_action, |
|
198 | 4 | )); |
|
199 | 4 | $this->assign_form_data($data); |
|
200 | 4 | } |
|
201 | |||
202 | /** |
||
203 | * Enable an advertisement |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | 3 | public function action_enable() |
|
211 | |||
212 | /** |
||
213 | * Disable an advertisement |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | 3 | public function action_disable() |
|
221 | |||
222 | /** |
||
223 | * Delete an advertisement |
||
224 | * |
||
225 | * @return void |
||
226 | */ |
||
227 | 3 | public function action_delete() |
|
259 | |||
260 | /** |
||
261 | * Display the ads |
||
262 | * |
||
263 | * @return void |
||
264 | */ |
||
265 | 1 | public function list_ads() |
|
291 | |||
292 | /** |
||
293 | * Enable/disable an advertisement |
||
294 | * |
||
295 | * @param bool $enable Enable or disable the advertisement? |
||
296 | * @return void |
||
297 | */ |
||
298 | 4 | protected function ad_enable($enable) |
|
328 | |||
329 | /** |
||
330 | * Check the form key. |
||
331 | * |
||
332 | * @param string $form_name The name of the form. |
||
333 | * @return void |
||
334 | */ |
||
335 | 9 | protected function check_form_key($form_name) |
|
342 | |||
343 | /** |
||
344 | * Get admin form data. |
||
345 | * |
||
346 | * @return array Form data |
||
347 | */ |
||
348 | 9 | protected function get_form_data() |
|
357 | |||
358 | /** |
||
359 | * Validate form data. |
||
360 | * |
||
361 | * @param array $data The form data. |
||
362 | * @return void |
||
363 | */ |
||
364 | 9 | protected function validate($data) |
|
375 | |||
376 | /** |
||
377 | * Assign form data to the template. |
||
378 | * |
||
379 | * @param array $data The form data. |
||
380 | * @return void |
||
381 | */ |
||
382 | 7 | protected function assign_form_data($data) |
|
394 | |||
395 | /** |
||
396 | * Print success message. |
||
397 | * |
||
398 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
399 | */ |
||
400 | 5 | protected function success() |
|
404 | |||
405 | /** |
||
406 | * Print error message. |
||
407 | * |
||
408 | * It takes arguments in the form of a language key, followed by language substitution values. |
||
409 | */ |
||
410 | 5 | protected function error() |
|
414 | } |
||
415 |