From 4eb6c59cb3b19ed533278ce00792d1006859d04b Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Thu, 1 Aug 2013 11:00:54 +0200 Subject: [PATCH] Refactored getTransactions, unified admin/user view * Removed getAllTransactions method * Unified getTransactions for users and admins * Added filter abilities to user transaction view This should speed up things a fair bit for transaction heavy pools. Addresses #536 --- public/include/classes/transaction.class.php | 49 ++--- .../pages/account/transactions.inc.php | 10 +- .../include/pages/admin/transactions.inc.php | 4 +- .../mmcFE/account/transactions/default.tpl | 171 +++++++----------- .../mmcFE/admin/transactions/default.tpl | 8 +- 5 files changed, 90 insertions(+), 152 deletions(-) diff --git a/public/include/classes/transaction.class.php b/public/include/classes/transaction.class.php index 4aa7fbd7..e4bb9d1f 100644 --- a/public/include/classes/transaction.class.php +++ b/public/include/classes/transaction.class.php @@ -33,42 +33,13 @@ class Transaction extends Base { /** * Get all transactions from start for account_id - * @param account_id int Account ID * @param start int Starting point, id of transaction + * @param filter array Filter to limit transactions + * @param limit int Only display this many transactions + * @param account_id int Account ID * @return data array Database fields as defined in SELECT **/ - public function getTransactions($account_id, $start=0) { - $this->debug->append("STA " . __METHOD__, 4); - $stmt = $this->mysqli->prepare(" - SELECT - t.id AS id, - t.type AS type, - t.amount AS amount, - t.coin_address AS coin_address, - t.timestamp AS timestamp, - b.height AS height, - b.confirmations AS confirmations - FROM transactions AS t - LEFT JOIN " . $this->block->getTableName() . " AS b ON t.block_id = b.id - WHERE t.account_id = ? - ORDER BY id DESC"); - if ($this->checkStmt($stmt)) { - if(!$stmt->bind_param('i', $account_id)) return false; - $stmt->execute(); - $result = $stmt->get_result(); - return $result->fetch_all(MYSQLI_ASSOC); - } - $this->debug->append('Unable to fetch transactions'); - return false; - } - - /** - * Fetch all transactions for all users - * Optionally apply a filter - * @param none - * @return mixed array or false - **/ - public function getAllTransactions($start=0,$filter=NULL,$limit=30) { + public function getTransactions($start=0, $filter=NULL, $limit=30, $account_id=NULL) { $this->debug->append("STA " . __METHOD__, 4); $sql = " SELECT @@ -124,6 +95,13 @@ class Transaction extends Base { $sql .= " WHERE " . implode(' AND ', $aFilter); } } + if (is_int($account_id) && empty($aFilter)) { + $sql .= " WHERE a.id = ?"; + $this->addParam('i', $account_id); + } else if (is_int($account_id)) { + $sql .= " AND a.id = ?"; + $this->addParam('i', $account_id); + } $sql .= " ORDER BY id DESC LIMIT ?,? @@ -132,15 +110,14 @@ class Transaction extends Base { $this->addParam('i', $start); $this->addParam('i', $limit); $stmt = $this->mysqli->prepare($sql); - if ($this->checkStmt($stmt) && $stmt->bind_param('ii', $start, $limit) && $stmt->execute() && $result = $stmt->get_result()) { + if ($this->checkStmt($stmt) && call_user_func_array( array($stmt, 'bind_param'), $this->getParam()) && $stmt->execute() && $result = $stmt->get_result()) { // Fetch matching row count $num_rows = $this->mysqli->prepare("SELECT FOUND_ROWS() AS num_rows"); if ($num_rows->execute() && $row_count = $num_rows->get_result()->fetch_object()->num_rows) $this->num_rows = $row_count; - if ($this->checkStmt($stmt) && call_user_func_array( array($stmt, 'bind_param'), $this->getParam()) && $stmt->execute() && $result = $stmt->get_result()) return $result->fetch_all(MYSQLI_ASSOC); } - $this->debug->append('Unable to fetch transactions'); + $this->debug->append('Failed to fetch transactions: ' . $this->mysqli->error); return false; } diff --git a/public/include/pages/account/transactions.inc.php b/public/include/pages/account/transactions.inc.php index 7bcfb4f2..bd8f2aa4 100644 --- a/public/include/pages/account/transactions.inc.php +++ b/public/include/pages/account/transactions.inc.php @@ -3,9 +3,17 @@ // Make sure we are called from index.php if (!defined('SECURITY')) die('Hacking attempt'); if ($user->isAuthenticated()) { - $aTransactions = $transaction->getTransactions($_SESSION['USERDATA']['id']); + $iLimit = 30; + empty($_REQUEST['start']) ? $start = 0 : $start = $_REQUEST['start']; + $aTransactions = $transaction->getTransactions($start, @$_REQUEST['filter'], $iLimit, $_SESSION['USERDATA']['id']); + $iCountTransactions = $transaction->num_rows; + $aTransactionTypes = $transaction->getTypes(); if (!$aTransactions) $_SESSION['POPUP'][] = array('CONTENT' => 'Could not find any transaction', 'TYPE' => 'errormsg'); + $smarty->assign('LIMIT', $iLimit); $smarty->assign('TRANSACTIONS', $aTransactions); + $smarty->assign('TRANSACTIONTYPES', $aTransactionTypes); + $smarty->assign('TXSTATUS', array('' => '', 'Confirmed' => 'Confirmed', 'Unconfirmed' => 'Unconfirmed', 'Orphan' => 'Orphan')); + $smarty->assign('COUNTTRANSACTIONS', $iCountTransactions); } $smarty->assign('CONTENT', 'default.tpl'); ?> diff --git a/public/include/pages/admin/transactions.inc.php b/public/include/pages/admin/transactions.inc.php index e068d293..505f25ab 100644 --- a/public/include/pages/admin/transactions.inc.php +++ b/public/include/pages/admin/transactions.inc.php @@ -12,9 +12,9 @@ if (!$user->isAuthenticated() || !$user->isAdmin($_SESSION['USERDATA']['id'])) { if (!$smarty->isCached('master.tpl', $smarty_cache_key)) { $iLimit = 30; $debug->append('No cached version available, fetching from backend', 3); - $aTransactions = $transaction->getAllTransactions(@$_REQUEST['start'], @$_REQUEST['filter'], $iLimit); - $iCountTransactions = $transaction->num_rows; empty($_REQUEST['start']) ? $start = 0 : $start = $_REQUEST['start']; + $aTransactions = $transaction->getTransactions($start, @$_REQUEST['filter'], $iLimit); + $iCountTransactions = $transaction->num_rows; $aTransactionTypes = $transaction->getTypes(); if (!$aTransactions) $_SESSION['POPUP'][] = array('CONTENT' => 'Could not find any transaction', 'TYPE' => 'errormsg'); $smarty->assign('LIMIT', $iLimit); diff --git a/public/templates/mmcFE/account/transactions/default.tpl b/public/templates/mmcFE/account/transactions/default.tpl index a7337bd6..5563aee4 100644 --- a/public/templates/mmcFE/account/transactions/default.tpl +++ b/public/templates/mmcFE/account/transactions/default.tpl @@ -1,40 +1,86 @@ -{include file="global/block_header.tpl" BLOCK_HEADER="Transaction Log" BUTTONS=array(Confirmed,Unconfirmed,Orphan)} -
+{include file="global/block_header.tpl" ALIGN="left" BLOCK_STYLE="width: 23%" BLOCK_HEADER="Transaction Filter"} +
+ + + + + + + + + + + + + + + + + + + + +
+{if $COUNTTRANSACTIONS / $LIMIT > 1} + {if $smarty.request.start|default:"0" > 0} + + {else} + + {/if} +{/if} + +{if $COUNTTRANSACTIONS / $LIMIT > 1} + {if $COUNTTRANSACTIONS - $smarty.request.start|default:"0" - $LIMIT > 0} + + {else} + + {/if} +{/if} +
TX Type{html_options name="filter[type]" options=$TRANSACTIONTYPES selected=$smarty.request.filter.type|default:""}
TX Status{html_options name="filter[status]" options=$TXSTATUS selected=$smarty.request.filter.status|default:""}
+
+{include file="global/block_footer.tpl"} + +{include file="global/block_header.tpl" ALIGN="right" BLOCK_STYLE="width: 75%" BLOCK_HEADER="Transaction History"} +
- {include file="global/pagination.tpl"} - +
+ + -{assign var=has_confirmed value=false} {section transaction $TRANSACTIONS} - {if ( - ( ( $TRANSACTIONS[transaction].type == 'Credit' or $TRANSACTIONS[transaction].type == 'Bonus' or $TRANSACTIONS[transaction].type == 'Donation' or $TRANSACTIONS[transaction].type == 'Fee' ) and $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations ) - or $TRANSACTIONS[transaction].type == 'Credit_PPS' or $TRANSACTIONS[transaction].type == 'Fee_PPS' or $TRANSACTIONS[transaction].type == 'Donation_PPS' - or $TRANSACTIONS[transaction].type == 'Debit_AP' or $TRANSACTIONS[transaction].type == 'Debit_MP' or $TRANSACTIONS[transaction].type == 'TXFee' - )} - {assign var=has_credits value=true} + + - + - {/if} {/section} -{if !$has_confirmed} - -{/if}
TX #Account Date TX TypeStatus Payment Address Block # Amount
{$TRANSACTIONS[transaction].id}{$TRANSACTIONS[transaction].username} {$TRANSACTIONS[transaction].timestamp} {$TRANSACTIONS[transaction].type} + {if $TRANSACTIONS[transaction].type == 'Credit_PPS' OR + $TRANSACTIONS[transaction].type == 'Fee_PPS' OR + $TRANSACTIONS[transaction].type == 'Donation_PPS' OR + $TRANSACTIONS[transaction].type == 'Debit_MP' OR + $TRANSACTIONS[transaction].type == 'Debit_AP' OR + $TRANSACTIONS[transaction].type == 'TXFee' OR + $TRANSACTIONS[transaction].confirmations >= $GLOBAL.confirmations + }Confirmed + {else if $TRANSACTIONS[transaction].confirmations == -1}Orphaned + {else}Unconfirmed{/if} + ({$TRANSACTIONS[transaction].confirmations|default:"n/a"}) + {$TRANSACTIONS[transaction].coin_address}{if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if}{if $TRANSACTIONS[transaction].height == 0}n/a{else}{if $GLOBAL.blockexplorer}{$TRANSACTIONS[transaction].height}{else}{$TRANSACTIONS[transaction].height}{/if}{/if} {$TRANSACTIONS[transaction].amount|number_format:"8"}
No data

