Completed
Pull Request — develop (#22)
by Mario
07:58
created

transactions::build_log_ary()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 2
eloc 15
nc 2
nop 4
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\operators;
12
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
class transactions
16
{
17
	protected $container;
18
	protected $db;
19
	protected $ppde_transactions_log_table;
20
21
	/**
22
	 * Constructor
23
	 *
24
	 * @param ContainerInterface                $container                   Service container interface
25
	 * @param \phpbb\db\driver\driver_interface $db                          Database connection
26
	 * @param string                            $ppde_transactions_log_table Table name
27
	 *
28
	 * @access public
29
	 */
30
	public function __construct(ContainerInterface $container, \phpbb\db\driver\driver_interface $db, $ppde_transactions_log_table)
31
	{
32
		$this->container = $container;
33
		$this->db = $db;
34
		$this->ppde_transactions_log_table = $ppde_transactions_log_table;
35
	}
36
37
	/**
38
	 * SQL Query to return Transaction log data table
39
	 *
40
	 * @param $transaction_id
41
	 *
42
	 * @return string
43
	 * @access public
44
	 */
45
	public function build_sql_data($transaction_id = 0)
46
	{
47
		// Build main sql request
48
		$sql_ary = array(
49
			'SELECT'    => '*, u.username',
50
			'FROM'      => array($this->ppde_transactions_log_table => 'txn'),
51
			'LEFT_JOIN' => array(
52
				array(
53
					'FROM' => array(USERS_TABLE => 'u'),
54
					'ON'   => 'u.user_id = txn.user_id',
55
				),
56
			),
57
			'ORDER_BY'  => 'txn.transaction_id',
58
		);
59
60
		// Use WHERE clause when $currency_id is different from 0
61
		if ((int) $transaction_id)
62
		{
63
			$sql_ary['WHERE'] = 'txn.transaction_id = ' . (int) $transaction_id;
64
		}
65
66
		// Return all transactions entities
67
		return $this->db->sql_build_query('SELECT', $sql_ary);
68
	}
69
70
	/**
71
	 * Returns the SQL Query for generation the donors list
72
	 *
73
	 * @param int $max_txn_id
74
	 *
75
	 * @return array
1 ignored issue
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string|arra...ay<*,string>|string>[]>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
76
	 * @access public
77
	 */
78
	public function get_sql_donorlist_ary($max_txn_id = 0)
79
	{
80
		// Build main sql request
81
		$donorlist_sql_ary = array(
82
			'SELECT'    => 'txn.*, MAX(txn.transaction_id) AS max_txn_id, SUM(txn.mc_gross) AS amount, u.username, u.user_colour',
83
			'FROM'      => array($this->ppde_transactions_log_table => 'txn'),
84
			'LEFT_JOIN' => array(
85
				array(
86
					'FROM' => array(USERS_TABLE => 'u'),
87
					'ON'   => 'u.user_id = txn.user_id',
88
				),
89
			),
90
			'WHERE'     => 'txn.user_id <> ' . ANONYMOUS . "
91
							AND txn.payment_status = 'Completed'",
92
			'GROUP_BY'  => 'txn.user_id',
93
			'ORDER_BY'  => 'txn.transaction_id DESC',
94
		);
95
96
		if ($max_txn_id)
97
		{
98
			$donorlist_sql_ary['WHERE'] = 'txn.transaction_id = ' . $max_txn_id;
99
			unset($donorlist_sql_ary['GROUP_BY'], $donorlist_sql_ary['ORDER_BY']);
100
		}
101
102
		// Return all transactions entities
103
		return $donorlist_sql_ary;
104
	}
105
106
	/**
107
	 * SQL Query to return donors list details
108
	 *
109
	 * @return string
110
	 * @access public
111
	 */
112
	public function build_sql_donorlist_data($sql_donorlist_ary)
113
	{
114
		// Return all transactions entities
115
		return $this->db->sql_build_query('SELECT', $sql_donorlist_ary);
116
	}
117
118
	/**
119
	 * Returns total entries of selected field
120
	 *
121
	 * @param array  $count_sql_ary
122
	 * @param string $selected_field
123
	 *
124
	 * @return int
125
	 * @access public
126
	 */
127
	public function query_sql_count($count_sql_ary, $selected_field)
128
	{
129
		$count_sql_ary['SELECT'] = 'COUNT(' . $selected_field . ') AS total_entries';
130
131
		if (array_key_exists('GROUP_BY', $count_sql_ary))
132
		{
133
			$count_sql_ary['SELECT'] = 'COUNT(DISTINCT ' . $count_sql_ary['GROUP_BY'] . ') AS total_entries';
134
		}
135
		unset($count_sql_ary['ORDER_BY'], $count_sql_ary['GROUP_BY']);
136
137
		$sql = $this->db->sql_build_query('SELECT', $count_sql_ary);
138
		$this->db->sql_query($sql);
139
140
		return (int) $this->db->sql_fetchfield('total_entries');
141
	}
142
143
	/**
144
	 * Returns the SQL Query for displaying simple transactions details
145
	 *
146
	 * @param $keywords
147
	 * @param $sort_by
148
	 * @param $log_time
149
	 *
150
	 * @return array
151
	 * @access public
152
	 */
153
	public function get_logs_sql_ary($keywords, $sort_by, $log_time)
154
	{
155
		$sql_keywords = '';
156
		if (!empty($keywords))
157
		{
158
			// Get the SQL condition for our keywords
159
			$sql_keywords = $this->generate_sql_keyword($keywords);
160
		}
161
162
		$get_logs_sql_ary = array(
163
			'SELECT'   => 'txn.transaction_id, txn.txn_id, txn.confirmed, txn.payment_date, txn.payment_status, txn.user_id, u.username, u.user_colour',
164
			'FROM'     => array(
165
				$this->ppde_transactions_log_table => 'txn',
166
				USERS_TABLE                        => 'u',
167
			),
168
			'WHERE'    => 'txn.user_id = u.user_id ' . $sql_keywords,
169
			'ORDER_BY' => $sort_by,
170
		);
171
172
		if ($log_time)
173
		{
174
			$get_logs_sql_ary['WHERE'] = 'txn.payment_date >= ' . (int) $log_time . '
175
					AND ' . $get_logs_sql_ary['WHERE'];
176
		}
177
178
		return $get_logs_sql_ary;
179
	}
180
181
	/**
182
	 * Generates a sql condition for the specified keywords
183
	 *
184
	 * @param string $keywords           The keywords the user specified to search for
185
	 * @param string $statement_operator The operator used to prefix the statement ('AND' by default)
186
	 *
187
	 * @return string Returns the SQL condition searching for the keywords
188
	 * @access private
189
	 */
190
	private function generate_sql_keyword($keywords, $statement_operator = 'AND')
191
	{
192
		// Use no preg_quote for $keywords because this would lead to sole
193
		// backslashes being added. We also use an OR connection here for
194
		// spaces and the | string. Currently, regex is not supported for
195
		// searching (but may come later).
196
		$keywords = preg_split('#[\s|]+#u', utf8_strtolower($keywords), 0, PREG_SPLIT_NO_EMPTY);
197
		$sql_keywords = '';
198
199
		if (!empty($keywords))
200
		{
201
			$keywords_pattern = array();
202
203
			// Build pattern and keywords...
204
			for ($i = 0, $num_keywords = sizeof($keywords); $i < $num_keywords; $i++)
205
			{
206
				$keywords_pattern[] = preg_quote($keywords[$i], '#');
207
				$keywords[$i] = $this->db->sql_like_expression($this->db->get_any_char() . $keywords[$i] . $this->db->get_any_char());
208
			}
209
210
			$sql_keywords = ' ' . $statement_operator . ' (';
211
			$sql_lower = $this->db->sql_lower_text('txn.txn_id');
212
			$sql_keywords .= ' ' . $sql_lower . ' ' . implode(' OR ' . $sql_lower . ' ', $keywords) . ')';
213
		}
214
215
		return $sql_keywords;
216
	}
217
218
	/**
219
	 * Returns user information based on the ID of the donor or they email
220
	 *
221
	 * @param string $type
222
	 * @param int    $arg
223
	 *
224
	 * @return array|bool
225
	 * @access public
226
	 */
227
	public function query_donor_user_data($type = 'user', $arg = 1)
228
	{
229
230
		switch ($type)
231
		{
232
			case 'user':
233
				$sql_where = ' WHERE user_id = ' . (int) $arg;
234
				break;
235
			case 'email':
236
				$sql_where = ' WHERE user_email_hash = ' . crc32(strtolower($arg)) . strlen($arg);
237
				break;
238
			default:
239
				$sql_where = '';
240
		}
241
242
		$sql = 'SELECT user_id, username
243
			FROM ' . USERS_TABLE .
244
			$sql_where;
245
		$result = $this->db->sql_query($sql);
246
247
		return $this->db->sql_fetchrow($result);
248
	}
249
250
	/**
251
	 * Returns simple details of all PayPal transactions logged in the database
252
	 *
253
	 * @param array $get_logs_sql_ary
254
	 * @param array $url_ary
255
	 * @param int   $limit
256
	 * @param int   $last_page_offset
257
	 *
258
	 * @return array $log
259
	 * @access public
260
	 */
261
	public function build_log_ary($get_logs_sql_ary, $url_ary, $limit = 0, $last_page_offset = 0)
262
	{
263
		$sql = $this->db->sql_build_query('SELECT', $get_logs_sql_ary);
264
		$result = $this->db->sql_query_limit($sql, $limit, $last_page_offset);
265
266
		$i = 0;
267
		$log = array();
268
269
		while ($row = $this->db->sql_fetchrow($result))
270
		{
271
			$log[$i] = array(
272
				'transaction_id' => $row['transaction_id'],
273
				'txn_id'         => $this->build_transaction_url($row['transaction_id'], $row['txn_id'], $url_ary['txn_url'], $row['confirmed']),
274
				'confirmed'      => $row['confirmed'],
275
				'payment_status' => $row['payment_status'],
276
				'payment_date'   => $row['payment_date'],
277
278
				'username_full'  => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], false, $url_ary['profile_url']),
279
			);
280
281
			$i++;
282
		}
283
284
		return $log;
285
	}
