Add currency option

This commit is contained in:
grandeljay 2022-11-25 22:25:42 +01:00
parent d9f4d45a06
commit d30126bfec
7 changed files with 126 additions and 30 deletions

View file

@ -68,11 +68,14 @@ $(function() {
* Preferences * Preferences
*/ */
/** Language */ /** Locale */
$('.ui.dropdown.locale').dropdown({ var dropdown_locale_settings = {
sortSelect : 'natural', 'sortSelect' : 'natural',
fullTextSearch : true, 'fullTextSearch' : true,
}); };
$('.ui.dropdown.language').dropdown(dropdown_locale_settings);
$('.ui.dropdown.currency').dropdown(dropdown_locale_settings);
/** Channel */ /** Channel */
$('.ui.dropdown.channel').dropdown(); $('.ui.dropdown.channel').dropdown();

View file

@ -44,7 +44,8 @@ class User
/** /**
* Private * Private
*/ */
private string $locale; private string $language;
private string $currency;
/** /**
* Non-Static * Non-Static
@ -61,12 +62,12 @@ class User
} }
} }
/** Set Locale */ /** Set Language */
if (!isset($this->locale)) { if (!isset($this->language)) {
$this->locale = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']) : DEFAULT_LOCALE; $this->language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']) : DEFAULT_LOCALE;
} }
$this->setLocale($this->locale); $this->setLocale($this->language);
} }
/** /**
@ -84,15 +85,34 @@ class User
if (file_exists($translationFilepath)) { if (file_exists($translationFilepath)) {
$loader = new \Gettext\Loader\PoLoader(); $loader = new \Gettext\Loader\PoLoader();
$this->translations = $loader->loadFile($translationFilepath); $this->translations = $loader->loadFile($translationFilepath);
} else {
trigger_error('Unable to find translations for ' . $locale . ', defaulting to ' . DEFAULT_LOCALE . '.', E_USER_NOTICE);
} }
/** Set locale */ /** Set locale */
$this->locale = $locale; $this->language = $locale;
} }
public function getLocale(): string public function getLocale(): string
{ {
return $this->locale; return $this->language;
}
/**
* Set the user currency
*
* @param string $locale
*
* @return void
*/
public function setCurrency(string $locale): void
{
$this->currency = $locale;
}
public function getCurrency(): string
{
return $this->currency;
} }
/** /**

View file

@ -121,7 +121,7 @@ class Wish
$userCard = User::getFromID($ofUser); $userCard = User::getFromID($ofUser);
$numberFormatter = new \NumberFormatter( $numberFormatter = new \NumberFormatter(
$userCard->getLocale(), $userCard->getLocale() . '@currency=' . $userCard->getCurrency(),
\NumberFormatter::CURRENCY \NumberFormatter::CURRENCY
); );
$userIsCurrent = isset($_SESSION['user']->id) && $_SESSION['user']->id === $userCard->id; $userIsCurrent = isset($_SESSION['user']->id) && $_SESSION['user']->id === $userCard->id;

View file

@ -108,16 +108,22 @@ $page->navigation();
</li> </li>
<li> <li>
<?php <?php
/** TRANSLATORS: Changelog: Roadmap */ /** TRANSLATORS: Changelog: Added */
echo __('Compact/list view for wishes'); echo __('Compact/list view for wishes');
?> ?>
</li> </li>
<li> <li>
<?php <?php
/** TRANSLATORS: Changelog: Roadmap */ /** TRANSLATORS: Changelog: Added */
echo __('Option to enable advertisements'); echo __('Option to enable advertisements');
?> ?>
</li> </li>
<li>
<?php
/** TRANSLATORS: Changelog: Added */
echo __('Option to set currency');
?>
</li>
</ul> </ul>
<h3 class="ui header"><?= __('Improved') ?></h3> <h3 class="ui header"><?= __('Improved') ?></h3>

View file

