Completed
Push — develop ( 8a8fbb...b08540 )
by Mario
02:38
created

admin_settings_controller::depend_on()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
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 \phpbb\config\config     config             Config object
17
 * @property ContainerInterface       container          The phpBB log system
18
 * @property string                   id_prefix_name     Prefix name for identifier in the URL
19
 * @property string                   lang_key_prefix    Prefix for the messages thrown by exceptions
20
 * @property string                   module_name        Name of the module currently used
21
 * @property \phpbb\request\request   request            Request object
22
 * @property bool                     submit             State of submit $_POST variable
23
 * @property \phpbb\template\template template           Template object
24
 * @property string                   u_action           Action URL
25
 * @property \phpbb\user              user               User object
26
 */
27
class admin_settings_controller extends admin_main
28
{
29
	protected $ppde_controller_main;
30
31
	/**
32
	 * Constructor
33
	 *
34
	 * @param \phpbb\config\config                    $config                 Config object
35
	 * @param ContainerInterface                      $container              Service container interface
36
	 * @param \skouat\ppde\controller\main_controller $ppde_controller_main   Main controller object
37
	 * @param \phpbb\request\request                  $request                Request object
38
	 * @param \phpbb\template\template                $template               Template object
39
	 * @param \phpbb\user                             $user                   User object
40
	 *
41
	 * @access public
42
	 */
43
	public function __construct(\phpbb\config\config $config, ContainerInterface $container, \skouat\ppde\controller\main_controller $ppde_controller_main, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user)
44
	{
45
		$this->config = $config;
46
		$this->container = $container;
47
		$this->ppde_controller_main = $ppde_controller_main;
48
		$this->request = $request;
49
		$this->template = $template;
50
		$this->user = $user;
51
		parent::__construct(
52
			'settings',
53
			'PPDE_SETTINGS',
54
			''
55
		);
56
	}
57
58
	/**
59
	 * Display the general settings a user can configure for this extension
60
	 *
61
	 * @return void
62
	 * @access public
63
	 */
64
	public function display_settings()
65
	{
66
		$this->ppde_controller_main->first_start();
67
68
		// Define the name of the form for use as a form key
69
		add_form_key('ppde_settings');
70
71
		// Create an array to collect errors that will be output to the user
72
		$errors = array();
73
74
		$this->submit_settings();
75
76
		// Set output vars for display in the template
77
		$this->s_error_assign_template_vars($errors);
78
		$this->u_action_assign_template_vars();
79
		$this->template->assign_vars(array(
80
			// Global Settings vars
81
			'PPDE_ACCOUNT_ID'                => $this->check_config($this->config['ppde_account_id'], 'string', ''),
82
			'PPDE_DEFAULT_CURRENCY'          => $this->container->get('skouat.ppde.controller')->build_currency_select_menu($this->config['ppde_default_currency']),
83
			'PPDE_DEFAULT_VALUE'             => $this->check_config($this->config['ppde_default_value'], 'integer', 0),
84
			'PPDE_DROPBOX_VALUE'             => $this->check_config($this->config['ppde_dropbox_value'], 'string', '1,2,3,4,5,10,20,25,50,100'),
85
			'S_PPDE_DROPBOX_ENABLE'          => $this->check_config($this->config['ppde_dropbox_enable']),
86
			'S_PPDE_ENABLE'                  => $this->check_config($this->config['ppde_enable']),
87
			'S_PPDE_HEADER_LINK'             => $this->check_config($this->config['ppde_header_link']),
88
89
			// Statistics Settings vars
90
			'PPDE_RAISED'                    => $this->check_config($this->config['ppde_raised'], 'float', 0),
91
			'PPDE_GOAL'                      => $this->check_config($this->config['ppde_goal'], 'float', 0),
92
			'PPDE_USED'                      => $this->check_config($this->config['ppde_used'], 'float', 0),
93
			'S_PPDE_STATS_INDEX_ENABLE'      => $this->check_config($this->config['ppde_stats_index_enable']),
94
			'S_PPDE_RAISED_ENABLE'           => $this->check_config($this->config['ppde_raised_enable']),
95
			'S_PPDE_GOAL_ENABLE'             => $this->check_config($this->config['ppde_goal_enable']),
96
			'S_PPDE_USED_ENABLE'             => $this->check_config($this->config['ppde_used_enable']),
97
		));
98
	}
99
100
	/**
101
	 * The form submitting if 'submit' is true
102
	 *
103
	 * @return void
104
	 * @access private
105
	 */
106 View Code Duplication
	private function submit_settings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
	{
108
		$this->submit = $this->request->is_set_post('submit');
109
110
		// Test if the submitted form is valid
111
		$errors = $this->is_invalid_form('ppde_settings', $this->submit);
112
113
		if ($this->can_submit_data($errors))
114
		{
115
			// Set the options the user configured
116
			$this->set_settings();
117
118
			// Add option settings change action to the admin log
119
			$phpbb_log = $this->container->get('log');
120
			$phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED');
121
122
			// Option settings have been updated and logged
123
			// Confirm this to the user and provide link back to previous page
124
			trigger_error($this->user->lang($this->lang_key_prefix . '_SAVED') . adm_back_link($this->u_action));
125
		}
126
	}
127
128
	/**
129
	 * Set the options a user can configure
130
	 *
131
	 * @return void
132
	 * @access private
133
	 */
134
	private function set_settings()
135
	{
136
		// Set options for Global settings
137
		$this->config->set('ppde_default_currency', $this->request->variable('ppde_default_currency', 0));
138
		$this->config->set('ppde_default_value', $this->request->variable('ppde_default_value', 0));
139
		$this->config->set('ppde_dropbox_enable', $this->request->variable('ppde_dropbox_enable', false));
140
		$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']));
141
		$this->config->set('ppde_enable', $this->request->variable('ppde_enable', false));
142
		$this->config->set('ppde_header_link', $this->request->variable('ppde_header_link', false));
143
144
		// Set options for Statistics Settings
145
		$this->config->set('ppde_stats_index_enable', $this->request->variable('ppde_stats_index_enable', false));
146
		$this->config->set('ppde_raised_enable', $this->request->variable('ppde_raised_enable', false));
147
		$this->config->set('ppde_raised', $this->request->variable('ppde_raised', 0.0));
148
		$this->config->set('ppde_goal_enable', $this->request->variable('ppde_goal_enable', false));
149
		$this->config->set('ppde_goal', $this->request->variable('ppde_goal', 0.0));
150
		$this->config->set('ppde_used_enable', $this->request->variable('ppde_used_enable', false));
151
		$this->config->set('ppde_used', $this->request->variable('ppde_used', 0.0));
152
153
		// Set misc settings
154
		$this->ppde_controller_main->set_curl_info();
155
		$this->ppde_controller_main->set_remote_detected();
156
157
		// Settings with dependencies are the last to be set.
158
		$this->config->set('ppde_account_id', $this->required_settings($this->request->variable('ppde_account_id', ''), $this->depend_on('ppde_enable')));
159
	}
160
161
	/**
162
	 * Rebuild items list to conserve only numeric values
163
	 *
164
	 * @param string $config_value
165
	 * @param string $added_value
166
	 *
167
	 * @return string
168
	 * @access private
169
	 */
170
	private function rebuild_items_list($config_value, $added_value = '')
171
	{
172
		$items_list = explode(',', $config_value);
173
		$merge_items = array();
174
175
		$this->add_int_data_in_array($merge_items, $added_value);
176
177
		foreach ($items_list as $item)
178
		{
179
			$this->add_int_data_in_array($merge_items, $item);
180
		}
181
		unset($items_list, $item);
182
183
		natsort($merge_items);
184
185
		return $this->check_config(implode(',', array_unique($merge_items)), 'string', '');
186
	}
187
188
	/**
189
	 * Add integer data in an array()
190
	 *
191
	 * @param array $array
192
	 * @param int   $var
193
	 *
194
	 * @return void
195
	 * @access private
196
	 */
197
	private function add_int_data_in_array(&$array, $var)
198
	{
199
		if (settype($var, 'integer') && $var != 0)
200
		{
201
			$array[] = $var;
202
		}
203
	}
204
}
205