Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shop module: Prevent invalid currency in settings #1105

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions application/libraries/Ilch/Translations/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
'validation.errors.max.string' => '%s darf höchstens %s Zeichen lang sein.',
'validation.errors.max.array' => '%s darf höchstens %s Einträge haben.',
'validation.errors.timezone.notAValidTimezone' => '%s ist keine gültige Zeitzone.',
'validation.errors.exists.resourceNotFound' => '%s konnte nicht gefunden werden.',
'validation.errors.exists.resourceFound' => '%s wurde gefunden.',

// general
'saveSuccess' => 'Erfolgreich gespeichert',
Expand Down
2 changes: 2 additions & 0 deletions application/libraries/Ilch/Translations/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
'validation.errors.max.string' => '%s may not be greater than %s characters.',
'validation.errors.max.array' => '%s may not have more than %s items.',
'validation.errors.timezone.notAValidTimezone' => '%s is not a valid timezone.',
'validation.errors.exists.resourceNotFound' => '%s was not found.',
'validation.errors.exists.resourceFound' => '%s was found.',

// general
'saveSuccess' => 'Saved successful',
Expand Down
2 changes: 1 addition & 1 deletion application/modules/shop/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Config extends Install
{
public $config = [
'key' => 'shop',
'version' => '1.3.2',
'version' => '1.3.3',
'icon_small' => 'fa-solid fa-cart-shopping',
'author' => 'blackcoder & LordSchirmer',
'link' => 'https://ilch.de',
Expand Down
2 changes: 1 addition & 1 deletion application/modules/shop/controllers/Customerarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function showAction()
if (!empty($order)) {
$order = $order[0];
// Get the currency from the order as you don't want a currency change for existing orders.
$currency = $currencyMapper->getCurrencyById($order->getCurrencyId())[0];
$currency = $currencyMapper->getCurrencyById($order->getCurrencyId());
} else {
$order = [];
}
Expand Down
8 changes: 4 additions & 4 deletions application/modules/shop/controllers/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function indexAction()
$itemsMapper = new ItemsMapper();
$userMapper = new UserMapper();

$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'))[0];
$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'));

$user = null;
if ($this->getUser()) {
Expand Down Expand Up @@ -129,7 +129,7 @@ public function cartAction()
$currencyMapper = new CurrencyMapper();
$itemsMapper = new ItemsMapper();
$settingsMapper = new SettingsMapper();
$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'))[0];
$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'));

$this->getLayout()->header()->css('static/css/style_front.css');
$this->getLayout()->getTitle()
Expand Down Expand Up @@ -170,7 +170,7 @@ public function orderAction()
$settingsMapper = new SettingsMapper();
$ilchDate = new Date();
$captchaNeeded = captchaNeeded();
$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'))[0];
$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'));
$addresses = [];
$customer = null;

Expand Down Expand Up @@ -479,7 +479,7 @@ public function showAction()
$this->redirect(['action' => 'index']);
}

$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'))[0];
$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'));
$category = $categoryMapper->getCategoryById($shopItem->getCatId());

$user = null;
Expand Down
2 changes: 1 addition & 1 deletion application/modules/shop/controllers/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ public function indexAction()
$this->getView()->set('itemsMapper', $itemsMapper);
$this->getView()->set('order', $order);
$this->getView()->set('settings', $settings);
$this->getView()->set('currency', $currencyMapper->getCurrencyById($order->getCurrencyId())[0]);
$this->getView()->set('currency', $currencyMapper->getCurrencyById($order->getCurrencyId()));
}
}
16 changes: 5 additions & 11 deletions application/modules/shop/controllers/admin/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function indexAction()
$currencyIds[] = $order->getCurrencyId();
}
foreach ($this->getRequest()->getPost('check_currencies') as $id) {
$currencyInUse = (in_array($this->getConfig()->get('shop_currency'), $currencyIds) && $currencyMapper->getCurrencyById($id)[0]->getId() == $this->getConfig()->get('shop_currency'));
$currencyInUse = (in_array($this->getConfig()->get('shop_currency'), $currencyIds) && $currencyMapper->getCurrencyById($id)->getId() == $this->getConfig()->get('shop_currency'));

if ($currencyInUse) {
$this->addMessage('currencyInUse', 'danger');
Expand Down Expand Up @@ -145,7 +145,7 @@ public function treatAction()
foreach ($orders as $order) {
$currencyIds[] = $order->getCurrencyId();
}
$currencyInUse = (in_array($this->getConfig()->get('shop_currency'), $currencyIds) && $currencyMapper->getCurrencyById($id)[0]->getId() == $this->getConfig()->get('shop_currency'));
$currencyInUse = (in_array($this->getConfig()->get('shop_currency'), $currencyIds) && $currencyMapper->getCurrencyById($id)->getId() == $this->getConfig()->get('shop_currency'));
} else {
$this->getLayout()->getAdminHmenu()
->add($this->getTranslator()->trans('menuShops'), ['action' => 'index'])
Expand Down Expand Up @@ -190,15 +190,9 @@ public function treatAction()
}
}

