From 7d63de44035ea97367e09f3db3f2e4acd87a14ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 17:36:03 +0200 Subject: [PATCH 01/11] RVRNEXT-5 add new endpoints for currency handling --- web.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/web.php b/web.php index 991180b..15addbe 100644 --- a/web.php +++ b/web.php @@ -66,6 +66,10 @@ Container::$routeCollection->group('communities', function (RouteCollection $rou $routeCollection->post('community-members-new', 'newMember', [CommunityController::class, 'newMember']); $routeCollection->post('community-members-edit', 'editMember', [CommunityController::class, 'editMember']); $routeCollection->post('community-members-delete', 'deleteMember', [CommunityController::class, 'deleteMember']); + $routeCollection->get('community-currencies', 'currencies', [CommunityController::class, 'getCurrenciesEdit']); + $routeCollection->post('community-currencies-new', 'newCurrency', [CommunityController::class, 'newCurrency']); + $routeCollection->post('community-currencies-edit', 'editCurrency', [CommunityController::class, 'editCurrency']); + $routeCollection->post('community-currencies-delete', 'deleteCurrency', [CommunityController::class, 'deleteCurrency']); }); }); From 0fc21cc4614914437ff0877c3d09b5bb15749511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 17:36:22 +0200 Subject: [PATCH 02/11] RVRNEXT-5 add migration for currencies --- .../migrations/structure/20230421_2234_currencies.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 database/migrations/structure/20230421_2234_currencies.sql diff --git a/database/migrations/structure/20230421_2234_currencies.sql b/database/migrations/structure/20230421_2234_currencies.sql new file mode 100644 index 0000000..70a0bb4 --- /dev/null +++ b/database/migrations/structure/20230421_2234_currencies.sql @@ -0,0 +1,10 @@ +CREATE TABLE `currencies` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `community_id` int(10) unsigned NOT NULL, + `code` varchar(3) NOT NULL, + `round_digits` tinyint(1) unsigned NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `unique_currency_for_community` (`community_id`, `code`), + KEY `community_id` (`community_id`), + CONSTRAINT `currencies_community_id` FOREIGN KEY (`community_id`) REFERENCES `communities` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; From 12065b052512be90f3a08b441e3d31ea0a769e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 17:38:04 +0200 Subject: [PATCH 03/11] RVRNEXT-5 add model and repository for currencies --- src/PersistentData/Model/Currency.php | 60 +++++++++++++++++++++++++++ src/Repository/CurrencyRepository.php | 32 ++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/PersistentData/Model/Currency.php create mode 100644 src/Repository/CurrencyRepository.php diff --git a/src/PersistentData/Model/Currency.php b/src/PersistentData/Model/Currency.php new file mode 100644 index 0000000..06cbbfd --- /dev/null +++ b/src/PersistentData/Model/Currency.php @@ -0,0 +1,60 @@ + Community::class]; + + private ?Community $community = null; + + private ?int $communityId = null; + + private string $code = ''; + + private int $roundDigits = 0; + + public function setCommunity(Community $community): void + { + $this->community = $community; + } + + public function setCommunityId(int $communityId): void + { + $this->communityId = $communityId; + } + + public function setCode(string $code): void + { + $this->code = $code; + } + + public function setRoundDigits(int $roundDigits): void + { + $this->roundDigits = $roundDigits; + } + + public function getCommunity(): ?Community + { + return $this->community; + } + + public function getCommunityId(): ?int + { + return $this->communityId; + } + + public function getCode(): string + { + return $this->code; + } + + public function getRoundDigits(): int + { + return $this->roundDigits; + } +} diff --git a/src/Repository/CurrencyRepository.php b/src/Repository/CurrencyRepository.php new file mode 100644 index 0000000..b34b089 --- /dev/null +++ b/src/Repository/CurrencyRepository.php @@ -0,0 +1,32 @@ +selectFromDbById($id, Currency::class); + } + + public function getAllByCommunity(Community $community, bool $useRelations = false): Generator + { + $select = new Select(Container::$dbConnection); + $select->where('community_id', '=', $community->getId()); + + yield from Container::$persistentDataManager->selectMultipleFromDb($select, Currency::class, $useRelations); + } + + public function getByCommunityAndCurrencyCode(Community $community, string $code): ?Currency + { + $select = new Select(Container::$dbConnection); + $select->where('community_id', '=', $community->getId()); + $select->where('code', '=', $code); + + return Container::$persistentDataManager->selectFromDb($select, Currency::class); + } +} From be0a15d02d26fc28067f81804bcb4cdb77d548e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 19:57:47 +0200 Subject: [PATCH 04/11] RVRNEXT-5 extend views with currency edit --- views/communities/community.php | 8 +++- views/communities/community_currencies.php | 44 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 views/communities/community_currencies.php diff --git a/views/communities/community.php b/views/communities/community.php index 6ce29dc..4630656 100644 --- a/views/communities/community.php +++ b/views/communities/community.php @@ -17,7 +17,13 @@

