Passed
Pull Request — develop-3.2.x (#63)
by Mario
02:51
created

admin_transactions_controller::get_log_count()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\controller;
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 skouat\ppde\operators\transactions;
21
use Symfony\Component\DependencyInjection\ContainerInterface;
22
23
/**
24
 * @property config             config             Config object
25
 * @property ContainerInterface container          Service container interface
26
 * @property string             id_prefix_name     Prefix name for identifier in the URL
27
 * @property string             lang_key_prefix    Prefix for the messages thrown by exceptions
28
 * @property language           language           Language user object
29
 * @property log                log                The phpBB log system.
30
 * @property string             module_name        Name of the module currently used
31
 * @property request            request            Request object.
32
 * @property bool               submit             State of submit $_POST variable
33
 * @property template           template           Template object
34
 * @property string             u_action           Action URL
35
 * @property user               user               User object.
36
 */
37
class admin_transactions_controller extends admin_main
38
{
39
	public $ppde_operator;
40
	protected $adm_relative_path;
41
	protected $auth;
42
	protected $entry_count;
43
	protected $last_page_offset;
44
	protected $php_ext;
45
	protected $phpbb_admin_path;
46
	protected $phpbb_root_path;
47
	protected $ppde_actions;
48
	protected $table_prefix;
49
	protected $table_ppde_transactions;
50
51
	/**
52
	 * Constructor
53
	 *
54
	 * @param auth               $auth                       Authentication object
55
	 * @param config             $config                     Config object
56
	 * @param ContainerInterface $container                  Service container interface
57
	 * @param language           $language                   Language user object
58
	 * @param log                $log                        The phpBB log system
59
	 * @param core_actions       $ppde_actions               PPDE actions object
60
	 * @param transactions       $ppde_operator_transactions Operator object
61
	 * @param request            $request                    Request object
62
	 * @param template           $template                   Template object
63
	 * @param user               $user                       User object.
64
	 * @param string             $adm_relative_path          phpBB admin relative path
65
	 * @param string             $phpbb_root_path            phpBB root path
66
	 * @param string             $php_ext                    phpEx
67
	 * @param string             $table_prefix               The table prefix
68
	 * @param string             $table_ppde_transactions    Name of the table used to store data
69
	 *
70
	 * @access public
71
	 */
72
	public function __construct(auth $auth, config $config, ContainerInterface $container, language $language, log $log, core_actions $ppde_actions, transactions $ppde_operator_transactions, request $request, template $template, user $user, $adm_relative_path, $phpbb_root_path, $php_ext, $table_prefix, $table_ppde_transactions)
73
	{
74
		$this->auth = $auth;
75
		$this->config = $config;
76
		$this->container = $container;
77
		$this->language = $language;
78
		$this->log = $log;
79
		$this->ppde_actions = $ppde_actions;
80
		$this->ppde_operator = $ppde_operator_transactions;
81
		$this->request = $request;
82
		$this->template = $template;
83
		$this->user = $user;
84
		$this->adm_relative_path = $adm_relative_path;
85
		$this->phpbb_admin_path = $phpbb_root_path . $adm_relative_path;
86
		$this->phpbb_root_path = $phpbb_root_path;
87
		$this->php_ext = $php_ext;
88
		$this->table_prefix = $table_prefix;
89
		$this->table_ppde_transactions = $table_ppde_transactions;
90
		parent::__construct(
91
			'transactions',
92
			'PPDE_DT',
93
			'transaction'
94
		);
95
	}
96
97
	/**
98
	 * Display the transactions list
99
	 *
100
	 * @param string $id     Module id
101
	 * @param string $mode   Module categorie
102
	 * @param string $action Action name
103
	 *
104
	 * @return void
105
	 * @access public
106
	 */
107
	public function display_transactions($id, $mode, $action)
108
	{
109
		// Set up general vars
110
		$args = array();
111
		$start = $this->request->variable('start', 0);
112
		$deletemark = $this->request->is_set('delmarked') ? $this->request->variable('delmarked', false) : false;
113
		$deleteall = $this->request->is_set('delall') ? $this->request->variable('delall', false) : false;
114
		$marked = $this->request->variable('mark', array(0));
115
		$txn_approve = $this->request->is_set('approve');
116
		$txn_approved = $this->request->variable('txn_errors_approved', 0);
117
		// Sort keys
118
		$sort_days = $this->request->variable('st', 0);
119
		$sort_key = $this->request->variable('sk', 't');
120
		$sort_dir = $this->request->variable('sd', 'd');
121
122
		// Prepares args for entries deletion
123
		if (($deletemark || $deleteall) && $this->auth->acl_get('a_ppde_manage'))
124
		{
125
			$action = 'delete';
126
			$args = array(
127
				'hidden_fields' => array(
128
					'start'     => $start,
129
					'delall'    => $deleteall,
130
					'delmarked' => $deletemark,
131
					'mark'      => $marked,
132
					'st'        => $sort_days,
133
					'sk'        => $sort_key,
134
					'sd'        => $sort_dir,
135
					'i'         => $id,
136
					'mode'      => $mode,
137
				),
138
			);
139
		}
140
141
		if ($txn_approve)
142
		{
143
			$transaction_id = $this->request->variable('id', 0);
144
			$action = 'approve';
145
			$args = array(
146
				'hidden_fields' => array(
147
					'approve'             => true,
148
					'id'                  => $transaction_id,
149
					'txn_errors_approved' => $txn_approved,
150
				),
151
			);
152
		}
153
154
		$action = $this->do_action($action, $args);
155
156
		if (!$action)
157
		{
158
			/** @type \phpbb\pagination $pagination */
159
			$pagination = $this->container->get('pagination');
160
161
			// Sorting
162
			$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'));
163
			$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'));
164
			$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');
165
166
			$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
167
			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);
168
169
			// Define where and sort sql for use in displaying transactions
170
			$sql_where = ($sort_days) ? (time() - ($sort_days * 86400)) : 0;
171
			$sql_sort = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC');
172
173
			$keywords = $this->request->variable('keywords', '', true);
174
			$keywords_param = !empty($keywords) ? '&amp;keywords=' . urlencode(htmlspecialchars_decode($keywords)) : '';
175
176
			// Grab log data
177
			$log_data = array();
178
			$log_count = 0;
179
180
			$this->view_txn_log($log_data, $log_count, (int) $this->config['topics_per_page'], $start, $sql_where, $sql_sort, $keywords);
181
182
			$base_url = $this->u_action . '&amp;' . $u_sort_param . $keywords_param;
183
			$pagination->generate_template_pagination($base_url, 'pagination', 'start', $log_count, (int) $this->config['topics_per_page'], $start);
184
185
			$this->template->assign_vars(array(
186
				'S_CLEARLOGS'  => $this->auth->acl_get('a_ppde_manage'),
187
				'S_KEYWORDS'   => $keywords,
188
				'S_LIMIT_DAYS' => $s_limit_days,
189
				'S_SORT_KEY'   => $s_sort_key,
190
				'S_SORT_DIR'   => $s_sort_dir,
191
				'S_TXN'        => $mode,
192
				'U_ACTION'     => $this->u_action . '&amp;' . $u_sort_param . $keywords_param . '&amp;start=' . $start,
193
			));
194
195
			array_map(array($this, 'display_log_assign_template_vars'), $log_data);
196
		}
197
	}
198
199
	/**
200
	 * Do action regarding the value of $action
201
	 *
202
	 * @param string $action Requested action
203
	 * @param array  $args   Arguments required for the action
204
	 *
205
	 * @return string
206
	 * @access private
207
	 */
208
	private function do_action($action, $args)
209
	{
210
		// Initiate an entity
211
		/** @type \skouat\ppde\entity\transactions $entity */
212
		$entity = $this->get_container_entity();
213
214
		switch ($action)
215
		{
216
			case 'view':
217
				// Request Identifier of the transaction
218
				$transaction_id = $this->request->variable('id', 0);
219
220
				// add field username to the table schema needed by entity->import()
221
				$additional_table_schema = array(
222
					'item_username'     => array('name' => 'username', 'type' => 'string'),
223
					'item_user_colour'  => array('name' => 'user_colour', 'type' => 'string'),
224
				);
225
226
				// Grab transaction data
227
				$data_ary = $entity->get_data($this->ppde_operator->build_sql_data($transaction_id), $additional_table_schema);
228
229
				array_map(array($this, 'action_assign_template_vars'), $data_ary);
230
231
				$this->template->assign_vars(array(
232
					'U_ACTION' => $this->u_action,
233
					'U_BACK'   => $this->u_action,
234
					'S_VIEW'   => true,
235
				));
236
			break;
237
			case 'delete':
238
				if (confirm_box(true))
239
				{
240
					$where_sql = '';
241
242
					if ($args['hidden_fields']['delmarked'] && count($args['hidden_fields']['mark']))
243
					{
244
						$where_sql = $this->ppde_operator->build_marked_where_sql($args['hidden_fields']['mark']);
245
					}
246
247
					if ($where_sql || $args['hidden_fields']['delall'])
248
					{
249
						$entity->delete(0, '', $where_sql, $args['hidden_fields']['delall']);
250
						$this->ppde_actions->set_ipn_test_properties(true);
251
						$this->ppde_actions->update_overview_stats();
252
						$this->ppde_actions->set_ipn_test_properties(false);
253
						$this->ppde_actions->update_overview_stats();
254
						$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_PURGED', time());
255
					}
256
				}
257
				else
258
				{
259
					confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields($args['hidden_fields']));
260
				}
261
				// Clear $action status
262
				$action = '';
263
			break;
264
			case 'approve':
265
				if (confirm_box(true))
266
				{
267
					$transaction_id = (int) $args['hidden_fields']['id'];
268
					$txn_approved = !empty($args['hidden_fields']['txn_errors_approved']) ? false : true;
269
270
					// Update DB record
271
					$entity->load($transaction_id);
272
					$entity->set_txn_errors_approved($txn_approved);
273
					$entity->save(false);
274
275
					// Prepare transaction settings before doing actions
276
					$this->ppde_actions->set_transaction_data($entity->get_data($this->ppde_operator->build_sql_data($transaction_id)));
277
					$this->ppde_actions->set_ipn_test_properties($entity->get_test_ipn());
278
					$this->ppde_actions->is_donor_is_member();
279
280
					// Do the actions related to the approval of the transaction
281
					$this->ppde_actions->update_overview_stats();
282
					$this->ppde_actions->update_raised_amount();
283
					if (!$this->ppde_actions->get_ipn_test() && $this->ppde_actions->get_donor_is_member())
284
					{
285
						$this->ppde_actions->update_donor_stats();
286
						$this->ppde_actions->donors_group_user_add();
287
						$this->ppde_actions->notification->notify_donor_donation_received();
288
					}
289
290
					$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_' . $this->lang_key_prefix . '_UPDATED', time());
291
				}
292
				else
293
				{
294
					confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields($args['hidden_fields']));
295
				}
296
				// Clear $action status
297
				$action = '';
298
		}
