Passed
Branch 3.2.x (99ec7e)
by Mario
04:20
created

donation_pages_controller   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 460
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 143
dl 0
loc 460
rs 9.68
c 0
b 0
f 0
wmc 34

16 Methods

Rating   Name   Duplication   Size   Complexity  
B add_edit_donation_page_data() 0 79 5
A get_message_parse_options() 0 3 2
A submit_data() 0 13 2
A __construct() 0 27 1
A add() 0 23 1
A delete() 0 16 1
A assign_predefined_block_vars() 0 8 2
A include_function() 0 5 2
A display() 0 33 4
A assign_preview_template_vars() 0 8 3
A include_custom_bbcodes() 0 6 2
A get_lang_local_name() 0 5 2
A assign_langs_template_vars() 0 6 2
A create_language_options() 0 9 2
A edit() 0 27 1
A include_smilies() 0 6 2
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\admin;
12
13
use phpbb\language\language;
14
use phpbb\log\log;
15
use phpbb\request\request;
16
use phpbb\template\template;
17
use phpbb\user;
18
use skouat\ppde\operators\donation_pages;
19
use Symfony\Component\DependencyInjection\ContainerInterface;
20
21
/**
22
 * @property ContainerInterface container         Service container interface
23
 * @property string             id_prefix_name    Prefix name for identifier in the URL
24
 * @property string             lang_key_prefix   Prefix for the messages thrown by exceptions
25
 * @property language           language          Language user object
26
 * @property log                log               The phpBB log system.
27
 * @property string             module_name       Name of the module currently used
28
 * @property bool               preview           State of preview $_POST variable
29
 * @property request            request           Request object.
30
 * @property bool               submit            State of submit $_POST variable
31
 * @property template           template          Template object
32
 * @property user               user              User object.
33
 */
