Passed
Pull Request — develop-3.2.x (#67)
by
unknown
03:03
created

admin_transactions_controller::do_action()   D

Complexity

Conditions 21
Paths 25

Size

Total Lines 167
Code Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
eloc 92
nc 25
nop 2
dl 0
loc 167
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 phpbb\auth\auth;
14
use phpbb\config\config;
15
use phpbb\language\language;
16
use phpbb\log\log;
17
use phpbb\request\request;
18
use phpbb\template\template;
19
use phpbb\user;
20
use phpbb\user_loader;
21
use skouat\ppde\actions\core;
22
use skouat\ppde\actions\currency;
23
use skouat\ppde\exception\transaction_exception;
24
use skouat\ppde\operators\transactions;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26
27
/**
28
 * @property config             config             Config object
29
 * @property ContainerInterface container          Service container interface
30
 * @property string             id_prefix_name     Prefix name for identifier in the URL
31
 * @property string             lang_key_prefix    Prefix for the messages thrown by exceptions
32
 * @property language           language           Language user object
33
 * @property log                log                The phpBB log system.
34
 * @property string             module_name        Name of the module currently used
35
 * @property request            request            Request object.
36
 * @property bool               submit             State of submit $_POST variable
37
 * @property template           template           Template object
38
 * @property string             u_action           Action URL
39
 * @property user               user               User object.
40
 * @property user_loader        user_loader        User loader object
41
 */
42
class admin_transactions_controller extends admin_main
43
{
44
	public $ppde_operator;
45
	protected $adm_relative_path;
46
	protected $auth;
47
	protected $user_loader;
48
	protected $entry_count;
49
	protected $last_page_offset;
50
	protected $php_ext;
51
	protected $phpbb_admin_path;
52
	protected $phpbb_root_path;
53
	protected $ppde_actions;
54
	protected $ppde_actions_currency;
55
	protected $table_prefix;
56
	protected $table_ppde_transactions;
57
58
	/**
59
	 * Constructor
60
	 *
61
	 * @param auth               $auth                       Authentication object
62
	 * @param config             $config                     Config object
63
	 * @param ContainerInterface $container                  Service container interface
64
	 * @param language           $language                   Language user object
65
	 * @param log                $log                        The phpBB log system
66
	 * @param core               $ppde_actions               PPDE actions object
67
	 * @param currency           $ppde_actions_currency      PPDE currency actions object
68
	 * @param transactions       $ppde_operator_transactions Operator object
69
	 * @param request            $request                    Request object
70
	 * @param template           $template                   Template object
71
	 * @param user               $user                       User object.
72
	 * @param user_loader        $user_loader                User loader object
73
	 * @param string             $adm_relative_path          phpBB admin relative path
74
	 * @param string             $phpbb_root_path            phpBB root path
75
	 * @param string             $php_ext                    phpEx
76
	 * @param string             $table_prefix               The table prefix
77
	 * @param string             $table_ppde_transactions    Name of the table used to store data
78
	 *
79
	 * @access public
80
	 */
81
	public function __construct(auth $auth, config $config, ContainerInterface $container, language $language, log $log, core $ppde_actions, currency $ppde_actions_currency, transactions $ppde_operator_transactions, request $request, template $template, user $user, user_loader $user_loader, $adm_relative_path, $phpbb_root_path, $php_ext, $table_prefix, $table_ppde_transactions)
82
	{
83
		$this->auth = $auth;
84
		$this->config = $config;
85
		$this->container = $container;
86
		$this->language = $language;
87
		$this->log = $log;
88
		$this->ppde_actions = $ppde_actions;
89
		$this->ppde_actions_currency = $ppde_actions_currency;
90
		$this->ppde_operator = $ppde_operator_transactions;
91
		$this->request = $request;
92
		$this->template = $template;
93
		$this->user = $user;
94
		$this->user_loader = $user_loader;
95
		$this->adm_relative_path = $adm_relative_path;
96
		$this->phpbb_admin_path = $phpbb_root_path . $adm_relative_path;
97
		$this->phpbb_root_path = $phpbb_root_path;
98
		$this->php_ext = $php_ext;
99
		$this->table_prefix = $table_prefix;
100
		$this->table_ppde_transactions = $table_ppde_transactions;
101
		parent::__construct(
102
			'transactions',
103
			'PPDE_DT',
104
			'transaction'
105
		);
106
	}
107
108
	/**
109
	 * Display the transactions list
110
	 *
111
	 * @param string $id     Module id
112
	 * @param string $mode   Module categorie
113
	 * @param string $action Action name
114
	 *
115
	 * @return void
116
	 * @access public
117
	 */
118
	public function display_transactions($id, $mode, $action)
119
	{
120
		// Set up general vars
121
		$args = array();
122
		$start = $this->request->variable('start', 0);
123
		$deletemark = $this->request->is_set('delmarked') ? $this->request->variable('delmarked', false) : false;
124
		$deleteall = $this->request->is_set('delall') ? $this->request->variable('delall', false) : false;
125
		$marked = $this->request->variable('mark', array(0));
126
		$txn_approve = $this->request->is_set('approve');
127
		$txn_approved = $this->request->variable('txn_errors_approved', 0);
128
		$txn_add = $this->request->is_set('add');
129
		$txn_change = $this->request->is_set_post('change');
130
		// Sort keys
131
		$sort_days = $this->request->variable('st', 0);
132
		$sort_key = $this->request->variable('sk', 't');
133
		$sort_dir = $this->request->variable('sd', 'd');
134
135
		// Prepares args for entries deletion
136
		if (($deletemark || $deleteall) && $this->auth->acl_get('a_ppde_manage'))
137
		{
138
			$action = 'delete';
139
			$args = array(
140
				'hidden_fields' => array(
141
					'start'     => $start,
142
					'delall'    => $deleteall,
143
					'delmarked' => $deletemark,
144
					'mark'      => $marked,
145
					'st'        => $sort_days,
146
					'sk'        => $sort_key,
147
					'sd'        => $sort_dir,
148
					'i'         => $id,
149
					'mode'      => $mode,
150
				),
151
			);
152
		}
153
154
		if ($txn_approve)
155
		{
156
			$transaction_id = $this->request->variable('id', 0);
157
			$action = 'approve';
158
			$args = array(
159
				'hidden_fields' => array(
160
					'approve'             => true,
161
					'id'                  => $transaction_id,
162
					'txn_errors_approved' => $txn_approved,
163
				),
164
			);
165
		}
166
167
		if ($txn_add)
168
		{
169
			$action = 'add';
170
		}
171
		else if ($txn_change)
172
		{
173
			$action = 'change';
174
		}
175
176
		$action = $this->do_action($action, $args);
177
178
		if (!$action)
179
		{
180
			/** @type \phpbb\pagination $pagination */
181
			$pagination = $this->container->get('pagination');
182
183
			// Sorting
184
			$limit_days = array(0 => $this->language->lang('ALL_ENTRIES'), 1 => $this->language->lang('1_DAY'), 7 => $this->language->lang('7_DAYS'), 14 => $this->language->lang('2_WEEKS'), 30 => $this->language->lang('1_MONTH'), 90 => $this->language->lang('3_MONTHS'), 180 => $this->language->lang('6_MONTHS'), 365 => $this->language->lang('1_YEAR'));
185
			$sort_by_text = array('txn' => $this->language->lang('PPDE_DT_SORT_TXN_ID'), 'u' => $this->language->lang('PPDE_DT_SORT_DONORS'), 'ipn' => $this->language->lang('PPDE_DT_SORT_IPN_STATUS'), 'ipn_test' => $this->language->lang('PPDE_DT_SORT_IPN_TYPE'), 'ps' => $this->language->lang('PPDE_DT_SORT_PAYMENT_STATUS'), 't' => $this->language->lang('SORT_DATE'));
186
			$sort_by_sql = array('txn' => 'txn.txn_id', 'u' => 'u.username_clean', 'ipn' => 'txn.confirmed', 'ipn_test' => 'txn.test_ipn', 'ps' => 'txn.payment_status', 't' => 'txn.payment_date');
187
188
			$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
189
			gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
190
191
			// Define where and sort sql for use in displaying transactions
192
			$sql_where = ($sort_days) ? (time() - ($sort_days * 86400)) : 0;
193
			$sql_sort = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
194
195
			$keywords = $this->request->variable('keywords', '', true);
196
			$keywords_param = !empty($keywords) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($keywords)) : '';
197
198
			// Grab log data
199
			$log_data = array();
200
			$log_count = 0;
201
202
			$this->view_txn_log($log_data, $log_count, (int) $this->config['topics_per_page'], $start, $sql_where, $sql_sort, $keywords);
203
204
			$base_url = $this->u_action . '&amp;' . $u_sort_param . $keywords_param;
205
			$pagination->generate_template_pagination($base_url, 'pagination', 'start', $log_count, (int) $this->config['topics_per_page'], $start);
206
207
			$this->template->assign_vars(array(
208
				'S_CLEARLOGS'  => $this->auth->acl_get('a_ppde_manage'),
209
				'S_KEYWORDS'   => $keywords,
210
				'S_LIMIT_DAYS' => $s_limit_days,
211
				'S_SORT_KEY'   => $s_sort_key,
212
				'S_SORT_DIR'   => $s_sort_dir,
213
				'S_TXN'        => $mode,
214
				'U_ACTION'     => $this->u_action . '&amp;' . $u_sort_param . $keywords_param . '&amp;start=' . $start,
215
			));
216
217
			array_map(array($this, 'display_log_assign_template_vars'), $log_data);
218
		}
219
	}
220
221
	/**
222
	 * Do action regarding the value of $action
223
	 *
224
	 * @param string $action Requested action
225
	 * @param array  $args   Arguments required for the action
226
	 *
227
	 * @return string
228
	 * @access private
229
	 */
230
	private function do_action($action, $args)
231
	{
232
		// Initiate an entity
233
		/** @type \skouat\ppde\entity\transactions $entity */
234
		$entity = $this->get_container_entity();
235
236
		switch ($action)
237
		{
238
			case 'view':
239
				// Request Identifier of the transaction
240
				$transaction_id = $this->request->variable('id', 0);
241
242
				// add field username to the table schema needed by entity->import()
243
				$additional_table_schema = array(
244
					'item_username'     => array('name' => 'username', 'type' => 'string'),
245
					'item_user_colour'  => array('name' => 'user_colour', 'type' => 'string'),
246
				);
247
248
				// Grab transaction data
249
				$data_ary = $entity->get_data($this->ppde_operator->build_sql_data($transaction_id), $additional_table_schema);
250
251
				array_map(array($this, 'action_assign_template_vars'), $data_ary);
252
253
				$this->template->assign_vars(array(
254
					'U_FIND_USERNAME' => append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=searchuser&amp;form=view_transactions&amp;field=username&amp;select_single=true'),
255
					'U_ACTION'        => $this->u_action,
256
					'U_BACK'          => $this->u_action,
257
					'S_VIEW'          => true,
258
				));
259
			break;
260
			case 'delete':
261
				if (confirm_box(true))
262
				{
263
					$where_sql = '';
264
265
					if ($args['hidden_fields']['delmarked'] && count($args['hidden_fields']['mark']))
266
					{
267
						$where_sql = $this->ppde_operator->build_marked_where_sql($args['hidden_fields']['mark']);
268
					}
269
270
					if ($where_sql || $args['hidden_fields']['delall'])
271
					{
272
						$entity->delete(0, '', $where_sql, $args['hidden_fields']['delall']);
273
						$this->ppde_actions->set_ipn_test_properties(true);
274
						$this->ppde_actions->update_overview_stats();
275
						$this->ppde_actions->set_ipn_test_properties(false);
276
						$this->ppde_actions->update_overview_stats();
277
						$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_PURGED', time());
278
					}
279
				}
280
				else
281
				{
282
					confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields($args['hidden_fields']));
283
				}
284
				// Clear $action status
285
				$action = '';
286
			break;
287
			case 'approve':
288
				if (confirm_box(true))
289
				{
290
					$transaction_id = (int) $args['hidden_fields']['id'];
291
					$txn_approved = !empty($args['hidden_fields']['txn_errors_approved']) ? false : true;
292
293
					// Update DB record
294
					$entity->load($transaction_id);
295
					$entity->set_txn_errors_approved($txn_approved);
296
					$entity->save(false);
297
298
					// Prepare transaction settings before doing actions
299
					$this->ppde_actions->set_transaction_data($entity->get_data($this->ppde_operator->build_sql_data($transaction_id)));
300
					$this->ppde_actions->set_ipn_test_properties($entity->get_test_ipn());
301
					$this->ppde_actions->is_donor_is_member();
302
303
					$this->do_transactions_actions(!$this->ppde_actions->get_ipn_test() && $this->ppde_actions->get_donor_is_member());
304
305
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED', time());
306
				}
307
				else
308
				{
309
					confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields($args['hidden_fields']));
310
				}
311
				// Clear $action status
312
				$action = '';
313
			break;
314
			case 'add':
315
				$errors = array();
316
317
				$transaction_data = $this->request_transaction_vars();
318
319
				if ($this->request->is_set_post('submit'))
320
				{
321
					try
322
					{
323
						$data_ary = $this->build_data_ary($transaction_data);
324
325
						$this->ppde_actions->log_to_db($data_ary);
326
327
						// Prepare transaction settings before doing actions
328
						$this->ppde_actions->set_transaction_data($transaction_data);
329
						$this->ppde_actions->is_donor_is_member();
330
331
						$this->do_transactions_actions($this->ppde_actions->get_donor_is_member() && !$transaction_data['MT_ANONYMOUS']);
332
333
						$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_PPDE_MT_ADDED', time(), array($transaction_data['MT_USERNAME']));
334
						trigger_error($this->language->lang('PPDE_MT_ADDED') . adm_back_link($this->u_action));
335
					}
336
					catch (transaction_exception $e)
337
					{
338
						$errors = $e->get_errors();
339
					}
340
				}
341
342
				$this->ppde_actions_currency->build_currency_select_menu((int) $this->config['ppde_default_currency']);
343
344
				$this->s_error_assign_template_vars($errors);
345
346
				$this->template->assign_vars($transaction_data);
347
348
				$this->template->assign_vars(array(
349
					'U_ACTION'             => $this->u_action,
350
					'U_BACK'               => $this->u_action,
351
					'S_ADD'                => true,
352
					'ANONYMOUS_USER_ID'    => ANONYMOUS,
353
					'U_FIND_USERNAME'      => append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=searchuser&amp;form=manual_transaction&amp;field=username&amp;select_single=true'),
354
					'PAYMENT_TIME_FORMATS' => $this->get_payment_time_examples(),
355
				));
356
			break;
357
			case 'change':
358
359
				$username = $this->request->variable('username', '', true);
360
361
				if ($this->request->is_set('u') && $username === '')
362
				{
363
					$user_id = ANONYMOUS;
364
				}
365
				else
366
				{
367
					$user_id = $this->user_loader->load_user_by_username($username);
368
369
					if ($user_id == ANONYMOUS)
370
					{
371
						trigger_error($this->language->lang('NO_USER') . adm_back_link($this->u_action), E_USER_WARNING);
372
					}
373
				}
374
375
				// Request Identifier of the transaction
376
				$transaction_id = $this->request->variable('id', 0);
377
378
				/** @type \skouat\ppde\entity\transactions $entity */
379
				$entity = $this->get_container_entity();
380
				$entity->load($transaction_id);
381
382
				if (!$entity->data_exists($entity->build_sql_data_exists()))
383
				{
384
					trigger_error($this->language->lang('PPDE_DT_NO_TRANSACTION') . adm_back_link($this->u_action), E_USER_WARNING);
385
				}
386
387
				$log_action = $entity
388
					->set_user_id($user_id)
389
					->add_edit_data();
390
391
				$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_' . strtoupper($log_action));
392
				trigger_error($this->language->lang($this->lang_key_prefix . '_' . strtoupper($log_action)) . adm_back_link($this->u_action));
393
			break;
394
		}
