Passed
Push — develop-3.3.x ( 1d9287...356c9f )
by Mario
02:47
created

currency_controller::validate_form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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