Completed
Push — develop ( b4b65d...4507e2 )
by Mario
03:29
created

admin_donation_pages_controller::include_smileys()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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)
1 ignored issue
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...
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
		// Load posting language file for the BBCode editor
204
		$this->user->add_lang('posting');
205
206
		// Create an array to collect errors that will be output to the user
207
		$errors = array();
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
			try
219
			{
220
				call_user_func(array($entity, ($enabled ? 'message_enable_' : 'message_disable_') . $function));
221
			}
222
			catch (\skouat\ppde\exception\base $e)
223
			{
224
				// Catch exceptions and add them to errors array
225
				$errors[] = $e->get_message($this->user);
226
			}
227
		}
228
229
		unset($message_parse_options);
230
231
		// Set the donation page's data in the entity
232
		$item_fields = array(
233
			'lang_id' => $data['page_lang_id'],
234
			'name'    => $data['page_title'],
235
			'message' => $data['page_content'],
236
		);
237
		$errors = array_merge($errors, $this->set_entity_data($entity, $item_fields));
238
239
		// Check some settings before loading and submitting form
240
		$errors = array_merge($errors,
241
			$this->is_invalid_form('add_edit_' . $this->module_name, $this->submit_or_preview($this->submit)),
242
			$this->is_empty_data($entity, 'name', '', $this->submit_or_preview($this->submit)),
243
			$this->is_empty_data($entity, 'lang_id', 0, $this->submit_or_preview($this->submit))
244
		);
245
246
		// Grab predefined template vars
247
		$vars = $entity->get_vars();
248
249
		// Assign variables in a template block vars
250
		$this->assign_preview_template_vars($entity, $errors);
251
		$this->assign_predefined_block_vars($vars);
252
253
		// Submit form data
254
		$this->submit_data($entity, $errors);
255
256
		// Set output vars for display in the template
257
		$this->s_error_assign_template_vars($errors);
258
		$this->template->assign_vars(array(
259
			'DONATION_BODY'                  => $entity->get_message_for_edit(),
260
			'L_DONATION_PAGES_TITLE'         => $this->user->lang[strtoupper($entity->get_name())],
261
			'L_DONATION_PAGES_TITLE_EXPLAIN' => $this->user->lang[strtoupper($entity->get_name()) . '_EXPLAIN'],
262
263
			'S_BBCODE_DISABLE_CHECKED'    => !$entity->message_bbcode_enabled(),
264
			'S_MAGIC_URL_DISABLE_CHECKED' => !$entity->message_magic_url_enabled(),
265
			'S_SMILIES_DISABLE_CHECKED'   => !$entity->message_smilies_enabled(),
266
267
			'BBCODE_STATUS'  => $this->user->lang('BBCODE_IS_ON', '<a href="' . append_sid("{$this->phpbb_root_path}faq.{$this->php_ext}", 'mode=bbcode') . '">', '</a>'),
268
			'FLASH_STATUS'   => $this->user->lang['FLASH_IS_ON'],
269
			'IMG_STATUS'     => $this->user->lang['IMAGES_ARE_ON'],
270
			'SMILIES_STATUS' => $this->user->lang['SMILIES_ARE_ON'],
271
			'URL_STATUS'     => $this->user->lang['URL_IS_ON'],
272
273
			'S_BBCODE_ALLOWED'  => true,
274
			'S_BBCODE_FLASH'    => true,
275
			'S_BBCODE_IMG'      => true,
276
			'S_LINKS_ALLOWED'   => true,
277
			'S_SMILIES_ALLOWED' => true,
278
			'S_HIDDEN_FIELDS'   => '<input type="hidden" name="page_title" value="' . $entity->get_name() . '" />',
279
		));
280
281
		// Display custom bbcodes and smilies
282
		$this->include_custom_bbcodes($entity->message_bbcode_enabled());
283
		$this->include_smileys($entity->message_smilies_enabled());
284
	}
285
286
	/**
287
	 * @param $bbcode_enabled
288
	 *
289
	 * @return null
290
	 * @access private
291
	 */
292
	private function include_custom_bbcodes($bbcode_enabled)
293
	{
294
		if ($bbcode_enabled)
295
		{
296
			include_once($this->phpbb_root_path . 'includes/functions_display.' . $this->php_ext);
297
			display_custom_bbcodes();
298
		}
299
	}
300
301
	/**
302
	 * @param $smilies_enabled
303
	 *
304
	 * @return null
305
	 * @access private
306
	 */
307
	private function include_smileys($smilies_enabled)
308
	{
309
		if ($smilies_enabled)
310
		{
311
			include_once($this->phpbb_root_path . 'includes/functions_posting.' . $this->php_ext);
312
			generate_smilies('inline', 0);
313
		}
314
	}
315
316
	/**
317
	 * Get parse options of the message
318
	 *
319
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
320
	 * @param array                              $data   The form data to be processed
321
	 * @param string                             $type
322
	 *
323
	 * @return array
324
	 * @access private
325
	 */
326
	private function get_message_parse_options($entity, $data, $type)
327
	{
328
		return array($type => $this->submit_or_preview($this->submit, $this->preview) ? $data[$type] : (bool) call_user_func(array($entity, 'message_' . $type . '_enabled')));
329
	}
