Passed
Push — develop-3.3.x ( 6e4779...accdf9 )
by Mario
04:23 queued 01:51
created

currency_controller::submit_data()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 16
rs 10
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\locale_icu;
20
use skouat\ppde\operators\currency;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
/**
24
 * @property config             config             Config object
25
 * @property ContainerInterface container          Service container interface
26
 * @property string             id_prefix_name     Prefix name for identifier in the URL
27
 * @property string             lang_key_prefix    Prefix for the messages thrown by exceptions
28
 * @property language           language           Language user object
29
 * @property log                log                The phpBB log system
30
 * @property string             module_name        Name of the module currently used
31
 * @property request            request            Request object
32
 * @property bool               submit             State of submit $_POST variable
33
 * @property template           template           Template object
34
 * @property string             u_action           Action URL
35
 * @property user               user               User object
36
 */
37
class currency_controller extends admin_main
38
{
39
	protected $ppde_entity;
40
	protected $ppde_locale;
41
	protected $ppde_operator;
42
43
	/**
44
	 * Constructor
45
	 *
46
	 * @param config                       $config
47
	 * @param ContainerInterface           $container
48
	 * @param language                     $language
49
	 * @param log                          $log
50
	 * @param locale_icu                   $ppde_action_locale     PPDE Locale object
51
	 * @param \skouat\ppde\entity\currency $ppde_entity_currency   PPDE Entity object
52
	 * @param currency                     $ppde_operator_currency PPDE Operator object
53
	 * @param request                      $request
54
	 * @param template                     $template
55
	 * @param user                         $user
56
	 *
57
	 * @access public
58
	 */
59
	public function __construct(
60
		config $config,
61
		ContainerInterface $container,
62
		language $language,
63
		log $log,
64
		locale_icu $ppde_action_locale,
65
		\skouat\ppde\entity\currency $ppde_entity_currency,
66
		currency $ppde_operator_currency,
67
		request $request,
68
		template $template,
69
		user $user
70
	)
71
	{
72
		$this->config = $config;
73
		$this->container = $container;
74
		$this->language = $language;
75
		$this->log = $log;
76
		$this->ppde_locale = $ppde_action_locale;
77
		$this->ppde_entity = $ppde_entity_currency;
78
		$this->ppde_operator = $ppde_operator_currency;
79
		$this->request = $request;
80
		$this->template = $template;
81
		$this->user = $user;
82
		parent::__construct(
83
			'currency',
84
			'PPDE_DC',
85
			'currency'
86
		);
87
	}
88
89
	/**
90
	 * {@inheritdoc}
91
	 */
92
	public function display(): void
93
	{
94
		// Check if currency_order is valid and fix it if necessary
95
		$this->ppde_operator->fix_currency_order();
96
97
		// Grab all the currencies from the db
98
		$data_ary = $this->ppde_entity->get_data($this->ppde_operator->build_sql_data());
99
100
		array_map([$this, 'currency_assign_template_vars'], $data_ary);
101
102
		$this->u_action_assign_template_vars();
103
	}
104
105
	/**
106
	 * {@inheritdoc}
107
	 */
108
	public function add(): void
109
	{
110
		// Add form key
111
		add_form_key('add_edit_currency');
112
113
		// Collect the form data
114
		$data = [
115
			'currency_name'     => $this->request->variable('currency_name', '', true),
116
			'currency_iso_code' => $this->request->variable('currency_iso_code', '', true),
117
			'currency_symbol'   => $this->request->variable('currency_symbol', '', true),
118
			'currency_on_left'  => $this->request->variable('currency_on_left', true),
119
			'currency_enable'   => $this->request->variable('currency_enable', false),
120
		];
121
122
		// Process the new page
123
		$this->add_edit_currency_data($data);
124
125
		// Set output vars for display in the template
126
		$this->add_edit_action_assign_template_vars('add');
127
	}
128
129
	/**
130
	 * Process currency data to be added or edited
131
	 *
132
	 * @param array $data The form data to be processed
133
	 *
134
	 * @return void
135
	 * @access private
136
	 */
137
	private function add_edit_currency_data(array $data): void
138
	{
139
		$this->submit = $this->is_form_submitted();
140
141
		$errors = $this->validate_currency_data();
142
143
		$this->set_currency_entity_data($data);
144
145
		// Insert or update currency
146
		$this->submit_data($errors);
147
148
		$this->assign_template_vars($errors);
149
	}
150
151
	private function validate_currency_data(): array
152
	{
153
		$errors = [];
154
155
		return array_merge($errors,
156
			$this->is_invalid_form('add_edit_' . $this->module_name, $this->submit_or_preview($this->submit)),
157
			$this->is_empty_data($this->ppde_entity, 'name', '', $this->submit_or_preview($this->submit)),
158
			$this->is_empty_data($this->ppde_entity, 'iso_code', '', $this->submit_or_preview($this->submit)),
159
			$this->is_empty_data($this->ppde_entity, 'symbol', '', $this->submit_or_preview($this->submit))
160
		);
161
	}
162
163
	private function set_currency_entity_data(array $data): void
164
	{
165
		if ($this->ppde_locale->is_locale_configured())
166
		{
167
			$data['currency_symbol'] = $this->ppde_locale->get_currency_symbol($data['currency_iso_code']);
168
		}
169
170
		$item_fields = [
171
			'name'              => $data['currency_name'],
172
			'iso_code'          => $data['currency_iso_code'],
173
			'symbol'            => $data['currency_symbol'],
174
			'currency_position' => $data['currency_on_left'],
175
			'currency_enable'   => $data['currency_enable'],
176
		];
177
178
		$this->ppde_entity->set_entity_data($item_fields);
179
	}
180
181
	private function assign_template_vars(array $errors): void
182
	{
183
		$this->s_error_assign_template_vars($errors);
184
		$this->template->assign_vars([
185
			'CURRENCY_NAME'     => $this->ppde_entity->get_name(),
186
			'CURRENCY_ISO_CODE' => $this->ppde_entity->get_iso_code(),
187
			'CURRENCY_SYMBOL'   => $this->ppde_entity->get_symbol(),
188
			'CURRENCY_POSITION' => $this->ppde_entity->get_currency_position(),
189
			'CURRENCY_ENABLE'   => $this->ppde_entity->get_currency_enable(),
190
191
			'S_HIDDEN_FIELDS'          => '<input type="hidden" name="' . $this->id_prefix_name . '_id" value="' . $this->ppde_entity->get_id() . '">',
192
			'S_PPDE_LOCALE_AVAILABLE'  => $this->ppde_locale->icu_requirements(),
193
			'S_PPDE_LOCALE_CONFIGURED' => $this->ppde_locale->is_locale_configured(),
194
		]);
195
	}
196
197
	/**
198
	 * Submit data to the database
199
	 *
200
	 * @param array $errors
201
	 *
202
	 * @return void
203
	 * @access private
204
	 */
205
	private function submit_data(array $errors): void
206
	{
207
		if (!$this->ppde_entity->get_id())
208
		{
209
			$this->trigger_error_data_already_exists($this->ppde_entity);
210
		}
211
212
		if (!$this->can_submit_data($errors))
213
		{
214
			return;
215
		}
216
217
		$log_action = $this->ppde_entity->add_edit_data('set_order');
218
		// Log and show user confirmation of the saved item and provide link back to the previous page
219
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($log_action), time(), [$this->ppde_entity->get_name()]);
220
		trigger_error($this->language->lang($this->lang_key_prefix . '_' . strtoupper($log_action)) . adm_back_link($this->u_action));
221
	}