395
396
		return $action;
397
	}
398
399
	/**
400
	 * Does actions for validated transaction
401
	 *
402
	 * @param bool $is_member
403
	 *
404
	 * @return void
405
	 * @access private
406
	 */
407
	private function do_transactions_actions($is_member)
408
	{
409
		$this->ppde_actions->update_overview_stats();
410
		$this->ppde_actions->update_raised_amount();
411
412
		if ($is_member)
413
		{
414
			$this->ppde_actions->update_donor_stats();
415
			$this->ppde_actions->donors_group_user_add();
416
			$this->ppde_actions->notification->notify_donor_donation_received();
417
		}
418
	}
419
420
	/**
421
	 * Returns requested data from manual transaction form
422
	 *
423
	 * @return array
424
	 * @access private
425
	 */
426
	private function request_transaction_vars()
427
	{
428
		return array(
429
			'MT_ANONYMOUS'          => $this->request->is_set('u'),
430
			'MT_USERNAME'           => $this->request->variable('username', '', true),
431
			'MT_FIRST_NAME'         => $this->request->variable('first_name', '', true),
432
			'MT_LAST_NAME'          => $this->request->variable('last_name', '', true),
433
			'MT_PAYER_EMAIL'        => $this->request->variable('payer_email', '', true),
434
			'MT_RESIDENCE_COUNTRY'  => $this->request->variable('residence_country', ''),
435
			'MT_MC_GROSS'           => $this->request->variable('mc_gross', (float) 0),
436
			'MT_MC_CURRENCY'        => $this->request->variable('mc_currency', ''),
437
			'MT_MC_FEE'             => $this->request->variable('mc_fee', (float) 0),
438
			'MT_PAYMENT_DATE_YEAR'  => $this->request->variable('payment_date_year', (int) $this->user->format_date(time(), 'Y')),
439
			'MT_PAYMENT_DATE_MONTH' => $this->request->variable('payment_date_month', (int) $this->user->format_date(time(), 'n')),
440
			'MT_PAYMENT_DATE_DAY'   => $this->request->variable('payment_date_day', (int) $this->user->format_date(time(), 'j')),
441
			'MT_PAYMENT_TIME'       => $this->request->variable('payment_time', $this->user->format_date(time(), 'H:i:s')),
442
			'MT_MEMO'               => $this->request->variable('memo', '', true),
443
		);
444
	}