Currencies

Main currency: getCurrency() ?>

-

Further currencies:

+ 0): ?> +

Further currencies:

+ + +
+

Edit currencies

+

Upcoming events

diff --git a/views/communities/community_currencies.php b/views/communities/community_currencies.php new file mode 100644 index 0000000..fd308d1 --- /dev/null +++ b/views/communities/community_currencies.php @@ -0,0 +1,44 @@ +@extends(templates/layout_normal) + +@section(main) +

getName() ?> - Edit currencies

+
+ + + + + + + + + + + + + + + + + + + + +
CurrencyRound digits
+
+
+ +
+ + + +
+
+ +
+ + + +
+
+@endsection From 0f02c4b5be789934119eec3e2d53dd5e855020a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 19:58:33 +0200 Subject: [PATCH 05/11] RVRNEXT-5 update CommunityController with currency operations --- src/Controller/CommunityController.php | 90 +++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 2 deletions(-) diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index 101b5c0..baa182d 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -3,9 +3,11 @@ use DateTime; use RVR\PersistentData\Model\Community; use RVR\PersistentData\Model\CommunityMember; +use RVR\PersistentData\Model\Currency; use RVR\PersistentData\Model\User; use RVR\Repository\CommunityRepository; use RVR\Repository\CommunityMemberRepository; +use RVR\Repository\CurrencyRepository; use RVR\Repository\UserRepository; use SokoWeb\Interfaces\Authentication\IAuthenticationRequired; use SokoWeb\Interfaces\Response\IContent; @@ -20,11 +22,14 @@ class CommunityController implements IAuthenticationRequired private CommunityMemberRepository $communityMemberRepository; + private CurrencyRepository $currencyRepository; + public function __construct() { $this->userRepository = new UserRepository(); $this->communityRepository = new CommunityRepository(); $this->communityMemberRepository = new CommunityMemberRepository(); + $this->currencyRepository = new CurrencyRepository(); } public function isAuthenticationRequired(): bool @@ -38,10 +43,15 @@ class CommunityController implements IAuthenticationRequired return null; } + $currencyCodes = []; + foreach ($this->getCurrencies($community) as $currency) { + $currencyCodes[] = $currency->getCode(); + } + return new HtmlContent('communities/community', [ 'community' => $community, 'members' => $this->getMembers($community), - 'currencyNames' => [], + 'currencyCodes' => $currencyCodes, 'upcomingEvents' => [], 'editPermission' => $ownCommunityMember->getOwner() ]); @@ -84,7 +94,6 @@ class CommunityController implements IAuthenticationRequired return $members; } - public function newMember(): ?IContent { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { @@ -139,6 +148,83 @@ class CommunityController implements IAuthenticationRequired return new JsonContent(['success' => true]); } + public function getCurrenciesEdit(): ?IContent + { + if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { + return null; + } + + return new HtmlContent('communities/community_currencies', [ + 'community' => $community, + 'currencies' => $this->getCurrencies($community) + ]); + } + + private function getCurrencies(Community $community): array + { + $currencies = iterator_to_array($this->currencyRepository->getAllByCommunity($community)); + usort($currencies, function($a, $b) { + return strnatcmp($a->getCode(), $b->getCode()); + }); + return $currencies; + } + + public function newCurrency(): ?IContent + { + if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { + return null; + } + + $code = \Container::$request->post('code'); + $roundDigits = (int)\Container::$request->post('round_digits'); + if (strlen($code) === 0 || strlen($code) > 3 || $roundDigits < 0 || $roundDigits > 9) { + return new JsonContent([ + 'error' => ['errorText' => 'Please fill all required fields!'] + ]); + } + + $existingCurrency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, $code); + if ($existingCurrency !== null) { + return new JsonContent([ + 'error' => ['errorText' => 'A currency with the same code exists for this community.'] + ]); + } + + $currency = new Currency(); + $currency->setCommunity($community); + $currency->setCode($code); + $currency->setRoundDigits($roundDigits); + \Container::$persistentDataManager->saveToDb($currency); + + return new JsonContent(['success' => true]); + } + + public function editCurrency(): ?IContent + { + if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { + return null; + } + + $currency = $this->currencyRepository->getById(\Container::$request->query('currency_id')); + $currency->setCode(\Container::$request->post('code')); + $currency->setRoundDigits((int)\Container::$request->post('round_digits')); + \Container::$persistentDataManager->saveToDb($currency); + + return new JsonContent(['success' => true]); + } + + public function deleteCurrency(): ?IContent + { + if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { + return null; + } + + $currency = $this->currencyRepository->getById(\Container::$request->query('currency_id')); + \Container::$persistentDataManager->deleteFromDb($currency); + + return new JsonContent(['success' => true]); + } + public function saveCommunity(): ?IContent { $communityId = \Container::$request->query('communityId'); From 3a1054e1df75fd4e0dbf89852a7805da10d8a0ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 19:59:23 +0200 Subject: [PATCH 06/11] RVRNEXT-5 adjust table style --- public/static/css/rvr.css | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/public/static/css/rvr.css b/public/static/css/rvr.css index 9ea08df..bef5c87 100644 --- a/public/static/css/rvr.css +++ b/public/static/css/rvr.css @@ -459,6 +459,14 @@ table th, table td { vertical-align: middle; } +table th:not(:first-child), table td:not(:first-child) { + padding-left: 3px; +} + +table th:not(:last-child), table td:not(:last-child) { + padding-right: 3px; +} + .choices__inner { box-sizing: border-box; } From ac09342c9d6381e26b729a6ac4b2b1e81c728653 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 19:59:48 +0200 Subject: [PATCH 07/11] RVRNEXT-5 fix layouts for communities --- views/communities/community_edit.php | 2 +- views/communities/community_members.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/views/communities/community_edit.php b/views/communities/community_edit.php index dba2f97..7b03346 100644 --- a/views/communities/community_edit.php +++ b/views/communities/community_edit.php @@ -11,7 +11,7 @@
-

+

diff --git a/views/communities/community_members.php b/views/communities/community_members.php index fd35325..83d7ea8 100644 --- a/views/communities/community_members.php +++ b/views/communities/community_members.php @@ -29,18 +29,18 @@ + --> -
+
+ From 2ad637e55fcb67995740ac38df3234b34496b386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 20:00:29 +0200 Subject: [PATCH 08/11] RVRNEXT-5 make prepare-commit-msg accept fixup commits --- scripts/gitHooks/prepare-commit-msg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/gitHooks/prepare-commit-msg b/scripts/gitHooks/prepare-commit-msg index 6d109a7..716f326 100755 --- a/scripts/gitHooks/prepare-commit-msg +++ b/scripts/gitHooks/prepare-commit-msg @@ -7,7 +7,7 @@ if [[ "${BRANCH_NAME}" =~ $BRANCH_PATTERN ]]; then TICKET_ID=$(echo $BRANCH_NAME | sed -E "s@$BRANCH_PATTERN@\\2@") COMMIT_MESSAGE=$(head -n 1 $1) - COMMIT_MESSAGE_REGEX="^$TICKET_ID .*" + COMMIT_MESSAGE_REGEX="^(fixup! )?$TICKET_ID .*" if [[ ! "${COMMIT_MESSAGE}" =~ $COMMIT_MESSAGE_REGEX ]]; then sed -i.bak -e "1s/^/$TICKET_ID /" $1 From d24c3bb1cbea6b961635ea78a8577e85f83b6458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 20:14:46 +0200 Subject: [PATCH 09/11] RVRNEXT-5 move private methods to the end of CommunityController --- src/Controller/CommunityController.php | 36 +++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index baa182d..fb5eeca 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -85,15 +85,6 @@ class CommunityController implements IAuthenticationRequired ]); } - private function getMembers(Community $community): array - { - $members = iterator_to_array($this->communityMemberRepository->getAllByCommunity($community, true)); - usort($members, function($a, $b) { - return strnatcmp($a->getUser()->getDisplayName(), $b->getUser()->getDisplayName()); - }); - return $members; - } - public function newMember(): ?IContent { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { @@ -160,15 +151,6 @@ class CommunityController implements IAuthenticationRequired ]); } - private function getCurrencies(Community $community): array - { - $currencies = iterator_to_array($this->currencyRepository->getAllByCommunity($community)); - usort($currencies, function($a, $b) { - return strnatcmp($a->getCode(), $b->getCode()); - }); - return $currencies; - } - public function newCurrency(): ?IContent { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { @@ -269,6 +251,24 @@ class CommunityController implements IAuthenticationRequired ]); } + private function getMembers(Community $community): array + { + $members = iterator_to_array($this->communityMemberRepository->getAllByCommunity($community, true)); + usort($members, function($a, $b) { + return strnatcmp($a->getUser()->getDisplayName(), $b->getUser()->getDisplayName()); + }); + return $members; + } + + private function getCurrencies(Community $community): array + { + $currencies = iterator_to_array($this->currencyRepository->getAllByCommunity($community)); + usort($currencies, function($a, $b) { + return strnatcmp($a->getCode(), $b->getCode()); + }); + return $currencies; + } + private function checkPermission( int $communityId, bool $needToBeOwner, From 9a6e95673eb48e424d228368612d6faee8509575 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 20:19:38 +0200 Subject: [PATCH 10/11] RVRNEXT-5 add formError for multi for layouts --- views/communities/community_currencies.php | 1 + views/communities/community_members.php | 1 + 2 files changed, 2 insertions(+) diff --git a/views/communities/community_currencies.php b/views/communities/community_currencies.php index fd308d1..5fe60ca 100644 --- a/views/communities/community_currencies.php +++ b/views/communities/community_currencies.php @@ -40,5 +40,6 @@ +

@endsection diff --git a/views/communities/community_members.php b/views/communities/community_members.php index 83d7ea8..431f489 100644 --- a/views/communities/community_members.php +++ b/views/communities/community_members.php @@ -47,6 +47,7 @@ +

@endsection From 91d064947e497c7a1221c4469097435647380904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 23 Apr 2023 20:20:03 +0200 Subject: [PATCH 11/11] RVRNEXT-5 formError should be searched in the whole document --- public/static/js/rvr.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/static/js/rvr.js b/public/static/js/rvr.js index 45096bb..ccf30b6 100644 --- a/public/static/js/rvr.js +++ b/public/static/js/rvr.js @@ -49,7 +49,7 @@ var RVR = { document.getElementById('loading').style.visibility = 'visible'; var formData = new FormData(form); - var formError = form.getElementsByClassName('formError')[0]; + var formError = document.getElementsByClassName('formError')[0]; var pageLeaveOnSuccess = form.dataset.redirectOnSuccess || form.dataset.reloadOnSuccess; RVR.httpRequest('POST', form.action, function () {