222
223
	/**
224
	 * {@inheritdoc}
225
	 */
226
	public function edit(): void
227
	{
228
		$currency_id = (int) $this->args[$this->id_prefix_name . '_id'];
229
		// Add form key
230
		add_form_key('add_edit_currency');
231
232
		$this->ppde_entity->set_page_url($this->u_action);
233
		$this->ppde_entity->load($currency_id);
234
235
		// Collect the form data
236
		$data = [
237
			'currency_id'       => $this->ppde_entity->get_id(),
238
			'currency_name'     => $this->request->variable('currency_name', $this->ppde_entity->get_name(), true),
239
			'currency_iso_code' => $this->request->variable('currency_iso_code', $this->ppde_entity->get_iso_code(), true),
240
			'currency_symbol'   => $this->request->variable('currency_symbol', $this->ppde_entity->get_symbol(), true),
241
			'currency_on_left'  => $this->request->variable('currency_on_left', $this->ppde_entity->get_currency_position()),
242
			'currency_enable'   => $this->request->variable('currency_enable', $this->ppde_entity->get_currency_enable()),
243
		];
244
245
		// Process the new page
246
		$this->add_edit_currency_data($data);
247
248
		// Set output vars for display in the template
249
		$this->add_edit_action_assign_template_vars('edit', $currency_id);
250
	}
251
252
	/**
253
	 * {@inheritdoc}
254
	 */
255
	public function move(): void
