Completed
Push — develop ( b4b65d...4507e2 )
by Mario
03:29
created

admin_currency_controller::enable_currency()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 35
Code Lines 14

Duplication

Lines 8
Ratio 22.86 %

Importance

Changes 6
Bugs 1 Features 1
Metric Value
c 6
b 1
f 1
dl 8
loc 35
rs 5.3846
cc 8
eloc 14
nc 8
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
	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
		$errors = array_merge($errors, $this->set_entity_data($entity, $item_fields));
159
160
		// Check some settings before submitting data
161
		$errors = array_merge($errors,
162
			$this->is_invalid_form('add_edit_' . $this->module_name, $this->submit_or_preview($this->submit)),
163
			$this->is_empty_data($entity, 'name', '', $this->submit_or_preview($this->submit)),
164
			$this->is_empty_data($entity, 'iso_code', '', $this->submit_or_preview($this->submit)),
165
			$this->is_empty_data($entity, 'symbol', '', $this->submit_or_preview($this->submit))
166
		);
167
168
		// Insert or update currency
169
		$this->submit_data($entity, $errors);
170
171
		// Set output vars for display in the template
172
		$this->s_error_assign_template_vars($errors);
173
		$this->template->assign_vars(array(
174
			'CURRENCY_NAME'     => $entity->get_name(),
175
			'CURRENCY_ISO_CODE' => $entity->get_iso_code(),
176
			'CURRENCY_SYMBOL'   => $entity->get_symbol(),
177
			'CURRENCY_POSITION' => $entity->get_currency_position(),
178
			'CURRENCY_ENABLE'   => $entity->get_currency_enable(),
179
180
			'S_HIDDEN_FIELDS'   => '<input type="hidden" name="' . $this->id_prefix_name . '_id" value="' . $entity->get_id() . '" />',
181
		));
182
	}
183
184
	/**
185
	 * Submit data to the database
186
	 *
187
	 * @param \skouat\ppde\entity\currency $entity The currency entity object
188
	 * @param array                        $errors
189
	 *
190
	 * @return null
191
	 * @access private
192
	 */
193
	private function submit_data($entity, array $errors)
194
	{
195
		if (!$entity->get_id())
196
		{
197
			$this->trigger_error_data_already_exists($entity);
198
		}
199
200
		if ($this->can_submit_data($errors))
201
		{
202
			$log_action = $this->add_edit_data($entity, 'set_order');
203
			// Log and show user confirmation of the saved item and provide link back to the previous page
204
			$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()));
205
			trigger_error($this->user->lang[$this->lang_key_prefix . '_' . strtoupper($log_action)] . adm_back_link($this->u_action));
206
		}
207
	}
208
209
	/**
210
	 * Edit a Currency
211
	 *
212
	 * @param int $currency_id Currency Identifier
213
	 *
214
	 * @return null
215
	 * @access   public
216
	 */
217
	public function edit_currency($currency_id)
218
	{
219
		// Add form key
220
		add_form_key('add_edit_currency');
221
222
		// Initiate an entity
223
		/** @type \skouat\ppde\entity\currency $entity */
224
		$entity = $this->get_container_entity();
225
		$entity->set_page_url($this->u_action);
226
		$entity->load($currency_id);
227
228
		// Collect the form data
229
		$data = array(
230
			'currency_id'       => $entity->get_id(),
231
			'currency_name'     => $this->request->variable('currency_name', $entity->get_name(), true),
232
			'currency_iso_code' => $this->request->variable('currency_iso_code', $entity->get_iso_code(), true),
233
			'currency_symbol'   => $this->request->variable('currency_symbol', $entity->get_symbol(), true),
234
			'currency_on_left'  => $this->request->variable('currency_on_left', $entity->get_currency_position()),
235
			'currency_enable'   => $this->request->variable('currency_enable', $entity->get_currency_enable()),
236
		);
237
238
		// Process the new page
239
		$this->add_edit_currency_data($entity, $data);
240
241
		// Set output vars for display in the template
242
		$this->template->assign_vars(array(
243
			'S_EDIT'        => true,
244
			'U_EDIT_ACTION' => $this->u_action . '&amp;action=edit&amp;' . $this->id_prefix_name . '_id=' . $currency_id,
245
			'U_BACK'        => $this->u_action,
246
		));
247
	}