445
446
	/**
447
	 * Returns a list of valid times that the user can provide in the manual transaction form
448
	 *
449
	 * @return array Array of strings representing the current time, each in a different format
450
	 * @access private
451
	 */
452
	private function get_payment_time_examples()
453
	{
454
		$formats = array(
455
			'H:i:s',
456
			'G:i',
457
			'h:i:s a',
458
			'g:i A',
459
		);
460
461
		$examples = array();
462
463
		foreach ($formats as $format)
464
		{
465
			$examples[] = $this->user->format_date(time(), $format);
466
		}
467
468
		return $examples;
469
	}
470
471
	/**
472
	 * View log
473
	 *
474
	 * @param array  &$log         The result array with the logs
475
	 * @param mixed  &$log_count   If $log_count is set to false, we will skip counting all entries in the
476
	 *                             database. Otherwise an integer with the number of total matching entries is returned.
477
	 * @param int     $limit       Limit the number of entries that are returned
478
	 * @param int     $offset      Offset when fetching the log entries, f.e. when paginating
479
	 * @param int     $limit_days
480
	 * @param string  $sort_by     SQL order option, e.g. 'l.log_time DESC'
481
	 * @param string  $keywords    Will only return log entries that have the keywords in log_operation or log_data
482
	 *
483
	 * @return int Returns the offset of the last valid page, if the specified offset was invalid (too high)
484
	 * @access private
485
	 */
