Compare commits
No commits in common. "7b450699046b65fb9413300131db53dbf6027949" and "882b326aca5294f4f6154b1165a2da2725638ddc" have entirely different histories.
7b45069904
...
882b326aca
6
app.php
6
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 = null;
|
||||
static ?SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler = null;
|
||||
static ?SokoWeb\Interfaces\Request\IRequest $request = null;
|
||||
static SokoWeb\Interfaces\Routing\IRouteCollection $routeCollection;
|
||||
static SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler;
|
||||
static SokoWeb\Interfaces\Request\IRequest $request;
|
||||
}
|
||||
|
||||
Container::$dbConnection = new SokoWeb\Database\Mysql\Connection($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
|
||||
|
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
use RVR\PersistentData\Model\Community;
|
||||
use RVR\PersistentData\Model\Currency;
|
||||
use SokoWeb\Database\Query\Select;
|
||||
|
||||
$select = new Select(Container::$dbConnection);
|
||||
$communities = Container::$persistentDataManager->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);
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
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`);
|
@ -48,8 +48,6 @@ class CommunityController implements IAuthenticationRequired
|
||||
return null;
|
||||
}
|
||||
|
||||
\Container::$persistentDataManager->loadRelationsFromDb($community, false);
|
||||
|
||||
return new HtmlContent('communities/community', [
|
||||
'community' => $community,
|
||||
'members' => $this->getMembers($community),
|
||||
@ -75,63 +73,6 @@ 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)) {
|
||||
@ -244,10 +185,6 @@ 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]);
|
||||
@ -260,7 +197,7 @@ class CommunityController implements IAuthenticationRequired
|
||||
}
|
||||
|
||||
$currency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, \Container::$request->query('code'));
|
||||
if ($currency === null || $currency->getId() === $community->getMainCurrencyId()) {
|
||||
if ($currency === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -324,9 +261,51 @@ 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, [User::class]));
|
||||
$members = iterator_to_array($this->communityMemberRepository->getAllByCommunity($community, true));
|
||||
usort($members, function($a, $b) {
|
||||
return strnatcmp($a->getUser()->getDisplayName(), $b->getUser()->getDisplayName());
|
||||
});
|
||||
@ -339,9 +318,6 @@ 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;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
<?php namespace RVR\Controller;
|
||||
|
||||
use RVR\PersistentData\Model\Community;
|
||||
use RVR\PersistentData\Model\User;
|
||||
use RVR\Repository\CommunityMemberRepository;
|
||||
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
|
||||
@ -28,7 +27,7 @@ class HomeController implements IAuthenticationRequired
|
||||
*/
|
||||
$user = \Container::$request->user();
|
||||
|
||||
$ownCommunityMembers = $this->communityMemberRepository->getAllByUser($user, true, [Community::class]);
|
||||
$ownCommunityMembers = $this->communityMemberRepository->getAllByUser($user, true);
|
||||
$communities = [];
|
||||
foreach ($ownCommunityMembers as $ownCommunityMember) {
|
||||
$communities[] = $ownCommunityMember->getCommunity();
|
||||
|
@ -6,9 +6,6 @@ class AuditLogger extends AuditLoggerBase
|
||||
{
|
||||
protected function getModifierId()
|
||||
{
|
||||
if (\Container::$request === null) {
|
||||
return null;
|
||||
}
|
||||
$user = \Container::$request->user();
|
||||
if ($user === null) {
|
||||
return null;
|
||||
|
@ -7,18 +7,12 @@ class Community extends Model
|
||||
{
|
||||
protected static string $table = 'communities';
|
||||
|
||||
protected static array $fields = ['name', 'currency', 'main_currency_id', 'created'];
|
||||
|
||||
protected static array $relations = ['main_currency' => Currency::class];
|
||||
protected static array $fields = ['name', 'currency', 'created'];
|
||||
|
||||
private string $name = '';
|
||||
|
||||
private string $currency = '';
|
||||
|
||||
private ?Currency $mainCurrency = null;
|
||||
|
||||
private ?int $mainCurrencyId = null;
|
||||
|
||||
private DateTime $created;
|
||||
|
||||
public function setName(string $name): void
|
||||
@ -31,16 +25,6 @@ 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;
|
||||
@ -61,16 +45,6 @@ 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;
|
||||
|
@ -13,20 +13,20 @@ class CommunityMemberRepository
|
||||
return \Container::$persistentDataManager->selectFromDbById($id, CommunityMember::class);
|
||||
}
|
||||
|
||||
public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator
|
||||
public function getAllByCommunity(Community $community, bool $useRelations = false): Generator
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection);
|
||||
$select->where('community_id', '=', $community->getId());
|
||||
|
||||
yield from \Container::$persistentDataManager->selectMultipleFromDb($select, CommunityMember::class, $useRelations, $withRelations);
|
||||
yield from \Container::$persistentDataManager->selectMultipleFromDb($select, CommunityMember::class, $useRelations);
|
||||
}
|
||||
|
||||
public function getAllByUser(User $user, bool $useRelations = false, array $withRelations = []): Generator
|
||||
public function getAllByUser(User $user, bool $useRelations = false): Generator
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection);
|
||||
$select->where('user_id', '=', $user->getId());
|
||||
|
||||
yield from \Container::$persistentDataManager->selectMultipleFromDb($select, CommunityMember::class, $useRelations, $withRelations);
|
||||
yield from \Container::$persistentDataManager->selectMultipleFromDb($select, CommunityMember::class, $useRelations);
|
||||
}
|
||||
|
||||
public function getByCommunityAndUser(Community $community, User $user) : ?CommunityMember
|
||||
|
@ -16,15 +16,14 @@
|
||||
</div>
|
||||
<div>
|
||||
<h3 class="marginBottom">Currencies</h3>
|
||||
<?php foreach ($currencies as $currency): ?>
|
||||
<p>
|
||||
<?php if ($currency->getId() === $community->getMainCurrencyId()): ?>
|
||||
<b><?= $currency->getCode() ?></b>
|
||||
<?php else: ?>
|
||||
<p>Main currency: <b><?= $community->getCurrency() ?></b></p>
|
||||
<?php if (count($currencies) > 0): ?>
|
||||
<p>Further currencies: <b>
|
||||
<?php foreach ($currencies as $currency): ?>
|
||||
<a href="<?= Container::$routeCollection->getRoute('community-currency-exchange-rates')->generateLink(['communityId' => $community->getId(), 'code' => $currency->getCode()]) ?>"><?= $currency->getCode() ?></a>
|
||||
<?php endif; ?>
|
||||
</p>
|
||||
<?php endforeach; ?>
|
||||
<?php endforeach; ?>
|
||||
</b></p>
|
||||
<?php endif; ?>
|
||||
<?php if ($editPermission): ?>
|
||||
<hr>
|
||||
<p><a href="<?= Container::$routeCollection->getRoute('community-currencies')->generateLink(['communityId' => $community->getId()]) ?>">Edit currencies</a></p>
|
||||
@ -41,23 +40,19 @@
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div>
|
||||
<?php
|
||||
$mainCurrencyCode = $community->getMainCurrency()->getCode();
|
||||
$mainCurrencyRoundDigits = $community->getMainCurrency()->getRoundDigits();
|
||||
?>
|
||||
<h3 class="marginBottom">Finances</h3>
|
||||
<table class="fullWidth">
|
||||
<tr>
|
||||
<td>You owe</td>
|
||||
<td style="text-align: right; color: red;"><?= number_format(0, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
||||
<td style="text-align: right; color: red;">0 <?= $community->getCurrency() ?></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>You're owed</td>
|
||||
<td style="text-align: right; color: green;"><?= number_format(0, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
||||
<td style="text-align: right; color: green;">0 <?= $community->getCurrency() ?></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Your balance</td>
|
||||
<td style="text-align: right;"><?= number_format(0, $mainCurrencyRoundDigits) ?> <?= $mainCurrencyCode ?></td>
|
||||
<td style="text-align: right;">0 <?= $community->getCurrency() ?></td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
|
@ -21,11 +21,9 @@
|
||||
<td>
|
||||
<input type="number" form="editCurrency_<?= $currency->getId() ?>" class="text fullWidth" name="round_digits" value="<?= $currency->getRoundDigits() ?>" min="0" max="9" required>
|
||||
</td>
|
||||
<td style="text-align: right; font-size: 0;">
|
||||
<button type="submit" form="editCurrency_<?= $currency->getId() ?>" name="submit" class="small" disabled>Save</button>
|
||||
<?php if ($currency->getId() !== $community->getMainCurrencyId()): ?>
|
||||
<button type="submit" form="deleteCurrency_<?= $currency->getId() ?>" class="small red marginLeft">Delete</button>
|
||||
<?php endif; ?>
|
||||
<td style="text-align: right;">
|
||||
<button type="submit" form="editCurrency_<?= $currency->getId() ?>" name="submit" class="small marginRight" disabled>Save</button><!--
|
||||
--><button type="submit" form="deleteCurrency_<?= $currency->getId() ?>" class="small red">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
|
@ -16,10 +16,7 @@
|
||||
?>
|
||||
<form id="communityForm" action="<?= $formAction ?>" method="post" data-redirect-on-success="true">
|
||||
<input type="text" class="text big fullWidth" name="name" placeholder="Name" value="<?= isset($community) ? $community->getName() : '' ?>" required>
|
||||
<?php if (!isset($community)): ?>
|
||||
<input type="text" class="text big fullWidth marginTop" name="main_currency_code" placeholder="Main currency" maxlength="3" required>
|
||||
<input type="number" class="text big fullWidth marginTop" name="main_currency_round_digits" placeholder="Main currency round digits" min="0" max="9" required>
|
||||
<?php endif; ?>
|
||||
<input type="text" class="text big fullWidth marginTop" name="currency" value="<?= isset($community) ? $community->getCurrency() : '' ?>" placeholder="Default currency" maxlength="3" required>
|
||||
<p id="communityFormError" class="formError justify marginTop"></p>
|
||||
<div class="right marginTop">
|
||||
<button type="submit" name="submit"><?= isset($community) ? 'Save' : 'Create' ?></button>
|
||||
|
Loading…
Reference in New Issue
Block a user