Skip to content

Commit

Permalink
Added language model and added select to user views.
Browse files Browse the repository at this point in the history
The language is now used to select the language all labels will be translated to.
  • Loading branch information
MasterZydra committed Feb 18, 2024
1 parent 663bc40 commit c8ea6db
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function store(): void
->setPassword(Http::param('password'))
->setIsLocked(Http::param('isLocked'))
->setIsPwdChangeForced(Http::param('isPwdChangeForced'))
->setLanguageId(Http::param('language') === '' ? null : Http::param('language'))
->save();

$user = User::findByUsername(Http::param('username'));
Expand Down Expand Up @@ -87,7 +88,8 @@ public function update(): void
->setLastname(Http::param('lastname'))
->setUsername(Http::param('username'))
->setIsLocked(Http::param('isLocked'))
->setIsPwdChangeForced(Http::param('isPwdChangeForced'));
->setIsPwdChangeForced(Http::param('isPwdChangeForced'))
->setLanguageId(Http::param('language') === '' ? null : Http::param('language'));

if (Http::param('password', '') !== '') {
$user->setPassword(Http::param('password'));
Expand Down
61 changes: 61 additions & 0 deletions app/Models/Language.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Models;

use Framework\Database\BaseModel;
use Framework\Database\Database;

class Language extends BaseModel
{
private const CODE = 'code';
private const NAME = 'name';

protected static function new(array $data = []): self
{
return new self($data);
}

public function save(): self
{
if ($this->getId() === null) {
Database::prepared(
'INSERT INTO ' . $this->getTableName() . ' (code, `name`) VALUES (?, ?)',
'ss',
$this->getCode(),
$this->getName()
);
} else {
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET code=?, `name`=? WHERE id=?',
'ssi',
$this->getCode(),
$this->getName(),
$this->getId()
);
}

return $this;
}

/* Getter & Setter */

public function getCode(): string
{
return $this->getDataString(self::CODE);
}

public function setCode(string $value): self
{
return $this->setDataString(self::CODE, $value);
}

public function getName(): string
{
return $this->getDataString(self::NAME);
}

public function setName(string $value): self
{
return $this->setDataString(self::NAME, $value);
}
}
28 changes: 23 additions & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class User extends BaseModel
private const PASSWORD = 'password';
private const IS_LOCKED = 'isLocked';
private const IS_PWD_CHANGE_FORCED = 'isPwdChangeForced';
private const LANGUAGE_ID = 'languageId';