486
	private function view_txn_log(&$log, &$log_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'txn.payment_date DESC', $keywords = '')
487
	{
488
		$count_logs = ($log_count !== false);
489
490
		$log = $this->get_logs($count_logs, $limit, $offset, $limit_days, $sort_by, $keywords);
491
		$log_count = $this->get_log_count();
492
493
		return $this->get_valid_offset();
494
	}
495
496
	/**
497
	 * @param bool   $count_logs
498
	 * @param int    $limit
499
	 * @param int    $offset
500
	 * @param int    $log_time
501
	 * @param string $sort_by
502
	 * @param string $keywords
503
	 *
504
	 * @return array $log
505
	 * @access private
506
	 */
507
	private function get_logs($count_logs = true, $limit = 0, $offset = 0, $log_time = 0, $sort_by = 'txn.payment_date DESC', $keywords = '')
508
	{
509
		$this->entry_count = 0;
510
		$this->last_page_offset = $offset;
511
		$url_ary = array();
512
513
		if ($this->get_container_entity()->is_in_admin() && $this->phpbb_admin_path)
514
		{
515
			$url_ary['profile_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&amp;mode=overview');
516
			$url_ary['txn_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=-skouat-ppde-acp-ppde_module&amp;mode=transactions');
517
518
		}
519
		else
520
		{
521
			$url_ary['profile_url'] = append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=viewprofile');
522
			$url_ary['txn_url'] = '';
523
		}
524
525
		$get_logs_sql_ary = $this->ppde_operator->get_logs_sql_ary($keywords, $sort_by, $log_time);
526
527
		if ($count_logs)
528
		{
529
			$this->entry_count = $this->ppde_operator->query_sql_count($get_logs_sql_ary, 'txn.transaction_id');
530
531
			if ($this->entry_count == 0)
532
			{
533
				// Save the queries, because there are no logs to display
534
				$this->last_page_offset = 0;
535
536
				return array();
537
			}
538
539
			// Return the user to the last page that is valid
540
			while ($this->last_page_offset >= $this->entry_count)
541
			{
542
				$this->last_page_offset = max(0, $this->last_page_offset - $limit);
543
			}
544
		}
545
546
		return $this->ppde_operator->build_log_ary($get_logs_sql_ary, $url_ary, $limit, $this->last_page_offset);
547
	}