256
	{
257
		$direction = $this->args['action'];
258
259
		// Before moving the currency, with check the link hash.
260
		// If the hash, is invalid we return an error.
261
		if (!check_link_hash($this->request->variable('hash', ''), 'ppde_move'))
262
		{
263
			trigger_error($this->language->lang('PPDE_DC_INVALID_HASH') . adm_back_link($this->u_action), E_USER_WARNING);
264
		}
265
266
		// Load data
267
		$this->ppde_entity->load($this->args[$this->id_prefix_name . '_id']);
268
		$current_order = $this->ppde_entity->get_currency_order();
269
270
		if (($current_order === 0) && ($direction === 'move_up'))
271
		{
272
			return;
273
		}
274
275
		// on move_down, switch position with next order_id...
276
		// on move_up, switch position with previous order_id...
277
		$switch_order_id = ($direction === 'move_down') ? $current_order + 1 : $current_order - 1;
278
279
		$move_executed = $this->ppde_operator->move($switch_order_id, $current_order, $this->ppde_entity->get_id());
280
281
		// Log action if data was moved
282
		if ($move_executed)
283
		{
284
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($direction), time(), [$this->ppde_entity->get_name()]);
285
		}
286
287
		if ($this->request->is_ajax())
288
		{
289
			$json_response = new \phpbb\json_response;
290
			$json_response->send(['success' => $move_executed]);
291
		}
292
	}
293
294
	/**
295
	 * {@inheritdoc}
296
	 */
297
	public function enable(): void
298
	{
299
		$action = $this->args['action'];
300
		$currency_id = (int) $this->args[$this->id_prefix_name . '_id'];
301
302
		// Return an error if no currency
303
		if (!$currency_id)
304
		{
305
			trigger_error($this->language->lang($this->lang_key_prefix . '_NO_CURRENCY') . adm_back_link($this->u_action), E_USER_WARNING);
306
		}
307
308
		// Return an error if it's the default currency
309
		if (((int) $this->config['ppde_default_currency'] === $currency_id) && ($action === 'deactivate'))
310
		{
311
			trigger_error($this->language->lang('PPDE_CANNOT_DISABLE_DEFAULT_CURRENCY') . adm_back_link($this->u_action), E_USER_WARNING);
312
		}
313
314
		// Load data
315
		$this->ppde_entity->load($currency_id);
316
317
		// Set the new status for this currency
318
		$this->ppde_entity->set_currency_enable($action === 'activate');
319
320
		// Save data to the database
321
		$this->ppde_entity->save($this->ppde_entity->check_required_field());
322
		// Log action
323
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($action) . 'D', time(), [$this->ppde_entity->get_name()]);
324
325
		if ((($action === 'activate') || ($action === 'deactivate')) && $this->request->is_ajax())
326
		{
327
			$action_lang = ($action === 'activate') ? 'DISABLE' : 'ENABLE';
328
			$json_response = new \phpbb\json_response;
329
			$json_response->send(['text' => $this->language->lang($action_lang)]);
330
		}
331
	}
332
333
	/**
334
	 * {@inheritdoc}
335
	 */
336
	public function delete(): void
337
	{
338
		$currency_id = (int) $this->args[$this->id_prefix_name . '_id'];
339
340
		// Load data
341
		$this->ppde_entity->load($currency_id);
342
		$this->ppde_entity->delete($currency_id, 'check_currency_enable');
343
344
		// Log the action
345
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_DELETED', time(), [$this->ppde_entity->get_name()]);
346
		trigger_error($this->language->lang($this->lang_key_prefix . '_DELETED') . adm_back_link($this->u_action));
347
	}
348
349
	/**
350
	 * Set output vars for display in the template
351
	 *
352
	 * @param array $data
353
	 *
354
	 * @return void
355
	 * @access protected
356
	 */
357
	protected function currency_assign_template_vars(array $data): void
358
	{
359
		if (!$data['currency_enable'])
360
		{
361
			$enable_lang = 'ENABLE';
362
			$enable_value = 'activate';
363
		}
364
		else
365
		{
366
			$enable_lang = 'DISABLE';
367
			$enable_value = 'deactivate';
368
		}
369
370
		$this->template->assign_block_vars('currency', [
371
			'CURRENCY_NAME'    => $data['currency_name'],
372
			'CURRENCY_ENABLED' => (bool) $data['currency_enable'],
373
			'L_ENABLE_DISABLE' => $this->language->lang($enable_lang),
374
			'S_DEFAULT'        => (int) $data['currency_id'] === (int) $this->config['ppde_default_currency'],
375
			'U_DELETE'         => $this->u_action . '&amp;action=delete&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'],
376
			'U_EDIT'           => $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'],
377
			'U_ENABLE_DISABLE' => $this->u_action . '&amp;action=' . $enable_value . '&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'],
378
			'U_MOVE_DOWN'      => $this->u_action . '&amp;action=move_down&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'] . '&amp;hash=' . generate_link_hash('ppde_move'),
379
			'U_MOVE_UP'        => $this->u_action . '&amp;action=move_up&amp;' . $this->id_prefix_name . '_id=' . $data['currency_id'] . '&amp;hash=' . generate_link_hash('ppde_move'),
380
		]);
381
	}
382
}
383