1 | <?php |
||
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 Identifier of the transaction logged in the DB |
||
74 | * @param string $order_by |
||
75 | * |
||
76 | * @return array |
||
77 | * @access public |
||
78 | */ |
||
79 | public function get_sql_donorlist_ary($max_txn_id = 0, $order_by = '') |
||
80 | { |
||
81 | // Build main sql request |
||
82 | $donorlist_sql_ary = array( |
||
83 | 'SELECT' => 'txn.*, MAX(txn.transaction_id) AS max_txn_id, SUM(txn.mc_gross) AS amount, u.username, u.user_colour', |
||
84 | 'FROM' => array($this->ppde_transactions_log_table => 'txn'), |
||
85 | 'LEFT_JOIN' => array( |
||
86 | array( |
||
87 | 'FROM' => array(USERS_TABLE => 'u'), |
||
88 | 'ON' => 'u.user_id = txn.user_id', |
||
89 | ), |
||
90 | ), |
||
91 | 'WHERE' => 'txn.user_id <> ' . ANONYMOUS . " |
||
92 | AND txn.payment_status = 'Completed'", |
||
93 | 'GROUP_BY' => 'txn.user_id', |
||
94 | 'ORDER_BY' => 'txn.transaction_id DESC', |
||
95 | ); |
||
96 | |||
97 | if ($order_by) |
||
98 | { |
||
99 | $donorlist_sql_ary['ORDER_BY'] = $order_by; |
||
100 | } |
||
101 | |||
102 | if ($max_txn_id) |
||
103 | { |
||
104 | $donorlist_sql_ary['WHERE'] = 'txn.transaction_id = ' . $max_txn_id; |
||
105 | unset($donorlist_sql_ary['GROUP_BY'], $donorlist_sql_ary['ORDER_BY']); |
||
106 | } |
||
107 | |||
108 | // Return all transactions entities |
||
109 | return $donorlist_sql_ary; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * SQL Query to return donors list details |
||
114 | * |
||
115 | * @return string |
||
116 | * @access public |
||
117 | */ |
||
118 | public function build_sql_donorlist_data($sql_donorlist_ary) |
||
119 | { |
||
120 | // Return all transactions entities |
||
121 | return $this->db->sql_build_query('SELECT', $sql_donorlist_ary); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Returns total entries of selected field |
||
126 | * |
||
127 | * @param array $count_sql_ary |
||
128 | * @param string $selected_field |
||
129 | * |
||
130 | * @return int |
||
131 | * @access public |
||
132 | */ |
||
133 | public function query_sql_count($count_sql_ary, $selected_field) |
||
134 | { |
||
135 | $count_sql_ary['SELECT'] = 'COUNT(' . $selected_field . ') AS total_entries'; |
||
136 | |||
137 | if (array_key_exists('GROUP_BY', $count_sql_ary)) |
||
138 | { |
||
139 | $count_sql_ary['SELECT'] = 'COUNT(DISTINCT ' . $count_sql_ary['GROUP_BY'] . ') AS total_entries'; |
||
140 | } |
||
141 | unset($count_sql_ary['ORDER_BY'], $count_sql_ary['GROUP_BY']); |
||
142 | |||
143 | $sql = $this->db->sql_build_query('SELECT', $count_sql_ary); |
||
144 | $this->db->sql_query($sql); |
||
145 | |||
146 | return (int) $this->db->sql_fetchfield('total_entries'); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Returns the SQL Query for displaying simple transactions details |
||
151 | * |
||
152 | * @param $keywords |
||
153 | * @param $sort_by |
||
154 | * @param $log_time |
||
155 | * |
||
156 | * @return array |
||
157 | * @access public |
||
158 | */ |
||
159 | public function get_logs_sql_ary($keywords, $sort_by, $log_time) |
||
160 | { |
||
161 | $sql_keywords = ''; |
||
162 | if (!empty($keywords)) |
||
163 | { |
||
164 | // Get the SQL condition for our keywords |
||
165 | $sql_keywords = $this->generate_sql_keyword($keywords); |
||
166 | } |
||
167 | |||
168 | $get_logs_sql_ary = array( |
||
169 | 'SELECT' => 'txn.transaction_id, txn.txn_id, txn.confirmed, txn.payment_date, txn.payment_status, txn.user_id, u.username, u.user_colour', |
||
170 | 'FROM' => array( |
||
171 | $this->ppde_transactions_log_table => 'txn', |
||
172 | USERS_TABLE => 'u', |
||
173 | ), |
||
174 | 'WHERE' => 'txn.user_id = u.user_id ' . $sql_keywords, |
||
175 | 'ORDER_BY' => $sort_by, |
||
176 | ); |
||
177 | |||
178 | if ($log_time) |
||
179 | { |
||
180 | $get_logs_sql_ary['WHERE'] = 'txn.payment_date >= ' . (int) $log_time . ' |
||
181 | AND ' . $get_logs_sql_ary['WHERE']; |
||
182 | } |
||
183 | |||
184 | return $get_logs_sql_ary; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Generates a sql condition for the specified keywords |
||
189 | * |
||
190 | * @param string $keywords The keywords the user specified to search for |
||
191 | * @param string $statement_operator The operator used to prefix the statement ('AND' by default) |
||
192 | * |
||
193 | * @return string Returns the SQL condition searching for the keywords |
||
194 | * @access private |
||
195 | */ |
||
196 | private function generate_sql_keyword($keywords, $statement_operator = 'AND') |
||
197 | { |
||
198 | // Use no preg_quote for $keywords because this would lead to sole |
||
199 | // backslashes being added. We also use an OR connection here for |
||
200 | // spaces and the | string. Currently, regex is not supported for |
||
201 | // searching (but may come later). |
||
202 | $keywords = preg_split('#[\s|]+#u', utf8_strtolower($keywords), 0, PREG_SPLIT_NO_EMPTY); |
||
203 | $sql_keywords = ''; |
||
204 | |||
205 | if (!empty($keywords)) |
||
206 | { |
||
207 | $keywords_pattern = array(); |
||
208 | |||
209 | // Build pattern and keywords... |
||
210 | for ($i = 0, $num_keywords = sizeof($keywords); $i < $num_keywords; $i++) |
||
211 | { |
||
212 | $keywords_pattern[] = preg_quote($keywords[$i], '#'); |
||
213 | $keywords[$i] = $this->db->sql_like_expression($this->db->get_any_char() . $keywords[$i] . $this->db->get_any_char()); |
||
214 | } |
||
215 | |||
216 | $sql_keywords = ' ' . $statement_operator . ' ('; |
||
217 | $sql_lower = $this->db->sql_lower_text('txn.txn_id'); |
||
218 | $sql_keywords .= ' ' . $sql_lower . ' ' . implode(' OR ' . $sql_lower . ' ', $keywords) . ')'; |
||
219 | } |
||
220 | |||
221 | return $sql_keywords; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Returns user information based on the ID of the donor or they email |
||
226 | * |
||
227 | * @param string $type |
||
228 | * @param int $arg |
||
229 | * |
||
230 | * @return array|bool |
||
231 | * @access public |
||
232 | */ |
||
233 | public function query_donor_user_data($type = 'user', $arg = 1) |
||
255 | |||
256 | /** |
||
257 | * Returns simple details of all PayPal transactions logged in the database |
||
258 | * |
||
259 | * @param array $get_logs_sql_ary |
||
260 | * @param array $url_ary |
||
261 | * @param int $limit |
||
262 | * @param int $last_page_offset |
||
263 | * |
||
264 | * @return array $log |
||
265 | * @access public |
||
266 | */ |
||
267 | public function build_log_ary($get_logs_sql_ary, $url_ary, $limit = 0, $last_page_offset = 0) |
||
292 | |||
293 | /** |
||
294 | * Build transaction url for placing into templates. |
||
295 | * |
||
296 | * @param int $id The users transaction id |
||
297 | * @param string $txn_id The txn number id |
||
298 | * @param string $custom_url optional parameter to specify a profile url. The transaction id get appended to this |
||
299 | * url as &id={id} |
||
300 | * @param bool $colour |
||
301 | * |
||
302 | * @return string A string consisting of what is wanted. |
||
303 | * @access private |
||
304 | */ |
||
305 | private function build_transaction_url($id, $txn_id, $custom_url = '', $colour = false) |
||
333 | |||
334 | /** |
||
335 | * Returns SQL WHERE clause for all marked items |
||
336 | * |
||
337 | * @param $marked |
||
338 | * |
||
339 | * @return string |
||
340 | * @access public |
||
341 | */ |
||
342 | public function build_marked_where_sql($marked) |
||
352 | } |
||
353 |