248
249
	/**
250
	 * Move a currency up/down
251
	 *
252
	 * @param int    $currency_id The currency identifier to move
253
	 * @param string $direction   The direction (up|down)
254
	 *
255
	 * @return null
256
	 * @access   public
257
	 */
258
	public function move_currency($currency_id, $direction)
259
	{
260
		// Initiate an entity and load data
261
		/** @type \skouat\ppde\entity\currency $entity */
262
		$entity = $this->get_container_entity();
263
		$entity->load($currency_id);
264
		$current_order = $entity->get_currency_order();
265
266
		if ($current_order == 0 && $direction == 'move_up')
267
		{
268
			return;
269
		}
270
271
		// on move_down, switch position with next order_id...
272
		// on move_up, switch position with previous order_id...
273
		$switch_order_id = ($direction == 'move_down') ? $current_order + 1 : $current_order - 1;
274
275
		$move_executed = $this->ppde_operator->move($switch_order_id, $current_order, $entity->get_id());
276
277
		// Log action if data was moved
278
		if ($move_executed)
279
		{
280
			$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($direction), time(), array($entity->get_name()));
281
		}
282
283
		if ($this->request->is_ajax())
284
		{
285
			$json_response = new \phpbb\json_response;
286
			$json_response->send(array(
287
				'success' => $move_executed,
288
			));
289
		}
290
	}
291
292
	/**
293
	 * Enable/disable a currency
294
	 *
295
	 * @param int    $currency_id
296
	 * @param string $action
297
	 *
298
	 * @return null
299
	 * @access public
300
	 */
301
	public function enable_currency($currency_id, $action)
302
	{
303
		// Return an error if no currency
304 View Code Duplication
		if (!$currency_id)
305
		{
306
			trigger_error($this->user->lang[$this->lang_key_prefix . '_NO_CURRENCY'] . adm_back_link($this->u_action), E_USER_WARNING);
307
		}
308
309
		// Return an error if it's the last enabled currency
310 View Code Duplication
		if ($this->ppde_operator->last_currency_enabled($action) && ($action == 'disable'))
311
		{
312
			trigger_error($this->user->lang['PPDE_CANNOT_DISABLE_ALL_CURRENCIES'] . adm_back_link($this->u_action), E_USER_WARNING);
313
		}
314
315
		// Initiate an entity and load data
316
		/** @type \skouat\ppde\entity\currency $entity */
317
		$entity = $this->get_container_entity();
318
		$entity->load($currency_id);
319
320
		// Set the new status for this currency
321
		$entity->set_currency_enable(($action == 'enable') ? 1 : 0);
0 ignored issues
show
Documentation introduced by
$action == 'enable' ? 1 : 0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
322
323
		// Save data to the database
324
		$entity->save($entity->check_required_field());
325
		// Log action
326
		$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()));
327
328
		if ($this->request->is_ajax() && ($action == 'enable' || $action == 'disable'))
329
		{
330
			$json_response = new \phpbb\json_response;
331
			$json_response->send(array(
332
				'text' => $this->user->lang[strtoupper($action)],
333
			));
334
		}
335
	}
336
337
	/**
338
	 * Delete a currency
339
	 *
340
	 * @param int $currency_id
341
	 *
342
	 * @return null
343
	 * @access public
344
	 */
345
	public function delete_currency($currency_id)
346
	{
347
		// Initiate an entity and load data
348
		/** @type \skouat\ppde\entity\currency $entity */
349
		$entity = $this->get_container_entity();
350
		$entity->load($currency_id);
351
		$entity->delete($currency_id, 'check_currency_enable');
352
353
		// Log the action
354
		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_DELETED', time(), array($entity->get_name()));
355
		trigger_error($this->user->lang[$this->lang_key_prefix . '_DELETED'] . adm_back_link($this->u_action));
356
	}
357
}
358