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

transactions_controller::build_data_ary()   D

Complexity

Conditions 11
Paths 384

Size

Total Lines 103
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

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