Passed
Push — develop-3.3.x ( 1d9287...356c9f )
by Mario
02:47
created

donation_pages_controller   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 433
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 142
dl 0
loc 433
rs 9.28
c 6
b 0
f 0
wmc 39

23 Methods

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

288
		$this->include_custom_bbcodes($this->user->optionget(/** @scrutinizer ignore-type */ 'bbcode') || $this->donation_pages_entity->message_bbcode_enabled());
Loading history...
289
		$this->include_smilies($this->user->optionget('smilies') || $this->donation_pages_entity->message_smilies_enabled());
290
	}
291
292
	/**
293
	 * Assign template variables for previewing donation page content.
294
	 *
295
	 * @param array $errors An array of error messages.
296
	 */
297
	private function assign_preview_template_vars($errors): void
298
	{
299
		if ($this->preview && empty($errors))
300
		{
301
			// Set output vars for display in the template
302
			$this->template->assign_vars([
303
				'PPDE_DP_PREVIEW'   => $this->ppde_vars_helper->replace_template_vars($this->donation_pages_entity->get_message_for_display()),
304
				'S_PPDE_DP_PREVIEW' => $this->preview,
305
			]);
306
		}
307
	}
308
309
	/**
310
	 * Assign predefined variables to template block_vars.
311
	 *
312
	 * @param array $vars Variables to be assigned.
313
	 */
314
	private function assign_predefined_block_vars($vars): void
315
	{
316
		foreach ($vars as $var)
317
		{
318
			$this->template->assign_block_vars('dp_vars', [
319
					'NAME'     => $var['name'],
320
					'VARIABLE' => $var['var'],
321
					'EXAMPLE'  => $var['value']]
322
			);
323
		}
324
	}
325
326
	/**
327
	 * Submit donation page data to the database.
328
	 *
329
	 * @param array $errors Errors encountered during data processing.
330
	 */
331
	private function submit_data(array $errors): void
332
	{
333
		if (!$this->can_submit_data($errors))
334
		{
335
			return;
336
		}
337
338
		$this->trigger_error_data_already_exists($this->donation_pages_entity);
339
340
		// Grab the local language name
341
		$this->set_lang_local_name($this->donation_pages_operator->get_languages($this->donation_pages_entity->get_lang_id()));
342
343
		$log_action = $this->donation_pages_entity->add_edit_data();
344
		// Log and show user confirmation of the saved item and provide link back to the previous page
345
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($log_action), time(), [$this->language->lang(strtoupper($this->donation_pages_entity->get_name())), $this->lang_local_name]);
346
347
		trigger_error($this->language->lang($this->lang_key_prefix . '_' . strtoupper($log_action), $this->lang_local_name) . adm_back_link($this->u_action));
348
	}
349
350
	/**
351
	 * Set local language name.
352
	 *
353
	 * Extract and store the local name of the language from the provided language data.
354
	 *
355
	 * @param array $langs Array containing the details of the languages.
356
	 *                     Each language array should have a 'name' key.
357
	 *                     $langs is expected to contain only one language array.
358
	 */
359
	private function set_lang_local_name($langs): void
360
	{
361
		foreach ($langs as $lang)
362
		{
363
			$this->lang_local_name = $lang['name'];
364
		}
365
	}
366
367
	/**
368
	 * Include custom bbcodes if enabled.
369
	 *
370
	 * @param bool $bbcode_enabled Whether BBCode is enabled.
371
	 */
372
	private function include_custom_bbcodes($bbcode_enabled): void
373
	{
374
		if ($bbcode_enabled)
375
		{
376
			$this->include_function('functions_display', 'display_custom_bbcodes');
377
			display_custom_bbcodes();
378
		}
379
	}
380
381
	/**
382
	 * Include the specified function if it is not loaded.
383
	 *
384
	 * @param string $file          The file containing the function.
385
	 * @param string $function_name The name of the function.
386
	 */
387
	private function include_function($file, $function_name): void
388
	{
389
		if (!function_exists($function_name))
390
		{
391
			include($this->phpbb_root_path . 'includes/' . $file . '.' . $this->php_ext);
392
		}
393
	}
394
395
	/**
396
	 * Include smilies if enabled.
397
	 *
398
	 * @param bool $smilies_enabled Whether smilies are enabled.
399
	 */
400
	private function include_smilies($smilies_enabled): void
401
	{
402
		if ($smilies_enabled)
403
		{
404
			$this->include_function('functions_posting', 'generate_smilies');
405
			generate_smilies('inline', 0);
406
		}
407
	}
408
409
	/**
410
	 * Edit an existing donation page.
411
	 */
412
	protected function edit(): void
413
	{
414
		$page_id = (int) $this->args[$this->id_prefix_name . '_id'];
415
416
		// Add form key
417
		add_form_key('add_edit_donation_pages');
418
419
		// Load data
420
		$this->donation_pages_entity->load($page_id);
421
422
		// Collect the form data
423
		$data = [
424
			'page_id'      => $page_id,
425
			'page_title'   => $this->request->variable('page_title', $this->donation_pages_entity->get_name()),
426
			'page_lang_id' => $this->request->variable('page_lang_id', $this->donation_pages_entity->get_lang_id()),
427
			'page_content' => $this->request->variable('page_content', $this->donation_pages_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->process_donation_page_form($data);
438
439
		// Set output vars for display in the template
440
		$this->add_edit_action_assign_template_vars('edit', $page_id);
441
	}
442
443
	/**
444
	 * Delete a donation page.
445
	 */
446
	protected function delete(): void
447
	{
448
		$page_id = (int) $this->args[$this->id_prefix_name . '_id'];
449
450
		$this->donation_pages_entity->set_page_url($this->u_action);
451
452
		// Load data in the entity
453
		$this->donation_pages_entity->load($page_id);
454
455
		// Before deletion, grab the local language name
456
		$this->set_lang_local_name($this->donation_pages_operator->get_languages($this->donation_pages_entity->get_lang_id()));
457
458
		$this->donation_pages_entity->delete($page_id);
459
460
		// Log the action
461
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_DELETED', time(), [$this->language->lang(strtoupper($this->donation_pages_entity->get_name())), $this->lang_local_name]);
462
463
		// If AJAX was used, show user a result message
464
		$message = $this->language->lang($this->lang_key_prefix . '_DELETED', $this->lang_local_name);
465
		$this->ajax_delete_result_message($message);
466
	}
467
}
468