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] 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 @@ ?>
- + + + +