Completed
Branch master (915db5)
by Mario
03:03
created

admin_settings_controller::display_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 2
Metric Value
c 8
b 0
f 2
dl 0
loc 49
rs 9.2258
cc 1
eloc 32
nc 1
nop 0
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 string                   module_name        Name of the module currently used
20
 * @property \phpbb\request\request   request            Request object
21
 * @property bool                     submit             State of submit $_POST variable
22
 * @property \phpbb\template\template template           Template object
23
 * @property string                   u_action           Action URL
24
 * @property \phpbb\user              user               User object
25
 */
26
class admin_settings_controller extends admin_main
27
{
28
	protected $config;
29
	protected $ppde_controller_main;
30
	protected $ppde_operator_currency;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param \phpbb\config\config                    $config                 Config object
36
	 * @param ContainerInterface                      $container              Service container interface
37
	 * @param \skouat\ppde\controller\main_controller $ppde_controller_main   Main controller object
38
	 * @param \skouat\ppde\operators\currency         $ppde_operator_currency Operator object
39
	 * @param \phpbb\request\request                  $request                Request object
40
	 * @param \phpbb\template\template                $template               Template object
41
	 * @param \phpbb\user                             $user                   User object
42
	 *
43
	 * @access public
44
	 */
45
	public function __construct(\phpbb\config\config $config, ContainerInterface $container, \skouat\ppde\controller\main_controller $ppde_controller_main, \skouat\ppde\operators\currency $ppde_operator_currency, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user)
46
	{
47
		$this->config = $config;
48
		$this->container = $container;
49
		$this->ppde_controller_main = $ppde_controller_main;
50
		$this->ppde_operator_currency = $ppde_operator_currency;
51
		$this->request = $request;
52
		$this->template = $template;
53
		$this->user = $user;
54
		parent::__construct(
55
			'settings',
56
			'PPDE_SETTINGS',
57
			''
58
		);
59
	}
60
61
	/**
62
	 * Display the general settings a user can configure for this extension
63
	 *
64
	 * @return null
65
	 * @access public
66
	 */
67
	public function display_settings()
68
	{
69
		$this->ppde_controller_main->first_start();
70
71
		// Define the name of the form for use as a form key
72
		add_form_key('ppde_settings');
73
74
		// Create an array to collect errors that will be output to the user
75
		$errors = array();
76
77
		$this->submit_settings();
78
79
		// Set output vars for display in the template
80
		$this->s_error_assign_template_vars($errors);
81
		$this->u_action_assign_template_vars();
82
		$this->template->assign_vars(array(
83
			// Global Settings vars
84
			'PPDE_ACCOUNT_ID'                => $this->check_config($this->config['ppde_account_id'], 'string', ''),
85
			'PPDE_DEFAULT_CURRENCY'          => $this->container->get('skouat.ppde.controller')->build_currency_select_menu($this->config['ppde_default_currency']),
86
			'PPDE_DEFAULT_VALUE'             => $this->check_config($this->config['ppde_default_value'], 'integer', 0),
87
			'PPDE_DROPBOX_VALUE'             => $this->check_config($this->config['ppde_dropbox_value'], 'string', '1,2,3,4,5,10,20,25,50,100'),
88
			'S_PPDE_DROPBOX_ENABLE'          => $this->check_config($this->config['ppde_dropbox_enable']),
89
			'S_PPDE_ENABLE'                  => $this->check_config($this->config['ppde_enable']),
90
			'S_PPDE_HEADER_LINK'             => $this->check_config($this->config['ppde_header_link']),
91
92
			// PayPal IPN vars
93
			'S_PPDE_IPN_AG_ENABLE'           => $this->check_config($this->config['ppde_ipn_autogroup_enable']),
94
			'S_PPDE_IPN_AG_GROUP_AS_DEFAULT' => $this->check_config($this->config['ppde_ipn_group_as_default']),
95
			'S_PPDE_IPN_DL_ENABLE'           => $this->check_config($this->config['ppde_ipn_donorlist_enable']),
96
			'S_PPDE_IPN_ENABLE'              => $this->check_config($this->config['ppde_ipn_enable']),
97
			'S_PPDE_IPN_GROUP_OPTIONS'       => group_select_options($this->config['ppde_ipn_group_id']),
98
			'S_PPDE_IPN_LOGGING'             => $this->check_config($this->config['ppde_ipn_logging']),
99
			'S_PPDE_IPN_NOTIFICATION_ENABLE' => $this->check_config($this->config['ppde_ipn_notification_enable']),
100
101
			// Sandbox Settings vars
102
			'PPDE_SANDBOX_ADDRESS'           => $this->check_config($this->config['ppde_sandbox_address'], 'string', ''),
103
			'S_PPDE_SANDBOX_ENABLE'          => $this->check_config($this->config['ppde_sandbox_enable']),
104
			'S_PPDE_SANDBOX_FOUNDER_ENABLE'  => $this->check_config($this->config['ppde_sandbox_founder_enable'], 'boolean', true),
105
106
			// Statistics Settings vars
107
			'PPDE_RAISED'                    => $this->check_config($this->config['ppde_raised'], 'float', 0),
108
			'PPDE_GOAL'                      => $this->check_config($this->config['ppde_goal'], 'float', 0),
109
			'PPDE_USED'                      => $this->check_config($this->config['ppde_used'], 'float', 0),
110
			'S_PPDE_STATS_INDEX_ENABLE'      => $this->check_config($this->config['ppde_stats_index_enable']),
111
			'S_PPDE_RAISED_ENABLE'           => $this->check_config($this->config['ppde_raised_enable']),
112
			'S_PPDE_GOAL_ENABLE'             => $this->check_config($this->config['ppde_goal_enable']),
113
			'S_PPDE_USED_ENABLE'             => $this->check_config($this->config['ppde_used_enable']),
114
		));
115
	}
116
117
	/**
118
	 * The form submitting if 'submit' is true
119
	 *
120
	 * @return null
121
	 * @access private
122
	 */
123
	private function submit_settings()
124
	{
125
		$this->submit = $this->request->is_set_post('submit');
126
127
		// Test if the submitted form is valid
128
		$errors = $this->is_invalid_form('ppde_settings', $this->submit);
129
130
		if ($this->can_submit_data($errors))
131
		{
132
			// Set the options the user configured
133
			$this->set_settings();
134
135
			// Add option settings change action to the admin log
136
			$phpbb_log = $this->container->get('log');
137
			$phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED');
138
139
			// Option settings have been updated and logged
140
			// Confirm this to the user and provide link back to previous page
141
			trigger_error($this->user->lang($this->lang_key_prefix . '_SAVED') . adm_back_link($this->u_action));
142
		}
143
	}
144
145
	/**
146
	 * Set the options a user can configure
147
	 *
148
	 * @return null
149
	 * @access private
150
	 */
151
	private function set_settings()
152
	{
153
		// Set options for Global settings
154
		$this->config->set('ppde_default_currency', $this->request->variable('ppde_default_currency', 0));
155
		$this->config->set('ppde_default_value', $this->request->variable('ppde_default_value', 0));
156
		$this->config->set('ppde_dropbox_enable', $this->request->variable('ppde_dropbox_enable', false));
157
		$this->config->set('ppde_dropbox_value', $this->clean_items_list($this->request->variable('ppde_dropbox_value', '1,2,3,4,5,10,20,25,50,100')));
158
		$this->config->set('ppde_enable', $this->request->variable('ppde_enable', false));
159
		$this->config->set('ppde_header_link', $this->request->variable('ppde_header_link', false));
160
161
		// Set options for PayPal IPN
162
		$this->config->set('ppde_ipn_autogroup_enable', $this->request->variable('ppde_ipn_autogroup_enable', false));
163
		$this->config->set('ppde_ipn_donorlist_enable', $this->request->variable('ppde_ipn_donorlist_enable', false));
164
		$this->config->set('ppde_ipn_enable', $this->request->variable('ppde_ipn_enable', false));
165
		$this->config->set('ppde_ipn_group_as_default', $this->request->variable('ppde_ipn_group_as_default', false));
166
		$this->config->set('ppde_ipn_group_id', $this->request->variable('ppde_ipn_group_id', 0));
167
		$this->config->set('ppde_ipn_logging', $this->request->variable('ppde_ipn_logging', false));
168
		$this->config->set('ppde_ipn_notification_enable', $this->request->variable('ppde_ipn_notification_enable', false));
169
170
		// Set options for Sandbox Settings
171
		$this->config->set('ppde_sandbox_enable', $this->request->variable('ppde_sandbox_enable', false));
172
		$this->config->set('ppde_sandbox_founder_enable', $this->request->variable('ppde_sandbox_founder_enable', true));
173
174
		// Set options for Statistics Settings
175
		$this->config->set('ppde_stats_index_enable', $this->request->variable('ppde_stats_index_enable', false));
176
		$this->config->set('ppde_raised_enable', $this->request->variable('ppde_raised_enable', false));
177
		$this->config->set('ppde_raised', $this->request->variable('ppde_raised', 0.0));
178
		$this->config->set('ppde_goal_enable', $this->request->variable('ppde_goal_enable', false));
179
		$this->config->set('ppde_goal', $this->request->variable('ppde_goal', 0.0));
180
		$this->config->set('ppde_used_enable', $this->request->variable('ppde_used_enable', false));
181
		$this->config->set('ppde_used', $this->request->variable('ppde_used', 0.0));
182
183
		// Set misc settings
184
		$this->ppde_controller_main->set_curl_info();
185
		$this->ppde_controller_main->set_remote_detected();
186
187
		// Settings with dependencies are the last to be set.
188
		$this->config->set('ppde_account_id', $this->required_settings($this->request->variable('ppde_account_id', ''), $this->depend_on('ppde_enable')));
189
		$this->config->set('ppde_sandbox_address', $this->required_settings($this->request->variable('ppde_sandbox_address', ''), $this->depend_on('ppde_sandbox_enable')));
190
	}
191
192
	/**
193
	 * Clean items list to conserve only numeric values
194
	 *
195
	 * @param string $config_value
196
	 *
197
	 * @return string
198
	 * @access private
199
	 */
200
	private function clean_items_list($config_value)
201
	{
202
		$items_list = explode(',', $config_value);
203
		$merge_items = array();
204
205
		foreach ($items_list as $item)
206
		{
207
			if (settype($item, 'integer') && $item != 0)
208
			{
209
				$merge_items[] = $item;
210
			}
211
		}
212
		unset($items_list, $item);
213
214
		natsort($merge_items);
215
216
		return $this->check_config(implode(',', array_unique($merge_items)), 'string', '');
217
	}
218
219
	/**
220
	 * Check if a config value is true
221
	 *
222
	 * @param mixed  $config Config value
223
	 * @param string $type   (see settype())
224
	 * @param mixed  $default
225
	 *
226
	 * @return mixed
227
	 * @access private
228
	 */
229
	private function check_config($config, $type = 'boolean', $default = '')
230
	{
231
		// We're using settype to enforce data types
232
		settype($config, $type);
233
		settype($default, $type);
234
235
		return $config ? $config : $default;
236
	}
237
238
	/**
239
	 * Check if settings is required
240
	 *
241
	 * @param $settings
242
	 * @param $depend_on
243
	 *
244
	 * @return mixed
245
	 * @access private
246
	 */
247
	private function required_settings($settings, $depend_on)
248
	{
249 View Code Duplication
		if (empty($settings) && $depend_on == true)
250
		{
251
			trigger_error($this->user->lang($this->lang_key_prefix . '_MISSING') . adm_back_link($this->u_action), E_USER_WARNING);
252
		}
253
254
		return $settings;
255
	}
256
257
	/**
258
	 * Check if a settings depend on another.
259
	 *
260
	 * @param $config_name
261
	 *
262
	 * @return bool
263
	 * @access private
264
	 */
265
	private function depend_on($config_name)
266
	{
267
		return !empty($this->config[$config_name]) ? (bool) $this->config[$config_name] : false;
268
	}
269
}
270