From 0b261b627220adb30d2c006558e9630c5e3a0143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Fri, 28 Apr 2023 20:58:19 +0200 Subject: [PATCH 1/5] RVRNEXT-5 fix audit logger for non-web access --- app.php | 6 +++--- src/Database/AuditLogger.php | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app.php b/app.php index aa69fef..ddeef90 100644 --- a/app.php +++ b/app.php @@ -17,9 +17,9 @@ class Container static SokoWeb\Interfaces\Database\IConnection $dbConnection; static SokoWeb\Interfaces\Database\IAuditLogger $auditLogger; static SokoWeb\Interfaces\PersistentData\IPersistentDataManager $persistentDataManager; - static SokoWeb\Interfaces\Routing\IRouteCollection $routeCollection; - static SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler; - static SokoWeb\Interfaces\Request\IRequest $request; + static ?SokoWeb\Interfaces\Routing\IRouteCollection $routeCollection = null; + static ?SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler = null; + static ?SokoWeb\Interfaces\Request\IRequest $request = null; } Container::$dbConnection = new SokoWeb\Database\Mysql\Connection($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']); diff --git a/src/Database/AuditLogger.php b/src/Database/AuditLogger.php index 2e32009..ee477c0 100644 --- a/src/Database/AuditLogger.php +++ b/src/Database/AuditLogger.php @@ -6,6 +6,9 @@ class AuditLogger extends AuditLoggerBase { protected function getModifierId() { + if (\Container::$request === null) { + return null; + } $user = \Container::$request->user(); if ($user === null) { return null; -- 2.45.2 From 157f530ad5085d8f77237371768f56813478f89c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Fri, 28 Apr 2023 20:59:14 +0200 Subject: [PATCH 2/5] RVRNEXT-5 add withRelations to CommunityMemberRepository getters --- src/Repository/CommunityMemberRepository.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Repository/CommunityMemberRepository.php b/src/Repository/CommunityMemberRepository.php index b15b8ff..d0a1e2f 100644 --- a/src/Repository/CommunityMemberRepository.php +++ b/src/Repository/CommunityMemberRepository.php @@ -13,20 +13,20 @@ class CommunityMemberRepository return \Container::$persistentDataManager->selectFromDbById($id, CommunityMember::class); } - public function getAllByCommunity(Community $community, bool $useRelations = false): Generator + public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator { $select = new Select(\Container::$dbConnection); $select->where('community_id', '=', $community->getId()); - yield from \Container::$persistentDataManager->selectMultipleFromDb($select, CommunityMember::class, $useRelations); + yield from \Container::$persistentDataManager->selectMultipleFromDb($select, CommunityMember::class, $useRelations, $withRelations); } - public function getAllByUser(User $user, bool $useRelations = false): Generator + public function getAllByUser(User $user, bool $useRelations = false, array $withRelations = []): Generator { $select = new Select(\Container::$dbConnection); $select->where('user_id', '=', $user->getId()); - yield from \Container::$persistentDataManager->selectMultipleFromDb($select, CommunityMember::class, $useRelations); + yield from \Container::$persistentDataManager->selectMultipleFromDb($select, CommunityMember::class, $useRelations, $withRelations); } public function getByCommunityAndUser(Community $community, User $user) : ?CommunityMember -- 2.45.2 From 20a850f011b1e3ebe9801051c4e1782a0dc60811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Fri, 28 Apr 2023 21:06:18 +0200 Subject: [PATCH 3/5] RVRNEXT-5 make community main currency a general currency --- src/Controller/CommunityController.php | 34 ++++++++++++++++++---- src/Controller/HomeController.php | 3 +- src/PersistentData/Model/Community.php | 28 +++++++++++++++++- views/communities/community.php | 25 +++++++++------- views/communities/community_currencies.php | 8 +++-- views/communities/community_edit.php | 5 +++- 6 files changed, 82 insertions(+), 21 deletions(-) diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index a936674..bf5419a 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -48,6 +48,8 @@ class CommunityController implements IAuthenticationRequired return null; } + \Container::$persistentDataManager->loadRelationsFromDb($community, false); + return new HtmlContent('communities/community', [ 'community' => $community, 'members' => $this->getMembers($community), @@ -185,6 +187,10 @@ class CommunityController implements IAuthenticationRequired } $currency = $this->currencyRepository->getById(\Container::$request->query('currency_id')); + if ($currency->getId() === $community->getMainCurrencyId()) { + return null; + } + \Container::$persistentDataManager->deleteFromDb($currency); return new JsonContent(['success' => true]); @@ -197,7 +203,7 @@ class CommunityController implements IAuthenticationRequired } $currency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, \Container::$request->query('code')); - if ($currency === null) { + if ($currency === null || $currency->getId() === $community->getMainCurrencyId()) { return null; } @@ -264,8 +270,7 @@ class CommunityController implements IAuthenticationRequired public function saveCommunity(): ?IContent { $name = \Container::$request->post('name'); - $currency = \Container::$request->post('currency'); - if (strlen($name) === 0 || strlen($currency) === 0 || strlen($currency) > 3) { + if (strlen($name) === 0) { return new JsonContent([ 'error' => ['errorText' => 'Please fill all required fields!'] ]); @@ -277,12 +282,19 @@ class CommunityController implements IAuthenticationRequired return null; } } else { + $mainCurrencyCode = \Container::$request->post('main_currency_code'); + $mainCurrencyRoundDigits = \Container::$request->post('main_currency_round_digits'); + if (strlen($mainCurrencyCode) === 0 || strlen($mainCurrencyCode) > 3 || $mainCurrencyRoundDigits < 0 || $mainCurrencyRoundDigits > 9) { + return new JsonContent([ + 'error' => ['errorText' => 'Please fill all required fields!'] + ]); + } + $community = new Community(); $community->setCreatedDate(new DateTime()); } $community->setName($name); - $community->setCurrency($currency); \Container::$persistentDataManager->saveToDb($community); if (!$communityId) { @@ -296,6 +308,15 @@ class CommunityController implements IAuthenticationRequired $communityMember->setUser($user); $communityMember->setOwner(true); \Container::$persistentDataManager->saveToDb($communityMember); + + $mainCurrency = new Currency(); + $mainCurrency->setCommunity($community); + $mainCurrency->setCode($mainCurrencyCode); + $mainCurrency->setRoundDigits($mainCurrencyRoundDigits); + \Container::$persistentDataManager->saveToDb($mainCurrency); + + $community->setMainCurrency($mainCurrency); + \Container::$persistentDataManager->saveToDb($community); } return new JsonContent([ @@ -305,7 +326,7 @@ class CommunityController implements IAuthenticationRequired private function getMembers(Community $community): array { - $members = iterator_to_array($this->communityMemberRepository->getAllByCommunity($community, true)); + $members = iterator_to_array($this->communityMemberRepository->getAllByCommunity($community, true, [User::class])); usort($members, function($a, $b) { return strnatcmp($a->getUser()->getDisplayName(), $b->getUser()->getDisplayName()); }); @@ -318,6 +339,9 @@ class CommunityController implements IAuthenticationRequired usort($currencies, function($a, $b) { return strnatcmp($a->getCode(), $b->getCode()); }); + usort($currencies, function($a, $b) use ($community) { + return (int)($b->getId() === $community->getMainCurrencyId()) - (int)($a->getId() === $community->getMainCurrencyId()); + }); return $currencies; } diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index f17ceff..2094852 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -1,5 +1,6 @@ user(); - $ownCommunityMembers = $this->communityMemberRepository->getAllByUser($user, true); + $ownCommunityMembers = $this->communityMemberRepository->getAllByUser($user, true, [Community::class]); $communities = []; foreach ($ownCommunityMembers as $ownCommunityMember) { $communities[] = $ownCommunityMember->getCommunity(); diff --git a/src/PersistentData/Model/Community.php b/src/PersistentData/Model/Community.php index 43b057f..b5b279f 100644 --- a/src/PersistentData/Model/Community.php +++ b/src/PersistentData/Model/Community.php @@ -7,12 +7,18 @@ class Community extends Model { protected static string $table = 'communities'; - protected static array $fields = ['name', 'currency', 'created']; + protected static array $fields = ['name', 'currency', 'main_currency_id', 'created']; + + protected static array $relations = ['main_currency' => Currency::class]; private string $name = ''; private string $currency = ''; + private ?Currency $mainCurrency = null; + + private ?int $mainCurrencyId = null; + private DateTime $created; public function setName(string $name): void @@ -25,6 +31,16 @@ class Community extends Model $this->currency = $currency; } + public function setMainCurrency(Currency $mainCurrency): void + { + $this->mainCurrency = $mainCurrency; + } + + public function setMainCurrencyId(int $mainCurrencyId): void + { + $this->mainCurrencyId = $mainCurrencyId; + } + public function setCreatedDate(DateTime $created): void { $this->created = $created; @@ -45,6 +61,16 @@ class Community extends Model return $this->currency; } + public function getMainCurrency(): ?Currency + { + return $this->mainCurrency; + } + + public function getMainCurrencyId(): ?int + { + return $this->mainCurrencyId; + } + public function getCreatedDate(): DateTime { return $this->created; diff --git a/views/communities/community.php b/views/communities/community.php index c703adc..826b724 100644 --- a/views/communities/community.php +++ b/views/communities/community.php @@ -16,14 +16,15 @@

Currencies

-

Main currency: getCurrency() ?>

- 0): ?> -