299
300
		return $action;
301
	}
302
303
	/**
304
	 * View log
305
	 *
306
	 * @param array  &$log         The result array with the logs
307
	 * @param mixed  &$log_count   If $log_count is set to false, we will skip counting all entries in the
308
	 *                             database. Otherwise an integer with the number of total matching entries is returned.
309
	 * @param int     $limit       Limit the number of entries that are returned
310
	 * @param int     $offset      Offset when fetching the log entries, f.e. when paginating
311
	 * @param int     $limit_days
312
	 * @param string  $sort_by     SQL order option, e.g. 'l.log_time DESC'
313
	 * @param string  $keywords    Will only return log entries that have the keywords in log_operation or log_data
314
	 *
315
	 * @return int Returns the offset of the last valid page, if the specified offset was invalid (too high)
316
	 * @access private
317
	 */
318
	private function view_txn_log(&$log, &$log_count, $limit = 0, $offset = 0, $limit_days = 0, $sort_by = 'txn.payment_date DESC', $keywords = '')
319
	{
320
		$count_logs = ($log_count !== false);
321
322
		$log = $this->get_logs($count_logs, $limit, $offset, $limit_days, $sort_by, $keywords);
323
		$log_count = $this->get_log_count();
324
325
		return $this->get_valid_offset();
326
	}