330
331
	/**
332
	 * Assign vars to the template if preview is true.
333
	 *
334
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
335
	 * @param                                    $errors
336
	 *
337
	 * @access private
338
	 */
339
	private function assign_preview_template_vars($entity, $errors)
340
	{
341
		if ($this->preview && empty($errors))
342
		{
343
			// Set output vars for display in the template
344
			$this->template->assign_vars(array(
345
				'PPDE_DP_PREVIEW'   => $entity->replace_template_vars($entity->get_message_for_display()),
346
				'S_PPDE_DP_PREVIEW' => $this->preview,
347
			));
348
		}
349
	}
350
351
	/**
352
	 * Assign Predefined variables to a template block_vars
353
	 *
354
	 * @param array $vars
355
	 *
356
	 * @return null
357
	 * @access   private
358
	 */
359
	private function assign_predefined_block_vars($vars)
360
	{
361
		for ($i = 0, $size = sizeof($vars); $i < $size; $i++)
362
		{
363
			$this->template->assign_block_vars('dp_vars', array(
364
					'NAME'     => $vars[$i]['name'],
365
					'VARIABLE' => $vars[$i]['var'],
366
					'EXAMPLE'  => $vars[$i]['value'])
367
			);
368
		}
369
	}
370
371
	/**
372
	 *  Submit data to the database
373
	 *
374
	 * @param \skouat\ppde\entity\donation_pages $entity The donation pages entity object
375
	 * @param array                              $errors
376
	 *
377
	 * @return null
378
	 * @access private
379
	 */
380
	private function submit_data($entity, array $errors)
381
	{
382
		if ($this->can_submit_data($errors))
383
		{
384
			$this->trigger_error_data_already_exists($entity);
385
386
			// Grab the local language name
387
			$this->get_lang_local_name($this->ppde_operator->get_languages($entity->get_lang_id()));
388
389
			$log_action = $this->add_edit_data($entity);
390
			// Log and show user confirmation of the saved item and provide link back to the previous page
391
			$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));
392
			trigger_error($this->user->lang($this->lang_key_prefix . '_' . strtoupper($log_action), $this->lang_local_name) . adm_back_link($this->u_action));
393
		}
394
	}
395
396
	/**
397
	 * Get Local lang name
398
	 *
399
	 * @param array $langs
400
	 *
401
	 * @return null
402
	 * @access private
403
	 */
404
	private function get_lang_local_name($langs)
405
	{
406
		foreach ($langs as $lang)
407
		{
408
			$this->lang_local_name = $lang['name'];
409
		}
410
	}
411
412
	/**
413
	 * Edit a donation page
414
	 *
415
	 * @param int $page_id Donation page identifier
416
	 *
417
	 * @return null
418
	 * @access public
419
	 */
420
	public function edit_donation_page($page_id)
421
	{
422
		// Add form key
423
		add_form_key('add_edit_' . $this->module_name);
424
425
		// Initiate a page donation entity
426
		/** @type \skouat\ppde\entity\donation_pages $entity */
427
		$entity = $this->get_container_entity();
428
		$entity->load($page_id);
429
430
		// Collect the form data
431
		$data = array(
432
			'page_id'      => (int) $page_id,
433
			'page_title'   => $this->request->variable('page_title', $entity->get_name(), false),
434
			'page_lang_id' => $this->request->variable('page_lang_id', $entity->get_lang_id()),
435
			'page_content' => $this->request->variable('page_content', $entity->get_message_for_edit(), true),
436
			'bbcode'       => !$this->request->variable('disable_bbcode', false),
437
			'magic_url'    => !$this->request->variable('disable_magic_url', false),
438
			'smilies'      => !$this->request->variable('disable_smilies', false),
439
		);
440
441
		// Set template vars for language select menu
442
		$this->create_language_options($data['page_lang_id']);
443
444
		// Process the new page
445
		$this->add_edit_donation_page_data($entity, $data);
446
447
		// Set output vars for display in the template
448
		$this->template->assign_vars(array(
449
			'S_EDIT_DONATION_PAGE' => true,
450
			'U_EDIT_ACTION'        => $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $page_id,
451
			'U_BACK'               => $this->u_action,
452
		));
453
	}
454
455
	/**
456
	 * Delete a donation page
457
	 *
458
	 * @param int $page_id The donation page identifier to delete
459
	 *
460
	 * @return null
461
	 * @access public
462
	 */
463
	public function delete_donation_page($page_id)
464
	{
465
		// Initiate an entity and load data
466
		/** @type \skouat\ppde\entity\donation_pages $entity */
467
		$entity = $this->get_container_entity();
468
		$entity->load($page_id);
469
470
		// Before deletion, grab the local language name
471
		$this->get_lang_local_name($this->ppde_operator->get_languages($entity->get_lang_id()));
472
473
		$entity->delete($page_id);
474
475
		// Log the action
476
		$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));
477
478
		// If AJAX was used, show user a result message
479
		$message = $this->user->lang($this->lang_key_prefix . '_DELETED', $this->lang_local_name);
480
		$this->ajax_delete_result_message($message);
481
	}
482
}
483