@@ -44,97 +90,4 @@

-
-
- {include file="global/pagination.tpl" ID=2} - - - - - - - - - - - - -{assign var=has_unconfirmed value=false} -{section transaction $TRANSACTIONS} - {if - (($TRANSACTIONS[transaction].type == 'Credit' or $TRANSACTIONS[transaction].type == 'Bonus' or $TRANSACTIONS[transaction].type == 'Donation' or $TRANSACTIONS[transaction].type == 'Fee') and $TRANSACTIONS[transaction].confirmations < $GLOBAL.confirmations and $TRANSACTIONS[transaction].confirmations >= 0) - } - {assign var=has_unconfirmed value=true} - - - - - - - - - {if $TRANSACTIONS[transaction].type == 'Credit' or $TRANSACTIONS[transaction].type == 'Bonus'} - {assign var="credits" value="`$credits|default:"0"+$TRANSACTIONS[transaction].amount`"} - {else} - {assign var="debits" value="`$debits|default:"0"+$TRANSACTIONS[transaction].amount`"} - {/if} - {/if} -{/section} -{if !$has_unconfirmed} - -{/if} - - - - - -
TX #DateTX TypePayment AddressBlock #Amount
{$TRANSACTIONS[transaction].id}{$TRANSACTIONS[transaction].timestamp}{$TRANSACTIONS[transaction].type}{$TRANSACTIONS[transaction].coin_address}{if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if}{$TRANSACTIONS[transaction].amount|number_format:"8"}
No data
Unconfirmed Totals:{($credits|default - $debits|default)|number_format:"8"}
-

