Completed
Push — master ( 915db5...cf22f5 )
by Mario
10s
created

admin_donation_pages_controller::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 16
rs 9.4285
cc 1
eloc 13
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller;
12
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * @property ContainerInterface       container         The phpBB log system
17
 * @property string                   id_prefix_name    Prefix name for identifier in the URL
18
 * @property string                   lang_key_prefix   Prefix for the messages thrown by exceptions
19
 * @property \phpbb\log\log           log               The phpBB log system.
20
 * @property string                   module_name       Name of the module currently used
21
 * @property bool                     preview           State of preview $_POST variable
22
 * @property \phpbb\request\request   request           Request object.
23
 * @property bool                     submit            State of submit $_POST variable
24
 * @property \phpbb\template\template template          Template object
25
 * @property \phpbb\user              user              User object.
26
 */
27
class admin_donation_pages_controller extends admin_main
28
{
29
	protected $phpbb_root_path;
30
	protected $php_ext;
31
	protected $ppde_operator;
32
	protected $lang_local_name;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param ContainerInterface                    $container                    Service container interface
38
	 * @param \phpbb\log\log                        $log                          The phpBB log system
39
	 * @param \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages Operator object
40
	 * @param \phpbb\request\request                $request                      Request object
41
	 * @param \phpbb\template\template              $template                     Template object
42
	 * @param \phpbb\user                           $user                         User object
43
	 * @param string                                $phpbb_root_path              phpBB root path
44
	 * @param string                                $php_ext                      phpEx
45
	 *
46
	 * @access public
47
	 */
48
	public function __construct(ContainerInterface $container, \phpbb\log\log $log, \skouat\ppde\operators\donation_pages $ppde_operator_donation_pages, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, $phpbb_root_path, $php_ext)
49
	{
50
		$this->container = $container;
51
		$this->log = $log;
52
		$this->ppde_operator = $ppde_operator_donation_pages;
53
		$this->request = $request;
54
		$this->template = $template;
55
		$this->user = $user;
56
		$this->phpbb_root_path = $phpbb_root_path;
57
		$this->php_ext = $php_ext;
58
		parent::__construct(
59
			'donation_pages',
60
			'PPDE_DP',
61
			'page'
62
		);
63
	}
64
65
	/**
66
	 * Display the pages
67
	 *
68
	 * @return null
69
	 * @access public
70
	 */
71
	public function display_donation_pages()
72
	{
73
		// Get list of available language packs
74
		$langs = $this->ppde_operator->get_languages();
75
76
		// Initiate an entity
77
		/** @type \skouat\ppde\entity\donation_pages $entity */
78
		$entity = $this->get_container_entity();
79
80
		// Set output vars
81
		foreach ($langs as $lang => $entry)
82
		{
83
			$this->assign_langs_template_vars($entry);
84
85
			// Grab all the pages from the db
86
			$data_ary = $entity->get_data($this->ppde_operator->build_sql_data($entry['id']));
87
88
			foreach ($data_ary as $data)
89
			{
90
				// Do not treat the item whether language identifier does not match
91
				if ($data['page_lang_id'] != $entry['id'])
92
				{
93
					continue;
94
				}
95
96
				$this->template->assign_block_vars('ppde_langs.dp_list', array(
97
					'DONATION_PAGE_TITLE' => $this->user->lang[strtoupper($data['page_title'])],
98
					'DONATION_PAGE_LANG'  => (string) $lang,
99
					'U_DELETE'            => $this->u_action . '&amp;action=delete&amp;' . $this->id_prefix_name . '_id=' . $data['page_id'],
100
					'U_EDIT'              => $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $data['page_id'],
101
				));
102
			}
103
			unset($data_ary, $data);
104
		}
105
		unset($entry, $langs, $lang);
106
107
		$this->u_action_assign_template_vars();
108
	}
109
110
	/**
111
	 * Assign language template vars to a block vars
112
	 * $s_select is for build options select menu
113
	 *
114
	 * @param array   $lang
115
	 * @param integer $current
116
	 *
117
	 * @return null
118
	 * @access private
119
	 */
120
	private function assign_langs_template_vars($lang, $current = 0)
121
	{
122
		$this->template->assign_block_vars('ppde_langs', array(
123
			'LANG_LOCAL_NAME' => $lang['name'],
124
			'VALUE'           => $lang['id'],
125
			'S_SELECTED'      => ((int) $lang['id'] == (int) $current) ? true : false,
126
		));
127
	}
128
129
	/**
130
	 * Add a donation page
131
	 *
132
	 * @return null
133
	 * @access public
134
	 */
135
	public function add_donation_page()
136
	{
137
		// Add form key
138
		add_form_key('add_edit_donation_pages');
139
140
		// Initiate a page donation entity
141
		/** @type \skouat\ppde\entity\donation_pages $entity */
142
		$entity = $this->get_container_entity();
143
144
		// Collect the form data
145
		$data = array(
146
			'page_title'   => $this->request->variable('page_title', ''),
147
			'page_lang_id' => $this->request->variable('lang_id', '', true),
148
			'page_content' => $this->request->variable('page_content', '', true),
149
			'bbcode'       => !$this->request->variable('disable_bbcode', false),
150
			'magic_url'    => !$this->request->variable('disable_magic_url', false),
151
			'smilies'      => !$this->request->variable('disable_smilies', false),
152
		);
153
154
		// Set template vars for language select menu
155
		$this->create_language_options($data['page_lang_id']);
156
157
		// Process the new page
158
		$this->add_edit_donation_page_data($entity, $data);
159
160
		// Set output vars for display in the template
161
		$this->template->assign_vars(array(
162
			'S_ADD_DONATION_PAGE' => true,
163
			'U_ADD_ACTION'        => $this->u_action . '&amp;action=add',
164
			'U_BACK'              => $this->u_action,
165
		));
166
	}
167
168
	/**
169
	 * Set template var options for language select menus
170
	 *
171
	 * @param integer $current ID of the language assigned to the donation page
172
	 *
173
	 * @return null
174
	 * @access protected
175
	 */
176
	protected function create_language_options($current)
177
	{
178
		// Grab all available language packs
179
		$langs = $this->ppde_operator->get_languages();
180
181
		// Set the options list template vars
182
		foreach ($langs as $lang)
183
		{
184
			$this->assign_langs_template_vars($lang, $current);
185
		}
186
	}
187
188
	/**
189
	 * Process donation pages data to be added or edited
190
	 *
191
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
192
	 * @param array                              $data   The form data to be processed
193
	 *
194
	 * @return null
195
	 * @access private
196
	 */
197
	private function add_edit_donation_page_data(\skouat\ppde\entity\donation_pages $entity, $data)
198
	{
199
		// Get form's POST actions (submit or preview)
200
		$this->submit = $this->request->is_set_post('submit');
201
		$this->preview = $this->request->is_set_post('preview');
202
203
		// Create an array to collect errors that will be output to the user
204
		$errors = array();
205
206
		// Load posting language file for the BBCode editor
207
		$this->user->add_lang('posting');
208
209
		$message_parse_options = array_merge(
210
			$this->get_message_parse_options($entity, $data, 'bbcode'),
211
			$this->get_message_parse_options($entity, $data, 'magic_url'),
212
			$this->get_message_parse_options($entity, $data, 'smilies')
213
		);
214
215
		// Set the message parse options in the entity
216
		foreach ($message_parse_options as $function => $enabled)
217
		{
218
			call_user_func(array($entity, ($enabled ? 'message_enable_' : 'message_disable_') . $function));
219
		}
220
221
		unset($message_parse_options);
222
223
		// Set the donation page's data in the entity
224
		$item_fields = array(
225
			'lang_id' => $data['page_lang_id'],
226
			'name'    => $data['page_title'],
227
			'message' => $data['page_content'],
228
		);
229
		$this->set_entity_data($entity, $item_fields);
230
231
		// Check some settings before loading and submitting form
232
		$errors = array_merge($errors,
233
			$this->is_invalid_form('add_edit_' . $this->module_name, $this->submit_or_preview($this->submit)),
234
			$this->is_empty_data($entity, 'name', '', $this->submit_or_preview($this->submit)),
235
			$this->is_empty_data($entity, 'lang_id', 0, $this->submit_or_preview($this->submit))
236
		);
237
238
		// Grab predefined template vars
239
		$vars = $entity->get_vars();
240
241
		// Assign variables in a template block vars
242
		$this->assign_preview_template_vars($entity, $errors);
243
		$this->assign_predefined_block_vars($vars);
244
245
		// Submit form data
246
		$this->submit_data($entity, $errors);
247
248
		// Set output vars for display in the template
249
		$this->s_error_assign_template_vars($errors);
250
		$this->template->assign_vars(array(
251
			'DONATION_BODY'                  => $entity->get_message_for_edit(),
252
			'L_DONATION_PAGES_TITLE'         => $this->user->lang[strtoupper($entity->get_name())],
253
			'L_DONATION_PAGES_TITLE_EXPLAIN' => $this->user->lang[strtoupper($entity->get_name()) . '_EXPLAIN'],
254
255
			'S_BBCODE_DISABLE_CHECKED'    => !$entity->message_bbcode_enabled(),
256
			'S_MAGIC_URL_DISABLE_CHECKED' => !$entity->message_magic_url_enabled(),
257
			'S_SMILIES_DISABLE_CHECKED'   => !$entity->message_smilies_enabled(),
258
259
			'BBCODE_STATUS'  => $this->user->lang('BBCODE_IS_ON', '<a href="' . append_sid("{$this->phpbb_root_path}faq.{$this->php_ext}", 'mode=bbcode') . '">', '</a>'),
260
			'FLASH_STATUS'   => $this->user->lang['FLASH_IS_ON'],
261
			'IMG_STATUS'     => $this->user->lang['IMAGES_ARE_ON'],
262
			'SMILIES_STATUS' => $this->user->lang['SMILIES_ARE_ON'],
263
			'URL_STATUS'     => $this->user->lang['URL_IS_ON'],
264
265
			'S_BBCODE_ALLOWED'  => true,
266
			'S_BBCODE_FLASH'    => true,
267
			'S_BBCODE_IMG'      => true,
268
			'S_LINKS_ALLOWED'   => true,
269
			'S_SMILIES_ALLOWED' => true,
270
			'S_HIDDEN_FIELDS'   => '<input type="hidden" name="page_title" value="' . $entity->get_name() . '" />',
271
		));
272
273
		// Display custom bbcodes and smilies
274
		$this->include_custom_bbcodes($entity->message_bbcode_enabled());
275
		$this->include_smileys($entity->message_smilies_enabled());
276
	}
277
278
	/**
279
	 * @param $bbcode_enabled
280
	 *
281
	 * @return null
282
	 * @access private
283
	 */
284
	private function include_custom_bbcodes($bbcode_enabled)
285
	{
286
		if ($bbcode_enabled)
287
		{
288
			$this->include_function('display_custom_bbcodes', $this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
289
			display_custom_bbcodes();
290
		}
291
	}
292
293
	/**
294
	 * @param $smilies_enabled
295
	 *
296
	 * @return null
297
	 * @access private
298
	 */
299
	private function include_smileys($smilies_enabled)
300
	{
301
		if ($smilies_enabled)
302
		{
303
			$this->include_function('generate_smilies', $this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext);
304
			generate_smilies('inline', 0);
305
		}
306
	}
307
308
	/**
309
	 * Includes the file that contains the function, if not loaded.
310
	 *
311
	 * @param $function_name     string Name of the function to test
312
	 * @param $function_filepath string Path of the file that containing the function
313
	 *
314
	 * @return null
315
	 * @access private
316
	 */
317
	private function include_function($function_name, $function_filepath)
318
	{
319
		if (!function_exists($function_name))
320
		{
321
			include($function_filepath);
322
		}
323
	}
324
325
	/**
326
	 * Get parse options of the message
327
	 *
328
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
329
	 * @param array                              $data   The form data to be processed
330
	 * @param string                             $type
331
	 *
332
	 * @return array
333
	 * @access private
334
	 */
335
	private function get_message_parse_options(\skouat\ppde\entity\donation_pages $entity, $data, $type)
336
	{
337
		return array($type => $this->submit_or_preview($this->submit, $this->preview) ? $data[$type] : (bool) call_user_func(array($entity, 'message_' . $type . '_enabled')));
338
	}
339
340
	/**
341
	 * Assign vars to the template if preview is true.
342
	 *
343
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
344
	 * @param                                    $errors
345
	 *
346
	 * @access private
347
	 */
348
	private function assign_preview_template_vars(\skouat\ppde\entity\donation_pages $entity, $errors)
349
	{
350
		if ($this->preview && empty($errors))
351
		{
352
			// Set output vars for display in the template
353
			$this->template->assign_vars(array(
354
				'PPDE_DP_PREVIEW'   => $entity->replace_template_vars($entity->get_message_for_display()),
355
				'S_PPDE_DP_PREVIEW' => $this->preview,
356
			));
357
		}
358
	}
359
360
	/**
361
	 * Assign Predefined variables to a template block_vars
362
	 *
363
	 * @param array $vars
364
	 *
365
	 * @return null
366
	 * @access   private
367
	 */
368
	private function assign_predefined_block_vars($vars)
369
	{
370
		for ($i = 0, $size = sizeof($vars); $i < $size; $i++)
371
		{
372
			$this->template->assign_block_vars('dp_vars', array(
373
					'NAME'     => $vars[$i]['name'],
374
					'VARIABLE' => $vars[$i]['var'],
375
					'EXAMPLE'  => $vars[$i]['value'])
376
			);
377
		}
378
	}
379
380
	/**
381
	 *  Submit data to the database
382
	 *
383
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
384
	 * @param array                              $errors
385
	 *
386
	 * @return null
387
	 * @access private
388
	 */
389
	private function submit_data(\skouat\ppde\entity\donation_pages $entity, array $errors)
390
	{
391
		if ($this->can_submit_data($errors))
392
		{
393
			$this->trigger_error_data_already_exists($entity);
394
395
			// Grab the local language name
396
			$this->get_lang_local_name($this->ppde_operator->get_languages($entity->get_lang_id()));
397
398
			$log_action = $this->add_edit_data($entity);
399
			// Log and show user confirmation of the saved item and provide link back to the previous page
400
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($log_action), time(), array($this->user->lang(strtoupper($entity->get_name())), $this->lang_local_name));
401
			trigger_error($this->user->lang($this->lang_key_prefix . '_' . strtoupper($log_action), $this->lang_local_name) . adm_back_link($this->u_action));
402
		}
403
	}
404
405
	/**
406
	 * Get Local lang name
407
	 *
408
	 * @param array $langs
409
	 *
410
	 * @return null
411
	 * @access private
412
	 */
413
	private function get_lang_local_name($langs)
414
	{
415
		foreach ($langs as $lang)
416
		{
417
			$this->lang_local_name = $lang['name'];
418
		}
419
	}
420
421
	/**
422
	 * Edit a donation page
423
	 *
424
	 * @param int $page_id Donation page identifier
425
	 *
426
	 * @return null
427
	 * @access public
428
	 */
429
	public function edit_donation_page($page_id)
430
	{
431
		// Add form key
432
		add_form_key('add_edit_donation_pages');
433
434
		// Initiate a page donation entity
435
		/** @type \skouat\ppde\entity\donation_pages $entity */
436
		$entity = $this->get_container_entity();
437
		$entity->load($page_id);
438
439
		// Collect the form data
440
		$data = array(
441
			'page_id'      => (int) $page_id,
442
			'page_title'   => $this->request->variable('page_title', $entity->get_name(), false),
443
			'page_lang_id' => $this->request->variable('page_lang_id', $entity->get_lang_id()),
444
			'page_content' => $this->request->variable('page_content', $entity->get_message_for_edit(), true),
445
			'bbcode'       => !$this->request->variable('disable_bbcode', false),
446
			'magic_url'    => !$this->request->variable('disable_magic_url', false),
447
			'smilies'      => !$this->request->variable('disable_smilies', false),
448
		);
449
450
		// Set template vars for language select menu
451
		$this->create_language_options($data['page_lang_id']);
452
453
		// Process the new page
454
		$this->add_edit_donation_page_data($entity, $data);
455
456
		// Set output vars for display in the template
457
		$this->template->assign_vars(array(
458
			'S_EDIT_DONATION_PAGE' => true,
459
			'U_EDIT_ACTION'        => $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $page_id,
460
			'U_BACK'               => $this->u_action,
461
		));
462
	}
463
464
	/**
465
	 * Delete a donation page
466
	 *
467
	 * @param int $page_id The donation page identifier to delete
468
	 *
469
	 * @return null
470
	 * @access public
471
	 */
472
	public function delete_donation_page($page_id)
473
	{
474
		// Initiate an entity and load data
475
		/** @type \skouat\ppde\entity\donation_pages $entity */
476
		$entity = $this->get_container_entity();
477
		$entity->load($page_id);
478
479
		// Before deletion, grab the local language name
480
		$this->get_lang_local_name($this->ppde_operator->get_languages($entity->get_lang_id()));
481
482
		$entity->delete($page_id);
483
484
		// Log the action
485
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG' . $this->lang_key_prefix . '_DELETED', time(), array($this->user->lang(strtoupper($entity->get_name())), $this->lang_local_name));
486
487
		// If AJAX was used, show user a result message
488
		$message = $this->user->lang($this->lang_key_prefix . '_DELETED', $this->lang_local_name);
489
		$this->ajax_delete_result_message($message);
490
	}
491
}
492