Completed
Push — master ( 9b85c2...3f27cb )
by Mario
02:45
created

admin_currency_controller::submit_data()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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