feature/RVRNEXT-5-handling-currencies #31

Merged
bence merged 11 commits from feature/RVRNEXT-5-handling-currencies into master 2023-04-23 20:21:31 +02:00
Showing only changes of commit 0f02c4b5be - Show all commits

View File

@ -3,9 +3,11 @@
use DateTime; use DateTime;
use RVR\PersistentData\Model\Community; use RVR\PersistentData\Model\Community;
use RVR\PersistentData\Model\CommunityMember; use RVR\PersistentData\Model\CommunityMember;
use RVR\PersistentData\Model\Currency;
use RVR\PersistentData\Model\User; use RVR\PersistentData\Model\User;
use RVR\Repository\CommunityRepository; use RVR\Repository\CommunityRepository;
use RVR\Repository\CommunityMemberRepository; use RVR\Repository\CommunityMemberRepository;
use RVR\Repository\CurrencyRepository;
use RVR\Repository\UserRepository; use RVR\Repository\UserRepository;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired; use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Response\IContent; use SokoWeb\Interfaces\Response\IContent;
@ -20,11 +22,14 @@ class CommunityController implements IAuthenticationRequired
private CommunityMemberRepository $communityMemberRepository; private CommunityMemberRepository $communityMemberRepository;
private CurrencyRepository $currencyRepository;
public function __construct() public function __construct()
{ {
$this->userRepository = new UserRepository(); $this->userRepository = new UserRepository();
$this->communityRepository = new CommunityRepository(); $this->communityRepository = new CommunityRepository();
$this->communityMemberRepository = new CommunityMemberRepository(); $this->communityMemberRepository = new CommunityMemberRepository();
$this->currencyRepository = new CurrencyRepository();
} }
public function isAuthenticationRequired(): bool public function isAuthenticationRequired(): bool
@ -38,10 +43,15 @@ class CommunityController implements IAuthenticationRequired
return null; return null;
} }
$currencyCodes = [];
foreach ($this->getCurrencies($community) as $currency) {
$currencyCodes[] = $currency->getCode();
}
return new HtmlContent('communities/community', [ return new HtmlContent('communities/community', [
'community' => $community, 'community' => $community,
'members' => $this->getMembers($community), 'members' => $this->getMembers($community),
'currencyNames' => [], 'currencyCodes' => $currencyCodes,
'upcomingEvents' => [], 'upcomingEvents' => [],
'editPermission' => $ownCommunityMember->getOwner() 'editPermission' => $ownCommunityMember->getOwner()
]); ]);
@ -84,7 +94,6 @@ class CommunityController implements IAuthenticationRequired
return $members; return $members;
} }
public function newMember(): ?IContent public function newMember(): ?IContent
{ {
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
@ -139,6 +148,83 @@ class CommunityController implements IAuthenticationRequired
return new JsonContent(['success' => true]); 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 public function saveCommunity(): ?IContent
{ {
$communityId = \Container::$request->query('communityId'); $communityId = \Container::$request->query('communityId');