diff --git a/Admin/Install/Types/types.php b/Admin/Install/Types/types.php new file mode 100644 index 0000000..6290700 --- /dev/null +++ b/Admin/Install/Types/types.php @@ -0,0 +1,43 @@ + 'phone', + 'l11n' => [ + [ + 'lang' => 'en', + 'title' => 'Phone', + ], + ], + ], + [ + 'name' => 'email', + 'l11n' => [ + [ + 'lang' => 'en', + 'title' => 'Email', + ], + ], + ], + [ + 'name' => 'meeting', + 'l11n' => [ + [ + 'lang' => 'en', + 'title' => 'Meeting', + ], + ], + ], +]; diff --git a/Admin/Install/db.json b/Admin/Install/db.json index ca64b9f..1368770 100755 --- a/Admin/Install/db.json +++ b/Admin/Install/db.json @@ -1,4 +1,53 @@ { + "editor_doc_type": { + "name": "editor_doc_type", + "fields": { + "editor_doc_type_id": { + "name": "editor_doc_type_id", + "type": "INT", + "null": false, + "primary": true, + "autoincrement": true + }, + "editor_doc_type_name": { + "name": "editor_doc_type_name", + "type": "VARCHAR(255)", + "null": false + } + } + }, + "editor_doc_type_l11n": { + "name": "editor_doc_type_l11n", + "fields": { + "editor_doc_type_l11n_id": { + "name": "editor_doc_type_l11n_id", + "type": "INT", + "null": false, + "primary": true, + "autoincrement": true + }, + "editor_doc_type_l11n_title": { + "name": "editor_doc_type_l11n_title", + "type": "VARCHAR(255)", + "null": false + }, + "editor_doc_type_l11n_type": { + "name": "editor_doc_type_l11n_type", + "type": "INT", + "null": false, + "foreignTable": "editor_doc_type", + "foreignKey": "editor_doc_type_id" + }, + "editor_doc_type_l11n_language": { + "name": "editor_doc_type_l11n_language", + "type": "VARCHAR(2)", + "default": null, + "null": true, + "foreignTable": "language", + "foreignKey": "language_639_1" + } + } + }, "editor_doc": { "name": "editor_doc", "fields": { diff --git a/Admin/Installer.php b/Admin/Installer.php index c4f38e8..1d4dbb6 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -14,7 +14,16 @@ declare(strict_types=1); namespace Modules\Editor\Admin; +use Modules\Editor\Models\EditorDocType; +use Modules\Editor\Models\EditorDocTypeL11n; +use Modules\Editor\Models\EditorDocTypeL11nMapper; +use Modules\Editor\Models\EditorDocTypeMapper; +use phpOMS\Application\ApplicationAbstract; +use phpOMS\Config\SettingsInterface; +use phpOMS\DataStorage\Database\DatabasePool; use phpOMS\Module\InstallerAbstract; +use phpOMS\Module\ModuleInfo; +use phpOMS\System\File\PathException; /** * Installer class. @@ -33,4 +42,97 @@ final class Installer extends InstallerAbstract * @since 1.0.0 */ public const PATH = __DIR__; + + /** + * {@inheritdoc} + */ + public static function install(DatabasePool $dbPool, ModuleInfo $info, SettingsInterface $cfgHandler) : void + { + parent::install($dbPool, $info, $cfgHandler); + + $types = include __DIR__ . '/Install/Types/types.php'; + foreach ($types as $type) { + self::createType($dbPool, $type); + } + } + + /** + * Install data from providing modules. + * + * The data can be either directories which should be created or files which should be "uploaded" + * + * @param ApplicationAbstract $app Application + * @param array $data Additional data + * + * @return array + * + * @throws PathException + * @throws \Exception + * + * @since 1.0.0 + */ + public static function installExternal(ApplicationAbstract $app, array $data) : array + { + try { + $app->dbPool->get()->con->query('select 1 from `editor_doc`'); + } catch (\Exception $e) { + return []; // @codeCoverageIgnore + } + + if (!\is_file($data['path'] ?? '')) { + throw new PathException($data['path'] ?? ''); + } + + $editorFile = \file_get_contents($data['path'] ?? ''); + if ($editorFile === false) { + throw new PathException($data['path'] ?? ''); // @codeCoverageIgnore + } + + $editorData = \json_decode($editorFile, true) ?? []; + if ($editorData === false) { + throw new \Exception(); // @codeCoverageIgnore + } + + $result = [ + 'type' => [], + ]; + + foreach ($editorData as $editor) { + switch ($editor['type']) { + case 'type': + $result['type'][] = self::createType($app->dbPool, $editor); + break; + default: + } + } + + return $result; + } + + /** + * Create type. + * + * @param DatabasePool $dbPool Database instance + * @param array $data Type info + * + * @return EditorDocType + * + * @since 1.0.0 + */ + private static function createType(DatabasePool $dbPool, array $data) : EditorDocType + { + $type = new EditorDocType(); + $type->name = $data['name'] ?? ''; + + $id = EditorDocTypeMapper::create($type); + + foreach ($data['l11n'] as $l11n) { + $l11n = new EditorDocTypeL11n($l11n['title'], $l11n['lang']); + $l11n->type = $id; + + EditorDocTypeL11nMapper::create($l11n); + } + + return $type; + } } diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cab9f5e..ad8944e 100755 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,9 +12,7 @@ If you have a good idea for improvement feel free to create a new issue with all ### Issues -Feel free to grab any open issue implement it and create a new pull request. Most issues can be found in the `Project.md` file in the `Docs` repository. - -The issue information can be used to provide additional information such as priority, difficulty and type. For your first issue try to find a issue marked `[d:first]` or `[d:beginner]`. +Feel free to grab any open issue implement it and create a new pull request. Most issues can be found in the code marked with `@todo` or in the [PROJECT.md](https://github.com/Orange-Management/Docs/blob/master/Project/PROJECT.md) file. ### Code Style diff --git a/Models/EditorDoc.php b/Models/EditorDoc.php index dc4bbbd..2970745 100755 --- a/Models/EditorDoc.php +++ b/Models/EditorDoc.php @@ -62,6 +62,14 @@ class EditorDoc implements \JsonSerializable, ArrayableInterface */ public string $plain = ''; + /** + * Type. + * + * @var null|int|EditorDoc + * @since 1.0.0 + */ + public null | int | EditorDocType $type = null; + /** * Doc path for organizing. * diff --git a/Models/EditorDocMapper.php b/Models/EditorDocMapper.php index f849d80..32285d8 100755 --- a/Models/EditorDocMapper.php +++ b/Models/EditorDocMapper.php @@ -37,14 +37,15 @@ final class EditorDocMapper extends DataMapperAbstract * @since 1.0.0 */ protected static array $columns = [ - 'editor_doc_id' => ['name' => 'editor_doc_id', 'type' => 'int', 'internal' => 'id'], - 'editor_doc_created_by' => ['name' => 'editor_doc_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], - 'editor_doc_title' => ['name' => 'editor_doc_title', 'type' => 'string', 'internal' => 'title'], - 'editor_doc_plain' => ['name' => 'editor_doc_plain', 'type' => 'string', 'internal' => 'plain'], - 'editor_doc_content' => ['name' => 'editor_doc_content', 'type' => 'string', 'internal' => 'content'], - 'editor_doc_virtual' => ['name' => 'editor_doc_virtual', 'type' => 'string', 'internal' => 'virtualPath'], - 'editor_doc_created_at' => ['name' => 'editor_doc_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], - 'editor_doc_visible' => ['name' => 'editor_doc_visible', 'type' => 'bool', 'internal' => 'isVisible'], + 'editor_doc_id' => ['name' => 'editor_doc_id', 'type' => 'int', 'internal' => 'id'], + 'editor_doc_created_by' => ['name' => 'editor_doc_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], + 'editor_doc_title' => ['name' => 'editor_doc_title', 'type' => 'string', 'internal' => 'title'], + 'editor_doc_plain' => ['name' => 'editor_doc_plain', 'type' => 'string', 'internal' => 'plain'], + 'editor_doc_content' => ['name' => 'editor_doc_content', 'type' => 'string', 'internal' => 'content'], + 'editor_doc_type' => ['name' => 'editor_doc_type', 'type' => 'int', 'internal' => 'type'], + 'editor_doc_virtual' => ['name' => 'editor_doc_virtual', 'type' => 'string', 'internal' => 'virtualPath'], + 'editor_doc_created_at' => ['name' => 'editor_doc_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], + 'editor_doc_visible' => ['name' => 'editor_doc_visible', 'type' => 'bool', 'internal' => 'isVisible'], ]; /** @@ -60,6 +61,19 @@ final class EditorDocMapper extends DataMapperAbstract ], ]; + /** + * Belongs to. + * + * @var array + * @since 1.0.0 + */ + protected static array $ownsOne = [ + 'type' => [ + 'mapper' => EdutirDocTypeMapper::class, + 'external' => 'editor_doc_type', + ], + ]; + /** * Has many relation. * diff --git a/Models/EditorDocType.php b/Models/EditorDocType.php new file mode 100644 index 0000000..14ecc0b --- /dev/null +++ b/Models/EditorDocType.php @@ -0,0 +1,132 @@ +setL11n($name); + } + + /** + * Get id + * + * @return int + * + * @since 1.0.0 + */ + public function getId() : int + { + return $this->id; + } + + /** + * @return string + * + * @since 1.0.0 + */ + public function getL11n() : string + { + return $this->title instanceof EditorDocTypeL11n ? $this->title->title : $this->title; + } + + /** + * Set title + * + * @param string|EditorDocTypeL11n $title EditorDoc article title + * @param string $lang Language + * + * @return void + * + * @since 1.0.0 + */ + public function setL11n(string | EditorDocTypeL11n $title, string $lang = ISO639x1Enum::_EN) : void + { + if ($title instanceof EditorDocTypeL11n) { + $this->title = $title; + } elseif (isset($this->title) && $this->title instanceof EditorDocTypeL11n) { + $this->title->title = $title; + } else { + $this->title = new EditorDocTypeL11n(); + $this->title->title = $title; + $this->title->setLanguage($lang); + } + } + + /** + * {@inheritdoc} + */ + public function toArray() : array + { + return [ + 'id' => $this->id, + 'title' => $this->title, + 'name' => $this->name, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); + } +} diff --git a/Models/EditorDocTypeL11n.php b/Models/EditorDocTypeL11n.php new file mode 100644 index 0000000..10680cb --- /dev/null +++ b/Models/EditorDocTypeL11n.php @@ -0,0 +1,133 @@ +title = $title; + $this->language = $language; + } + + /** + * Get id + * + * @return int + * + * @since 1.0.0 + */ + public function getId() : int + { + return $this->id; + } + + /** + * Get language + * + * @return string + * + * @since 1.0.0 + */ + public function getLanguage() : string + { + return $this->language; + } + + /** + * Set language + * + * @param string $language Language + * + * @return void + * + * @since 1.0.0 + */ + public function setLanguage(string $language) : void + { + $this->language = $language; + } + + /** + * {@inheritdoc} + */ + public function toArray() : array + { + return [ + 'id' => $this->id, + 'title' => $this->title, + 'type' => $this->type, + 'language' => $this->language, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); + } +} diff --git a/Models/EditorDocTypeL11nMapper.php b/Models/EditorDocTypeL11nMapper.php new file mode 100644 index 0000000..4b99ad5 --- /dev/null +++ b/Models/EditorDocTypeL11nMapper.php @@ -0,0 +1,57 @@ + + * @since 1.0.0 + */ + protected static array $columns = [ + 'editor_doc_type_l11n_id' => ['name' => 'editor_doc_type_l11n_id', 'type' => 'int', 'internal' => 'id'], + 'editor_doc_type_l11n_title' => ['name' => 'editor_doc_type_l11n_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true], + 'editor_doc_type_l11n_type' => ['name' => 'editor_doc_type_l11n_type', 'type' => 'int', 'internal' => 'type'], + 'editor_doc_type_l11n_language' => ['name' => 'editor_doc_type_l11n_language', 'type' => 'string', 'internal' => 'language'], + ]; + + /** + * Primary table. + * + * @var string + * @since 1.0.0 + */ + protected static string $table = 'editor_doc_type_l11n'; + + /** + * Primary field name. + * + * @var string + * @since 1.0.0 + */ + protected static string $primaryField = 'editor_doc_type_l11n_id'; +} diff --git a/Models/EditorDocTypeMapper.php b/Models/EditorDocTypeMapper.php new file mode 100644 index 0000000..33ed270 --- /dev/null +++ b/Models/EditorDocTypeMapper.php @@ -0,0 +1,80 @@ + + * @since 1.0.0 + */ + protected static array $columns = [ + 'editor_doc_type_id' => ['name' => 'editor_doc_type_id', 'type' => 'int', 'internal' => 'id'], + 'editor_doc_type_name' => ['name' => 'editor_doc_type_name', 'type' => 'string', 'internal' => 'name'], + ]; + + /** + * Has many relation. + * + * @var array + * @since 1.0.0 + */ + protected static array $hasMany = [ + 'title' => [ + 'mapper' => EditorDocTypeL11nMapper::class, + 'table' => 'editor_doc_type_l11n', + 'self' => 'editor_doc_type_l11n_type', + 'column' => 'title', + 'conditional' => true, + 'external' => null, + ], + ]; + + /** + * Model to use by the mapper. + * + * @var string + * @since 1.0.0 + */ + protected static string $model = EditorDocType::class; + + /** + * Primary table. + * + * @var string + * @since 1.0.0 + */ + protected static string $table = 'editor_doc_type'; + + /** + * Primary field name. + * + * @var string + * @since 1.0.0 + */ + protected static string $primaryField = 'editor_doc_type_id'; +} diff --git a/tests/Models/EditorDocMapperTest.php b/tests/Models/EditorDocMapperTest.php index 114b431..3b2cbc4 100755 --- a/tests/Models/EditorDocMapperTest.php +++ b/tests/Models/EditorDocMapperTest.php @@ -17,7 +17,6 @@ namespace Modules\Editor\tests\Models; use Modules\Admin\Models\NullAccount; use Modules\Editor\Models\EditorDoc; use Modules\Editor\Models\EditorDocMapper; -use phpOMS\Utils\RnG\Text; /** * @internal