Passed
Branch 3.2.x (99ec7e)
by Mario
04:20
created

currency_controller::move()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 35
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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