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/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`); diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index a936674..f14225e 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), @@ -73,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)) { @@ -185,6 +244,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 +260,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; } @@ -261,51 +324,9 @@ class CommunityController implements IAuthenticationRequired return new JsonContent(['success' => true]); } - public function saveCommunity(): ?IContent - { - $name = \Container::$request->post('name'); - $currency = \Container::$request->post('currency'); - if (strlen($name) === 0 || strlen($currency) === 0 || strlen($currency) > 3) { - 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 { - $community = new Community(); - $community->setCreatedDate(new DateTime()); - } - - $community->setName($name); - $community->setCurrency($currency); - \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); - } - - 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)); + $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/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; 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/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 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 @@ ?>
- + + + +