if ($this->getRequest()->getParam('id')) {
$currency = $currencyMapper->getCurrencyById($id);
} else {
$currency = [];
}
$currency = $this->getRequest()->getParam('id') ? $currencyMapper->getCurrencyById($id) : null;

if (count($currency) > 0) {
$currency = $currency[0];
} else {
if (!$currency) {
$currency = new CurrencyModel();
}

Expand All @@ -219,7 +213,7 @@ public function deleteAction()
foreach ($orders as $order) {
$currencyIds[] = $order->getCurrencyId();
}
$currencyInUse = (in_array($this->getConfig()->get('shop_currency'), $currencyIds) && $currencyMapper->getCurrencyById($id)[0]->getId() == $this->getConfig()->get('shop_currency'));
$currencyInUse = (in_array($this->getConfig()->get('shop_currency'), $currencyIds) && $currencyMapper->getCurrencyById($id)->getId() == $this->getConfig()->get('shop_currency'));

if ($currencyInUse) {
$this->addMessage('currencyInUse', 'danger');
Expand Down
4 changes: 2 additions & 2 deletions application/modules/shop/controllers/admin/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function indexAction()
$ordersMapper = new OrdersMapper();
$itemsMapper = new ItemsMapper();

$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'))[0];
$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'));

$this->getLayout()->getAdminHmenu()
->add($this->getTranslator()->trans('menuShops'), ['controller' => 'index', 'action' => 'index'])
Expand Down Expand Up @@ -137,7 +137,7 @@ public function treatAction()
$itemsMapper = new ItemsMapper();
$settingsMapper = new SettingsMapper();

$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'))[0];
$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'));

if ($this->getRequest()->getParam('id')) {
if (!is_numeric($this->getRequest()->getParam('id'))) {
Expand Down
2 changes: 1 addition & 1 deletion application/modules/shop/controllers/admin/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function treatAction()
if ($this->getRequest()->getParam('id') && is_numeric($this->getRequest()->getParam('id'))) {
$order = $ordersMapper->getOrderById($this->getRequest()->getParam('id'));
// Get the currency from the order as you don't want a currency change for existing orders.
$currency = $currencyMapper->getCurrencyById($order->getCurrencyId())[0];
$currency = $currencyMapper->getCurrencyById($order->getCurrencyId());
$this->getView()->set('order', $order);
$this->getView()->set('currency', $currency->getName());
$this->getView()->set('itemsMapper', $itemsMapper);
Expand Down
5 changes: 3 additions & 2 deletions application/modules/shop/controllers/admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function defaultAction()
$currencyMapper = new CurrencyMapper();
$settingsMapper = new SettingsMapper();

$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'))[0];
$currency = $currencyMapper->getCurrencyById($this->getConfig()->get('shop_currency'));

$this->getLayout()->getAdminHmenu()
->add($this->getTranslator()->trans('menuShops'), ['controller' => 'index', 'action' => 'index'])
Expand All @@ -178,7 +178,8 @@ public function defaultAction()
'allowWillCollect' => 'required|numeric|integer|min:0|max:1',
'deliveryTextTop' => 'required',
'invoiceTextTop' => 'required',
'invoiceTextBottom' => 'required'
'invoiceTextBottom' => 'required',
'shopCurrency' => 'required|exists:shop_currencies,id',
]);

if ($validation->isValid()) {
Expand Down
12 changes: 9 additions & 3 deletions application/modules/shop/mappers/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ public function getCurrencies(array $where = []): array
* Gets the currencies by id.
*
* @param int $id
* @return CurrencyModel[]|array
* @return CurrencyModel|null
*/
public function getCurrencyById(int $id): array
public function getCurrencyById(int $id): ?CurrencyModel
{
return $this->getCurrencies(['id' => $id]);
$currency = $this->getCurrencies(['id' => $id]);

if (empty($currency)) {
return null;
}

return reset($currency);
}

/**
Expand Down