Passed
Push — develop-3.3.x ( eb02c6...696004 )
by Mario
02:37
created

admin_main::can_submit_data()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 1
nc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller\admin;
12
13
use skouat\ppde\controller\ipn_paypal;
14
15
abstract class admin_main
16
{
17
	protected const SECONDS_IN_A_DAY = 86400;
18
19
	/** @var array */
20
	protected $args = [];
21
	/** @var \phpbb\config\config */
22
	protected $config;
23
	/** @var \Symfony\Component\DependencyInjection\ContainerInterface */
24
	protected $container;
25
	/** @var string */
26
	protected $id_prefix_name;
27
	/** @var string */
28
	protected $lang_key_prefix;
29
	/** @var \phpbb\language\language */
30
	protected $language;
31
	/** @var \phpbb\log\log */
32
	protected $log;
33
	/** @var string */
34
	protected $module_name;
35
	/** @var \skouat\ppde\actions\locale_icu */
36
	protected $ppde_actions_locale;
37
	/** @var ipn_paypal */
38
	protected $ppde_ipn_paypal;
39
	/** @var bool */
40
	protected $preview;
41
	/** @var \phpbb\request\request */
42
	protected $request;
43
	/** @var bool */
44
	protected $submit;
45
	/** @var \phpbb\template\template */
46
	protected $template;
47
	/** @var \phpbb\user */
48
	protected $user;
49
50
	/** @var string */
51
	public $u_action;
52
53
	/**
54
	 * Constructor
55
	 *
56
	 * @param string $lang_key_prefix Prefix for the messages thrown by exceptions
57
	 * @param string $id_prefix_name  Prefix name for identifier in the URL
58
	 * @param string $module_name     Name of the module currently used
59
	 *
60
	 * @access public
61
	 */
62
	public function __construct($module_name, $lang_key_prefix, $id_prefix_name)
63
	{
64
		$this->module_name = $module_name;
65
		$this->lang_key_prefix = $lang_key_prefix;
66
		$this->id_prefix_name = $id_prefix_name;
67
	}
68
69
	/**
70
	 * Set page url
71
	 *
72
	 * @param string $u_action Custom form action
73
	 *
74
	 * @return void
75
	 * @access public
76
	 */
77
	public function set_page_url($u_action): void
78
	{
79
		$this->u_action = $u_action;
80
	}
81
82
	/**
83
	 * Gets vars from POST then build a array of them
84
	 *
85
	 * @param string $id     Module id
86
	 * @param string $mode   Module categorie
87
	 * @param string $action Action name
88
	 *
89
	 * @return void
90
	 * @access private
91
	 */
92
	public function set_hidden_fields($id, $mode, $action): void
93
	{
94
		$this->args = array_merge($this->args, [
95
			'i'             => $id,
96
			'mode'          => $mode,
97
			'action'        => $action,
98
			'hidden_fields' => [],
99
		]);
100
	}
101
102
	public function get_hidden_fields(): array
103
	{
104
		return count($this->args) ? array_merge(
105
			['i'                           => $this->args['i'],
106
			 'mode'                        => $this->args['mode'],
107
			 'action'                      => $this->args['action'],
108
			 $this->id_prefix_name . '_id' => $this->args[$this->id_prefix_name . '_id']],
109
			$this->args['hidden_fields']) : ['id' => '', 'mode' => '', 'action' => ''];
110
	}
111
112
	public function set_action($action): void
113
	{
114
		$this->args['action'] = $action;
115
	}
116
117
	public function get_action(): string
118
	{
119
		return (string) ($this->args['action'] ?? '');
120
	}
121
122
	public function set_item_id($item_id): void
123
	{
124
		$this->args[$this->id_prefix_name . '_id'] = (int) $item_id;
125
	}
126
127
	/**
128
	 * Display items of the called controller
129
	 *
130
	 * @return void
131
	 * @access public
132
	 */
133
	public function display(): void
134
	{
135
	}
136
137
	/**
138
	 * Add item for the called controller
139
	 *
140
	 * @return void
141
	 * @access public
142
	 */
143
	public function add(): void
144
	{
145
	}
146
147
	public function approve(): void
148
	{
149
	}
150
151
	/**
152
	 * Change item details for the called controller
153
	 *
154
	 * @return void
155
	 * @access public
156
	 */
157
	public function change(): void
158
	{
159
	}
160
161
	/**
162
	 * Delete item for the called controller
163
	 *
164
	 * @return void
165
	 * @access public
166
	 */
167
	public function delete(): void
168
	{
169
	}
170
171
	/**
172
	 * Edit item on the called controller
173
	 *
174
	 * @return void
175
	 * @access public
176
	 */
177
	public function edit(): void
178
	{
179
	}
180
181
	/**
182
	 * Enable/disable item on the called controller
183
	 *
184
	 * @return void
185
	 * @access public
186
	 */
187
	public function enable(): void
188
	{
189
	}
190
191
	/**
192
	 * Move up/down an item on the called controller
193
	 *
194
	 * @return void
195
	 * @access   public
196
	 */
197
	public function move(): void
198
	{
199
	}
200
201
	/**
202
	 * View a selected item on the called controller
203
	 *
204
	 * @return void
205
	 * @access   public
206
	 */
207
	public function view(): void
208
	{
209
	}
210
211
	/**
212
	 * Build pull down menu options of available remote URI
213
	 *
214
	 * @param int    $default ID of the selected value.
215
	 * @param string $type    Can be 'live' or 'sandbox'
216
	 *
217
	 * @return void
218
	 * @access public
219
	 */
220
	public function build_remote_uri_select_menu($default, $type): void
221
	{
222
		$type = $this->force_type($type);
223
224
		// Grab the list of remote uri for selected type
225
		$remote_list = ipn_paypal::get_remote_uri();
226
227
		// Process each menu item for pull-down
228
		foreach ($remote_list as $id => $remote)
229
		{
230
			if ($remote['type'] !== $type)
231
			{
232
				continue;
233
			}
234
235
			// Set output block vars for display in the template
236
			$this->template->assign_block_vars('remote_options', [
237
				'REMOTE_ID'   => (int) $id,
238
				'REMOTE_NAME' => $remote['hostname'],
239
				'S_DEFAULT'   => (int) $default === (int) $id,
240
			]);
241
		}
242
		unset ($remote_list, $id);
243
	}
244
245
	/**
246
	 * Enforce the type of remote provided
247
	 *
248
	 * @param string $type
249
	 *
250
	 * @return string
251
	 * @access private
252
	 */
253
	private function force_type($type): string
254
	{
255
		return $type === 'live' || $type === 'sandbox' ? (string) $type : 'live';
256
	}
257
258
	/**
259
	 * The form submitting if 'submit' is true
260
	 *
261
	 * @return void
262
	 * @access protected
263
	 */
264
	protected function submit_settings(): void
265
	{
266
		$this->submit = $this->is_form_submitted();
267
268
		// Test if the submitted form is valid
269
		$errors = $this->is_invalid_form('ppde_' . $this->module_name, $this->submit);
270
271
		if ($this->can_submit_data($errors))
272
		{
273
			// Set the options the user configured
274
			$this->set_settings();
275
276
			// Add option settings change action to the admin log
277
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED');
278
279
			// Option settings have been updated and logged
280
			// Confirm this to the user and provide link back to previous page
281
			trigger_error($this->language->lang($this->lang_key_prefix . '_SAVED') . adm_back_link($this->u_action));
282
		}
283
	}
284
285
	/**
286
	 * Check if form is valid or not
287
	 *
288
	 * @param string $form_name
289
	 * @param bool   $submit_or_preview
290
	 *
291
	 * @return array
292
	 * @access protected
293
	 */
294
	protected function is_invalid_form($form_name, $submit_or_preview = false): array
295
	{
296
		if ($submit_or_preview && !check_form_key($form_name))
297
		{
298
			return [$this->language->lang('FORM_INVALID')];
299
		}
300
301
		return [];
302
	}
303
304
	/**
305
	 * @param array $errors
306
	 *
307
	 * @return bool
308
	 * @access protected
309
	 */
310
	protected function can_submit_data(array $errors): bool
311
	{
312
		return $this->submit && empty($errors) && !$this->preview;
313
	}
314
315
	/**
316
	 * Set the options for called controller
317
	 *
318
	 * @return void
319
	 * @access protected
320
	 */
321
	protected function set_settings(): void
322
	{
323
	}
324
325
	/**
326
	 * Trigger error message if data already exists
327
	 *
328
	 * @param \skouat\ppde\entity\main $entity The entity object
329
	 *
330
	 * @return void
331
	 * @access protected
332
	 */
333
	protected function trigger_error_data_already_exists(\skouat\ppde\entity\main $entity): void
334
	{
335
		if ($this->is_added_data_exists($entity))
336
		{
337
			// Show user warning for an already exist page and provide link back to the edit page
338
			$message = $this->language->lang($this->lang_key_prefix . '_EXISTS');
339
			$message .= '<br><br>';
340
			$message .= $this->language->lang($this->lang_key_prefix . '_GO_TO_PAGE', '<a href="' . $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $entity->get_id() . '">&raquo; ', '</a>');
341
			trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING);
342
		}
343
	}
