Completed
Push — master ( 06acd3...66093a )
by Nazar
06:23
created

transactions.php ➔ make_header()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 2
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   Blockchain payment
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2015-2017, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Blockchain_payment;
10
use
11
	h,
12
	cs\Language\Prefix,
13
	cs\Page,
14
	cs\User;
15
16
function make_url ($arguments) {
17
	$base_url = 'admin/Blockchain_payment/transactions?';
18
	return $base_url.http_build_query(array_merge((array)$_GET, $arguments));
19
}
20
21
function make_header ($title, $field) {
22
	$order_by = $_GET['order_by'] ?? 'created';
23
	$icon     = $order_by == $field ? h::icon(@$_GET['asc'] ? 'caret-up' : 'caret-down') : '';
24
	$asc      = $order_by == $field ? !@$_GET['asc'] : false;
25
	return h::a(
26
		"$title $icon",
27
		[
28
			'href' => make_url(
29
				[
30
					'order_by' => $field,
31
					'asc'      => $asc,
32
					'page'     => 1
33
				]
34
			)
35
		]
36
	);
37
}
38
39
$L                  = new Prefix('blockchain_payment_');
40
$Transactions       = Transactions::instance();
41
$page               = $_GET['page'] ?? 1;
42
$count              = $_GET['count'] ?? 100;
43
$transactions       = $Transactions->get(
44
	$Transactions->search(
45
		(array)$_GET,
46
		$page,
47
		$count,
48
		$_GET['order_by'] ?? 'created',
49
		@$_GET['asc']
50
	)
51
);
52
$transactions_total = $Transactions->search(
53
	[
54
		'total_count' => 1
55
	] + (array)$_GET,
56
	$page,
57
	$count,
58
	$_GET['order_by'] ?? 'created',
59
	@$_GET['asc']
60
);
61
Page::instance()
62
	->title($L->transactions)
63
	->content(
64
		h::{'h2.cs-text-center'}($L->transactions).
65
		h::{'table.cs-table[list]'}(
66
			h::{'tr th'}(
67
				make_header('id', 'id'),
68
				make_header($L->amount, 'amount'),
69
				make_header($L->currency, 'currency'),
70
				make_header($L->amount_btc, 'amount_btc'),
71
				make_header($L->user, 'user'),
72
				make_header($L->created, 'created'),
73
				make_header($L->paid, 'paid'),
74
				make_header($L->confirmed, 'confirmed')
75
			).
76
			h::tr(
77
				array_map(
78
					function ($transaction) use ($L) {
79
						$time      = time();
80
						$created   = $transaction['created']
81
							? $L->to_locale(
82
								date($L->{$time - $transaction['created'] < 24 * 3600 ? '_time' : '_datetime_long'}, $transaction['created'])
83
							)
84
							: '-';
85
						$paid      = $transaction['paid']
86
							? $L->to_locale(
87
								date($L->{$time - $transaction['paid'] < 24 * 3600 ? '_time' : '_datetime_long'}, $transaction['paid'])
88
							)
89
							: '-';
90
						$confirmed = $transaction['confirmed']
91
							? $L->to_locale(
92
								date($L->{$time - $transaction['confirmed'] < 24 * 3600 ? '_time' : '_datetime_long'}, $transaction['confirmed'])
93
							)
94
							: '-';
95
						$username  = User::instance()->username($transaction['user']);
96
						$class     =
97
							$transaction['confirmed'] ? 'cs-block-success.cs-text-success' :
98
								($transaction['paid'] ? 'cs-block-warning.cs-text-warning' : 'cs-block-error.cs-text-error');
99
						$tag       = "td.$class";
100
						return [
101
							[
102
								h::$tag(
103
									$transaction['id'],
104
									$transaction['amount'],
105
									$transaction['currency'],
106
									$transaction['amount_btc'],
107
									h::a(
108
										$username,
109
										[
110
											'href' => "admin/Blockchain_payment/transactions/?user=$transaction[user]"
111
										]
112
									),
113
									$created,
114
									$paid,
115
									$confirmed
116
								),
117
								[
118
									'style' => 'border-bottom: none;'
119
								]
120
							],
121
							h::$tag(
122
								"$L->module: ".
123
								h::a(
124
									$transaction['module'],
125
									[
126
										'href' => "admin/Blockchain_payment/transactions/?module=$transaction[module]"
127
									]
128
								).
129
								" $L->purpose: $transaction[purpose]".
130
								h::br().
131
								"$L->destination_address: ".
132
								h::{'a[target=_blank]'}(
133
									$transaction['destination_address'],
134
									[
135
										'href' => "https://blockchain.info/address/$transaction[destination_address]"
136
									]
137
								).
138
								" $L->intermediate_address: ".
139
								h::{'a[target=_blank]'}(
140
									$transaction['input_address'],
141
									[
142
										'href' => "https://blockchain.info/address/$transaction[input_address]"
143
									]
144
								).
145
								h::br().
146
								" $L->transaction_hash: ".
147
								h::{'a[target=_blank]'}(
148
									$transaction['transaction_hash'],
149
									[
150
										'href' => "https://blockchain.info/tx/$transaction[transaction_hash]"
151
									]
152
								).
153
								" $L->intermediate_transaction_hash: ".
154
								h::{'a[target=_blank]'}(
155
									$transaction['input_transaction_hash'],
156
									[
157
										'href' => "https://blockchain.info/tx/$transaction[input_transaction_hash]"
158
									]
159
								).
160
								h::br().
161
								nl2br($transaction['description']),
162
								[
163
									'colspan' => 8
164
								]
165
							)
166
						];
167
					},
168
					$transactions
169
				) ?: false
170
			)
171
		).
172
		pages(
173
			$page,
174
			ceil($transactions_total / $count),
175
			function ($page) {
176
				return make_url(
177
					[
178
						'page' => $page
179
					]
180
				);
181
			},
182
			true
183
		)
184
	);
185