548
549
	/**
550
	 * @return integer
551
	 */
552
	public function get_log_count()
553
	{
554
		return ($this->entry_count) ? (int) $this->entry_count : 0;
555
	}
556
557
	/**
558
	 * @return integer
559
	 */
560
	public function get_valid_offset()
561
	{
562
		return ($this->last_page_offset) ? (int) $this->last_page_offset : 0;
563
	}
564
565
	/**
566
	 * Prepare data array() before send it to $entity
567
	 *
568
	 * @param array $transaction_data
569
	 *
570
	 * @return array
571
	 * @throws transaction_exception
572
	 */
573
	private function build_data_ary($transaction_data)
574
	{
575
		$errors = array();
576
577
		if ($this->request->is_set('u') && $transaction_data['MT_USERNAME'] === '')
578
		{
579
			$user_id = ANONYMOUS;
580
		}
581
		else
582
		{
583
			$user_ary = $this->ppde_operator->query_donor_user_data('username', $transaction_data['MT_USERNAME']);
584
585
			if ($user_ary)
586
			{
587
				$user_id = $user_ary['user_id'];
588
			}
589
			else
590
			{
591
				$errors[] = $this->language->lang('PPDE_MT_DONOR_NOT_FOUND', $transaction_data['MT_USERNAME']);
592
			}
593
		}
594
595
		if ($transaction_data['MT_MC_GROSS'] <= 0)
596
		{
597
			$errors[] = $this->language->lang('PPDE_MT_MC_GROSS_TOO_LOW');
598
		}
599
600
		if ($transaction_data['MT_MC_FEE'] < 0)
601
		{
602
			$errors[] = $this->language->lang('PPDE_MT_MC_FEE_NEGATIVE');
603
		}
604
605
		if ($transaction_data['MT_MC_FEE'] >= $transaction_data['MT_MC_GROSS'])
606
		{
607
			$errors[] = $this->language->lang('PPDE_MT_MC_FEE_TOO_HIGH');
608
		}
609
610
		$payment_date = implode('-', [
611
			$transaction_data['MT_PAYMENT_DATE_YEAR'],
612
			$transaction_data['MT_PAYMENT_DATE_MONTH'],
613
			$transaction_data['MT_PAYMENT_DATE_DAY'],
614
		]);
615
616
		$payment_date_timestamp_at_midnight = $this->user->get_timestamp_from_format('Y-m-d H:i:s', $payment_date . ' 00:00:00');
617
618
		if ($payment_date_timestamp_at_midnight === false)
0 ignored issues
show
introduced by
The condition $payment_date_timestamp_at_midnight === false is always false.
Loading history...
619
		{
620
			$errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_ERROR', $payment_date);
621
		}
622
623
		$payment_time = $transaction_data['MT_PAYMENT_TIME'];
624
		$payment_time_timestamp = strtotime($payment_time);
625
626
		if ($payment_time_timestamp === false)
627
		{
628
			$errors[] = $this->language->lang('PPDE_MT_PAYMENT_TIME_ERROR', $payment_time);
629
		}
630
631
		// Normalize payment time to start from today at midnight
632
		$payment_time_timestamp_from_midnight = $payment_time_timestamp - strtotime('00:00:00');
633
634
		$payment_date_time = $payment_date_timestamp_at_midnight + $payment_time_timestamp_from_midnight;
635
636
		if ($payment_date_time > time())
637
		{
638
			$errors[] = $this->language->lang('PPDE_MT_PAYMENT_DATE_FUTURE', $this->user->format_date($payment_date_time));
639
		}
640
641
		if ($errors)
642
		{
643
			throw (new transaction_exception())->set_errors($errors);
644
		}
645
646
		return array(
647
			'business'          => $this->config['ppde_account_id'],
648
			'confirmed'         => true,
649
			'exchange_rate'     => '',
650
			'first_name'        => $transaction_data['MT_FIRST_NAME'],
651
			'item_name'         => '',
652
			'item_number'       => implode('_', ['uid', $user_id, time()]),
1 ignored issue
show
Comprehensibility Best Practice introduced by
The variable $user_id does not seem to be defined for all execution paths leading up to this point.
Loading history...
653
			'last_name'         => $transaction_data['MT_LAST_NAME'],
654
			'mc_currency'       => $transaction_data['MT_MC_CURRENCY'],
655
			'mc_gross'          => $transaction_data['MT_MC_GROSS'],
656
			'mc_fee'            => $transaction_data['MT_MC_FEE'],
657
			'net_amount'        => (float) 0, // This value is calculated in core_actions:log_to_db()
658
			'parent_txn_id'     => '',
659
			'payer_email'       => $transaction_data['MT_PAYER_EMAIL'],
660
			'payer_id'          => '',
661
			'payer_status'      => '',
662
			'payment_date'      => $payment_date_time,
663
			'payment_status'    => 'Completed',
664
			'payment_type'      => '',
665
			'memo'              => $transaction_data['MT_MEMO'],
666
			'receiver_id'       => '',
667
			'receiver_email'    => '',
668
			'residence_country' => strtoupper($transaction_data['MT_RESIDENCE_COUNTRY']),
669
			'settle_amount'     => (float) 0,
670
			'settle_currency'   => '',
671
			'test_ipn'          => false,
672
			'txn_errors'        => '',
673
			'txn_id'            => 'PPDE' . gen_rand_string(13),
674
			'txn_type'          => 'ppde_manual_donation',
675
			'user_id'           => $user_id,
676
		);
677
	}
