From 4d5bdbd5b89f2753caec9252dac5d2c282a00937 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 6 Mar 2020 23:10:46 +0100 Subject: [PATCH] Started implementing Orange-Management/Orange-Management#222 read is already working --- Admin/Install/db.json | 37 +++++++- Controller/ApiController.php | 28 +++++- Models/L11nTag.php | 172 +++++++++++++++++++++++++++++++++++ Models/L11nTagMapper.php | 73 +++++++++++++++ Models/TagMapper.php | 17 +++- 5 files changed, 320 insertions(+), 7 deletions(-) create mode 100644 Models/L11nTag.php create mode 100644 Models/L11nTagMapper.php diff --git a/Admin/Install/db.json b/Admin/Install/db.json index 27587c5..e7ef5f6 100644 --- a/Admin/Install/db.json +++ b/Admin/Install/db.json @@ -9,11 +9,6 @@ "primary": true, "autoincrement": true }, - "tag_title": { - "name": "tag_title", - "type": "VARCHAR(255)", - "null": false - }, "tag_color": { "name": "tag_color", "type": "VARCHAR(9)", @@ -33,5 +28,37 @@ "foreignKey": "account_id" } } + }, + "tag_l11n": { + "name": "tag_l11n", + "fields": { + "tag_l11n_id": { + "name": "tag_l11n_id", + "type": "INT", + "null": false, + "primary": true, + "autoincrement": true + }, + "tag_l11n_title": { + "name": "tag_l11n_title", + "type": "VARCHAR(255)", + "null": false + }, + "tag_l11n_tag": { + "name": "tag_l11n_tag", + "type": "INT", + "null": false, + "foreignTable": "tag", + "foreignKey": "tag_id" + }, + "tag_l11n_language": { + "name": "tag_l11n_language", + "type": "VARCHAR(2)", + "default": null, + "null": true, + "foreignTable": "language", + "foreignKey": "language_639_1" + } + } } } \ No newline at end of file diff --git a/Controller/ApiController.php b/Controller/ApiController.php index a582f4e..4ae2773 100644 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -14,6 +14,7 @@ declare(strict_types=1); namespace Modules\Tag\Controller; +use Modules\Tag\Models\L11nTag; use Modules\Tag\Models\Tag; use Modules\Tag\Models\TagMapper; @@ -22,6 +23,8 @@ use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\Model\Message\FormValidation; use phpOMS\System\MimeType; +use Modules\Tag\Models\L11nTagMapper; +use phpOMS\Localization\ISO639x1Enum; /** * Tag controller class. @@ -120,6 +123,11 @@ final class ApiController extends Controller $tag = $this->createTagFromRequest($request); $this->createModel($request->getHeader()->getAccount(), $tag, TagMapper::class, 'tag'); + + $request->setData('tag', $tag->getId(), true); + $l11nTag = $this->createL11nTagFromRequest($request); + $this->createModel($request->getHeader()->getAccount(), $l11nTag, L11nTagMapper::class, 'tag_l11n'); + $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Tag', 'Tag successfully created', $tag); } @@ -135,12 +143,30 @@ final class ApiController extends Controller private function createTagFromRequest(RequestAbstract $request) : Tag { $tag = new Tag(); - $tag->setTitle((string) ($request->getData('title') ?? '')); $tag->setColor($request->getData('color') ?? '#00000000'); return $tag; } + /** + * Method to create tag localization from request. + * + * @param RequestAbstract $request Request + * + * @return L11nTag + * + * @since 1.0.0 + */ + private function createL11nTagFromRequest(RequestAbstract $request) : L11nTag + { + $l11nTag = new L11nTag(); + $l11nTag->setTag((int) ($request->getData('tag') ?? 0)); + $l11nTag->setLanguage((string) ($request->getData('language') ?? ISO639x1Enum::_EN)); + $l11nTag->setTitle((string) ($request->getData('title') ?? '')); + + return $l11nTag; + } + /** * Api method to get a tag * diff --git a/Models/L11nTag.php b/Models/L11nTag.php new file mode 100644 index 0000000..2906f2e --- /dev/null +++ b/Models/L11nTag.php @@ -0,0 +1,172 @@ +id; + } + + /** + * Set tag. + * + * @param int $tag Tag id + * + * @return void + * + * @since 1.0.0 + */ + public function setTag(int $tag) : void + { + $this->tag = $tag; + } + + /** + * Get tag + * + * @return int + * + * @since 1.0.0 + */ + public function getTag() : int + { + return $this->tag; + } + + /** + * Get language + * + * @return string + * + * @since 1.0.0 + */ + public function getLanguage() : string + { + return $this->title; + } + + /** + * Set language + * + * @param string $language Language + * + * @return void + * + * @since 1.0.0 + */ + public function setLanguage(string $language) : void + { + $this->language = $language; + } + + /** + * Get tag title. + * + * @return string + * + * @since 1.0.0 + */ + public function getTitle() : string + { + return $this->title; + } + + /** + * Set title + * + * @param string $title Title + * + * @return void + * + * @since 1.0.0 + */ + public function setTitle(string $title) : void + { + $this->title = $title; + } + + /** + * {@inheritdoc} + */ + public function toArray() : array + { + return [ + 'id' => $this->id, + 'title' => $this->title, + 'tag' => $this->tag, + 'language' => $this->language, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); + } +} diff --git a/Models/L11nTagMapper.php b/Models/L11nTagMapper.php new file mode 100644 index 0000000..5bf8d9c --- /dev/null +++ b/Models/L11nTagMapper.php @@ -0,0 +1,73 @@ + + * @since 1.0.0 + */ + protected static array $columns = [ + 'tag_l11n_id' => ['name' => 'tag_l11n_id', 'type' => 'int', 'internal' => 'id'], + 'tag_l11n_title' => ['name' => 'tag_l11n_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true], + 'tag_l11n_tag' => ['name' => 'tag_l11n_tag', 'type' => 'int', 'internal' => 'tag'], + 'tag_l11n_language' => ['name' => 'tag_l11n_language', 'type' => 'string', 'internal' => 'language'], + ]; + + /** + * Has one relation. + * + * @var array + * @since 1.0.0 + */ + protected static array $ownsOne = [ + 'language' => [ + 'mapper' => LanguageMapper::class, + 'self' => 'tag_l11n_language', + 'by' => 'code2', + 'column' => 'code2', + ], + ]; + + /** + * Primary table. + * + * @var string + * @since 1.0.0 + */ + protected static string $table = 'tag_l11n'; + + /** + * Primary field name. + * + * @var string + * @since 1.0.0 + */ + protected static string $primaryField = 'tag_l11n_id'; +} diff --git a/Models/TagMapper.php b/Models/TagMapper.php index 0a238fa..105b5d9 100644 --- a/Models/TagMapper.php +++ b/Models/TagMapper.php @@ -35,12 +35,27 @@ final class TagMapper extends DataMapperAbstract */ protected static array $columns = [ 'tag_id' => ['name' => 'tag_id', 'type' => 'int', 'internal' => 'id'], - 'tag_title' => ['name' => 'tag_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true], 'tag_color' => ['name' => 'tag_color', 'type' => 'string', 'internal' => 'color'], 'tag_type' => ['name' => 'tag_type', 'type' => 'int', 'internal' => 'type'], 'tag_owner' => ['name' => 'tag_owner', 'type' => 'int', 'internal' => 'owner'], ]; + /** + * Has many relation. + * + * @var array + * @since 1.0.0 + */ + protected static array $hasMany = [ + 'title' => [ + 'mapper' => L11nTagMapper::class, + 'table' => 'tag_l11n', + 'external' => 'tag_l11n_tag', + 'column' => 'title', + 'self' => null, + ], + ]; + /** * Belongs to. *