Compare commits

..

No commits in common. "882b326aca5294f4f6154b1165a2da2725638ddc" and "a021aff92c57d8da3b70d574b1459109e668ac34" have entirely different histories.

2 changed files with 81 additions and 48 deletions

View File

@ -85,25 +85,34 @@ class CommunityController implements IAuthenticationRequired
]);
}
public function saveMember(): ?IContent
public function newMember(): ?IContent
{
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
return null;
}
$communityMemberId = \Container::$request->query('community_member_id');
if ($communityMemberId) {
$communityMember = $this->communityMemberRepository->getById($communityMemberId);
if ($communityMember->getUserId() === $ownCommunityMember->getUserId()) {
return new JsonContent([
'error' => ['errorText' => 'Own user cannot be edited.']
]);
}
} else {
$user = $this->userRepository->getById(\Container::$request->post('user_id'));
$communityMember = new CommunityMember();
$communityMember->setCommunity($community);
$communityMember->setUser($user);
$user = $this->userRepository->getById(\Container::$request->post('user_id'));
$communityMember = new CommunityMember();
$communityMember->setCommunity($community);
$communityMember->setUser($user);
$communityMember->setOwner((bool)\Container::$request->post('owner'));
\Container::$persistentDataManager->saveToDb($communityMember);
return new JsonContent(['success' => true]);
}
public function editMember(): ?IContent
{
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
return null;
}
$communityMember = $this->communityMemberRepository->getById(\Container::$request->query('community_member_id'));
if ($communityMember->getUserId() === \Container::$request->user()->getUniqueId()) {
return new JsonContent([
'error' => ['errorText' => 'Own user cannot be edited.']
]);
}
$communityMember->setOwner((bool)\Container::$request->post('owner'));
@ -142,7 +151,7 @@ class CommunityController implements IAuthenticationRequired
]);
}
public function saveCurrency(): ?IContent
public function newCurrency(): ?IContent
{
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
return null;
@ -156,21 +165,15 @@ class CommunityController implements IAuthenticationRequired
]);
}
$currencyId = \Container::$request->query('currency_id');
if ($currencyId){
$currency = $this->currencyRepository->getById($currencyId);
} else {
$currency = new Currency();
$currency->setCommunity($community);
}
$existingCurrency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, $code);
if ($existingCurrency !== null && $currency->getId() !== $existingCurrency->getId()) {
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);
@ -178,6 +181,20 @@ class CommunityController implements IAuthenticationRequired
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)) {
@ -211,7 +228,7 @@ class CommunityController implements IAuthenticationRequired
]);
}
public function saveCurrencyExchangeRate(): ?IContent
public function newCurrencyExchangeRate(): ?IContent
{
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
return null;
@ -229,15 +246,29 @@ class CommunityController implements IAuthenticationRequired
]);
}
$currencyExchangeRateId = \Container::$request->query('currency_exchange_rate_id');
if ($currencyExchangeRateId){
$currencyExchangeRate = $this->currencyExchangeRatesRepository->getById($currencyExchangeRateId);
} else {
$currencyExchangeRate = new CurrencyExchangeRate();
$currencyExchangeRate->setCurrency($currency);
$currencyExchangeRate = new CurrencyExchangeRate();
$currencyExchangeRate->setCurrency($currency);
$currencyExchangeRate->setExchangeRate($exchangeRate);
$currencyExchangeRate->setValidFromDate(new DateTime(\Container::$request->post('valid_from')));
\Container::$persistentDataManager->saveToDb($currencyExchangeRate);
return new JsonContent(['success' => true]);
}
public function editCurrencyExchangeRate(): ?IContent
{
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
return null;
}
$currencyExchangeRate->setExchangeRate($exchangeRate);
$currency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, \Container::$request->query('code'));
if ($currency === null) {
return null;
}
$currencyExchangeRate = $this->currencyExchangeRatesRepository->getById(\Container::$request->query('currency_exchange_rate_id'));
$currencyExchangeRate->setCurrency($currency);
$currencyExchangeRate->setExchangeRate((float)\Container::$request->post('exchange_rate'));
$currencyExchangeRate->setValidFromDate(new DateTime(\Container::$request->post('valid_from')));
\Container::$persistentDataManager->saveToDb($currencyExchangeRate);
@ -263,6 +294,15 @@ class CommunityController implements IAuthenticationRequired
public function saveCommunity(): ?IContent
{
$communityId = \Container::$request->query('communityId');
if ($communityId){
if (!$this->checkPermission($communityId, true, $community, $ownCommunityMember)) {
return null;
}
} else {
$community = new Community();
}
$name = \Container::$request->post('name');
$currency = \Container::$request->post('currency');
if (strlen($name) === 0 || strlen($currency) === 0 || strlen($currency) > 3) {
@ -271,18 +311,11 @@ class CommunityController implements IAuthenticationRequired
]);
}
$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);
if (!$communityId) {
$community->setCreatedDate(new DateTime());
}
\Container::$persistentDataManager->saveToDb($community);
if (!$communityId) {

12
web.php
View File

@ -63,17 +63,17 @@ Container::$routeCollection->group('communities', function (RouteCollection $rou
$routeCollection->get('community-edit', 'edit', [CommunityController::class, 'getCommunityEdit']);
$routeCollection->post('community-edit-action', 'edit', [CommunityController::class, 'saveCommunity']);
$routeCollection->get('community-members', 'members', [CommunityController::class, 'getMembersEdit']);
$routeCollection->post('community-members-new', 'newMember', [CommunityController::class, 'saveMember']);
$routeCollection->post('community-members-edit', 'editMember', [CommunityController::class, 'saveMember']);
$routeCollection->post('community-members-new', 'newMember', [CommunityController::class, 'newMember']);
$routeCollection->post('community-members-edit', 'editMember', [CommunityController::class, 'editMember']);
$routeCollection->post('community-members-delete', 'deleteMember', [CommunityController::class, 'deleteMember']);
$routeCollection->get('community-currencies', 'currencies', [CommunityController::class, 'getCurrenciesEdit']);
$routeCollection->post('community-currencies-new', 'newCurrency', [CommunityController::class, 'saveCurrency']);
$routeCollection->post('community-currencies-edit', 'editCurrency', [CommunityController::class, 'saveCurrency']);
$routeCollection->post('community-currencies-new', 'newCurrency', [CommunityController::class, 'newCurrency']);
$routeCollection->post('community-currencies-edit', 'editCurrency', [CommunityController::class, 'editCurrency']);
$routeCollection->post('community-currencies-delete', 'deleteCurrency', [CommunityController::class, 'deleteCurrency']);
$routeCollection->group('currencyExchangeRates', function (RouteCollection $routeCollection) {
$routeCollection->get('community-currency-exchange-rates', '{code}', [CommunityController::class, 'getCurrencyExchangeRates']);
$routeCollection->post('community-currency-exchange-rates-new', '{code}/new', [CommunityController::class, 'saveCurrencyExchangeRate']);
$routeCollection->post('community-currency-exchange-rates-edit', '{code}/edit', [CommunityController::class, 'saveCurrencyExchangeRate']);
$routeCollection->post('community-currency-exchange-rates-new', '{code}/new', [CommunityController::class, 'newCurrencyExchangeRate']);
$routeCollection->post('community-currency-exchange-rates-edit', '{code}/edit', [CommunityController::class, 'editCurrencyExchangeRate']);
$routeCollection->post('community-currency-exchange-rates-delete', '{code}/delete', [CommunityController::class, 'deleteCurrencyExchangeRate']);
});
});