381 lines
14 KiB
PHP
381 lines
14 KiB
PHP
<?php namespace RVR\Controller;
|
|
|
|
use DateTime;
|
|
use RVR\PersistentData\Model\Community;
|
|
use RVR\PersistentData\Model\CommunityMember;
|
|
use RVR\PersistentData\Model\Currency;
|
|
use RVR\PersistentData\Model\CurrencyExchangeRate;
|
|
use RVR\PersistentData\Model\User;
|
|
use RVR\Repository\CommunityRepository;
|
|
use RVR\Repository\CommunityMemberRepository;
|
|
use RVR\Repository\CurrencyExchangeRateRepository;
|
|
use RVR\Repository\CurrencyRepository;
|
|
use RVR\Repository\UserRepository;
|
|
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
|
use SokoWeb\Interfaces\Response\IContent;
|
|
use SokoWeb\Response\HtmlContent;
|
|
use SokoWeb\Response\JsonContent;
|
|
|
|
class CommunityController implements IAuthenticationRequired
|
|
{
|
|
private UserRepository $userRepository;
|
|
|
|
private CommunityRepository $communityRepository;
|
|
|
|
private CommunityMemberRepository $communityMemberRepository;
|
|
|
|
private CurrencyRepository $currencyRepository;
|
|
|
|
private CurrencyExchangeRateRepository $currencyExchangeRatesRepository;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->userRepository = new UserRepository();
|
|
$this->communityRepository = new CommunityRepository();
|
|
$this->communityMemberRepository = new CommunityMemberRepository();
|
|
$this->currencyRepository = new CurrencyRepository();
|
|
$this->currencyExchangeRatesRepository = new CurrencyExchangeRateRepository();
|
|
}
|
|
|
|
public function isAuthenticationRequired(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public function getCommunityHome(): ?IContent
|
|
{
|
|
if (!$this->checkPermission(\Container::$request->query('communityId'), false, $community, $ownCommunityMember)) {
|
|
return null;
|
|
}
|
|
|
|
return new HtmlContent('communities/community', [
|
|
'community' => $community,
|
|
'members' => $this->getMembers($community),
|
|
'currencies' => $this->getCurrencies($community),
|
|
'upcomingEvents' => [],
|
|
'editPermission' => $ownCommunityMember->getOwner()
|
|
]);
|
|
}
|
|
|
|
public function getCommunityNew(): IContent
|
|
{
|
|
return new HtmlContent('communities/community_edit');
|
|
}
|
|
|
|
public function getCommunityEdit(): ?IContent
|
|
{
|
|
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
|
|
return null;
|
|
}
|
|
|
|
return new HtmlContent('communities/community_edit', [
|
|
'community' => $community
|
|
]);
|
|
}
|
|
|
|
public function getMembersEdit(): ?IContent
|
|
{
|
|
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
|
|
return null;
|
|
}
|
|
|
|
return new HtmlContent('communities/community_members', [
|
|
'community' => $community,
|
|
'members' => $this->getMembers($community)
|
|
]);
|
|
}
|
|
|
|
public function newMember(): ?IContent
|
|
{
|
|
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
|
|
return null;
|
|
}
|
|
|
|
$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'));
|
|
\Container::$persistentDataManager->saveToDb($communityMember);
|
|
|
|
return new JsonContent(['success' => true]);
|
|
}
|
|
|
|
public function deleteMember(): ?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 deleted.']
|
|
]);
|
|
}
|
|
|
|
\Container::$persistentDataManager->deleteFromDb($communityMember);
|
|
|
|
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)
|
|
]);
|
|
}
|
|
|
|
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 getCurrencyExchangeRates(): ?IContent
|
|
{
|
|
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
|
|
return null;
|
|
}
|
|
|
|
$currency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, \Container::$request->query('code'));
|
|
if ($currency === null) {
|
|
return null;
|
|
}
|
|
|
|
$currencyExchangeRates = $this->currencyExchangeRatesRepository->getAllByCurrency($currency);
|
|
|
|
return new HtmlContent('communities/currency_exchange_rates', [
|
|
'community' => $community,
|
|
'currency' => $currency,
|
|
'currencyExchangeRates' => $currencyExchangeRates,
|
|
'editPermission' => $ownCommunityMember->getOwner()
|
|
]);
|
|
}
|
|
|
|
public function newCurrencyExchangeRate(): ?IContent
|
|
{
|
|
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
|
|
return null;
|
|
}
|
|
|
|
$currency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, \Container::$request->query('code'));
|
|
if ($currency === null) {
|
|
return null;
|
|
}
|
|
|
|
$exchangeRate = (float)\Container::$request->post('exchange_rate');
|
|
if ($exchangeRate < 0) {
|
|
return new JsonContent([
|
|
'error' => ['errorText' => 'Please fill all required fields!']
|
|
]);
|
|
}
|
|
|
|
$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;
|
|
}
|
|
|
|
$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);
|
|
|
|
return new JsonContent(['success' => true]);
|
|
}
|
|
|
|
public function deleteCurrencyExchangeRate(): ?IContent
|
|
{
|
|
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
|
|
return null;
|
|
}
|
|
|
|
$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'));
|
|
\Container::$persistentDataManager->deleteFromDb($currencyExchangeRate);
|
|
|
|
return new JsonContent(['success' => true]);
|
|
}
|
|
|
|
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) {
|
|
return new JsonContent([
|
|
'error' => ['errorText' => 'Please fill all required fields!']
|
|
]);
|
|
}
|
|
|
|
$community->setName($name);
|
|
$community->setCurrency($currency);
|
|
if (!$communityId) {
|
|
$community->setCreatedDate(new DateTime());
|
|
}
|
|
\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));
|
|
usort($members, function($a, $b) {
|
|
return strnatcmp($a->getUser()->getDisplayName(), $b->getUser()->getDisplayName());
|
|
});
|
|
return $members;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private function checkPermission(
|
|
int $communityId,
|
|
bool $needToBeOwner,
|
|
?Community &$community,
|
|
?CommunityMember &$ownCommunityMember): bool
|
|
{
|
|
$community = $this->communityRepository->getById($communityId);
|
|
if ($community === null) {
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @var User $user
|
|
*/
|
|
$user = \Container::$request->user();
|
|
|
|
$ownCommunityMember = $this->communityMemberRepository->getByCommunityAndUser($community, $user);
|
|
if ($ownCommunityMember === null || ($needToBeOwner && !$ownCommunityMember->getOwner())) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|