Completed
Pull Request — master (#54)
by Jakub
08:52
created

admin_controller::get_form_data()   F

Complexity

Conditions 13
Paths 768

Size

Total Lines 88
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 59
CRAP Score 13

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 88
ccs 59
cts 59
cp 1
rs 2.4575
cc 13
eloc 40
nc 768
nop 1
crap 13

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
* Admin controller
15
*/
16
class admin_controller
17
{
18
	const DATE_FORMAT = 'Y-m-d';
19
20
	/** @var \phpbb\template\template */
21
	protected $template;
22
23
	/** @var \phpbb\user */
24
	protected $user;
25
26
	/** @var \phpbb\request\request */
27
	protected $request;
28
29
	/** @var \phpbb\ads\ad\manager */
30
	protected $manager;
31
32
	/** @var \phpbb\config\db_text */
33
	protected $config_text;
34
35
	/** @var \phpbb\config\config */
36
	protected $config;
37
38
	/** @var \phpbb\ads\controller\admin_input */
39
	protected $input;
40
41
	/** @var \phpbb\ads\controller\admin_helper */
42
	protected $helper;
43
44
	/** @var string Custom form action */
45
	protected $u_action;
46
47
	/**
48
	* Constructor
49
	*
50
	* @param \phpbb\template\template		    $template		  Template object
51
	* @param \phpbb\user                        $user             User object
52
	* @param \phpbb\request\request             $request          Request object
53
	* @param \phpbb\ads\ad\manager              $manager          Advertisement manager object
54
	* @param \phpbb\config\db_text              $config_text      Config text object
55
	* @param \phpbb\config\config               $config           Config object
56
	* @param \phpbb\ads\controller\admin_input 	$input			  Admin input object
57
	* @param \phpbb\ads\controller\admin_helper $helper			  Admin helper object
58
	*/
59 View Code Duplication
	public function __construct(\phpbb\template\template $template, \phpbb\user $user, \phpbb\request\request $request, \phpbb\ads\ad\manager $manager, \phpbb\config\db_text $config_text, \phpbb\config\config $config, \phpbb\ads\controller\admin_input $input, \phpbb\ads\controller\admin_helper $helper)
0 ignored issues
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...
60
	{
61
		$this->template = $template;
62
		$this->user = $user;
63
		$this->request = $request;
64
		$this->manager = $manager;
65
		$this->config_text = $config_text;
66
		$this->config = $config;
67
		$this->input = $input;
68
		$this->helper = $helper;
69
	}
70
71
	/**
72
	 * Process user request for manage mode
73
	 *
74
	 * @return void
75
	 */
76
	public function mode_manage()
77
	{
78
		$this->setup();
79
		$this->input->setup();
80
81
		// Trigger specific action
82
		$action = $this->request->variable('action', '');
83
		if (in_array($action, array('add', 'edit', 'enable', 'disable', 'delete')))
84
		{
85
			$this->{'action_' . $action}();
86
		}
87
88
		// Otherwise default to this
89
		$this->list_ads();
90
	}
91
92
	/**
93
	 * Process user request for settings mode
94
	 *
95
	 * @return void
96
	 */
97
	public function mode_settings()
98
	{
99
		$this->setup();
100
101
		add_form_key('phpbb/ads/settings');
102
		if ($this->request->is_set_post('submit'))
103
		{
104
			// Validate form key
105
			if (check_form_key('phpbb/ads/settings'))
106
			{
107
				$this->config->set('phpbb_ads_adblocker_message', $this->request->variable('adblocker_message', 0));
108
				$this->config->set('phpbb_ads_enable_views', $this->request->variable('enable_views', 0));
109
				$this->config->set('phpbb_ads_enable_clicks', $this->request->variable('enable_clicks', 0));
110
				$this->config_text->set('phpbb_ads_hide_groups', json_encode($this->request->variable('hide_groups', array(0))));
111
112
				$this->success('ACP_AD_SETTINGS_SAVED');
113
			}
114
115
			$this->helper->assign_errors(array($this->user->lang('FORM_INVALID')));
116
		}
117
118
		$hide_groups = json_decode($this->config_text->get('phpbb_ads_hide_groups'), true);
119
		$groups = $this->manager->load_groups();
120
		foreach ($groups as $group)
121
		{
122
			$group_name = ($group['group_type'] == GROUP_SPECIAL) ? $this->user->lang('G_' . $group['group_name']) : $group['group_name'];
123
124
			$this->template->assign_block_vars('groups', array(
125
				'ID'         => $group['group_id'],
126
				'NAME'       => $group_name,
127
				'S_SELECTED' => in_array($group['group_id'], $hide_groups),
128
			));
129
		}
130
131
		$this->template->assign_vars(array(
132
			'U_ACTION'          => $this->u_action,
133
			'ADBLOCKER_MESSAGE' => $this->config['phpbb_ads_adblocker_message'],
134
			'ENABLE_VIEWS'      => $this->config['phpbb_ads_enable_views'],
135
			'ENABLE_CLICKS'     => $this->config['phpbb_ads_enable_clicks'],
136
		));
137
	}
138
139
	/**
140
	 * Set page url
141
	 *
142
	 * @param string $u_action Custom form action
143
	 * @return void
144
	 */
145
	public function set_page_url($u_action)
146
	{
147
		$this->u_action = $u_action;
148
	}
149
150
	/**
151
	 * Get ACP page title for Ads module
152
	 *
153
	 * @return string    Language string for Ads ACP module
154
	 */
155
	public function get_page_title()
156
	{
157
		return $this->user->lang('ACP_PHPBB_ADS_TITLE');
158
	}
159
160
	/**
161
	 * Add an advertisement
162
	 *
163
	 * @return void
164
	 */
165
	public function action_add()
166
	{
167
		$preview = $this->request->is_set_post('preview');
168
		$submit = $this->request->is_set_post('submit');
169
		$upload_banner = $this->request->is_set_post('upload_banner');
170
171
		add_form_key('phpbb/ads/add');
172
		if ($preview || $submit || $upload_banner)
173
		{
174
			$data = $this->input->get_form_data('phpbb/ads/add');
175
176 View Code Duplication
			if ($preview)
177
			{
178
				$this->ad_preview($data['ad_code']);
179
			}
180
			else if ($upload_banner)
181
			{
182
				$data['ad_code'] = $this->input->process_banner_upload($data['ad_code']);
183
			}
184
			else if (empty($this->input->get_errors()))
185
			{
186
				$ad_id = $this->manager->insert_ad($data);
187
				$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
188
189
				$this->helper->log('ADD', $data['ad_name']);
190
191
				$this->success('ACP_AD_ADD_SUCCESS');
192
			}
193
194
			$this->helper->assign_locations($data['ad_locations']);
195
			$this->helper->assign_form_data($data);
196
			$this->helper->assign_errors($this->input->get_errors());
197
		}
198
		else
199
		{
200
			$this->helper->assign_locations();
201
		}
202
203
		// Set output vars for display in the template
204
		$this->template->assign_vars(array(
205
			'S_ADD_AD'           => true,
206
			'U_BACK'             => $this->u_action,
207
			'U_ACTION'           => "{$this->u_action}&amp;action=add",
208
			'PICKER_DATE_FORMAT' => self::DATE_FORMAT,
209
			'U_FIND_USERNAME'    => $this->helper->get_find_username_link(),
210
		));
211
	}
212
213
	/**
214
	 * Edit an advertisement
215
	 *
216
	 * @return void
217
	 */
218
	public function action_edit()
219
	{
220
		$ad_id = $this->request->variable('id', 0);
221
		$preview = $this->request->is_set_post('preview');
222
		$submit = $this->request->is_set_post('submit');
223
		$upload_banner = $this->request->is_set_post('upload_banner');
224
225
		add_form_key('phpbb/ads/edit/' . $ad_id);
226
		if ($preview || $submit || $upload_banner)
227
		{
228
			$data = $this->input->get_form_data('phpbb/ads/edit/' . $ad_id);
229
230
			if ($preview)
231
			{
232
				$this->ad_preview($data['ad_code']);
233
			}
234 View Code Duplication
			else if ($upload_banner)
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...
235
			{
236
				$data['ad_code'] = $this->input->process_banner_upload($data['ad_code']);
237
			}
238
			else if (empty($this->input->get_errors()))
239
			{
240
				$success = $this->manager->update_ad($ad_id, $data);
241
242
				if ($success)
243
				{
244
					// Only insert new ad locations to DB when ad exists
245
					$this->manager->delete_ad_locations($ad_id);
246
					$this->manager->insert_ad_locations($ad_id, $data['ad_locations']);
247
248
					$this->helper->log('EDIT', $data['ad_name']);
249
250
					$this->success('ACP_AD_EDIT_SUCCESS');
251
				}
252
253
				$this->error('ACP_AD_DOES_NOT_EXIST');
254
			}
255
		}
256
		else
257
		{
258
			$data = $this->manager->get_ad($ad_id);
259
			if (empty($data))
260
			{
261
				$this->error('ACP_AD_DOES_NOT_EXIST');
262
			}
263
264
			// Load ad template locations
265
			$data['ad_locations'] = $this->manager->get_ad_locations($ad_id);
266
		}
267
268
		// Set output vars for display in the template
269
		$this->template->assign_vars(array(
270
			'S_EDIT_AD'          => true,
271
			'EDIT_ID'            => $ad_id,
272
			'U_BACK'             => $this->u_action,
273
			'U_ACTION'           => "{$this->u_action}&amp;action=edit&amp;id=" . $ad_id,
274
			'PICKER_DATE_FORMAT' => self::DATE_FORMAT,
275
			'U_FIND_USERNAME'    => $this->helper->get_find_username_link(),
276
		));
277
		$this->helper->assign_locations($data['ad_locations']);
278
		$this->helper->assign_form_data($data);
279
		$this->helper->assign_errors($this->input->get_errors());
280
	}
281
282
	/**
283
	 * Enable an advertisement
284
	 *
285
	 * @return void
286
	 */
287
	public function action_enable()
288
	{
289
		$this->ad_enable(true);
290
	}
291
292
	/**
293
	 * Disable an advertisement
294
	 *
295
	 * @return void
296
	 */
297
	public function action_disable()
298
	{
299
		$this->ad_enable(false);
300
	}
301
302
	/**
303
	 * Delete an advertisement
304
	 *
305
	 * @return void
306
	 */
307
	public function action_delete()
308
	{
309
		$ad_id = $this->request->variable('id', 0);
310
		if ($ad_id)
311
		{
312
			if (confirm_box(true))
313
			{
314
				// Get ad data so that we can log ad name
315
				$ad_data = $this->manager->get_ad($ad_id);
316
317
				// Delete ad and it's template locations
318
				$this->manager->delete_ad_locations($ad_id);
319
				$success = $this->manager->delete_ad($ad_id);
320
321
				// Only notify user on error or if not ajax
322
				if (!$success)
323
				{
324
					$this->error('ACP_AD_DELETE_ERRORED');
325
				}
326
				else
327
				{
328
					$this->helper->log('DELETE', $ad_data['ad_name']);
329
330
					if (!$this->request->is_ajax())
331
					{
332
						$this->success('ACP_AD_DELETE_SUCCESS');
333
					}
334
				}
335
			}
336
			else
337
			{
338
				confirm_box(false, $this->user->lang('CONFIRM_OPERATION'), build_hidden_fields(array(
339
					'id'     => $ad_id,
340
					'i'      => $this->request->variable('i', ''),
341
					'mode'   => $this->request->variable('mode', ''),
342
					'action' => 'delete'
343
				)));
344
			}
345
		}
346
	}
347
348
	/**
349
	 * Display the ads
350
	 *
351
	 * @return void
352
	 */
353
	public function list_ads()
354
	{
355
		foreach ($this->manager->get_all_ads() as $row)
356
		{
357
			$ad_enabled = (int) $row['ad_enabled'];
358
			$ad_end_date = (int) $row['ad_end_date'];
359
			$ad_expired = $ad_end_date > 0 && $ad_end_date < time();
360
			if ($ad_expired && $ad_enabled)
361
			{
362
				$ad_enabled = 0;
363
				$this->manager->update_ad($row['ad_id'], array('ad_enabled' => 0));
364
			}
365
366
			$this->template->assign_block_vars('ads', array(
367
				'NAME'               => $row['ad_name'],
368
				'END_DATE'           => $ad_end_date ? $this->user->format_date($ad_end_date, self::DATE_FORMAT) : '',
369
				'VIEWS'              => $row['ad_views'],
370
				'CLICKS'             => $row['ad_clicks'],
371
				'VIEWS_LIMIT'        => $row['ad_views_limit'],
372
				'CLICKS_LIMIT'       => $row['ad_clicks_limit'],
373
				'S_END_DATE_EXPIRED' => $ad_expired,
374
				'S_ENABLED'          => $ad_enabled,
375
				'U_ENABLE'           => $this->u_action . '&amp;action=' . ($ad_enabled ? 'disable' : 'enable') . '&amp;id=' . $row['ad_id'],
376
				'U_EDIT'             => $this->u_action . '&amp;action=edit&amp;id=' . $row['ad_id'],
377
				'U_DELETE'           => $this->u_action . '&amp;action=delete&amp;id=' . $row['ad_id'],
378
			));
379
		}
380
381
		// Set output vars for display in the template
382
		$this->template->assign_vars(array(
383
			'U_ACTION_ADD'     => $this->u_action . '&amp;action=add',
384
			'S_VIEWS_ENABLED'  => $this->config['phpbb_ads_enable_views'],
385
			'S_CLICKS_ENABLED' => $this->config['phpbb_ads_enable_clicks'],
386
		));
387
	}
388
389
	/**
390
	 * Perform general tasks
391
	 *
392
	 * @return void
393
	 */
394
	protected function setup()
395
	{
396
		$this->user->add_lang('posting'); // Used by process_banner_upload() file errors
397
		$this->user->add_lang_ext('phpbb/ads', 'acp');
398
399
		$this->template->assign_var('S_PHPBB_ADS', true);
400
	}
401
402
	/**
403
	 * Enable/disable an advertisement
404
	 *
405
	 * @param    bool $enable Enable or disable the advertisement?
406
	 * @return void
407
	 */
408
	protected function ad_enable($enable)
409
	{
410
		$ad_id = $this->request->variable('id', 0);
411
412
		$success = $this->manager->update_ad($ad_id, array(
413
			'ad_enabled' => (int) $enable,
414
		));
415
416
		// If AJAX was used, show user a result message
417
		if ($this->request->is_ajax())
418
		{
419
			$json_response = new \phpbb\json_response;
420
			$json_response->send(array(
421
				'text'  => $this->user->lang($enable ? 'ENABLED' : 'DISABLED'),
422
				'title' => $this->user->lang('AD_ENABLE_TITLE', (int) $enable),
423
			));
424
		}
425
426
		// Otherwise, show traditional infobox
427
		if ($success)
428
		{
429
			$this->success($enable ? 'ACP_AD_ENABLE_SUCCESS' : 'ACP_AD_DISABLE_SUCCESS');
430
		}
431
		else
432
		{
433
			$this->error($enable ? 'ACP_AD_ENABLE_ERRORED' : 'ACP_AD_DISABLE_ERRORED');
434
		}
435
	}
436
437
	/**
438
	 * Prepare advertisement preview
439
	 *
440
	 * @param    string $code Ad code to preview
441
	 * @return    void
442
	 */
443
	protected function ad_preview($code)
444
	{
445
		$this->template->assign_var('PREVIEW', htmlspecialchars_decode($code));
446
	}
447
448
	/**
449
	 * Print success message.
450
	 *
451
	 * It takes arguments in the form of a language key, followed by language substitution values.
452
	 */
453
	protected function success()
454
	{
455
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action));
456
	}
457
458
	/**
459
	 * Print error message.
460
	 *
461
	 * It takes arguments in the form of a language key, followed by language substitution values.
462
	 */
463
	protected function error()
464
	{
465
		trigger_error(call_user_func_array(array($this->user, 'lang'), func_get_args()) . adm_back_link($this->u_action), E_USER_WARNING);
466
	}
467
}
468