Listed are your estimated rewards and donations/fees for all blocks awaiting {$GLOBAL.confirmations} confirmations.

-
-
-
-
- {include file="global/pagination.tpl"} - - - - - - - - - - - - -{assign var=has_orphaned value=false} -{section transaction $TRANSACTIONS} - {if ($TRANSACTIONS[transaction].type == 'Credit' or $TRANSACTIONS[transaction].type == 'Fee' or $TRANSACTIONS[transaction].type == 'Donation' or $TRANSACTIONS[transaction].type == 'Bonus') and $TRANSACTIONS[transaction].confirmations == -1} - - - - - - - - - {if $TRANSACTIONS[transaction].type == 'Credit' or $TRANSACTIONS[transaction].type == 'Bonus'} - {assign var="orphan_credits" value="`$orphan_credits|default:"0"+$TRANSACTIONS[transaction].amount`"} - {else} - {assign var="orphan_debits" value="`$orphan_debits|default:"0"+$TRANSACTIONS[transaction].amount`"} - {/if} - {/if} -{/section} -{if !$has_orphaned} - -{/if} - - - - - -
TX #DateTX TypePayment AddressBlock #Amount
{$TRANSACTIONS[transaction].id}{$TRANSACTIONS[transaction].timestamp}{$TRANSACTIONS[transaction].type}{$TRANSACTIONS[transaction].coin_address}{if $TRANSACTIONS[transaction].height == 0}n/a{else}{$TRANSACTIONS[transaction].height}{/if}{$TRANSACTIONS[transaction].amount|number_format:"8"}
No data
Orphaned Totals:{($orphan_credits|default - $orphan_debits|default)|number_format:"8"}
-

Listed are your orphaned transactions for blocks not part of the main blockchain.

-
-
{include file="global/block_footer.tpl"} diff --git a/public/templates/mmcFE/admin/transactions/default.tpl b/public/templates/mmcFE/admin/transactions/default.tpl index f23b265a..cbbc7e86 100644 --- a/public/templates/mmcFE/admin/transactions/default.tpl +++ b/public/templates/mmcFE/admin/transactions/default.tpl @@ -1,4 +1,7 @@ {include file="global/block_header.tpl" ALIGN="left" BLOCK_STYLE="width: 23%" BLOCK_HEADER="Transaction Filter"} +
+ + @@ -21,9 +24,6 @@ {/if} - - - @@ -43,9 +43,9 @@ -
TX Type {html_options name="filter[type]" options=$TRANSACTIONTYPES selected=$smarty.request.filter.type|default:""}
+ {include file="global/block_footer.tpl"} {include file="global/block_header.tpl" ALIGN="right" BLOCK_STYLE="width: 75%" BLOCK_HEADER="Transaction History"}