Passed
Push — develop-3.3.x ( 95f552...b7db6e )
by Mario
03:10
created

vars::add_predefined_lang_vars()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
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\actions;
12
13
use phpbb\config\config;
14
use phpbb\language\language;
15
use phpbb\user;
16
17
class vars
18
{
19
	protected $actions_core;
20
	protected $actions_currency;
21
	protected $config;
22
	protected $dp_vars;
23
	protected $language;
24
	protected $user;
25
26
	/**
27
	 * Constructor
28
	 *
29
	 * @param \skouat\ppde\actions\core     $actions_core     PPDE actions core object
30
	 * @param \skouat\ppde\actions\currency $actions_currency PPDE actions currency object
31
	 * @param config                        $config           Config object
32
	 * @param language                      $language         Language object
33
	 * @param user                          $user             User object
34
	 */
35
	public function __construct(
36
		core $actions_core,
37
		currency $actions_currency,
38
		config $config,
39
		language $language,
40
		user $user
41
	)
42
	{
43
		$this->actions_core = $actions_core;
44
		$this->actions_currency = $actions_currency;
45
		$this->config = $config;
46
		$this->language = $language;
47
		$this->user = $user;
48
	}
49
50
	/**
51
	 * Get template vars
52
	 *
53
	 * @return array Array of template variables
54
	 */
55
	public function get_vars(): array
56
	{
57
		$this->actions_currency->set_default_currency_data((int) $this->config['ppde_default_currency']);
58
		$this->dp_vars = $this->populate_template_vars();
59
60
		if ($this->actions_core->is_in_admin())
61
		{
62
			$this->add_predefined_lang_vars();
63
		}
64
65
		return $this->dp_vars;
66
	}
67
68
	/**
69
	 * Populate template vars
70
	 *
71
	 * @return array Array of template variables
72
	 */
73
	private function populate_template_vars(): array
74
	{
75
		return [
76
			['var' => '{USER_ID}',         'value' => $this->user->data['user_id']],
77
			['var' => '{USERNAME}',        'value' => $this->user->data['username']],
78
			['var' => '{SITE_NAME}',       'value' => $this->config['sitename']],
79
			['var' => '{SITE_DESC}',       'value' => $this->config['site_desc']],
80
			['var' => '{BOARD_CONTACT}',   'value' => $this->config['board_contact']],
81
			['var' => '{BOARD_EMAIL}',     'value' => $this->config['board_email']],
82
			['var' => '{BOARD_SIG}',       'value' => $this->config['board_email_sig']],
83
			['var' => '{DONATION_GOAL}',   'value' => $this->actions_currency->format_currency((float) $this->config['ppde_goal'])],
84
			['var' => '{DONATION_RAISED}', 'value' => $this->actions_currency->format_currency((float) $this->config['ppde_raised'])],
85
		];
86
	}
87
88
	/**
89
	 * Adds predefined language keys variables to the donation pages.
90
	 */
91
	private function add_predefined_lang_vars(): void
92
	{
93
		foreach ($this->dp_vars as &$value)
94
		{
95
			$value['name'] = $this->language->lang('PPDE_DP_' . trim($value['var'], '{}'));
96
		}
97
	}
98
99
	/**
100
	 * Replace template vars in the message
101
	 *
102
	 * @param string $message The message containing template variables
103
	 * @return string The message with template variables replaced
104
	 */
105
	public function replace_template_vars(string $message): string
106
	{
107
		$tpl_ary = array_column($this->dp_vars, 'value', 'var');
108
		return strtr($message, $tpl_ary);
109
	}
110
}
111