Passed
Branch develop-3.3.x-donors_module (d1eba4)
by Mario
03:56
created

settings_controller::display_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 0
dl 0
loc 38
rs 9.504
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 phpbb\config\config;
14
use phpbb\language\language;
15
use phpbb\log\log;
16
use phpbb\request\request;
17
use phpbb\template\template;
18
use phpbb\user;
19
use skouat\ppde\actions\auth;
20
use skouat\ppde\actions\currency;
21
use skouat\ppde\actions\locale_icu;
22
23
/**
24
 * @property config   config             Config object
25
 * @property string   id_prefix_name     Prefix name for identifier in the URL
26
 * @property string   lang_key_prefix    Prefix for the messages thrown by exceptions
27
 * @property language language           Language user object
28
 * @property log      log                The phpBB log system
29
 * @property string   module_name        Name of the module currently used
30
 * @property request  request            Request object
31
 * @property bool     submit             State of submit $_POST variable
32
 * @property template template           Template object
33
 * @property string   u_action           Action URL
34
 * @property user     user               User object
35
 */
36
class settings_controller extends admin_main
37
{
38
	protected $ppde_actions_auth;
39
	protected $ppde_actions_currency;
40
	protected $ppde_actions_locale;
41
	protected $php_ext;
42
	protected $phpbb_admin_path;
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param config     $config                Config object
48
	 * @param language   $language              Language user object
49
	 * @param log        $log                   The phpBB log system
50
	 * @param currency   $ppde_actions_currency PPDE currency actions object
51
	 * @param locale_icu $ppde_actions_locale   PPDE locale actions object
52
	 * @param auth       $ppde_actions_auth     PPDE auth actions object
53
	 * @param request    $request               Request object
54
	 * @param template   $template              Template object
55
	 * @param user       $user                  User object
56
	 *
57
	 * @access public
58
	 */
59
	public function __construct(
60
		config $config,
61
		language $language,
62
		log $log,
63
		auth $ppde_actions_auth,
64
		currency $ppde_actions_currency,
65
		locale_icu $ppde_actions_locale,
66
		request $request,
67
		template $template,
68
		user $user,
69
		$adm_relative_path,
70
		$phpbb_root_path,
71
		$php_ext
72
	)
73
	{
74
		$this->config = $config;
75
		$this->language = $language;
76
		$this->log = $log;
77
		$this->ppde_actions_auth = $ppde_actions_auth;
78
		$this->ppde_actions_currency = $ppde_actions_currency;
79
		$this->ppde_actions_locale = $ppde_actions_locale;
80
		$this->request = $request;
81
		$this->template = $template;
82
		$this->user = $user;
83
		$this->php_ext = $php_ext;
84
		$this->phpbb_admin_path = $phpbb_root_path . $adm_relative_path;
85
		parent::__construct(
86
			'settings',
87
			'PPDE_SETTINGS',
88
			''
89
		);
90
	}
91
92
	/**
93
	 * Display the general settings a user can configure for this extension
94
	 *
95
	 * @return void
96
	 * @access public
97
	 */
98
	public function display()
99
	{
100
		// Define the name of the form for use as a form key
101
		add_form_key('ppde_settings');
102
103
		// Create an array to collect errors that will be output to the user
104
		$errors = [];
105
106
		$this->submit_settings();
107
108
		// Set output vars for display in the template
109
		$this->s_error_assign_template_vars($errors);
110
		$this->u_action_assign_template_vars();
111
		$this->ppde_actions_currency->build_currency_select_menu((int) $this->config['ppde_default_currency']);
112
		$this->ppde_actions_locale->build_locale_select_menu($this->config['ppde_default_locale']);
113
		$this->build_remote_uri_select_menu($this->config['ppde_default_remote'], 'live');
114
		$this->build_stat_position_select_menu($this->config['ppde_stats_position']);
115
116
		$this->template->assign_vars([
117
			// Global Settings vars
118
			'PPDE_ACCOUNT_ID'           => $this->check_config($this->config['ppde_account_id'], 'string', ''),
119
			'S_PPDE_DEFAULT_LOCALE'     => $this->ppde_actions_locale->icu_requirements(),
120
			'S_PPDE_ENABLE'             => $this->check_config($this->config['ppde_enable']),
121
			'S_PPDE_HEADER_LINK'        => $this->check_config($this->config['ppde_header_link']),
122
			'S_PPDE_ALLOW_GUEST'        => $this->check_config($this->config['ppde_allow_guest'], 'boolean', false),
123
124
			// Donation Settings vars
125
			'PPDE_DEFAULT_VALUE'        => $this->check_config($this->config['ppde_default_value'], 'integer', 0),
126
			'PPDE_DROPBOX_VALUE'        => $this->check_config($this->config['ppde_dropbox_value'], 'string', '1,2,3,4,5,10,20,25,50,100'),
127
			'PPDE_TIME_EXPIRATION'      => $this->check_config($this->config['ppde_time_expiration'], 'integer', 0),
128
			'S_PPDE_DROPBOX_ENABLE'     => $this->check_config($this->config['ppde_dropbox_enable']),
129
			'U_PPDE_MORE_FEATURES'      => append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=-skouat-ppde-acp-ppde_module&amp;mode=paypal_features'),
130
131
			// Statistics Settings vars
132
			'PPDE_GOAL'                 => $this->check_config($this->config['ppde_goal'], 'float', 0),
133
			'PPDE_RAISED'               => $this->check_config($this->config['ppde_raised'], 'float', 0),
134
			'PPDE_USED'                 => $this->check_config($this->config['ppde_used'], 'float', 0),
135
			'S_PPDE_GOAL_ENABLE'        => $this->check_config($this->config['ppde_goal_enable']),
136
			'S_PPDE_RAISED_ENABLE'      => $this->check_config($this->config['ppde_raised_enable']),
137
			'S_PPDE_STATS_INDEX_ENABLE' => $this->check_config($this->config['ppde_stats_index_enable']),
138
			'S_PPDE_STATS_TEXT_ONLY'    => $this->check_config($this->config['ppde_stats_text_only']),
139
			'S_PPDE_USED_ENABLE'        => $this->check_config($this->config['ppde_used_enable']),
140
		]);
141
	}
142
143
	/**
144
	 * Set the options a user can configure
145
	 *
146
	 * @return void
147
	 * @access protected
148
	 */
149
	protected function set_settings()
150
	{
151
		// Set options for Global settings
152
		$this->config->set('ppde_allow_guest', $this->request->variable('ppde_allow_guest', false));
153
		$this->config->set('ppde_default_locale', $this->request->variable('ppde_default_locale', $this->ppde_actions_locale->locale_get_default()));
154
		$this->config->set('ppde_enable', $this->request->variable('ppde_enable', false));
155
		$this->config->set('ppde_header_link', $this->request->variable('ppde_header_link', false));
156
157
		// Set options for Donation settings
158
		$this->config->set('ppde_default_currency', $this->request->variable('ppde_default_currency', 0));
159
		$this->config->set('ppde_default_value', $this->request->variable('ppde_default_value', 0));
160
		$this->config->set('ppde_dropbox_enable', $this->request->variable('ppde_dropbox_enable', false));
161
		$this->config->set('ppde_dropbox_value', $this->rebuild_items_list($this->request->variable('ppde_dropbox_value', '1,2,3,4,5,10,20,25,50,100'), $this->config['ppde_default_value']));
162
		$this->config->set('ppde_time_expiration', $this->request->variable('ppde_time_expiration', 0));
163
164
		// Set options for Advanced settings
165
		$this->config->set('ppde_default_remote', $this->request->variable('ppde_default_remote', 0));
166
167
		// Set options for Statistics Settings
168
		$this->config->set('ppde_stats_index_enable', $this->request->variable('ppde_stats_index_enable', false));
169
		$this->config->set('ppde_stats_position', $this->request->variable('ppde_stats_position', 'bottom'));
170
		$this->config->set('ppde_stats_text_only', $this->request->variable('ppde_stats_text_only', false));
171
		$this->config->set('ppde_raised_enable', $this->request->variable('ppde_raised_enable', false));
172
		$this->config->set('ppde_raised', $this->request->variable('ppde_raised', 0.0));
173
		$this->config->set('ppde_goal_enable', $this->request->variable('ppde_goal_enable', false));
174
		$this->config->set('ppde_goal', $this->request->variable('ppde_goal', 0.0));
175
		$this->config->set('ppde_used_enable', $this->request->variable('ppde_used_enable', false));
176
		$this->config->set('ppde_used', $this->request->variable('ppde_used', 0.0));
177
178
		// Settings with dependencies are the last to be set.
179
		$this->config->set('ppde_account_id', $this->required_settings($this->request->variable('ppde_account_id', ''), $this->depend_on('ppde_enable')));
180
		$this->ppde_actions_auth->set_guest_acl();
181
	}
182
183
	/**
184
	 * Rebuild items list to conserve only numeric values
185
	 *
186
	 * @param string $config_value
187
	 * @param string $added_value
188
	 *
189
	 * @return string
190
	 * @access private
191
	 */
192
	private function rebuild_items_list($config_value, $added_value = '')
193
	{
194
		$items_list = explode(',', $config_value);
195
		$merge_items = [];
196
197
		$this->add_int_data_in_array($merge_items, $added_value);
198
199
		foreach ($items_list as $item)
200
		{
201
			$this->add_int_data_in_array($merge_items, $item);
202
		}
203
		unset($items_list, $item);
204
205
		natsort($merge_items);
206
207
		return $this->check_config(implode(',', array_unique($merge_items)), 'string', '');
208
	}
209
210
	/**
211
	 * Add integer data in an array
212
	 *
213
	 * @param array  &$array
214
	 * @param string  $var
215
	 *
216
	 * @return void
217
	 * @access private
218
	 */
219
	private function add_int_data_in_array(&$array, $var)
220
	{
221
		if (settype($var, 'integer') && $var != 0)
222
		{
223
			$array[] = $var;
224
		}
225
	}
226
227
	/**
228
	 * Build pull down menu options of available positions
229
	 *
230
	 * @param string $default Value of the selected item.
231
	 *
232
	 * @return void
233
	 * @access public
234
	 */
235
	public function build_stat_position_select_menu($default)
236
	{
237
		// List of positions allowed
238
		$positions = ['top', 'bottom', 'both'];
239
240
		// Process each menu item for pull-down
241
		foreach ($positions as $position)
242
		{
243
			// Set output block vars for display in the template
244
			$this->template->assign_block_vars('positions_options', [
245
				'POSITION_NAME' => $position,
246
				'S_DEFAULT'     => (string) $default === $position,
247
			]);
248
		}
249
		unset ($positions, $position);
250
	}
251
}
252