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 @@
Main currency: = $community->getCurrency() ?>
- 0): ?> -Further currencies: - + +
+ getId() === $community->getMainCurrencyId()): ?> + = $currency->getCode() ?> + = $currency->getCode() ?> - -
- + + +You owe | -0 = $community->getCurrency() ?> | += number_format(0, $mainCurrencyRoundDigits) ?> = $mainCurrencyCode ?> |
You're owed | -0 = $community->getCurrency() ?> | += number_format(0, $mainCurrencyRoundDigits) ?> = $mainCurrencyCode ?> |
Your balance | -0 = $community->getCurrency() ?> | += number_format(0, $mainCurrencyRoundDigits) ?> = $mainCurrencyCode ?> |