327
328
	/**
329
	 * @param bool   $count_logs
330
	 * @param int    $limit
331
	 * @param int    $offset
332
	 * @param int    $log_time
333
	 * @param string $sort_by
334
	 * @param string $keywords
335
	 *
336
	 * @return array $log
337
	 * @access private
338
	 */
339
	private function get_logs($count_logs = true, $limit = 0, $offset = 0, $log_time = 0, $sort_by = 'txn.payment_date DESC', $keywords = '')
340
	{
341
		$this->entry_count = 0;
342
		$this->last_page_offset = $offset;
343
		$url_ary = array();
344
345
		if ($this->get_container_entity()->is_in_admin() && $this->phpbb_admin_path)
346
		{
347
			$url_ary['profile_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=users&amp;mode=overview');
348
			$url_ary['txn_url'] = append_sid($this->phpbb_admin_path . 'index.' . $this->php_ext, 'i=-skouat-ppde-acp-ppde_module&amp;mode=transactions');
349
350
		}
351
		else
352
		{
353
			$url_ary['profile_url'] = append_sid($this->phpbb_root_path . 'memberlist.' . $this->php_ext, 'mode=viewprofile');
354
			$url_ary['txn_url'] = '';
355
		}
356
357
		$get_logs_sql_ary = $this->ppde_operator->get_logs_sql_ary($keywords, $sort_by, $log_time);
358
359
		if ($count_logs)
360
		{
361
			$this->entry_count = $this->ppde_operator->query_sql_count($get_logs_sql_ary, 'txn.transaction_id');
362
363
			if ($this->entry_count == 0)
364
			{
365
				// Save the queries, because there are no logs to display
366
				$this->last_page_offset = 0;
367
368
				return array();
369
			}
370
371
			// Return the user to the last page that is valid
372
			while ($this->last_page_offset >= $this->entry_count)
373
			{
374
				$this->last_page_offset = max(0, $this->last_page_offset - $limit);
375
			}
376
		}
377
378
		return $this->ppde_operator->build_log_ary($get_logs_sql_ary, $url_ary, $limit, $this->last_page_offset);
379
	}