678
679
	/**
680
	 * Set log output vars for display in the template
681
	 *
682
	 * @param array $row
683
	 *
684
	 * @return void
685
	 * @access protected
686
	 */
687
	protected function display_log_assign_template_vars($row)
688
	{
689
		$this->template->assign_block_vars('log', array(
690
			'CONFIRMED'        => ($row['confirmed']) ? $this->language->lang('PPDE_DT_VERIFIED') : $this->language->lang('PPDE_DT_UNVERIFIED'),
691
			'DATE'             => $this->user->format_date($row['payment_date']),
692
			'ID'               => $row['transaction_id'],
693
			'PAYMENT_STATUS'   => $this->language->lang(array('PPDE_DT_PAYMENT_STATUS_VALUES', strtolower($row['payment_status']))),
694
			'TNX_ID'           => $row['txn_id'],
695
			'USERNAME'         => $row['username_full'],
696
			'S_CONFIRMED'      => (bool) $row['confirmed'],
697
			'S_PAYMENT_STATUS' => (strtolower($row['payment_status']) === 'completed') ? true : false,
698
			'S_TXN_ERRORS'     => !empty($row['txn_errors']),
699
			'S_TEST_IPN'       => (bool) $row['test_ipn'],
700
		));
701
	}
702
703
	/**
704
	 * Set output vars for display in the template
705
	 *
706
	 * @param array $data
707
	 *
708
	 * @return void
709
	 * @access protected
710
	 */