286
287
	/**
288
	 * Build transaction url for placing into templates.
289
	 *
290
	 * @param int    $id         The users transaction id
291
	 * @param string $txn_id     The txn number id
292
	 * @param string $custom_url optional parameter to specify a profile url. The transaction id get appended to this
293
	 *                           url as &amp;id={id}
294
	 * @param bool   $colour
295
	 *
296
	 * @return string A string consisting of what is wanted.
297
	 * @access private
298
	 */
299
	private function build_transaction_url($id, $txn_id, $custom_url = '', $colour = false)
300
	{
301
		static $_profile_cache;
302
303
		// We cache some common variables we need within this function
304
		if (empty($_profile_cache))
305
		{
306
			$_profile_cache['tpl_nourl'] = '{TRANSACTION}';
307
			$_profile_cache['tpl_url'] = '<a href="{TXN_URL}">{TRANSACTION}</a>';
308
			$_profile_cache['tpl_url_colour'] = '<a href="{TXN_URL}" style="{TXN_COLOUR};">{TRANSACTION}</a>';
309
		}
310
311
		// Build correct transaction url
312
		$txn_url = '';
313
		if ($txn_id)
314
		{
315
			$txn_url = ($custom_url !== '') ? $custom_url . '&amp;action=view&amp;id=' . $id : $txn_id;
316
		}
317
318
		// Return
319
320
		if (!$txn_url)
321
		{
322
			return str_replace('{TRANSACTION}', $txn_id, $_profile_cache['tpl_nourl']);
323
		}
324
325
		return str_replace(array('{TXN_URL}', '{TXN_COLOUR}', '{TRANSACTION}'), array($txn_url, '#FF0000', $txn_id), (!$colour) ? $_profile_cache['tpl_url'] : $_profile_cache['tpl_url_colour']);
326
	}
327
328
	/**
329
	 * Returns SQL WHERE clause for all marked items
330
	 *
331
	 * @param $marked
332
	 *
333
	 * @return string
334
	 * @access public
335
	 */
336
	public function build_marked_where_sql($marked)
337
	{
338
		$sql_in = array();
339
		foreach ($marked as $mark)
340
		{
341
			$sql_in[] = $mark;
342
		}
343
344
		return ' WHERE ' . $this->db->sql_in_set('transaction_id', $sql_in);
345
	}
346
}
347