380
381
	/**
382
	 * @return integer
383
	 */
384
	public function get_log_count()
385
	{
386
		return ($this->entry_count) ? (int) $this->entry_count : 0;
387
	}
388
389
	/**
390
	 * @return integer
391
	 */
392
	public function get_valid_offset()
393
	{
394
		return ($this->last_page_offset) ? (int) $this->last_page_offset : 0;
395
	}
396
397
	/**
398
	 * Set log output vars for display in the template
399
	 *
400
	 * @param array $row
401
	 *
402
	 * @return void
403
	 * @access protected
404
	 */
405
	protected function display_log_assign_template_vars($row)
406
	{
407
		$this->template->assign_block_vars('log', array(
408
			'CONFIRMED'        => ($row['confirmed']) ? $this->language->lang('PPDE_DT_VERIFIED') : $this->language->lang('PPDE_DT_UNVERIFIED'),
409
			'DATE'             => $this->user->format_date($row['payment_date']),
410
			'ID'               => $row['transaction_id'],
411
			'PAYMENT_STATUS'   => $this->language->lang(array('PPDE_DT_PAYMENT_STATUS_VALUES', strtolower($row['payment_status']))),
412
			'TNX_ID'           => $row['txn_id'],
413
			'USERNAME'         => $row['username_full'],
414
			'S_CONFIRMED'      => (bool) $row['confirmed'],
415
			'S_PAYMENT_STATUS' => (strtolower($row['payment_status']) === 'completed') ? true : false,
416
			'S_TXN_ERRORS'     => !empty($row['txn_errors']),
417
			'S_TEST_IPN'       => (bool) $row['test_ipn'],
418
		));
419
	}
