From a86a1be6a7b7faa97138d1d30dc7f2bdc2837007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 28 May 2023 03:45:36 +0200 Subject: [PATCH] RVRNEXT-11 add event for transactions --- public/static/js/communities/transaction.js | 30 +++++++++++++++++++++ src/Controller/TransactionController.php | 20 +++++++++++--- src/PersistentData/Model/Transaction.php | 27 ++++++++++++++++++- views/communities/transaction_edit.php | 19 ++++++++++++- views/communities/transactions.php | 3 +++ 5 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 public/static/js/communities/transaction.js diff --git a/public/static/js/communities/transaction.js b/public/static/js/communities/transaction.js new file mode 100644 index 0000000..0abaf6c --- /dev/null +++ b/public/static/js/communities/transaction.js @@ -0,0 +1,30 @@ +(function () { + const element = document.getElementById('transactionForm').elements['event_id']; + const select = new TomSelect(element, { + valueField: 'value', + labelField: 'label', + searchField: 'label', + loadThrottle: 300, + load: function (query, callback) { + var self = this; + RVR.httpRequest('GET', searchEventUrl.replace('QUERY', encodeURIComponent(query)), function () { + self.clearOptions(); + callback(this.response.results); + }); + }, + }); + + select.on('change', function (value) { + this.clearOptions(); + }); + + select.on('blur', function (value) { + this.clearOptions(); + }); + + select.on('type', function (value) { + if (value === '') { + this.clearOptions(); + } + }); +})(); diff --git a/src/Controller/TransactionController.php b/src/Controller/TransactionController.php index aafbea0..fb5311b 100644 --- a/src/Controller/TransactionController.php +++ b/src/Controller/TransactionController.php @@ -11,6 +11,7 @@ use RVR\Repository\CommunityMemberRepository; use RVR\Repository\CommunityRepository; use RVR\Repository\CurrencyRepository; use RVR\Repository\TransactionRepository; +use RVR\Repository\EventRepository; use SokoWeb\Interfaces\Authentication\IAuthenticationRequired; use SokoWeb\Interfaces\Authorization\ISecured; use SokoWeb\Interfaces\Response\IContent; @@ -27,9 +28,11 @@ class TransactionController implements IAuthenticationRequired, ISecured private TransactionRepository $transactionRepository; - private Community $community; + private EventRepository $eventRepository; - private CommunityMember $ownCommunityMember; + private ?Community $community; + + private ?CommunityMember $ownCommunityMember; public function __construct() { @@ -37,6 +40,7 @@ class TransactionController implements IAuthenticationRequired, ISecured $this->communityMemberRepository = new CommunityMemberRepository(); $this->currencyRepository = new CurrencyRepository(); $this->transactionRepository = new TransactionRepository(); + $this->eventRepository = new EventRepository(); } public function isAuthenticationRequired(): bool @@ -78,7 +82,7 @@ class TransactionController implements IAuthenticationRequired, ISecured $currentPage * $itemsPerPage, $itemsPerPage, true, - ['currency', 'payer_user', 'payee_user'] + ['event', 'currency', 'payer_user', 'payee_user'] ); return new HtmlContent('communities/transactions', [ @@ -99,13 +103,22 @@ class TransactionController implements IAuthenticationRequired, ISecured if ($transaction === null) { return null; } + Container::$persistentDataManager->loadRelationsFromDb($transaction, false, ['event']); + $event = $transaction->getEvent(); } else { $transaction = null; + $eventSlug = Container::$request->query('event'); + if ($eventSlug) { + $event = $this->eventRepository->getBySlug($eventSlug); + } else { + $event = null; + } } return new HtmlContent('communities/transaction_edit', [ 'community' => $this->community, 'transaction' => $transaction, + 'event' => $event, 'members' => $this->getMembers($this->community), 'currencies' => $this->getCurrencies($this->community) ]); @@ -121,6 +134,7 @@ class TransactionController implements IAuthenticationRequired, ISecured $transaction->setCommunity($this->community); } + $transaction->setEventId(Container::$request->post('event_id') ?: null); $transaction->setCurrencyId(Container::$request->post('currency_id')); $transaction->setPayerUserId(Container::$request->post('payer_user_id')); $transaction->setPayeeUserId(Container::$request->post('payee_user_id') ?: null); diff --git a/src/PersistentData/Model/Transaction.php b/src/PersistentData/Model/Transaction.php index a4a9b48..fe73d49 100644 --- a/src/PersistentData/Model/Transaction.php +++ b/src/PersistentData/Model/Transaction.php @@ -7,10 +7,11 @@ class Transaction extends Model { protected static string $table = 'transactions'; - protected static array $fields = ['community_id', 'currency_id', 'payer_user_id', 'payee_user_id', 'description', 'sum', 'time']; + protected static array $fields = ['community_id', 'event_id', 'currency_id', 'payer_user_id', 'payee_user_id', 'description', 'sum', 'time']; protected static array $relations = [ 'community' => Community::class, + 'event' => Event::class, 'currency' => Currency::class, 'payer_user' => User::class, 'payee_user' => User::class @@ -20,6 +21,10 @@ class Transaction extends Model private int $communityId; + private ?Event $event = null; + + private ?int $eventId = null; + private ?Currency $currency = null; private int $currencyId; @@ -48,6 +53,16 @@ class Transaction extends Model $this->communityId = $communityId; } + public function setEvent(?Event $event): void + { + $this->event = $event; + } + + public function setEventId(?int $eventId): void + { + $this->eventId = $eventId; + } + public function setCurrency(Currency $currency): void { $this->currency = $currency; @@ -108,6 +123,16 @@ class Transaction extends Model return $this->communityId; } + public function getEvent(): ?Event + { + return $this->event; + } + + public function getEventId(): ?int + { + return $this->eventId; + } + public function getCurrency(): ?Currency { return $this->currency; diff --git a/views/communities/transaction_edit.php b/views/communities/transaction_edit.php index 76c3392..b5eb7d7 100644 --- a/views/communities/transaction_edit.php +++ b/views/communities/transaction_edit.php @@ -1,3 +1,7 @@ +@css(/static/node_modules/tom-select/dist/css/tom-select.min.css) +@js(/static/node_modules/tom-select/dist/js/tom-select.base.min.js) +@js(js/communities/transaction.js) + @extends(templates/layout_normal) @section(main) @@ -17,7 +21,14 @@ Container::$routeCollection->getRoute('community.transactions.new-action')->generateLink(['communitySlug' => $community->getSlug()]); ?>
-

Payer

+

Event

+ +

Payer