344
345
	/**
346
	 * @param \skouat\ppde\entity\main $entity The entity object
347
	 *
348
	 * @return bool
349
	 * @access protected
350
	 */
351
	protected function is_added_data_exists(\skouat\ppde\entity\main $entity): bool
352
	{
353
		return $entity->data_exists($entity->build_sql_data_exists()) && $this->request->variable('action', '') === 'add';
354
	}
355
356
	/**
357
	 * Check some settings before submitting data
358
	 *
359
	 * @param \skouat\ppde\entity\main $entity            The entity object
360
	 * @param string                   $field_name        Name of the entity function to call
361
	 * @param string|int               $value_cmp         Default value to compare with the return value of the called function
362
	 * @param bool                     $submit_or_preview Form submit or preview status
363
	 *
364
	 * @return array $errors
365
	 * @access protected
366
	 */
367
	protected function is_empty_data(\skouat\ppde\entity\main $entity, $field_name, $value_cmp, $submit_or_preview = false): array
368
	{
369
		$errors = [];
370
371
		if ($submit_or_preview && $entity->{'get_' . $field_name}() == $value_cmp)
372
		{
373
			$errors[] = $this->language->lang($this->lang_key_prefix . '_EMPTY_' . strtoupper($field_name));
374
		}
375
376
		return $errors;
377
	}