420
421
	/**
422
	 * Set output vars for display in the template
423
	 *
424
	 * @param array $data
425
	 *
426
	 * @return void
427
	 * @access protected
428
	 */
429
	protected function action_assign_template_vars($data)
430
	{
431
		$s_hidden_fields = build_hidden_fields(array(
432
			'id'                  => $data['transaction_id'],
433
			'txn_errors_approved' => $data['txn_errors_approved'],
434
		));
435
436
		$this->template->assign_vars(array(
437
			'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')),
438
			'EXCHANGE_RATE'  => '1 ' . $data['mc_currency'] . ' = ' . $data['exchange_rate'] . ' ' . $data['settle_currency'],
439
			'ITEM_NAME'      => $data['item_name'],
440
			'ITEM_NUMBER'    => $data['item_number'],
441
			'MC_CURRENCY'    => $data['net_amount'] . ' ' . $data['mc_currency'],
442
			'MC_GROSS'       => $data['mc_gross'] . ' ' . $data['mc_currency'],
443
			'MC_FEE'         => $data['mc_fee'] . ' ' . $data['mc_currency'],
444
			'MC_NET'         => $data['net_amount'] . ' ' . $data['mc_currency'],
445
			'MEMO'           => $data['memo'],
446
			'NAME'           => $data['first_name'] . ' ' . $data['last_name'],
447
			'PAYER_EMAIL'    => $data['payer_email'],
448
			'PAYER_ID'       => $data['payer_id'],
449
			'PAYER_STATUS'   => $data['payer_status'] ? $this->language->lang('PPDE_DT_VERIFIED') : $this->language->lang('PPDE_DT_UNVERIFIED'),
450
			'PAYMENT_DATE'   => $this->user->format_date($data['payment_date']),
451
			'PAYMENT_STATUS' => $this->language->lang(array('PPDE_DT_PAYMENT_STATUS_VALUES', strtolower($data['payment_status']))),
452
			'RECEIVER_EMAIL' => $data['receiver_email'],
453
			'RECEIVER_ID'    => $data['receiver_id'],
454
			'SETTLE_AMOUNT'  => $data['settle_amount'] . ' ' . $data['settle_currency'],
455
			'TXN_ID'         => $data['txn_id'],
456
457
			'L_PPDE_DT_SETTLE_AMOUNT'         => $this->language->lang('PPDE_DT_SETTLE_AMOUNT', $data['settle_currency']),
458
			'L_PPDE_DT_EXCHANGE_RATE_EXPLAIN' => $this->language->lang('PPDE_DT_EXCHANGE_RATE_EXPLAIN', $this->user->format_date($data['payment_date'])),
459
			'S_CONVERT'                       => ($data['settle_amount'] == 0 && empty($data['exchange_rate'])) ? false : true,
460
			'S_ERROR'                         => !empty($data['txn_errors']),
461
			'S_ERROR_APPROVED'                => !empty($data['txn_errors_approved']),
462
			'S_HIDDEN_FIELDS'                 => $s_hidden_fields,
463
			'ERROR_MSG'                       => (!empty($data['txn_errors'])) ? $data['txn_errors'] : '',
464
		));
465
	}
466
}
467