protected static function new(array $data = []): self
{
Expand All @@ -27,25 +28,27 @@ public function save(): self
{
if ($this->getId() === null) {
Database::prepared(
'INSERT INTO ' . $this->getTableName() . ' (firstname, lastname, username, password, isLocked, isPwdChangeForced) VALUES (?, ?, ?, ?, ?, ?)',
'ssssii',
'INSERT INTO ' . $this->getTableName() . ' (firstname, lastname, username, password, isLocked, isPwdChangeForced, languageId) VALUES (?, ?, ?, ?, ?, ?, ?)',
'ssssiii',
$this->getFirstname(),
$this->getLastname(),
$this->getUsername(),
$this->getPassword(),
Convert::boolToInt($this->getIsLocked()),
Convert::boolToInt($this->getIsPwdChangeForced())
Convert::boolToInt($this->getIsPwdChangeForced()),
$this->getLanguageId()
);
} else {
Database::prepared(
'UPDATE ' . $this->getTableName() . ' SET firstname=?, lastname=?, username=?, password=?, isLocked=?, isPwdChangeForced=? WHERE id=?',
'ssssiii',
'UPDATE ' . $this->getTableName() . ' SET firstname=?, lastname=?, username=?, password=?, isLocked=?, isPwdChangeForced=?, languageId=? WHERE id=?',
'ssssiiii',
$this->getFirstname(),
$this->getLastname(),
$this->getUsername(),
$this->getPassword(),
Convert::boolToInt($this->getIsLocked()),
Convert::boolToInt($this->getIsPwdChangeForced()),
$this->getLanguageId(),
$this->getId()
);
}
Expand All @@ -63,6 +66,11 @@ public function canLogIn(): bool
return $this->getId() !== null && !$this->getIsLocked();
}

public function getLanguage(): Language
{
return Language::findById($this->getLanguageId());
}

/* Getter & Setter */

public function getFirstname(): string
Expand Down Expand Up @@ -124,4 +132,14 @@ public function setIsPwdChangeForced(bool $value): self
{
return $this->setDataBool(self::IS_PWD_CHANGE_FORCED, $value);
}

public function getLanguageId(): ?int
{
return $this->getDataIntOrNull(self::LANGUAGE_ID);
}

public function setLanguageId(?int $value): self
{
return $this->setDataIntOrNull(self::LANGUAGE_ID, $value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Framework\Database\Database;
use Framework\Database\Migration;

return new class extends Migration
{
public function run(): void
{
Database::unprepared(
'CREATE TABLE languages (' .
'id INT auto_increment,' .
'code VARCHAR(5) NOT NULL,' .
'name VARCHAR(30) NOT NULL,' .
'createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,' .
'updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,' .
'PRIMARY KEY (id),' .
'UNIQUE KEY `ukLanguageName` (code)' .
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;'
);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Framework\Database\Database;
use Framework\Database\Migration;

return new class extends Migration
{
public function run(): void
{
Database::unprepared(
'ALTER TABLE users ' .
'ADD languageId INT DEFAULT NULL;'
);

Database::unprepared(
'ALTER TABLE users ' .
'ADD CONSTRAINT `fkUserLanguageId` FOREIGN KEY (languageId) REFERENCES languages (id) ON DELETE CASCADE;'
);
}
};
12 changes: 12 additions & 0 deletions framework/Database/Migrations/2024_02_18_194736_seed_languages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Framework\Database\Migration;
use Framework\Database\Seeders\LanguageSeeder;

return new class extends Migration
{
public function run(): void
{
(new LanguageSeeder())->run();
}
};
1 change: 0 additions & 1 deletion framework/Database/Seeders/InvoiceSettingsSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Framework\Database\Seeders;

use App\Models\Setting;
use Framework\Database\Database;
use Framework\Database\Seeder;
use Framework\Database\SeederInterface;

Expand Down
16 changes: 16 additions & 0 deletions framework/Database/Seeders/LanguageSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Framework\Database\Seeders;

use App\Models\Language;
use Framework\Database\Seeder;
use Framework\Database\SeederInterface;

class LanguageSeeder extends Seeder implements SeederInterface
{
public function run(): void
{
(new Language())->setCode('de')->setName('German')->save();
(new Language())->setCode('en')->setName('English')->save();
}
}
16 changes: 16 additions & 0 deletions framework/i18n/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

namespace Framework\i18n;

use App\Models\User;
use Framework\Authentication\Auth;

class Language
{
/** Get the two letter language code from the browser */
public static function getBrowserLanguage(): string
{
return strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
}

/** Get the language from the browser or from the user settings */
public static function getLanguage(): string
{
if (Auth::isLoggedIn()) {
$user = User::findById(Auth::id());
if ($user->getLanguageId() !== null) {
return $user->getLanguage()->getCode();
}
}

return self::getBrowserLanguage();
}
}
3 changes: 1 addition & 2 deletions framework/i18n/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public static function readLabelFiles(string $languageDir = null): void
/** Translate the given label into user language */
public static function translate(string $label): string
{
// TODO get language from session ...
$lang = Language::getBrowserLanguage();
$lang = Language::getLanguage();
if (!array_key_exists($lang, self::$labels)) {
// TODO Fallback language en -> later as setting?
$lang = 'en';
Expand Down
4 changes: 4 additions & 0 deletions resources/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
'EditRecipient' => 'Empfänger bearbeiten',
'EditSetting' => 'Einstellung bearbeiten',
'EditUser' => 'Benutzer bearbeiten',
'English' => 'Englisch',
'Firstname' => 'Vorname',
'ForcePasswordChange' => 'Passwortänderung erzwingen',
'FullPayout' => 'Volle Auszahlung',
'German' => 'Deutsch',
'Home' => 'Startseite',
'Imprint' => 'Impressum',
'InsertSearchText' => 'Suchtext eingeben...',
Expand All @@ -46,6 +48,7 @@
'IsDiscontinued' => 'Ist abgekündigt',
'IsLocked' => 'Ist gesperrt',
'IsPaid' => 'Ist bezahlt',
'Language' => 'Sprache',
'Lastname' => 'Nachname',
'Login' => 'Anmelden',
'Logout' => 'Abmelden',
Expand All @@ -65,6 +68,7 @@
'PasswordIsIncorrect' => 'Passwort ist falsch',
'Payout' => 'Auszahlung',
'Permissions' => 'Berechtigungen',
'PleaseSelect' => 'Bitte auswählen',
'Plot' => 'Flurstück',
'Price' => 'Preis',
'Product' => 'Produkt',
Expand Down
4 changes: 4 additions & 0 deletions resources/Lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
'EditRecipient' => 'Edit recipient',
'EditSetting' => 'Edit setting',
'EditUser' => 'Edit user',
'English' => 'English',
'Firstname' => 'Firstname',
'ForcePasswordChange' => 'Force password change',
'FullPayout' => 'Full payout',
'German' => 'German',
'Home' => 'Home',
'Imprint' => 'Imprint',
'InsertSearchText' => 'Insert search text...',
Expand All @@ -46,6 +48,7 @@
'IsDiscontinued' => 'Is discontinued',
'IsLocked' => 'Is locked',
'IsPaid' => 'Is paid',
'Language' => 'Language',
'Lastname' => 'Lastname',
'Login' => 'Login',
'Logout' => 'Logout',
Expand All @@ -65,6 +68,7 @@
'PasswordIsIncorrect' => 'Password is incorrect',
'Payout' => 'Payout',
'Permissions' => 'Permissions',
'PleaseSelect' => 'Please select',
'Plot' => 'Plot',
'Price' => 'Price',
'Product' => 'Product',
Expand Down
15 changes: 15 additions & 0 deletions resources/Views/Components/languageSelect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
use App\Models\Language;
// Optional passed variables:
// $selected => Id that should be selected
?>
<label for="language"><?= __('Language') ?>:</label><br>
<select id="language" name="language">
<option value=""><?= __('PleaseSelect') ?></option>
<?php /** @var \App\Models\Language $language */
foreach (Language::all() as $language) { ?>
<option value="<?= $language->getId() ?>" <?php
if (isset($selected) && $language->getId() === $selected) { ?>selected<?php } ?>
><?= __($language->getName()) ?></option>
<?php } ?>
</select>
2 changes: 2 additions & 0 deletions resources/Views/entities/user/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<?= __('ForcePasswordChange') ?>
</label><br><br>

<?= component('languageSelect') ?>

<strong><?= __('Permissions') ?></strong><br>

<?php /** @var \App\Models\Role $role */
Expand Down
3 changes: 3 additions & 0 deletions resources/Views/entities/user/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<?= __('ForcePasswordChange') ?>
</label><br><br>

<?= 'lang' . $user->getLanguageId() ?>
<?= component('languageSelect', ['selected' => $user->getLanguageId()]) ?>

<strong><?= __('Permissions') ?></strong><br>

<?php /** @var \App\Models\Role $role */
Expand Down

0 comments on commit c8ea6db

Please sign in to comment.