378
379
	/**
380
	 * Get result of submit and preview expression
381
	 *
382
	 * @param bool $submit
383
	 * @param bool $preview
384
	 *
385
	 * @return bool
386
	 * @access protected
387
	 */
388
	protected function submit_or_preview(bool $submit = false, bool $preview = false): bool
389
	{
390
		return $submit || $preview;
391
	}
392
393
	/**
394
	 * Checks if the form has been submitted.
395
	 *
396
	 * @return bool Returns true if the form has been submitted, false otherwise.
397
	 */
398
	protected function is_form_submitted(): bool
399
	{
400
		return $this->request->is_set_post('submit');
401
	}
402
403
	/**
404
	 * Show user a result message if AJAX was used
405
	 *
406
	 * @param string $message Text message to show to the user
407
	 *
408
	 * @return void
409
	 * @access protected
410
	 */
411
	protected function ajax_delete_result_message($message = ''): void
412
	{
413
		if ($this->request->is_ajax())
414
		{
415
			$json_response = new \phpbb\json_response;
416
			$json_response->send([
417
				'MESSAGE_TITLE' => $this->language->lang('INFORMATION'),
418
				'MESSAGE_TEXT'  => $message,
419
				'REFRESH_DATA'  => [
420
					'time' => 3,
421
				],
422
			]);
423
		}
424
	}
425
426
	/**
427
	 * Set u_action output vars for display in the template
428
	 *
429
	 * @return void
430
	 * @access protected
431
	 */
432
	protected function u_action_assign_template_vars(): void
433
	{
434
		$this->template->assign_vars([
435
			'U_ACTION' => $this->u_action,
436
		]);
437
	}
438
439
	/**
440
	 * Set add/edit action output vars for display in the template
441
	 *
442
	 * @param string  $type Action type: 'add' or 'edit'
443
	 * @param integer $id   Identifier to Edit. If action = add, then let to '0'.
444
	 *
445
	 * @return void
446
	 * @access protected
447
	 */
448
	protected function add_edit_action_assign_template_vars($type, $id = 0): void
449
	{
450
		$id_action = !empty($id) ? '&amp;' . $this->id_prefix_name . '_id=' . (int) $id : '';
451
452
		$this->template->assign_vars([
453
			'S_ADD_EDIT' => true,
454
			'U_ACTION'   => $this->u_action . '&amp;action=' . $type . $id_action,
455
			'U_BACK'     => $this->u_action,
456
		]);
457
	}
458
459
	/**
460
	 * Set error output vars for display in the template
461
	 *
462
	 * @param array $errors
463
	 *
464
	 * @return void
465
	 * @access protected
466
	 */
467
	protected function s_error_assign_template_vars($errors): void
468
	{
469
		$this->template->assign_vars([
470
			'S_ERROR'   => (bool) count($errors),
471
			'ERROR_MSG' => (count($errors)) ? implode('<br>', $errors) : '',
472
		]);
473
	}
474
475
	/**
476
	 * Check if a config value is true
477
	 *
478
	 * @param mixed  $config Config value
479
	 * @param string $type   (see settype())
480
	 * @param mixed  $default
481
	 *
482
	 * @return mixed
483
	 * @access protected
484
	 */
485
	protected function check_config($config, $type = 'boolean', $default = '')
486
	{
487
		// We're using settype to enforce data types
488
		settype($config, $type);
489
		settype($default, $type);
490
491
		return $config ?: $default;
492
	}
493
494
	/**
495
	 * Check if settings is required
496
	 *
497
	 * @param $settings
498
	 * @param $depend_on
499
	 *
500
	 * @return mixed
501
	 * @access protected
502
	 */
503
	protected function required_settings($settings, $depend_on)
504
	{
505
		if (empty($settings) && (bool) $depend_on === true)
506
		{
507
			trigger_error($this->language->lang($this->lang_key_prefix . '_MISSING') . adm_back_link($this->u_action), E_USER_WARNING);
508
		}
509
510
		return $settings;
511
	}
512
513
	/**
514
	 * Run system checks if config 'ppde_first_start' is true
515
	 *
516
	 * @return void
517
	 * @throws \ReflectionException
518
	 * @access protected
519
	 */
520
	protected function ppde_first_start(): void
521
	{
522
		if ($this->config['ppde_first_start'])
523
		{
524
			$this->ppde_ipn_paypal->set_curl_info();
525
			$this->ppde_ipn_paypal->set_remote_detected();
526
			$this->ppde_ipn_paypal->check_tls();
527
			$this->ppde_actions_locale->set_intl_info();
528
			$this->ppde_actions_locale->set_intl_detected();
529
			$this->config->set('ppde_first_start', '0');
530
		}
531
	}
532
}
533