@ -146,6 +146,9 @@ switch ($step) {
/** /**
* Users * Users
*/ */
$currencyFormatter = new \NumberFormatter(DEFAULT_LOCALE, \NumberFormatter::CURRENCY);
$currencyISO = $currencyFormatter->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL);
$database->query('DROP TABLE IF EXISTS `users`;'); $database->query('DROP TABLE IF EXISTS `users`;');
$database->query( $database->query(
'CREATE TABLE `users` ( 'CREATE TABLE `users` (
@ -157,7 +160,8 @@ switch ($step) {
`last_login` DATETIME NOT NULL DEFAULT NOW(), `last_login` DATETIME NOT NULL DEFAULT NOW(),
`power` INT NOT NULL DEFAULT 0, `power` INT NOT NULL DEFAULT 0,
`birthdate` DATE NULL DEFAULT NULL, `birthdate` DATE NULL DEFAULT NULL,
`locale` VARCHAR(5) NOT NULL DEFAULT "' . DEFAULT_LOCALE . '", `language` VARCHAR(5) NOT NULL DEFAULT "' . DEFAULT_LOCALE . '",
`currency` VARCHAR(3) NOT NULL DEFAULT "' . $currencyISO . '",
`name_first` VARCHAR(32) NULL DEFAULT NULL, `name_first` VARCHAR(32) NULL DEFAULT NULL,
`name_last` VARCHAR(32) NULL DEFAULT NULL, `name_last` VARCHAR(32) NULL DEFAULT NULL,
`name_nick` VARCHAR(32) NULL DEFAULT NULL, `name_nick` VARCHAR(32) NULL DEFAULT NULL,

View file

@ -87,16 +87,34 @@ if (isset($_POST['user-id'], $_POST['section'])) {
/** /**
* Preferences * Preferences
*/ */
/** Locale */
if (isset($_POST['user-locale']) && $_POST['user-locale'] !== $_SESSION['user']->getLocale()) {
$_SESSION['user']->setLocale($_POST['user-locale']);
$set[] = '`locale` = "' . $_SESSION['user']->getLocale() . '"'; /** Language */
if (isset($_POST['user-language']) && $_POST['user-language'] !== $_SESSION['user']->getLocale()) {
$_SESSION['user']->setLocale($_POST['user-language']);
$set[] = '`language` = "' . $_SESSION['user']->getLocale() . '"';
$page->messages[] = Page::success( $page->messages[] = Page::success(
sprintf( sprintf(
__('Locale successfully updated!'), /** TRANSLATORS: %s: The new locale */
'<strong>Locale</strong>' __('Language set to %s.'),
'<strong>' . $_SESSION['user']->getLocale() . '</strong>'
),
__('Success')
);
}
/** Currency */
if (isset($_POST['user-currency']) && $_POST['user-currency'] !== $_SESSION['user']->getLocale()) {
$_SESSION['user']->setCurrency($_POST['user-currency']);
$set[] = '`currency` = "' . $_SESSION['user']->getCurrency() . '"';
$page->messages[] = Page::success(
sprintf(
/** TRANSLATORS: %s: The new locale */
__('Currency set to %s.'),
'<strong>' . $_SESSION['user']->getCurrency() . '</strong>'
), ),
__('Success') __('Success')
); );
@ -315,23 +333,54 @@ $page->navigation();
<div class="field"> <div class="field">
<label><?= __('Language') ?></label> <label><?= __('Language') ?></label>
<select class="ui search dropdown locale" name="user-locale"> <select class="ui search dropdown language" name="user-language">
<?php if (!in_array('en_GB', $locales)) { ?> <?php if (!in_array('en_GB', $locales)) { ?>
<option value="<?= 'en_GB' ?>"><?= \Locale::getDisplayName('en_GB', $_SESSION['user']->getLocale()) ?></option> <option value="<?= 'en_GB' ?>"><?= \Locale::getDisplayName('en_GB', $_SESSION['user']->getLocale()) ?></option>
<?php } ?> <?php } ?>
<?php foreach ($locales as $locale) { ?> <?php foreach ($locales as $locale) { ?>
<?php if (\Locale::getRegion($locale)) { ?>
<?php if ($locale === $_SESSION['user']->getLocale()) { ?> <?php if ($locale === $_SESSION['user']->getLocale()) { ?>
<option value="<?= $locale ?>" selected><?= \Locale::getDisplayName($locale, $_SESSION['user']->getLocale()) ?></option> <option value="<?= $locale ?>" selected><?= \Locale::getDisplayName($locale, $_SESSION['user']->getLocale()) ?></option>
<?php } else { ?> <?php } else { ?>
<option value="<?= $locale ?>"><?= \Locale::getDisplayName($locale, $_SESSION['user']->getLocale()) ?></option> <option value="<?= $locale ?>"><?= \Locale::getDisplayName($locale, $_SESSION['user']->getLocale()) ?></option>
<?php } ?> <?php } ?>
<?php } ?> <?php } ?>
<?php } ?>
</select> </select>
</div> </div>
<div class="field">
<label><?= __('Currency') ?></label>
<select class="ui search dropdown currency" name="user-currency">
<?php
$currencies = array();
?>
<?php foreach ($locales as $locale) { ?>
<?php
$currencyFormatter = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
$currencyISO = $currencyFormatter->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL);
$currencySymbol = $currencyFormatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
$currencyValue = $currencyISO . ' (' . $currencySymbol . ')';
if (in_array($currencyISO, $currencies, true) || $currencyISO === $currencySymbol) {
continue;
} else {
$currencies[] = $currencyISO;
}
?>
<?php if ($currencyISO === $_SESSION['user']->getCurrency()) { ?>
<option value="<?= $currencyISO ?>" selected><?= $currencyValue ?></option>
<?php } else { ?>
<option value="<?= $currencyISO ?>"><?= $currencyValue ?></option>
<?php } ?>
<?php } ?>
</select>
</div>
</div>
<div class="field">
<?php if (defined('CHANNELS') && is_array(CHANNELS)) { ?> <?php if (defined('CHANNELS') && is_array(CHANNELS)) { ?>
<script type="text/javascript"> <script type="text/javascript">
var CHANNELS = <?= json_encode(CHANNELS) ?>; var CHANNELS = <?= json_encode(CHANNELS) ?>;

View file

@ -6,7 +6,21 @@ TRUNCATE `sessions`;
/** /**
* Users * Users
*/ */
ALTER TABLE
`users` CHANGE COLUMN `locale` `language` VARCHAR(5) NOT NULL DEFAULT 'en_GB'
AFTER
`birthdate`;
ALTER TABLE ALTER TABLE
`users` `users`
ADD ADD
COLUMN `advertisements` TINYINT(1) NOT NULL DEFAULT 0; COLUMN `advertisements` TINYINT(1) NOT NULL DEFAULT 0,
ADD
COLUMN `currency` VARCHAR(3) NOT NULL DEFAULT 'EUR'
AFTER
`language`;
UPDATE
`users`
SET
`currency` = `language`;