711
	protected function action_assign_template_vars($data)
712
	{
713
		$s_hidden_fields = build_hidden_fields(array(
714
			'id'                  => $data['transaction_id'],
715
			'txn_errors_approved' => $data['txn_errors_approved'],
716
		));
717
718
		$this->template->assign_vars(array(
719
			'BOARD_USERNAME' => get_username_string('full', $data['user_id'], $data['username'], $data['user_colour'], $this->language->lang('GUEST'), append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&amp;mode=overview')),
720
			'EXCHANGE_RATE'  => '1 ' . $data['mc_currency'] . ' = ' . $data['exchange_rate'] . ' ' . $data['settle_currency'],
721
			'ITEM_NAME'      => $data['item_name'],
722
			'ITEM_NUMBER'    => $data['item_number'],
723
			'MC_CURRENCY'    => $data['net_amount'] . ' ' . $data['mc_currency'],
724
			'MC_GROSS'       => $data['mc_gross'] . ' ' . $data['mc_currency'],
725
			'MC_FEE'         => $data['mc_fee'] . ' ' . $data['mc_currency'],
726
			'MC_NET'         => $data['net_amount'] . ' ' . $data['mc_currency'],
727
			'MEMO'           => $data['memo'],
728
			'NAME'           => $data['first_name'] . ' ' . $data['last_name'],
729
			'PAYER_EMAIL'    => $data['payer_email'],
730
			'PAYER_ID'       => $data['payer_id'],
731
			'PAYER_STATUS'   => $data['payer_status'] ? $this->language->lang('PPDE_DT_VERIFIED') : $this->language->lang('PPDE_DT_UNVERIFIED'),
732
			'PAYMENT_DATE'   => $this->user->format_date($data['payment_date']),
733
			'PAYMENT_STATUS' => $this->language->lang(array('PPDE_DT_PAYMENT_STATUS_VALUES', strtolower($data['payment_status']))),
734
			'RECEIVER_EMAIL' => $data['receiver_email'],
735
			'RECEIVER_ID'    => $data['receiver_id'],
736
			'SETTLE_AMOUNT'  => $data['settle_amount'] . ' ' . $data['settle_currency'],
737
			'TXN_ID'         => $data['txn_id'],
738
739
			'L_PPDE_DT_SETTLE_AMOUNT'         => $this->language->lang('PPDE_DT_SETTLE_AMOUNT', $data['settle_currency']),
740
			'L_PPDE_DT_EXCHANGE_RATE_EXPLAIN' => $this->language->lang('PPDE_DT_EXCHANGE_RATE_EXPLAIN', $this->user->format_date($data['payment_date'])),
741
			'S_CONVERT'                       => ($data['settle_amount'] == 0 && empty($data['exchange_rate'])) ? false : true,
742
			'S_ERROR'                         => !empty($data['txn_errors']),
743
			'S_ERROR_APPROVED'                => !empty($data['txn_errors_approved']),
744
			'S_HIDDEN_FIELDS'                 => $s_hidden_fields,
745
			'ERROR_MSG'                       => (!empty($data['txn_errors'])) ? $data['txn_errors'] : '',
746
		));
747
	}
748
}
749