Passed
Push — develop-3.3.x ( 22a788...198f49 )
by Mario
02:53
created

admin_main::depend_on()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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