Further currencies: - + +

+ getId() === $community->getMainCurrencyId()): ?> + getCode() ?> + getCode() ?> - -

- + +

+

Edit currencies

@@ -40,19 +41,23 @@
+ getMainCurrency()->getCode(); + $mainCurrencyRoundDigits = $community->getMainCurrency()->getRoundDigits(); + ?>

Finances

- + - + - +
You owe0 getCurrency() ?>
You're owed0 getCurrency() ?>
Your balance0 getCurrency() ?>
diff --git a/views/communities/community_currencies.php b/views/communities/community_currencies.php index 9f51ec5..27b1d95 100644 --- a/views/communities/community_currencies.php +++ b/views/communities/community_currencies.php @@ -21,9 +21,11 @@ - - + + + getId() !== $community->getMainCurrencyId()): ?> + + diff --git a/views/communities/community_edit.php b/views/communities/community_edit.php index bd2b0e1..d7e08f7 100644 --- a/views/communities/community_edit.php +++ b/views/communities/community_edit.php @@ -16,7 +16,10 @@ ?>
- + + + +

-- 2.45.2 From e0ea18a6d055b31227406f801d85c1eae50447a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Fri, 28 Apr 2023 21:06:36 +0200 Subject: [PATCH 4/5] RVRNEXT-5 migration for main_currency_id --- .../20230428_1826_community_main_currency.php | 19 +++++++++++++++++++ .../20230428_1826_community_main_currency.sql | 4 ++++ 2 files changed, 23 insertions(+) create mode 100644 database/migrations/data/20230428_1826_community_main_currency.php create mode 100644 database/migrations/structure/20230428_1826_community_main_currency.sql diff --git a/database/migrations/data/20230428_1826_community_main_currency.php b/database/migrations/data/20230428_1826_community_main_currency.php new file mode 100644 index 0000000..ce679da --- /dev/null +++ b/database/migrations/data/20230428_1826_community_main_currency.php @@ -0,0 +1,19 @@ +selectMultipleFromDb($select, Community::class); + +foreach ($communities as $community) { + $mainCurrency = new Currency(); + $mainCurrency->setCommunity($community); + $mainCurrency->setCode($community->getCurrency()); + $mainCurrency->setRoundDigits(0); + Container::$persistentDataManager->saveToDb($mainCurrency); + + $community->setMainCurrency($mainCurrency); + Container::$persistentDataManager->saveToDb($community); +} diff --git a/database/migrations/structure/20230428_1826_community_main_currency.sql b/database/migrations/structure/20230428_1826_community_main_currency.sql new file mode 100644 index 0000000..a4b5044 --- /dev/null +++ b/database/migrations/structure/20230428_1826_community_main_currency.sql @@ -0,0 +1,4 @@ +ALTER TABLE `communities` +ADD `main_currency_id` int(10) unsigned DEFAULT NULL, +ADD KEY `main_currency_id` (`main_currency_id`), +ADD CONSTRAINT `communities_main_currency_id` FOREIGN KEY (`main_currency_id`) REFERENCES `currencies` (`id`); -- 2.45.2 From 49df206ddc9c0a578fb1050d464ebb71f4904829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Fri, 28 Apr 2023 21:20:08 +0200 Subject: [PATCH 5/5] RVRNEXT-5 move saveCommunity under getCommunityEdit --- src/Controller/CommunityController.php | 114 ++++++++++++------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index bf5419a..f14225e 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -75,6 +75,63 @@ class CommunityController implements IAuthenticationRequired ]); } + public function saveCommunity(): ?IContent + { + $name = \Container::$request->post('name'); + if (strlen($name) === 0) { + return new JsonContent([ + 'error' => ['errorText' => 'Please fill all required fields!'] + ]); + } + + $communityId = \Container::$request->query('communityId'); + if ($communityId){ + if (!$this->checkPermission($communityId, true, $community, $ownCommunityMember)) { + return null; + } + } else { + $mainCurrencyCode = \Container::$request->post('main_currency_code'); + $mainCurrencyRoundDigits = \Container::$request->post('main_currency_round_digits'); + if (strlen($mainCurrencyCode) === 0 || strlen($mainCurrencyCode) > 3 || $mainCurrencyRoundDigits < 0 || $mainCurrencyRoundDigits > 9) { + return new JsonContent([ + 'error' => ['errorText' => 'Please fill all required fields!'] + ]); + } + + $community = new Community(); + $community->setCreatedDate(new DateTime()); + } + + $community->setName($name); + \Container::$persistentDataManager->saveToDb($community); + + if (!$communityId) { + /** + * @var User $user + */ + $user = \Container::$request->user(); + + $communityMember = new CommunityMember(); + $communityMember->setCommunity($community); + $communityMember->setUser($user); + $communityMember->setOwner(true); + \Container::$persistentDataManager->saveToDb($communityMember); + + $mainCurrency = new Currency(); + $mainCurrency->setCommunity($community); + $mainCurrency->setCode($mainCurrencyCode); + $mainCurrency->setRoundDigits($mainCurrencyRoundDigits); + \Container::$persistentDataManager->saveToDb($mainCurrency); + + $community->setMainCurrency($mainCurrency); + \Container::$persistentDataManager->saveToDb($community); + } + + return new JsonContent([ + 'redirect' => ['target' => \Container::$routeCollection->getRoute('community')->generateLink(['communityId' => $community->getId()])] + ]); + } + public function getMembersEdit(): ?IContent { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { @@ -267,63 +324,6 @@ class CommunityController implements IAuthenticationRequired return new JsonContent(['success' => true]); } - public function saveCommunity(): ?IContent - { - $name = \Container::$request->post('name'); - if (strlen($name) === 0) { - return new JsonContent([ - 'error' => ['errorText' => 'Please fill all required fields!'] - ]); - } - - $communityId = \Container::$request->query('communityId'); - if ($communityId){ - if (!$this->checkPermission($communityId, true, $community, $ownCommunityMember)) { - return null; - } - } else { - $mainCurrencyCode = \Container::$request->post('main_currency_code'); - $mainCurrencyRoundDigits = \Container::$request->post('main_currency_round_digits'); - if (strlen($mainCurrencyCode) === 0 || strlen($mainCurrencyCode) > 3 || $mainCurrencyRoundDigits < 0 || $mainCurrencyRoundDigits > 9) { - return new JsonContent([ - 'error' => ['errorText' => 'Please fill all required fields!'] - ]); - } - - $community = new Community(); - $community->setCreatedDate(new DateTime()); - } - - $community->setName($name); - \Container::$persistentDataManager->saveToDb($community); - - if (!$communityId) { - /** - * @var User $user - */ - $user = \Container::$request->user(); - - $communityMember = new CommunityMember(); - $communityMember->setCommunity($community); - $communityMember->setUser($user); - $communityMember->setOwner(true); - \Container::$persistentDataManager->saveToDb($communityMember); - - $mainCurrency = new Currency(); - $mainCurrency->setCommunity($community); - $mainCurrency->setCode($mainCurrencyCode); - $mainCurrency->setRoundDigits($mainCurrencyRoundDigits); - \Container::$persistentDataManager->saveToDb($mainCurrency); - - $community->setMainCurrency($mainCurrency); - \Container::$persistentDataManager->saveToDb($community); - } - - return new JsonContent([ - 'redirect' => ['target' => \Container::$routeCollection->getRoute('community')->generateLink(['communityId' => $community->getId()])] - ]); - } - private function getMembers(Community $community): array { $members = iterator_to_array($this->communityMemberRepository->getAllByCommunity($community, true, [User::class])); -- 2.45.2