Completed
Branch master (915db5)
by Mario
03:03
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_' . $this->module_name);
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($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
			include_once($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
			include_once($this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext);
304
			generate_smilies('inline', 0);
305
		}
306
	}
307
308
	/**
309
	 * Get parse options of the message
310
	 *
311
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
312
	 * @param array                              $data   The form data to be processed
313
	 * @param string                             $type
314
	 *
315
	 * @return array
316
	 * @access private
317
	 */
318
	private function get_message_parse_options($entity, $data, $type)
319
	{
320
		return array($type => $this->submit_or_preview($this->submit, $this->preview) ? $data[$type] : (bool) call_user_func(array($entity, 'message_' . $type . '_enabled')));
321
	}
322
323
	/**
324
	 * Assign vars to the template if preview is true.
325
	 *
326
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
327
	 * @param                                    $errors
328
	 *
329
	 * @access private
330
	 */
331
	private function assign_preview_template_vars($entity, $errors)
332
	{
333
		if ($this->preview && empty($errors))
334
		{
335
			// Set output vars for display in the template
336
			$this->template->assign_vars(array(
337
				'PPDE_DP_PREVIEW'   => $entity->replace_template_vars($entity->get_message_for_display()),
338
				'S_PPDE_DP_PREVIEW' => $this->preview,
339
			));
340
		}
341
	}
342
343
	/**
344
	 * Assign Predefined variables to a template block_vars
345
	 *
346
	 * @param array $vars
347
	 *
348
	 * @return null
349
	 * @access   private
350
	 */
351
	private function assign_predefined_block_vars($vars)
352
	{
353
		for ($i = 0, $size = sizeof($vars); $i < $size; $i++)
354
		{
355
			$this->template->assign_block_vars('dp_vars', array(
356
					'NAME'     => $vars[$i]['name'],
357
					'VARIABLE' => $vars[$i]['var'],
358
					'EXAMPLE'  => $vars[$i]['value'])
359
			);
360
		}
361
	}
362
363
	/**
364
	 *  Submit data to the database
365
	 *
366
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
367
	 * @param array                              $errors
368
	 *
369
	 * @return null
370
	 * @access private
371
	 */
372
	private function submit_data($entity, array $errors)
373
	{
374
		if ($this->can_submit_data($errors))
375
		{
376
			$this->trigger_error_data_already_exists($entity);
377
378
			// Grab the local language name
379
			$this->get_lang_local_name($this->ppde_operator->get_languages($entity->get_lang_id()));
380
381
			$log_action = $this->add_edit_data($entity);
382
			// Log and show user confirmation of the saved item and provide link back to the previous page
383
			$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));
384
			trigger_error($this->user->lang($this->lang_key_prefix . '_' . strtoupper($log_action), $this->lang_local_name) . adm_back_link($this->u_action));
385
		}
386
	}
387
388
	/**
389
	 * Get Local lang name
390
	 *
391
	 * @param array $langs
392
	 *
393
	 * @return null
394
	 * @access private
395
	 */
396
	private function get_lang_local_name($langs)
397
	{
398
		foreach ($langs as $lang)
399
		{
400
			$this->lang_local_name = $lang['name'];
401
		}
402
	}
403
404
	/**
405
	 * Edit a donation page
406
	 *
407
	 * @param int $page_id Donation page identifier
408
	 *
409
	 * @return null
410
	 * @access public
411
	 */
412
	public function edit_donation_page($page_id)
413
	{
414
		// Add form key
415
		add_form_key('add_edit_' . $this->module_name);
416
417
		// Initiate a page donation entity
418
		/** @type \skouat\ppde\entity\donation_pages $entity */
419
		$entity = $this->get_container_entity();
420
		$entity->load($page_id);
421
422
		// Collect the form data
423
		$data = array(
424
			'page_id'      => (int) $page_id,
425
			'page_title'   => $this->request->variable('page_title', $entity->get_name(), false),
426
			'page_lang_id' => $this->request->variable('page_lang_id', $entity->get_lang_id()),
427
			'page_content' => $this->request->variable('page_content', $entity->get_message_for_edit(), true),
428
			'bbcode'       => !$this->request->variable('disable_bbcode', false),
429
			'magic_url'    => !$this->request->variable('disable_magic_url', false),
430
			'smilies'      => !$this->request->variable('disable_smilies', false),
431
		);
432
433
		// Set template vars for language select menu
434
		$this->create_language_options($data['page_lang_id']);
435
436
		// Process the new page
437
		$this->add_edit_donation_page_data($entity, $data);
438
439
		// Set output vars for display in the template
440
		$this->template->assign_vars(array(
441
			'S_EDIT_DONATION_PAGE' => true,
442
			'U_EDIT_ACTION'        => $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $page_id,
443
			'U_BACK'               => $this->u_action,
444
		));
445
	}
446
447
	/**
448
	 * Delete a donation page
449
	 *
450
	 * @param int $page_id The donation page identifier to delete
451
	 *
452
	 * @return null
453
	 * @access public
454
	 */
455
	public function delete_donation_page($page_id)
456
	{
457
		// Initiate an entity and load data
458
		/** @type \skouat\ppde\entity\donation_pages $entity */
459
		$entity = $this->get_container_entity();
460
		$entity->load($page_id);
461
462
		// Before deletion, grab the local language name
463
		$this->get_lang_local_name($this->ppde_operator->get_languages($entity->get_lang_id()));
464
465
		$entity->delete($page_id);
466
467
		// Log the action
468
		$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));
469
470
		// If AJAX was used, show user a result message
471
		$message = $this->user->lang($this->lang_key_prefix . '_DELETED', $this->lang_local_name);
472
		$this->ajax_delete_result_message($message);
473
	}
474
}
475