Add profile language options

This commit is contained in:
Jay Trees 2022-03-25 10:30:38 +01:00
parent 25024e2548
commit 0a3a6aded6
6 changed files with 51 additions and 25 deletions

View file

@ -8,6 +8,7 @@
define('VERSION', '0.4.0'); define('VERSION', '0.4.0');
define('ROOT', __DIR__); define('ROOT', __DIR__);
define('DEFAULT_LOCALE', 'en');
/** /**
* Include * Include
@ -72,12 +73,20 @@ setcookie(
$session['httponly'] $session['httponly']
); );
/**
* User
*/
if ($options) {
$user = new wishthis\User();
}
/** /**
* Language * Language
*/ */
\Locale::setDefault(DEFAULT_LOCALE);
/** Determine Locale */ /** Determine Locale */
$userLocale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); $locales = array_filter(
$locales = array_filter(
array_map( array_map(
function ($value) { function ($value) {
if ('po' === pathinfo($value, PATHINFO_EXTENSION)) { if ('po' === pathinfo($value, PATHINFO_EXTENSION)) {
@ -87,7 +96,7 @@ $locales = array_filter(
scandir(ROOT . '/translations') scandir(ROOT . '/translations')
) )
); );
$locale = \Locale::lookup($locales, $userLocale, false, 'en'); $locale = \Locale::lookup($locales, $user->locale, false, DEFAULT_LOCALE);
/** Load Translation */ /** Load Translation */
$translationFilepath = ROOT . '/translations/' . $locale . '.po'; $translationFilepath = ROOT . '/translations/' . $locale . '.po';
@ -117,13 +126,6 @@ if (!$options || !$options->getOption('isInstalled')) {
$page = 'install'; $page = 'install';
} }
/**
* User
*/
if ($options) {
$user = new wishthis\User();
}
/** /**
* Update * Update
* *

View file

@ -20,4 +20,6 @@ $(function() {
}, },
} }
}); });
$('.ui.dropdown').dropdown();
}); });

View file

@ -100,7 +100,7 @@ class Page
/** /**
* Non-Static * Non-Static
*/ */
public string $language = 'en'; public string $language = DEFAULT_LOCALE;
public array $messages = array(); public array $messages = array();
/** /**

View file

@ -23,6 +23,8 @@ class User
/** /**
* Non-Static * Non-Static
*/ */
public int $power = 0;
public function __construct(int $id = -1) public function __construct(int $id = -1)
{ {
if (-1 === $id) { if (-1 === $id) {
@ -33,6 +35,8 @@ class User
$this->id = $id; $this->id = $id;
} }
$this->locale = \Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (!isset($this->id)) { if (!isset($this->id)) {
return null; return null;
} }

View file

@ -129,11 +129,12 @@ switch ($step) {
`id` INT PRIMARY KEY AUTO_INCREMENT, `id` INT PRIMARY KEY AUTO_INCREMENT,
`email` VARCHAR(64) NOT NULL UNIQUE, `email` VARCHAR(64) NOT NULL UNIQUE,
`password` VARCHAR(128) NOT NULL, `password` VARCHAR(128) NOT NULL,
`password_reset_token` VARCHAR(128) NULL DEFAULT NULL, `password_reset_token` VARCHAR(128) NULL DEFAULT NULL,
`password_reset_valid_until` DATETIME NOT NULL DEFAULT NOW(), `password_reset_valid_until` DATETIME NOT NULL DEFAULT NOW(),
`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 . '"
);'); );');
$database->query('CREATE INDEX `idx_password` ON `users` (`password`);'); $database->query('CREATE INDEX `idx_password` ON `users` (`password`);');

View file

@ -31,6 +31,8 @@ if (isset($_POST['user-id'])) {
$set[] = '`birthdate` = NULL'; $set[] = '`birthdate` = NULL';
} }
$set[] = '`locale` = "' . $_POST['user-locale'] . '"';
$database $database
->query('UPDATE `users` ->query('UPDATE `users`
SET ' . implode(',', $set) . ' SET ' . implode(',', $set) . '
@ -43,9 +45,10 @@ if (isset($_POST['user-id'])) {
if ($loginRequired) { if ($loginRequired) {
session_destroy(); session_destroy();
header('Location: /?page=profile');
die();
} }
header('Location: /?page=profile');
die();
} }
$page = new Page(__FILE__, __('Profile')); $page = new Page(__FILE__, __('Profile'));
@ -82,19 +85,33 @@ $page->navigation();
</div> </div>
</div> </div>
<div class="field"> <div class="two fields">
<label><?= __('Birthdate') ?></label> <div class="field">
<label><?= __('Birthdate') ?></label>
<div class="ui calendar"> <div class="ui calendar">
<div class="ui input left icon"> <div class="ui input left icon">
<i class="calendar icon"></i> <i class="calendar icon"></i>
<input type="text" <input type="text"
name="user-birthdate" name="user-birthdate"
placeholder="<?= __('Pick a date') ?>" placeholder="<?= __('Pick a date') ?>"
value="<?= $user->birthdate ?>" value="<?= $user->birthdate ?>"
/> />
</div>
</div> </div>
</div> </div>
<div class="field">
<label><?= __('Language') ?></label>
<select class="ui search dropdown" name="user-locale">
<option value="<?= DEFAULT_LOCALE ?>"><?= \Locale::getDisplayName(DEFAULT_LOCALE, $user->locale) ?></option>
<?php foreach ($locales as $locale) { ?>
<option value="<?= $locale ?>"><?= \Locale::getDisplayName($locale, $user->locale) ?></option>
<?php } ?>
</select>
</div>
</div> </div>
<div class="ui error message"></div> <div class="ui error message"></div>