34
class donation_pages_controller extends admin_main
35
{
36
	protected $phpbb_root_path;
37
	protected $php_ext;
38
	protected $ppde_entity;
39
	protected $ppde_operator;
40
	protected $lang_local_name;
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param ContainerInterface                 $container                    Service container interface
46
	 * @param language                           $language                     Language user object
47
	 * @param log                                $log                          The phpBB log system
48
	 * @param \skouat\ppde\entity\donation_pages $ppde_entity_donation_pages   PPDE Entity object
49
	 * @param donation_pages                     $ppde_operator_donation_pages Operator object
50
	 * @param request                            $request                      Request object
51
	 * @param template                           $template                     Template object
52
	 * @param user                               $user                         User object
53
	 * @param string                             $phpbb_root_path              phpBB root path
54
	 * @param string                             $php_ext                      phpEx
55
	 *
56
	 * @access public
57
	 */
58
	public function __construct(
59
		ContainerInterface $container,
60
		language $language,
61
		log $log,
62
		\skouat\ppde\entity\donation_pages $ppde_entity_donation_pages,
63
		donation_pages $ppde_operator_donation_pages,
64
		request $request,
65
		template $template,
66
		user $user,
67
		$phpbb_root_path,
68
		$php_ext
69
	)
70
	{
71
		$this->container = $container;
72
		$this->language = $language;
73
		$this->log = $log;
74
		$this->ppde_entity = $ppde_entity_donation_pages;
75
		$this->ppde_operator = $ppde_operator_donation_pages;
76
		$this->request = $request;
77
		$this->template = $template;
78
		$this->user = $user;
79
		$this->phpbb_root_path = $phpbb_root_path;
80
		$this->php_ext = $php_ext;
81
		parent::__construct(
82
			'donation_pages',
83
			'PPDE_DP',
84
			'page'
85
		);
86
	}
87
88
	/**
89
	 * Display the pages
90
	 *
91
	 * @return void
92
	 * @access public
93
	 */
94
	public function display()
95
	{
96
		// Get list of available language packs
97
		$langs = $this->ppde_operator->get_languages();
98
99
		// Set output vars
100
		foreach ($langs as $lang => $entry)
101
		{
102
			$this->assign_langs_template_vars($entry);
103
104
			// Grab all the pages from the db
105
			$data_ary = $this->ppde_entity->get_data($this->ppde_operator->build_sql_data($entry['id']));
106
107
			foreach ($data_ary as $data)
108
			{
109
				// Do not treat the item whether language identifier does not match
110
				if ($data['page_lang_id'] != $entry['id'])
111
				{
112
					continue;
113
				}
114
115
				$this->template->assign_block_vars('ppde_langs.dp_list', array(
116
					'DONATION_PAGE_TITLE' => $this->language->lang(strtoupper($data['page_title'])),
117
					'DONATION_PAGE_LANG'  => (string) $lang,
118
					'U_DELETE'            => $this->u_action . '&amp;action=delete&amp;' . $this->id_prefix_name . '_id=' . $data['page_id'],
119
					'U_EDIT'              => $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $data['page_id'],
120
				));
121
			}
122
			unset($data_ary, $data);
123
		}
124
		unset($entry, $langs, $lang);
125
126
		$this->u_action_assign_template_vars();
127
	}
128
129
	/**
130
	 * Assign language template vars to a block vars
131
	 * $s_select is for build options select menu
132
	 *
133
	 * @param array $lang
134
	 * @param int   $current
135
	 *
136
	 * @return void
137
	 * @access private
138
	 */
139
	private function assign_langs_template_vars($lang, $current = 0)
140
	{
141
		$this->template->assign_block_vars('ppde_langs', array(
142
			'LANG_LOCAL_NAME' => $lang['name'],
143
			'VALUE'           => $lang['id'],
144
			'S_SELECTED'      => ((int) $lang['id'] == (int) $current) ? true : false,
145
		));
146
	}
147
148
	/**
149
	 * Add a donation page
150
	 *
151
	 * @return void
152
	 * @access public
153
	 */
154
	public function add()
155
	{
156
		// Add form key
157
		add_form_key('add_edit_donation_pages');
158
159
		// Collect the form data
160
		$data = array(
161
			'page_title'   => $this->request->variable('page_title', ''),
162
			'page_lang_id' => $this->request->variable('lang_id', '', true),
163
			'page_content' => $this->request->variable('page_content', '', true),
164
			'bbcode'       => !$this->request->variable('disable_bbcode', false),
165
			'magic_url'    => !$this->request->variable('disable_magic_url', false),
166
			'smilies'      => !$this->request->variable('disable_smilies', false),
167
		);
168
169
		// Set template vars for language select menu
170
		$this->create_language_options($data['page_lang_id']);
171
172
		// Process the new page
173
		$this->add_edit_donation_page_data($this->ppde_entity, $data);
174
175
		// Set output vars for display in the template
176
		$this->add_edit_action_assign_template_vars('add');
177
	}
178
179
	/**
180
	 * Set template var options for language select menus
181
	 *
182
	 * @param int $current ID of the language assigned to the donation page
183
	 *
184
	 * @return void
185
	 * @access protected
186
	 */
187
	protected function create_language_options($current)
188
	{
189
		// Grab all available language packs
190
		$langs = $this->ppde_operator->get_languages();
191
192
		// Set the options list template vars
193
		foreach ($langs as $lang)
194
		{
195
			$this->assign_langs_template_vars($lang, $current);
196
		}
197
	}
198
199
	/**
200
	 * Process donation pages data to be added or edited
201
	 *
202
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
203
	 * @param array                              $data   The form data to be processed
204
	 *
205
	 * @return void
206
	 * @access private
207
	 */
208
	private function add_edit_donation_page_data(\skouat\ppde\entity\donation_pages $entity, $data)
209
	{
210
		// Get form's POST actions (submit or preview)
211
		$this->submit = $this->request->is_set_post('submit');
212
		$this->preview = $this->request->is_set_post('preview');
213
214
		// Create an array to collect errors that will be output to the user
215
		$errors = array();
216
217
		// Load posting language file for the BBCode editor
218
		$this->language->add_lang('posting');
219
220
		$message_parse_options = array_merge(
221
			$this->get_message_parse_options($entity, $data, 'bbcode'),
222
			$this->get_message_parse_options($entity, $data, 'magic_url'),
223
			$this->get_message_parse_options($entity, $data, 'smilies')
224
		);
225
226
		// Set the message parse options in the entity
227
		foreach ($message_parse_options as $function => $enabled)
228
		{
229
			call_user_func(array($entity, ($enabled ? 'message_enable_' : 'message_disable_') . $function));
230
		}
231
232
		unset($message_parse_options);
233
234
		// Set the donation page's data in the entity
235
		$item_fields = array(
236
			'lang_id' => $data['page_lang_id'],
237
			'name'    => $data['page_title'],
238
			'message' => $data['page_content'],
239
		);
240
		$entity->set_entity_data($item_fields);
241
242
		// Check some settings before loading and submitting form
243
		$errors = array_merge($errors,
244
			$this->is_invalid_form('add_edit_' . $this->module_name, $this->submit_or_preview($this->submit)),
245
			$this->is_empty_data($entity, 'name', '', $this->submit_or_preview($this->submit)),
246
			$this->is_empty_data($entity, 'lang_id', 0, $this->submit_or_preview($this->submit))
247
		);
248
249
		// Grab predefined template vars
250
		$vars = $entity->get_vars();
251
252
		// Assign variables in a template block vars
253
		$this->assign_preview_template_vars($entity, $errors);
254
		$this->assign_predefined_block_vars($vars);
255
256
		// Submit form data
257
		$this->submit_data($entity, $errors);
258
259
		// Set output vars for display in the template
260
		$this->s_error_assign_template_vars($errors);
261
		$this->template->assign_vars(array(
262
			'DONATION_BODY'                  => $entity->get_message_for_edit(),
263
			'L_DONATION_PAGES_TITLE'         => $this->language->lang(strtoupper($entity->get_name())),
264
			'L_DONATION_PAGES_TITLE_EXPLAIN' => $this->language->lang(strtoupper($entity->get_name()) . '_EXPLAIN'),
265
266
			'S_BBCODE_DISABLE_CHECKED'    => !$entity->message_bbcode_enabled(),
267
			'S_MAGIC_URL_DISABLE_CHECKED' => !$entity->message_magic_url_enabled(),
268
			'S_SMILIES_DISABLE_CHECKED'   => !$entity->message_smilies_enabled(),
269
270
			'BBCODE_STATUS'  => $this->language->lang('BBCODE_IS_ON', '<a href="' . append_sid("{$this->phpbb_root_path}faq.{$this->php_ext}", 'mode=bbcode') . '">', '</a>'),
271
			'FLASH_STATUS'   => $this->language->lang('FLASH_IS_ON'),
272
			'IMG_STATUS'     => $this->language->lang('IMAGES_ARE_ON'),
273
			'SMILIES_STATUS' => $this->language->lang('SMILIES_ARE_ON'),
274
			'URL_STATUS'     => $this->language->lang('URL_IS_ON'),
275
276
			'S_BBCODE_ALLOWED'  => true,
277
			'S_BBCODE_FLASH'    => true,
278
			'S_BBCODE_IMG'      => true,
279
			'S_LINKS_ALLOWED'   => true,
280
			'S_SMILIES_ALLOWED' => true,
281
			'S_HIDDEN_FIELDS'   => '<input type="hidden" name="page_title" value="' . $entity->get_name() . '">',
282
		));
283
284
		// Display custom bbcodes and smilies
285
		$this->include_custom_bbcodes($this->user->optionget('bbcode') || $entity->message_bbcode_enabled());
0 ignored issues
show
Bug introduced by
'bbcode' of type string is incompatible with the type integer expected by parameter $key of phpbb\user::optionget(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

285
		$this->include_custom_bbcodes($this->user->optionget(/** @scrutinizer ignore-type */ 'bbcode') || $entity->message_bbcode_enabled());
Loading history...
286
		$this->include_smilies($this->user->optionget('smilies') || $entity->message_smilies_enabled());
287
	}
288
289
	/**
290
	 * Get parse options of the message
291
	 *
292
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
293
	 * @param array                              $data   The form data to be processed
294
	 * @param string                             $type
295
	 *
296
	 * @return array
297
	 * @access private
298
	 */
299
	private function get_message_parse_options(\skouat\ppde\entity\donation_pages $entity, $data, $type)
300
	{
301
		return array($type => $this->submit_or_preview($this->submit, $this->preview) ? $data[$type] : (bool) call_user_func(array($entity, 'message_' . $type . '_enabled')));
302
	}
303
304
	/**
305
	 * Assign vars to the template if preview is true.
306
	 *
307
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
308
	 * @param array                              $errors
309
	 *
310
	 * @return void
311
	 * @access private
312
	 */
313
	private function assign_preview_template_vars(\skouat\ppde\entity\donation_pages $entity, $errors)
314
	{
315
		if ($this->preview && empty($errors))
316
		{
317
			// Set output vars for display in the template
318
			$this->template->assign_vars(array(
319
				'PPDE_DP_PREVIEW'   => $entity->replace_template_vars($entity->get_message_for_display()),
320
				'S_PPDE_DP_PREVIEW' => $this->preview,
321
			));
322
		}
323
	}
324
325
	/**
326
	 * Assign Predefined variables to a template block_vars
327
	 *
328
	 * @param array $vars
329
	 *
330
	 * @return void
331
	 * @access private
332
	 */
333
	private function assign_predefined_block_vars($vars)
334
	{
335
		for ($i = 0, $size = count($vars); $i < $size; $i++)
336
		{
337
			$this->template->assign_block_vars('dp_vars', array(
338
					'NAME'     => $vars[$i]['name'],
339
					'VARIABLE' => $vars[$i]['var'],
340
					'EXAMPLE'  => $vars[$i]['value'])
341
			);
342
		}
343
	}
344
345
	/**
346
	 *  Submit data to the database
347
	 *
348
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
349
	 * @param array                              $errors
350
	 *
351
	 * @return void
352
	 * @access private
353
	 */
354
	private function submit_data(\skouat\ppde\entity\donation_pages $entity, array $errors)
355
	{
356
		if ($this->can_submit_data($errors))
357
		{
358
			$this->trigger_error_data_already_exists($entity);
359
360
			// Grab the local language name
361
			$this->get_lang_local_name($this->ppde_operator->get_languages($entity->get_lang_id()));
362
363
			$log_action = $entity->add_edit_data();
364
			// Log and show user confirmation of the saved item and provide link back to the previous page
365
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($log_action), time(), array($this->language->lang(strtoupper($entity->get_name())), $this->lang_local_name));
366
			trigger_error($this->language->lang($this->lang_key_prefix . '_' . strtoupper($log_action), $this->lang_local_name) . adm_back_link($this->u_action));
367
		}
368
	}
369
370
	/**
371
	 * Get Local lang name
372
	 *
373
	 * @param array $langs
374
	 *
375
	 * @return void
376
	 * @access private
377
	 */
378
	private function get_lang_local_name($langs)
379
	{
380
		foreach ($langs as $lang)
381
		{
382
			$this->lang_local_name = $lang['name'];
383
		}
384
	}
385
386
	/**
387
	 * @param $bbcode_enabled
388
	 *
389
	 * @return void
390
	 * @access private
391
	 */
392
	private function include_custom_bbcodes($bbcode_enabled)
393
	{
394
		if ($bbcode_enabled)
395
		{
396
			$this->include_function('display_custom_bbcodes', $this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
397
			display_custom_bbcodes();
398
		}
399
	}
400
401
	/**
402
	 * Includes the file that contains the function, if not loaded.
403
	 *
404
	 * @param string $function_name     Name of the function to test
405
	 * @param string $function_filepath Path of the file that containing the function
406
	 *
407
	 * @return void
408
	 * @access private
409
	 */
410
	private function include_function($function_name, $function_filepath)
411
	{
412
		if (!function_exists($function_name))
413
		{
414
			include($function_filepath);
415
		}
416
	}
417
418
	/**
419
	 * @param bool $smilies_enabled
420
	 *
421
	 * @return void
422
	 * @access private
423
	 */
424
	private function include_smilies($smilies_enabled)
425
	{
426
		if ($smilies_enabled)
427
		{
428
			$this->include_function('generate_smilies', $this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext);
429
			generate_smilies('inline', 0);
430
		}
431
	}
432
433
	/**
434
	 * Edit a donation page
435
	 *
436
	 * @param int $page_id Donation page identifier
437
	 *
438
	 * @return void
439
	 * @access public
440
	 */
441
	public function edit($page_id)
442
	{
443
		// Add form key
444
		add_form_key('add_edit_donation_pages');
445
446
		// Load data
447
		$this->ppde_entity->load($page_id);
448
449
		// Collect the form data
450
		$data = array(
451
			'page_id'      => (int) $page_id,
452
			'page_title'   => $this->request->variable('page_title', $this->ppde_entity->get_name(), false),
453
			'page_lang_id' => $this->request->variable('page_lang_id', $this->ppde_entity->get_lang_id()),
454
			'page_content' => $this->request->variable('page_content', $this->ppde_entity->get_message_for_edit(), true),
455
			'bbcode'       => !$this->request->variable('disable_bbcode', false),
456
			'magic_url'    => !$this->request->variable('disable_magic_url', false),
457
			'smilies'      => !$this->request->variable('disable_smilies', false),
458
		);
459
460
		// Set template vars for language select menu
461
		$this->create_language_options($data['page_lang_id']);
462
463
		// Process the new page
464
		$this->add_edit_donation_page_data($this->ppde_entity, $data);
465
466
		// Set output vars for display in the template
467
		$this->add_edit_action_assign_template_vars('edit', $page_id);
468
	}
469
470
	/**
471
	 * Delete a donation page
472
	 *
473
	 * @param int $page_id The donation page identifier to delete
474
	 *
475
	 * @return void
476
	 * @access public
477
	 */
478
	public function delete($page_id)
479
	{
480
		// Load data
481
		$this->ppde_entity->load($page_id);
482
483
		// Before deletion, grab the local language name
484
		$this->get_lang_local_name($this->ppde_operator->get_languages($this->ppde_entity->get_lang_id()));
485
486
		$this->ppde_entity->delete($page_id);
487
488
		// Log the action
489
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_DELETED', time(), array($this->language->lang(strtoupper($this->ppde_entity->get_name())), $this->lang_local_name));
490
491
		// If AJAX was used, show user a result message
492
		$message = $this->language->lang($this->lang_key_prefix . '_DELETED', $this->lang_local_name);
493
		$this->ajax_delete_result_message($message);
494
	}
495
}
496