-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added language model and added select to user views.
The language is now used to select the language all labels will be translated to.
- Loading branch information
1 parent
663bc40
commit c8ea6db
Showing
15 changed files
with
202 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
framework/Database/Migrations/2024_02_18_193512_create_languages_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;' | ||
); | ||
} | ||
}; |
20 changes: 20 additions & 0 deletions
20
framework/Database/Migrations/2024_02_18_193712_add_language_to_user_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
framework/Database/Migrations/2024_02_18_194736_seed_languages.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters