From 8687f9b9c5ff748958cef7e9da019b3f76cf3421 Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Mon, 16 Nov 2020 11:11:40 +0000 Subject: [PATCH 1/4] add State User Transactions for better transaction order and paging --- src/Controller/StateUserTransactionsController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Controller/StateUserTransactionsController.php b/src/Controller/StateUserTransactionsController.php index 8c252218c..e9008179e 100644 --- a/src/Controller/StateUserTransactionsController.php +++ b/src/Controller/StateUserTransactionsController.php @@ -20,6 +20,7 @@ class StateUserTransactionsController extends AppController $this->Auth->allow(['ajaxListTransactions']); //$this->loadComponent('JsonRequestClient'); } + /** * Index method * From 9e871d6d4b105a24e6b8a9da1a51ce38db0bc1ce Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Wed, 18 Nov 2020 18:37:00 +0000 Subject: [PATCH 2/4] add function to fill state_user_transactions, work on client-api --- .../transaction_group_addaddress.sql | 7 +- src/Controller/StateBalancesController.php | 96 ++++++++++++++- .../Entity/TransactionGroupAddaddres.php | 3 +- .../Table/TransactionGroupAddaddressTable.php | 5 + src/Model/Transactions/Record.php | 112 ++++++++++++------ 5 files changed, 179 insertions(+), 44 deletions(-) diff --git a/skeema/gradido_community/transaction_group_addaddress.sql b/skeema/gradido_community/transaction_group_addaddress.sql index b9ca95651..cb21ed99f 100644 --- a/skeema/gradido_community/transaction_group_addaddress.sql +++ b/skeema/gradido_community/transaction_group_addaddress.sql @@ -1,8 +1,9 @@ CREATE TABLE `transaction_group_addaddress` ( - `id` int(10) unsigned NOT NULL AUTO_INCREMENT, - `transaction_id` int(10) unsigned NOT NULL, - `address_type_id` int(10) unsigned NOT NULL, + `id` int unsigned NOT NULL AUTO_INCREMENT, + `transaction_id` int unsigned NOT NULL, + `address_type_id` int unsigned NOT NULL, `remove_from_group` BOOLEAN DEFAULT FALSE, `public_key` binary(32) NOT NULL, + `state_user_id` int unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/src/Controller/StateBalancesController.php b/src/Controller/StateBalancesController.php index 581869818..9a2641fdc 100644 --- a/src/Controller/StateBalancesController.php +++ b/src/Controller/StateBalancesController.php @@ -386,8 +386,100 @@ class StateBalancesController extends AppController public function ajaxListTransactions($page = 0, $count = 20) { - // TODO: add efficient paging with additional table: state_user_transactions - return $this->returnJson(['state' => 'success', 'transactions' => [], 'transactionExecutingCount' => 0, 'count' => 0]); + $session = $this->getRequest()->getSession(); + $user = $session->read('StateUser'); + if(!$user) { + return $this->returnJson(['state' => 'error', 'msg' => 'user not found', 'details' => 'exist a valid session cookie?']); + } + $stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions'); + $paged_state_user_transactions = $stateUserTransactionsTable + ->find('all') + ->where(['state_user_id' => $user['id'], 'transaction_type_id IN' => [1,2]]) + ->limit($count) + ->page($page) + ->order(['transaction_id']) + ; + $creationTransaction_ids = []; + $transferTransaction_ids = []; + $allTransaction_ids = []; + foreach($paged_state_user_transactions as $state_user_transaction) { + $allTransaction_ids[] = $state_user_transaction->transaction_id; + switch($state_user_transaction->transaction_type_id) { + case 1: $creationTransaction_ids[] = $state_user_transaction->transaction_id; break; + case 2: $transferTransaction_ids[] = $state_user_transaction->transaction_id; break; + } + } + $transactionsTable = TableRegistry::getTableLocator()->get('Transactions'); + $transactionCreationsTable = TableRegistry::getTableLocator()->get('TransactionCreations'); + $transactionSendCoinsTable = TableRegistry::getTableLocator()->get('TransactionSendCoins'); + $transactionEntries = $transactionsTable->find('all')->where(['id IN' => $allTransaction_ids])->order(['id'])->toArray(); + $transactionCreations = $transactionCreationsTable->find('all')->where(['transaction_id IN' => $creationTransaction_ids]); + $transactionTransfers = $transactionSendCoinsTable->find('all')->where(['transaction_id IN' => $transferTransaction_ids]); + //var_dump($transactions->all()); + + $transactions = []; + foreach ($transactionCreations as $creation) { + //var_dump($creation); + $transaction_entries_index = array_search($creation->transaction_id, $allTransaction_ids); + if(FALSE == $transaction_entries_index) { + return $this->returnJson(['state' => 'error', 'msg' => 'code error', 'details' => 'transaction_entries_index is FALSE, shouldn\'t occure']); + } + $transaction = $transactionEntries[$transaction_entries_index]; + array_push($transactions, [ + 'name' => 'Gradido Akademie', + 'type' => 'creation', + 'transaction_id' => $creation->transaction_id, + 'date' => $transaction->received, + 'balance' => $creation->amount, + 'memo' => $transaction->memo + ]); + } + + foreach($transactionTransfers as $transfer) + { + $transaction_entries_index = array_search($transfer->transaction_id, $allTransaction_ids); + if(FALSE == $transaction_entries_index) { + return $this->returnJson(['state' => 'error', 'msg' => 'code error', 'details' => 'transaction_entries_index is FALSE, shouldn\'t occure']); + } + $transaction = $transactionEntries[$transaction_entries_index]; + $type = ''; + $otherUser = null; + $other_user_public = ''; + /* + if ($sendCoins->state_user_id == $user['id']) { + $type = 'send'; + + if(isset($involvedUserIndices[$sendCoins->receiver_user_id])) { + $otherUser = $involvedUserIndices[$sendCoins->receiver_user_id]; + } + $other_user_public = bin2hex(stream_get_contents($sendCoins->receiver_public_key)); + } else if ($sendCoins->receiver_user_id == $user['id']) { + $type = 'receive'; + if(isset($involvedUserIndices[$sendCoins->state_user_id])) { + $otherUser = $involvedUserIndices[$sendCoins->state_user_id]; + } + if($sendCoins->sender_public_key) { + $other_user_public = bin2hex(stream_get_contents($sendCoins->sender_public_key)); + } + } + if(null == $otherUser) { + $otherUser = $this->StateBalances->StateUsers->newEntity(); + } + array_push($transactions, [ + 'name' => $otherUser->first_name . ' ' . $otherUser->last_name, + 'email' => $otherUser->email, + 'type' => $type, + 'transaction_id' => $sendCoins->transaction_id, + 'date' => $sendCoins->transaction->received, + 'balance' => $sendCoins->amount, + 'memo' => $sendCoins->transaction->memo, + 'pubkey' => $other_user_public + ]); + * */ + + } + + return $this->returnJson(['state' => 'success', 'transactions' => $transactions, 'transactionExecutingCount' => 0, 'count' => 0]); } public function ajaxGdtOverview() diff --git a/src/Model/Entity/TransactionGroupAddaddres.php b/src/Model/Entity/TransactionGroupAddaddres.php index 0559a6302..30819209f 100644 --- a/src/Model/Entity/TransactionGroupAddaddres.php +++ b/src/Model/Entity/TransactionGroupAddaddres.php @@ -31,6 +31,7 @@ class TransactionGroupAddaddres extends Entity 'remove_from_group' => true, 'public_key' => true, 'transaction' => true, - 'address_type' => true + 'address_type' => true, + 'state_user_id' => true ]; } diff --git a/src/Model/Table/TransactionGroupAddaddressTable.php b/src/Model/Table/TransactionGroupAddaddressTable.php index 3fd03f32a..cb9912083 100644 --- a/src/Model/Table/TransactionGroupAddaddressTable.php +++ b/src/Model/Table/TransactionGroupAddaddressTable.php @@ -45,6 +45,10 @@ class TransactionGroupAddaddressTable extends Table 'foreignKey' => 'address_type_id', 'joinType' => 'INNER' ]); + $this->belongsTo('StateUsers', [ + 'foreignKey' => 'state_user_id', + 'joinType' => 'INNER' + ]); } /** @@ -77,6 +81,7 @@ class TransactionGroupAddaddressTable extends Table { $rules->add($rules->existsIn(['transaction_id'], 'Transactions')); $rules->add($rules->existsIn(['address_type_id'], 'AddressTypes')); + $rules->add($rules->existsIn(['state_user_id'], 'StateUsers')); return $rules; } diff --git a/src/Model/Transactions/Record.php b/src/Model/Transactions/Record.php index e5e610c79..faec7c479 100644 --- a/src/Model/Transactions/Record.php +++ b/src/Model/Transactions/Record.php @@ -54,7 +54,55 @@ class Signature } -class ManageNodeGroupAdd + +class GradidoModifieUserBalance +{ + private $state_users = []; + + public function getUserId($userPublicKey) + { + $stateUsersTable = TableRegistry::getTableLocator()->get('StateUsers'); + $stateUser = $stateUsersTable->find('all')->where(['public_key' => hex2bin($userPublicKey)]); + if($stateUser->isEmpty()) { + return ['state' => 'error', 'msg' => 'couldn\'t find user via public key']; + } + $id = $stateUser->first()->id; + if($id && is_int($id) && (int)$id > 0 && !in_array((int)$id, $this->state_users)) { + array_push($this->state_users, (int)$id); + } + return $stateUser->first()->id; + } + + public function updateBalance($newBalance, $recordDate, $userId) + { + $stateBalancesTable = TableRegistry::getTableLocator()->get('StateBalances'); + $stateBalanceQuery = $stateBalancesTable->find('all')->where(['state_user_id' => $userId]); + $entity = null; + + if(!$stateBalanceQuery->isEmpty()) { + $entity = $stateBalanceQuery->first(); + if($entity->record_date != NULL && $entity->record_date > $recordDate) { + return false; + } + } else { + $entity = $stateBalancesTable->newEntity(); + $entity->state_user_id = $userId; + } + $entity->record_date = $recordDate; + $entity->amount = $newBalance; + if(!$stateBalancesTable->save($entity)) { + return ['state' => 'error', 'msg' => 'error saving state balance', 'details' => $entity->getErrors()]; + } + return true; + } + + public function getAllStateUsers() + { + return $this->state_users; + } +} + +class ManageNodeGroupAdd extends GradidoModifieUserBalance { /* "add_user": { @@ -100,11 +148,13 @@ class ManageNodeGroupAdd } $transactionGroupEntity->public_key = hex2bin($this->user_pubkey); + $transactionGroupEntity->state_user_id = $this->getUserId($this->user_pubkey); $transactionGroupEntity->remove_from_group = $this->remove_from_group; if(!$transactionGroupAddadressTable->save($transactionGroupEntity)) { return ['state' => 'error', 'msg' => 'error saving TransactionGroupAddaddress Entity', 'details' => $transactionGroupEntity->getErrors()]; } $userPubkeyBin = hex2bin($this->user_pubkey); + if($this->remove_from_group) { $stateGroup_query = $stateGroupAddresses->find('all')->where(['public_key' => hex2bin($this->user_pubkey)]); if(!$stateGroup_query->isEmpty()) { @@ -128,42 +178,6 @@ class ManageNodeGroupAdd } } -class GradidoModifieUserBalance -{ - - public function getUserId($userPublicKey) - { - $stateUsersTable = TableRegistry::getTableLocator()->get('StateUsers'); - $stateUser = $stateUsersTable->find('all')->where(['public_key' => hex2bin($userPublicKey)]); - if($stateUser->isEmpty()) { - return ['state' => 'error', 'msg' => 'couldn\'t find user via public key']; - } - return $stateUser->first()->id; - } - - public function updateBalance($newBalance, $recordDate, $userId) - { - $stateBalancesTable = TableRegistry::getTableLocator()->get('StateBalances'); - $stateBalanceQuery = $stateBalancesTable->find('all')->where(['state_user_id' => $userId]); - $entity = null; - - if(!$stateBalanceQuery->isEmpty()) { - $entity = $stateBalanceQuery->first(); - if($entity->record_date != NULL && $entity->record_date > $recordDate) { - return false; - } - } else { - $entity = $stateBalancesTable->newEntity(); - $entity->state_user_id = $userId; - } - $entity->record_date = $recordDate; - $entity->amount = $newBalance; - if(!$stateBalancesTable->save($entity)) { - return ['state' => 'error', 'msg' => 'error saving state balance', 'details' => $entity->getErrors()]; - } - return true; - } -} class GradidoCreation extends GradidoModifieUserBalance { @@ -180,6 +194,7 @@ class GradidoCreation extends GradidoModifieUserBalance private $targetDate; // seems currently not in node server implementet, use hedera date until it is implemented private $new_balance; + public function __construct($data) { $this->userPubkey = $data['user']; @@ -219,6 +234,8 @@ class GradidoCreation extends GradidoModifieUserBalance return true; } + + } class GradidoTransfer extends GradidoModifieUserBalance @@ -245,6 +262,7 @@ class GradidoTransfer extends GradidoModifieUserBalance private $receiver_pubkey; private $receiver_new_balance; + public function __construct($data) { $this->amount = $data['amount']; @@ -295,7 +313,7 @@ class GradidoTransfer extends GradidoModifieUserBalance } } if(is_int($receiver_id) && $receiver_id > 0) { - $transferEntity->receiver_user_id = $receiver_id; + $this->state_users[] = $receiver_id; $balance_result = $this->updateBalance($this->receiver_new_balance, $received, $receiver_id); if(is_array($balance_result)) { return $balance_result; @@ -308,6 +326,7 @@ class GradidoTransfer extends GradidoModifieUserBalance return true; } + } @@ -355,6 +374,7 @@ class Record { $transactionTypesTable = TableRegistry::getTableLocator()->get('TransactionTypes'); $transactionsTable = TableRegistry::getTableLocator()->get('Transactions'); + $stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions'); $transactionTypeName = $this->nodeTransactionTypeToDBTransactionType($this->transactionType); $transactionTypeResults = $transactionTypesTable->find('all')->where(['name' => $transactionTypeName]); @@ -404,6 +424,22 @@ class Record if($transaction_obj_result !== true) { return ['state' => 'error', 'msg' => 'error finalizing transaction object', 'details' => $transaction_obj_result]; } + $state_users = $this->transactionObj->getAllStateUsers(); + $sut_entities = []; + foreach($state_users as $state_user_id) { + $entity = $stateUserTransactionsTable->newEntity(); + $entity->state_user_id = $state_user_id; + $entity->transaction_id = $newTransaction->id; + $entity->transaction_type_id = $newTransaction->transaction_type_id; + $sut_entities[] = $entity; + } + $sut_results = $stateUserTransactionsTable->saveMany($sut_entities); + foreach($sut_results as $i => $result) { + if(false == $result) { + return ['state' => 'error', 'msg' => 'error saving state_user_transaction', 'details' => $sut_entities[$i]->getErrors()]; + } + } + return true; } From 433d3d5324eaef74143d41ee1a4090ed50f4dfbe Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Thu, 19 Nov 2020 10:18:07 +0000 Subject: [PATCH 3/4] add get state balance, finish list transactions --- src/Controller/StateBalancesController.php | 184 ++++++++++++++------- 1 file changed, 121 insertions(+), 63 deletions(-) diff --git a/src/Controller/StateBalancesController.php b/src/Controller/StateBalancesController.php index 9a2641fdc..74f598d19 100644 --- a/src/Controller/StateBalancesController.php +++ b/src/Controller/StateBalancesController.php @@ -21,7 +21,7 @@ class StateBalancesController extends AppController { parent::initialize(); //$this->Auth->allow(['add', 'edit']); - $this->Auth->allow(['overview', 'overviewGdt', 'ajaxGetBalance', 'ajaxListTransactions', 'ajaxGdtOverview']); + $this->Auth->allow(['overview', 'overviewGdt', 'ajaxListTransactions', 'ajaxGdtOverview', 'ajaxGetBalance']); $this->loadComponent('JsonRequestClient'); } /** @@ -208,6 +208,7 @@ class StateBalancesController extends AppController $this->set('timeUsed', microtime(true) - $startTime); $this->set('gdtSum', $gdtSum); } +<<<<<<< HEAD public function ajaxGetBalance($session_id = 0) { @@ -399,6 +400,11 @@ class StateBalancesController extends AppController ->page($page) ->order(['transaction_id']) ; + $all_user_transactions_count = $stateUserTransactionsTable + ->find('all') + ->where(['state_user_id' => $user['id'], 'transaction_type_id IN' => [1,2]]) + ->count() + ; $creationTransaction_ids = []; $transferTransaction_ids = []; $allTransaction_ids = []; @@ -412,74 +418,126 @@ class StateBalancesController extends AppController $transactionsTable = TableRegistry::getTableLocator()->get('Transactions'); $transactionCreationsTable = TableRegistry::getTableLocator()->get('TransactionCreations'); $transactionSendCoinsTable = TableRegistry::getTableLocator()->get('TransactionSendCoins'); - $transactionEntries = $transactionsTable->find('all')->where(['id IN' => $allTransaction_ids])->order(['id'])->toArray(); - $transactionCreations = $transactionCreationsTable->find('all')->where(['transaction_id IN' => $creationTransaction_ids]); - $transactionTransfers = $transactionSendCoinsTable->find('all')->where(['transaction_id IN' => $transferTransaction_ids]); + $stateUsersTable = TableRegistry::getTableLocator()->get('StateUsers'); + if(count($allTransaction_ids) > 0) { + $transactionEntries = $transactionsTable->find('all')->where(['id IN' => $allTransaction_ids])->order(['id'])->toArray(); + } + if(count($creationTransaction_ids) > 0) { + $transactionCreations = $transactionCreationsTable->find('all')->where(['transaction_id IN' => $creationTransaction_ids]); + } + if(count($transferTransaction_ids)) { + $transactionTransfers = $transactionSendCoinsTable->find('all')->where(['transaction_id IN' => $transferTransaction_ids]); + } //var_dump($transactions->all()); $transactions = []; - foreach ($transactionCreations as $creation) { - //var_dump($creation); - $transaction_entries_index = array_search($creation->transaction_id, $allTransaction_ids); - if(FALSE == $transaction_entries_index) { - return $this->returnJson(['state' => 'error', 'msg' => 'code error', 'details' => 'transaction_entries_index is FALSE, shouldn\'t occure']); - } - $transaction = $transactionEntries[$transaction_entries_index]; - array_push($transactions, [ - 'name' => 'Gradido Akademie', - 'type' => 'creation', - 'transaction_id' => $creation->transaction_id, - 'date' => $transaction->received, - 'balance' => $creation->amount, - 'memo' => $transaction->memo - ]); - } - - foreach($transactionTransfers as $transfer) - { - $transaction_entries_index = array_search($transfer->transaction_id, $allTransaction_ids); - if(FALSE == $transaction_entries_index) { - return $this->returnJson(['state' => 'error', 'msg' => 'code error', 'details' => 'transaction_entries_index is FALSE, shouldn\'t occure']); + // creations + if(isset($transactionCreations)) { + foreach ($transactionCreations as $creation) { + //var_dump($creation); + $transaction_entries_index = array_search($creation->transaction_id, $allTransaction_ids); + if(FALSE === $transaction_entries_index) { + return $this->returnJson(['state' => 'error', 'msg' => 'code error', 'details' => 'creation, transaction_entries_index is FALSE, shouldn\'t occure']); + } + $transaction = $transactionEntries[$transaction_entries_index]; + array_push($transactions, [ + 'name' => 'Gradido Akademie', + 'type' => 'creation', + 'transaction_id' => $creation->transaction_id, + 'date' => $transaction->received, + 'balance' => $creation->amount, + 'memo' => $transaction->memo + ]); } - $transaction = $transactionEntries[$transaction_entries_index]; - $type = ''; - $otherUser = null; - $other_user_public = ''; - /* - if ($sendCoins->state_user_id == $user['id']) { - $type = 'send'; - - if(isset($involvedUserIndices[$sendCoins->receiver_user_id])) { - $otherUser = $involvedUserIndices[$sendCoins->receiver_user_id]; - } - $other_user_public = bin2hex(stream_get_contents($sendCoins->receiver_public_key)); - } else if ($sendCoins->receiver_user_id == $user['id']) { - $type = 'receive'; - if(isset($involvedUserIndices[$sendCoins->state_user_id])) { - $otherUser = $involvedUserIndices[$sendCoins->state_user_id]; - } - if($sendCoins->sender_public_key) { - $other_user_public = bin2hex(stream_get_contents($sendCoins->sender_public_key)); - } - } - if(null == $otherUser) { - $otherUser = $this->StateBalances->StateUsers->newEntity(); - } - array_push($transactions, [ - 'name' => $otherUser->first_name . ' ' . $otherUser->last_name, - 'email' => $otherUser->email, - 'type' => $type, - 'transaction_id' => $sendCoins->transaction_id, - 'date' => $sendCoins->transaction->received, - 'balance' => $sendCoins->amount, - 'memo' => $sendCoins->transaction->memo, - 'pubkey' => $other_user_public - ]); - * */ - } - return $this->returnJson(['state' => 'success', 'transactions' => $transactions, 'transactionExecutingCount' => 0, 'count' => 0]); + // involved users + if(isset($transactionTransfers)) { + $involvedUserIds = []; + + foreach ($transactionTransfers as $transfer) { + //var_dump($sendCoins); + if ($transfer->state_user_id != $user['id']) { + array_push($involvedUserIds, intval($transfer->state_user_id)); + } elseif ($transfer->receiver_user_id != $user['id']) { + array_push($involvedUserIds, intval($transfer->receiver_user_id)); + } + } + + // exchange key with values and drop duplicates + $involvedUser_temp = array_flip($involvedUserIds); + // exchange back + $involvedUserIds = array_flip($involvedUser_temp); + + $involvedUser = $stateUsersTable->find('all', [ + 'contain' => false, + 'where' => ['id IN' => $involvedUserIds], + 'fields' => ['id', 'first_name', 'last_name', 'email'] + ]); + //var_dump($involvedUser->toArray()); + $involvedUserIndices = []; + foreach ($involvedUser as $involvedUser) { + $involvedUserIndices[$involvedUser->id] = $involvedUser; + } + + // transfers - send coins + foreach($transactionTransfers as $transfer) + { + $transaction_entries_index = array_search($transfer->transaction_id, $allTransaction_ids); + if(FALSE === $transaction_entries_index) { + + return $this->returnJson([ + 'state' => 'error', + 'msg' => 'code error', + 'details' => 'transfer, transaction_entries_index is FALSE, shouldn\'t occure', + 'data' => ['haystack' => $allTransaction_ids, 'needle' => $transfer->transaction_id] + ]); + } + $transaction = $transactionEntries[$transaction_entries_index]; + $type = ''; + $otherUser = null; + $other_user_public = ''; + + if ($transfer->state_user_id == $user['id']) { + $type = 'send'; + + if(isset($involvedUserIndices[$transfer->receiver_user_id])) { + $otherUser = $involvedUserIndices[$transfer->receiver_user_id]; + } + $other_user_public = bin2hex(stream_get_contents($transfer->receiver_public_key)); + } else if ($transfer->receiver_user_id == $user['id']) { + $type = 'receive'; + if(isset($involvedUserIndices[$transfer->state_user_id])) { + $otherUser = $involvedUserIndices[$transfer->state_user_id]; + } + if($transfer->sender_public_key) { + $other_user_public = bin2hex(stream_get_contents($transfer->sender_public_key)); + } + } + if(null == $otherUser) { + $otherUser = $stateUsersTable->newEntity(); + } + array_push($transactions, [ + 'name' => $otherUser->first_name . ' ' . $otherUser->last_name, + 'email' => $otherUser->email, + 'type' => $type, + 'transaction_id' => $transfer->transaction_id, + 'date' => $transaction->received, + 'balance' => $transfer->amount, + 'memo' => $transaction->memo, + 'pubkey' => $other_user_public + ]); + //*/ + + } + } + + return $this->returnJson([ + 'state' => 'success', + 'transactions' => $transactions, + 'transactionExecutingCount' => $session->read('Transaction.executing'), + 'count' => $all_user_transactions_count + ]); } public function ajaxGdtOverview() From 60449797e828d9beb8f26d6dc6843ec0282ff00e Mon Sep 17 00:00:00 2001 From: Dario Rekowski on RockPI Date: Mon, 23 Nov 2020 13:14:04 +0000 Subject: [PATCH 4/4] add tabs css and js --- src/Controller/StateBalancesController.php | 227 ++++++++------------- webroot/css/grd_styles.css | 40 ++++ webroot/js/tabs.js | 27 +++ websrc/src/less/21-tabs.less | 45 ++++ 4 files changed, 199 insertions(+), 140 deletions(-) create mode 100644 webroot/js/tabs.js create mode 100644 websrc/src/less/21-tabs.less diff --git a/src/Controller/StateBalancesController.php b/src/Controller/StateBalancesController.php index 74f598d19..31febe498 100644 --- a/src/Controller/StateBalancesController.php +++ b/src/Controller/StateBalancesController.php @@ -21,7 +21,7 @@ class StateBalancesController extends AppController { parent::initialize(); //$this->Auth->allow(['add', 'edit']); - $this->Auth->allow(['overview', 'overviewGdt', 'ajaxListTransactions', 'ajaxGdtOverview', 'ajaxGetBalance']); + $this->Auth->allow(['overview', 'overviewGdt', 'ajaxListTransactions', 'ajaxGdtOverview', 'ajaxGetBalance', 'ajaxGdtTransactions']); $this->loadComponent('JsonRequestClient'); } /** @@ -208,7 +208,6 @@ class StateBalancesController extends AppController $this->set('timeUsed', microtime(true) - $startTime); $this->set('gdtSum', $gdtSum); } -<<<<<<< HEAD public function ajaxGetBalance($session_id = 0) { @@ -387,148 +386,36 @@ class StateBalancesController extends AppController public function ajaxListTransactions($page = 0, $count = 20) { + + return $this->returnJson([ + 'state' => 'error', + 'msg' => 'moved', + 'details' => 'moved to state-user-transactions/ajaxListTransactions/' + ]); + } + + public function ajaxGdtOverview() + { + $gdtSum = 0; + $gdtCount = -1; $session = $this->getRequest()->getSession(); $user = $session->read('StateUser'); + if(!$user) { return $this->returnJson(['state' => 'error', 'msg' => 'user not found', 'details' => 'exist a valid session cookie?']); } - $stateUserTransactionsTable = TableRegistry::getTableLocator()->get('StateUserTransactions'); - $paged_state_user_transactions = $stateUserTransactionsTable - ->find('all') - ->where(['state_user_id' => $user['id'], 'transaction_type_id IN' => [1,2]]) - ->limit($count) - ->page($page) - ->order(['transaction_id']) - ; - $all_user_transactions_count = $stateUserTransactionsTable - ->find('all') - ->where(['state_user_id' => $user['id'], 'transaction_type_id IN' => [1,2]]) - ->count() - ; - $creationTransaction_ids = []; - $transferTransaction_ids = []; - $allTransaction_ids = []; - foreach($paged_state_user_transactions as $state_user_transaction) { - $allTransaction_ids[] = $state_user_transaction->transaction_id; - switch($state_user_transaction->transaction_type_id) { - case 1: $creationTransaction_ids[] = $state_user_transaction->transaction_id; break; - case 2: $transferTransaction_ids[] = $state_user_transaction->transaction_id; break; - } - } - $transactionsTable = TableRegistry::getTableLocator()->get('Transactions'); - $transactionCreationsTable = TableRegistry::getTableLocator()->get('TransactionCreations'); - $transactionSendCoinsTable = TableRegistry::getTableLocator()->get('TransactionSendCoins'); - $stateUsersTable = TableRegistry::getTableLocator()->get('StateUsers'); - if(count($allTransaction_ids) > 0) { - $transactionEntries = $transactionsTable->find('all')->where(['id IN' => $allTransaction_ids])->order(['id'])->toArray(); - } - if(count($creationTransaction_ids) > 0) { - $transactionCreations = $transactionCreationsTable->find('all')->where(['transaction_id IN' => $creationTransaction_ids]); - } - if(count($transferTransaction_ids)) { - $transactionTransfers = $transactionSendCoinsTable->find('all')->where(['transaction_id IN' => $transferTransaction_ids]); - } - //var_dump($transactions->all()); + $gdtEntries = $this->JsonRequestClient->sendRequestGDT(['email' => $user['email']], 'GdtEntries' . DS . 'sumPerEmailApi'); - $transactions = []; - // creations - if(isset($transactionCreations)) { - foreach ($transactionCreations as $creation) { - //var_dump($creation); - $transaction_entries_index = array_search($creation->transaction_id, $allTransaction_ids); - if(FALSE === $transaction_entries_index) { - return $this->returnJson(['state' => 'error', 'msg' => 'code error', 'details' => 'creation, transaction_entries_index is FALSE, shouldn\'t occure']); - } - $transaction = $transactionEntries[$transaction_entries_index]; - array_push($transactions, [ - 'name' => 'Gradido Akademie', - 'type' => 'creation', - 'transaction_id' => $creation->transaction_id, - 'date' => $transaction->received, - 'balance' => $creation->amount, - 'memo' => $transaction->memo - ]); + if('success' == $gdtEntries['state'] && 'success' == $gdtEntries['data']['state']) { + $gdtSum = intval($gdtEntries['data']['sum']); + if(isset($gdtEntries['data']['count'])) { + $gdtCount = intval($gdtEntries['data']['count']); } - } - - // involved users - if(isset($transactionTransfers)) { - $involvedUserIds = []; - - foreach ($transactionTransfers as $transfer) { - //var_dump($sendCoins); - if ($transfer->state_user_id != $user['id']) { - array_push($involvedUserIds, intval($transfer->state_user_id)); - } elseif ($transfer->receiver_user_id != $user['id']) { - array_push($involvedUserIds, intval($transfer->receiver_user_id)); - } - } - - // exchange key with values and drop duplicates - $involvedUser_temp = array_flip($involvedUserIds); - // exchange back - $involvedUserIds = array_flip($involvedUser_temp); - - $involvedUser = $stateUsersTable->find('all', [ - 'contain' => false, - 'where' => ['id IN' => $involvedUserIds], - 'fields' => ['id', 'first_name', 'last_name', 'email'] - ]); - //var_dump($involvedUser->toArray()); - $involvedUserIndices = []; - foreach ($involvedUser as $involvedUser) { - $involvedUserIndices[$involvedUser->id] = $involvedUser; - } - - // transfers - send coins - foreach($transactionTransfers as $transfer) - { - $transaction_entries_index = array_search($transfer->transaction_id, $allTransaction_ids); - if(FALSE === $transaction_entries_index) { - - return $this->returnJson([ - 'state' => 'error', - 'msg' => 'code error', - 'details' => 'transfer, transaction_entries_index is FALSE, shouldn\'t occure', - 'data' => ['haystack' => $allTransaction_ids, 'needle' => $transfer->transaction_id] - ]); - } - $transaction = $transactionEntries[$transaction_entries_index]; - $type = ''; - $otherUser = null; - $other_user_public = ''; - - if ($transfer->state_user_id == $user['id']) { - $type = 'send'; - - if(isset($involvedUserIndices[$transfer->receiver_user_id])) { - $otherUser = $involvedUserIndices[$transfer->receiver_user_id]; - } - $other_user_public = bin2hex(stream_get_contents($transfer->receiver_public_key)); - } else if ($transfer->receiver_user_id == $user['id']) { - $type = 'receive'; - if(isset($involvedUserIndices[$transfer->state_user_id])) { - $otherUser = $involvedUserIndices[$transfer->state_user_id]; - } - if($transfer->sender_public_key) { - $other_user_public = bin2hex(stream_get_contents($transfer->sender_public_key)); - } - } - if(null == $otherUser) { - $otherUser = $stateUsersTable->newEntity(); - } - array_push($transactions, [ - 'name' => $otherUser->first_name . ' ' . $otherUser->last_name, - 'email' => $otherUser->email, - 'type' => $type, - 'transaction_id' => $transfer->transaction_id, - 'date' => $transaction->received, - 'balance' => $transfer->amount, - 'memo' => $transaction->memo, - 'pubkey' => $other_user_public - ]); - //*/ - + } else { + if($user) { + $this->addAdminError('StateBalancesController', 'ajaxGdtOverview', $gdtEntries, $user['id']); + } else { + $this->addAdminError('StateBalancesController', 'ajaxGdtOverview', $gdtEntries, 0); } } @@ -590,9 +477,9 @@ class StateBalancesController extends AppController //echo "gdtSum: $gdtSum
"; $this->set('gdtSum', $gdtSum); $this->set('ownEntries', $ownEntries); - $this->set('gdtSumPerEmail', $requestResult['data']['gdtSumPerEmail']); - $this->set('moreEntrysAsShown', $requestResult['data']['moreEntrysAsShown']); - $this->set('user', $user); + $this->set('gdtSumPerEmail', $requestResult['data']['gdtSumPerEmail']); + $this->set('moreEntrysAsShown', $requestResult['data']['moreEntrysAsShown']); + $this->set('user', $user); if (isset($requestResult['data']['publishers'])) { $publishers = $requestResult['data']['publishers']; @@ -603,6 +490,66 @@ class StateBalancesController extends AppController $this->Flash->error(__('Fehler beim GDT Server, bitte abwarten oder den Admin benachrichtigen!')); } } + + public function ajaxGdtTransactions() + { + $startTime = microtime(true); + $session = $this->getRequest()->getSession(); + $user = $session->read('StateUser'); + if(!$user) { + return $this->returnJson(['state' => 'error', 'msg' => 'user not found', 'details' => 'exist a valid session cookie?']); + } + + $requestResult = $this->JsonRequestClient->sendRequestGDT(['email' => $user['email']], 'GdtEntries' . DS . 'listPerEmailApi'); + $connectEntries = []; + $publishers = []; + + //var_dump($requestResult); + if('success' === $requestResult['state'] && 'success' === $requestResult['data']['state']) { + + //var_dump(array_keys($requestResult['data'])); + $ownEntries = $requestResult['data']['ownEntries']; + //$gdtEntries = $requestResult['data']['entries']; + + $gdtSum = 0; + foreach ($ownEntries as $i => $gdtEntry) { + $gdtSum += $gdtEntry['gdt']; + //echo "index: $i
"; + //var_dump($gdtEntry); + } + if (isset($requestResult['data']['connectEntrys'])) { + $connectEntries = $requestResult['data']['connectEntrys']; + + foreach ($connectEntries as $entry) { + //if(!$count) var_dump($entry); + //$count++; + $gdtSum += $entry['connect']['gdt_entry']['gdt']; + } + } + + //echo "gdtSum: $gdtSum
"; + + if (isset($requestResult['data']['publishers'])) { + $publishers = $requestResult['data']['publishers']; + } + } else { + $this->addAdminError('StateBalancesController', 'ajaxGdtTransactions', $requestResult, $user['id']); + //$this->Flash->error(__('Fehler beim GDT Server, bitte abwarten oder den Admin benachrichtigen!')); + return $this->returnJson(['state' => 'error', 'msg' => 'error from gdt server', 'details' => $requestResult]); + } + + + return $this->returnJson([ + 'state' => 'success', + 'gdtSum' => $gdtSum, + 'ownEntries' => $ownEntries, + 'connectEntries' => $connectEntries, + 'publishers' => $publishers, + 'gdtSumPerEmail' => $requestResult['data']['gdtSumPerEmail'], + 'moreEntrysAsShown' => $requestResult['data']['moreEntrysAsShown'], + 'timeUsed' => microtime(true) - $startTime + ]); + } public function sortTransactions($a, $b) { diff --git a/webroot/css/grd_styles.css b/webroot/css/grd_styles.css index 08ecdc3a5..e745ea55e 100644 --- a/webroot/css/grd_styles.css +++ b/webroot/css/grd_styles.css @@ -1582,6 +1582,46 @@ To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. */ +/* + Created on : 23.11.2020, 14:04:28 + Author : einhornimmond +*/ +/* Style the tab */ +.tab { + overflow: hidden; + border: 1px solid #ccc; + background-color: #f1f1f1; +} +/* Style the buttons that are used to open the tab content */ +.tab button { + background-color: inherit; + float: left; + border: none; + outline: none; + cursor: pointer; + padding: 14px 16px; + transition: 0.3s; +} +/* Change background color of buttons on hover */ +.tab button:hover { + background-color: #ddd; +} +/* Create an active/current tablink class */ +.tab button.active { + background-color: #ccc; +} +/* Style the tab content */ +.tabcontent { + display: none; + padding: 6px 12px; + border: 1px solid #ccc; + border-top: none; +} +/* +To change this license header, choose License Headers in Project Properties. +To change this template file, choose Tools | Templates +and open the template in the editor. +*/ /* ============================================================ Screen styles for forms. diff --git a/webroot/js/tabs.js b/webroot/js/tabs.js new file mode 100644 index 000000000..2db9ab693 --- /dev/null +++ b/webroot/js/tabs.js @@ -0,0 +1,27 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + + +function openTab(evt, tabName) { + // Declare all variables + var i, tabcontent, tablinks; + + // Get all elements with class="tabcontent" and hide them + tabcontent = document.getElementsByClassName("tabcontent"); + for (i = 0; i < tabcontent.length; i++) { + tabcontent[i].style.display = "none"; + } + + // Get all elements with class="tablinks" and remove the class "active" + tablinks = document.getElementsByClassName("tablinks"); + for (i = 0; i < tablinks.length; i++) { + tablinks[i].className = tablinks[i].className.replace(" active", ""); + } + + // Show the current tab, and add an "active" class to the button that opened the tab + document.getElementById(tabName).style.display = "block"; + evt.currentTarget.className += " active"; +} \ No newline at end of file diff --git a/websrc/src/less/21-tabs.less b/websrc/src/less/21-tabs.less new file mode 100644 index 000000000..694dd1dab --- /dev/null +++ b/websrc/src/less/21-tabs.less @@ -0,0 +1,45 @@ +/* +To change this license header, choose License Headers in Project Properties. +To change this template file, choose Tools | Templates +and open the template in the editor. +*/ +/* + Created on : 23.11.2020, 14:04:28 + Author : einhornimmond +*/ + + /* Style the tab */ +.tab { + overflow: hidden; + border: 1px solid #ccc; + background-color: #f1f1f1; +} + +/* Style the buttons that are used to open the tab content */ +.tab button { + background-color: inherit; + float: left; + border: none; + outline: none; + cursor: pointer; + padding: 14px 16px; + transition: 0.3s; +} + +/* Change background color of buttons on hover */ +.tab button:hover { + background-color: #ddd; +} + +/* Create an active/current tablink class */ +.tab button.active { + background-color: #ccc; +} + +/* Style the tab content */ +.tabcontent { + display: none; + padding: 6px 12px; + border: 1px solid #ccc; + border-top: none; +} \ No newline at end of file