From 81519fe14456010ba5ae471e5d7afa1e60672d7f Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 7 Jan 2023 19:00:32 +0100 Subject: [PATCH] l11n, unit/app and simplification fixes --- Admin/Install/db.json | 24 ++++ Admin/Installer.php | 11 ++ Controller/ApiController.php | 82 +++++++---- Controller/BackendController.php | 2 +- Models/Media.php | 24 ++++ Models/MediaMapper.php | 3 + Models/MediaType.php | 24 ++-- Models/MediaTypeL11n.php | 132 ------------------ Models/MediaTypeL11nMapper.php | 13 +- Models/NullMediaTypeL11n.php | 47 ------- Theme/Api/spreadsheetAsHtml.tpl.php | 12 +- .../Components/Media/powerpoint.tpl.php | 26 +++- info.json | 3 +- tests/Models/MediaTypeL11nTest.php | 87 ------------ tests/Models/MediaTypeTest.php | 4 +- tests/Models/NullMediaTypeL11nTest.php | 42 ------ 16 files changed, 171 insertions(+), 365 deletions(-) delete mode 100755 Models/MediaTypeL11n.php delete mode 100755 Models/NullMediaTypeL11n.php delete mode 100755 tests/Models/MediaTypeL11nTest.php delete mode 100755 tests/Models/NullMediaTypeL11nTest.php diff --git a/Admin/Install/db.json b/Admin/Install/db.json index 31059fd..f870e60 100755 --- a/Admin/Install/db.json +++ b/Admin/Install/db.json @@ -174,6 +174,30 @@ "foreignTable": "media", "foreignKey": "media_id" }, + "media_unit": { + "name": "media_unit", + "type": "INT", + "default": null, + "null": true, + "foreignTable": "organization_unit", + "foreignKey": "organization_unit_id" + }, + "media_language": { + "name": "media_language", + "type": "VARCHAR(2)", + "null": true, + "default": null, + "foreignTable": "language", + "foreignKey": "language_639_1" + }, + "media_country": { + "name": "media_country", + "type": "VARCHAR(2)", + "null": true, + "default": null, + "foreignTable": "country", + "foreignKey": "country_code2" + }, "media_created_by": { "name": "media_created_by", "type": "INT", diff --git a/Admin/Installer.php b/Admin/Installer.php index 8fcc1e2..8b52c6b 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -210,6 +210,11 @@ final class Installer extends InstallerAbstract $request->header->account = 1; $request->setData('name', $data['name'] ?? ''); + if (!empty($data['l11n'])) { + $request->setData('title', \reset($data['l11n'])['title']); + $request->setData('lang', \reset($data['l11n'])['lang']); + } + $module->apiMediaTypeCreate($request, $response); $responseData = $response->get(''); @@ -220,7 +225,13 @@ final class Installer extends InstallerAbstract $type = $responseData['response']; $id = $type->getId(); + $isFirst = true; foreach ($data['l11n'] as $l11n) { + if ($isFirst) { + $isFirst = false; + continue; + } + $response = new HttpResponse(); $request = new HttpRequest(new HttpUri('')); diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 9ac8764..9f45860 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -22,7 +22,7 @@ use Modules\Media\Models\Media; use Modules\Media\Models\MediaContent; use Modules\Media\Models\MediaMapper; use Modules\Media\Models\MediaType; -use Modules\Media\Models\MediaTypeL11n; +use phpOMS\Localization\BaseStringL11n; use Modules\Media\Models\MediaTypeL11nMapper; use Modules\Media\Models\MediaTypeMapper; use Modules\Media\Models\NullCollection; @@ -52,7 +52,6 @@ use phpOMS\System\File\Local\Directory; use phpOMS\System\MimeType; use phpOMS\Utils\ImageUtils; use phpOMS\Utils\Parser\Markdown\Markdown; -use phpOMS\Utils\Parser\Pdf\PdfParser; use phpOMS\Views\View; /** @@ -81,16 +80,19 @@ final class ApiController extends Controller public function apiMediaUpload(RequestAbstract $request, ResponseAbstract $response, mixed $data = null) : void { $uploads = $this->uploadFiles( - names: $request->getDataList('names'), - fileNames: $request->getDataList('filenames'), - files: $request->getFiles(), - account: $request->header->account, - basePath: __DIR__ . '/../../../Modules/Media/Files' . \urldecode((string) ($request->getData('path') ?? '')), - virtualPath: \urldecode((string) ($request->getData('virtualpath') ?? '')), - type: $request->getData('type', 'int'), - password: (string) ($request->getData('password') ?? ''), - encryptionKey: (string) ($request->getData('encrypt') ?? ''), - pathSettings: (int) ($request->getData('pathsettings') ?? PathSettings::RANDOM_PATH) // IMPORTANT!!! + names: $request->getDataList('names'), + fileNames: $request->getDataList('filenames'), + files: $request->getFiles(), + account: $request->header->account, + basePath: __DIR__ . '/../../../Modules/Media/Files' . \urldecode((string) ($request->getData('path') ?? '')), + virtualPath: \urldecode((string) ($request->getData('virtualpath') ?? '')), + type: $request->getData('type', 'int'), + password: (string) ($request->getData('password') ?? ''), + encryptionKey: (string) ($request->getData('encrypt') ?? ''), + pathSettings: (int) ($request->getData('pathsettings') ?? PathSettings::RANDOM_PATH), // IMPORTANT!!! + hasAccountRelation: (bool) ($request->getData('link_account') ?? false), + readContent: (bool) ($request->getData('parse_content') ?? false), + unit: $request->getData('unit', 'int') ); $ids = []; @@ -166,7 +168,9 @@ final class ApiController extends Controller string $password = '', string $encryptionKey = '', int $pathSettings = PathSettings::RANDOM_PATH, - bool $hasAccountRelation = true + bool $hasAccountRelation = true, + bool $readContent = false, + int $unit = null ) : array { if (empty($files)) { @@ -208,7 +212,9 @@ final class ApiController extends Controller $account, $virtualPath, $type, - app: $hasAccountRelation ? $this->app : null + app: $hasAccountRelation ? $this->app : null, + readContent: $readContent, + unit: $unit ); } @@ -265,6 +271,7 @@ final class ApiController extends Controller * @param null|int $type Media type (internal categorization) * @param string $ip Ip of the origin * @param null|ApplicationAbstract $app Should create relation to uploader + * @param bool $readContent Should the content of the file be stored in the db * * @return Media * @@ -276,7 +283,9 @@ final class ApiController extends Controller string $virtualPath = '', int $type = null, string $ip = '127.0.0.1', - ApplicationAbstract $app = null + ApplicationAbstract $app = null, + bool $readContent = false, + int $unit = null ) : Media { if (!isset($status['status']) || $status['status'] !== UploadStatus::OK) { @@ -291,9 +300,10 @@ final class ApiController extends Controller $media->createdBy = new NullAccount($account); $media->extension = $status['extension']; $media->type = $type === null ? null : new NullMediaType($type); + $media->unit = $unit; $media->setVirtualPath($virtualPath); - if (\is_file($media->getAbsolutePath())) { + if ($readContent && \is_file($media->getAbsolutePath())) { $content = self::loadFileContent($media->getAbsolutePath(), $media->extension); if (!empty($content)) { @@ -335,23 +345,36 @@ final class ApiController extends Controller * * @since 1.0.0 */ - private static function loadFileContent(string $path, string $extension) : string + public static function loadFileContent(string $path, string $extension, string $output = 'html') : string { switch ($extension) { case 'pdf': - return PdfParser::pdf2text($path, __DIR__ . '/../../../Tools/OCRImageOptimizer/bin/App'); + return \phpOMS\Utils\Parser\Pdf\PdfParser::pdf2text($path/*, __DIR__ . '/../../../Tools/OCRImageOptimizer/bin/OCRImageOptimizerApp'*/); case 'doc': case 'docx': - Autoloader::addPath(__DIR__ . '/../../../Resources/'); + if (!Autoloader::inPaths($include = \realpath(__DIR__ . '/../../../Resources/'))) { + Autoloader::addPath($include); + } - $reader = IOFactory::createReader('Word2007'); - $doc = $reader->load($path); + return \phpOMS\Utils\Parser\Document\DocumentParser::parseDocument($path, $output); + case 'ppt': + case 'pptx': + if (!Autoloader::inPaths($include = \realpath(__DIR__ . '/../../../Resources/'))) { + Autoloader::addPath($include); + } - $writer = new HTML($doc); - return $writer->getContent(); + return \phpOMS\Utils\Parser\Presentation\PresentationParser::parsePresentation($path, $output); + case 'xls': + case 'xlsx': + if (!Autoloader::inPaths($include = \realpath(__DIR__ . '/../../../Resources/'))) { + Autoloader::addPath($include); + } + + return \phpOMS\Utils\Parser\Spreadsheet\SpreadsheetParser::parseSpreadsheet($path, $output); case 'txt': case 'md': $contents = \file_get_contents($path); + return $contents === false ? '' : $contents; default: return ''; @@ -710,7 +733,8 @@ final class ApiController extends Controller $virtualPath, $request->getData('type', 'int'), $request->getOrigin(), - $this->app + $this->app, + unit: $request->getData('unit', 'int') ); $ids[] = $created->getId(); @@ -1015,15 +1039,15 @@ final class ApiController extends Controller * * @param RequestAbstract $request Request * - * @return MediaTypeL11n + * @return BaseStringL11n * * @since 1.0.0 */ - private function createMediaTypeL11nFromRequest(RequestAbstract $request) : MediaTypeL11n + private function createMediaTypeL11nFromRequest(RequestAbstract $request) : BaseStringL11n { - $l11nMediaType = new MediaTypeL11n(); - $l11nMediaType->type = (int) ($request->getData('type') ?? 0); - $l11nMediaType->title = (string) ($request->getData('title') ?? ''); + $l11nMediaType = new BaseStringL11n(); + $l11nMediaType->ref = (int) ($request->getData('type') ?? 0); + $l11nMediaType->content = (string) ($request->getData('title') ?? ''); $l11nMediaType->setLanguage((string) ( $request->getData('language') ?? $request->getLanguage() )); diff --git a/Controller/BackendController.php b/Controller/BackendController.php index e91fb38..0b644f1 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -406,7 +406,7 @@ final class BackendController extends Controller $view = new View($this->app->l11nManager, $request, $response); $view->setTemplate('/Modules/' . static::NAME . '/Admin/Settings/Theme/Backend/settings-type'); - /** @var \Modules\Media\Models\MediaType $type */ + /** @var \phpOMS\Localization\BaseStringL11n $type */ $type = MediaTypeMapper::get() ->with('title') ->where('title/language', $response->getLanguage()) diff --git a/Models/Media.php b/Models/Media.php index e955e63..5fe396c 100755 --- a/Models/Media.php +++ b/Models/Media.php @@ -181,6 +181,14 @@ class Media implements \JsonSerializable */ public int $class = MediaClass::FILE; + /** + * Unit + * + * @var null|int + * @since 1.0.0 + */ + public ?int $unit = null; + /** * Tags. * @@ -189,6 +197,22 @@ class Media implements \JsonSerializable */ protected array $tags = []; + /** + * Language. + * + * @var null|string + * @since 1.0.0 + */ + protected ?string $language = null; + + /** + * Country. + * + * @var null|string + * @since 1.0.0 + */ + protected ?string $country = null; + /** * Constructor. * diff --git a/Models/MediaMapper.php b/Models/MediaMapper.php index b2ae592..663d32d 100755 --- a/Models/MediaMapper.php +++ b/Models/MediaMapper.php @@ -53,6 +53,9 @@ class MediaMapper extends DataMapperFactory 'media_size' => ['name' => 'media_size', 'type' => 'int', 'internal' => 'size'], 'media_source' => ['name' => 'media_source', 'type' => 'int', 'internal' => 'source'], 'media_class' => ['name' => 'media_class', 'type' => 'int', 'internal' => 'class'], + 'media_language' => ['name' => 'media_language', 'type' => 'string', 'internal' => 'language'], + 'media_country' => ['name' => 'media_country', 'type' => 'string', 'internal' => 'country'], + 'media_unit' => ['name' => 'media_unit', 'type' => 'int', 'internal' => 'unit', 'readonly' => true], 'media_created_by' => ['name' => 'media_created_by', 'type' => 'int', 'internal' => 'createdBy', 'readonly' => true], 'media_created_at' => ['name' => 'media_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], ]; diff --git a/Models/MediaType.php b/Models/MediaType.php index cfe57fd..9883697 100755 --- a/Models/MediaType.php +++ b/Models/MediaType.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Modules\Media\Models; use phpOMS\Localization\ISO639x1Enum; +use phpOMS\Localization\BaseStringL11n; /** * Media type class. @@ -55,10 +56,10 @@ class MediaType implements \JsonSerializable /** * Title. * - * @var string|MediaTypeL11n + * @var string|BaseStringL11n * @since 1.0.0 */ - protected $title = ''; + protected string | BaseStringL11n $title = ''; /** * Constructor. @@ -91,28 +92,29 @@ class MediaType implements \JsonSerializable */ public function getL11n() : string { - return $this->title instanceof MediaTypeL11n ? $this->title->title : $this->title; + return $this->title instanceof BaseStringL11n ? $this->title->content : $this->title; } /** * Set title * - * @param string|MediaTypeL11n $title Media article title - * @param string $lang Language + * @param string|BaseStringL11n $title Media article title + * @param string $lang Language * * @return void * * @since 1.0.0 */ - public function setL11n(string | MediaTypeL11n $title, string $lang = ISO639x1Enum::_EN) : void + public function setL11n(string | BaseStringL11n $title, string $lang = ISO639x1Enum::_EN) : void { - if ($title instanceof MediaTypeL11n) { + if ($title instanceof BaseStringL11n) { $this->title = $title; - } elseif ($this->title instanceof MediaTypeL11n) { - $this->title->title = $title; + } elseif ($this->title instanceof BaseStringL11n) { + $this->title->content = $title; } else { - $this->title = new MediaTypeL11n(); - $this->title->title = $title; + $this->title = new BaseStringL11n(); + $this->title->ref = $this->id; + $this->title->content = $title; $this->title->setLanguage($lang); } } diff --git a/Models/MediaTypeL11n.php b/Models/MediaTypeL11n.php deleted file mode 100755 index 4b608d0..0000000 --- a/Models/MediaTypeL11n.php +++ /dev/null @@ -1,132 +0,0 @@ -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() : mixed - { - return $this->toArray(); - } -} diff --git a/Models/MediaTypeL11nMapper.php b/Models/MediaTypeL11nMapper.php index b7f4083..5b68c01 100755 --- a/Models/MediaTypeL11nMapper.php +++ b/Models/MediaTypeL11nMapper.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Modules\Media\Models; use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; +use phpOMS\Localization\BaseStringL11n; /** * Media type l11n mapper class. @@ -34,8 +35,8 @@ final class MediaTypeL11nMapper extends DataMapperFactory */ public const COLUMNS = [ 'media_type_l11n_id' => ['name' => 'media_type_l11n_id', 'type' => 'int', 'internal' => 'id'], - 'media_type_l11n_title' => ['name' => 'media_type_l11n_title', 'type' => 'string', 'internal' => 'title', 'autocomplete' => true], - 'media_type_l11n_type' => ['name' => 'media_type_l11n_type', 'type' => 'int', 'internal' => 'type'], + 'media_type_l11n_title' => ['name' => 'media_type_l11n_title', 'type' => 'string', 'internal' => 'content', 'autocomplete' => true], + 'media_type_l11n_type' => ['name' => 'media_type_l11n_type', 'type' => 'int', 'internal' => 'ref'], 'media_type_l11n_language' => ['name' => 'media_type_l11n_language', 'type' => 'string', 'internal' => 'language'], ]; @@ -54,4 +55,12 @@ final class MediaTypeL11nMapper extends DataMapperFactory * @since 1.0.0 */ public const PRIMARYFIELD ='media_type_l11n_id'; + + /** + * Model to use by the mapper. + * + * @var string + * @since 1.0.0 + */ + public const MODEL = BaseStringL11n::class; } diff --git a/Models/NullMediaTypeL11n.php b/Models/NullMediaTypeL11n.php deleted file mode 100755 index 6d5601d..0000000 --- a/Models/NullMediaTypeL11n.php +++ /dev/null @@ -1,47 +0,0 @@ -id = $id; - parent::__construct(); - } - - /** - * {@inheritdoc} - */ - public function jsonSerialize() : mixed - { - return ['id' => $this->id]; - } -} diff --git a/Theme/Api/spreadsheetAsHtml.tpl.php b/Theme/Api/spreadsheetAsHtml.tpl.php index f88ae13..957a68b 100755 --- a/Theme/Api/spreadsheetAsHtml.tpl.php +++ b/Theme/Api/spreadsheetAsHtml.tpl.php @@ -15,18 +15,10 @@ declare(strict_types=1); use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Writer\Html; use phpOMS\Autoloader; +use phpOMS\Utils\Parser\Spreadsheet\SpreadsheetParser; Autoloader::addPath(__DIR__ . '/../../../../Resources/'); $media = $this->getData('media'); -$reader = IOFactory::createReaderforFile(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath()); -$reader->setReadDataOnly(true); -$spreadsheet = $reader->load(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath()); - -$writer = new Html($spreadsheet); -$writer->writeAllSheets(); - -$writer->save('php://output'); - -echo ''; +echo SpreadsheetParser::parseSpreadsheet(($media->isAbsolute ? '' : __DIR__ . '/../../../../') . $media->getPath(), 'html'); diff --git a/Theme/Backend/Components/Media/powerpoint.tpl.php b/Theme/Backend/Components/Media/powerpoint.tpl.php index 4c6effd..4ca8bb9 100755 --- a/Theme/Backend/Components/Media/powerpoint.tpl.php +++ b/Theme/Backend/Components/Media/powerpoint.tpl.php @@ -12,7 +12,31 @@ */ declare(strict_types=1); -require_once __DIR__ . '/../../../../../../phpOMS/Autoloader.php'; use phpOMS\Autoloader; +use phpOMS\Uri\UriFactory; +use phpOMS\Utils\Parser\Presentation\PresentationParser; Autoloader::addPath(__DIR__ . '/../../../../../../Resources/'); +?> +
+
+
+ +
+ +
+ +
+ +
+ media->getPath(), 'html'); + ?> +
+
+
+
+
\ No newline at end of file diff --git a/info.json b/info.json index 8862d00..4af0564 100755 --- a/info.json +++ b/info.json @@ -14,10 +14,11 @@ "name": "Karaka", "website": "jingga.app" }, - "description": "The profile module.", + "description": "The media module.", "directory": "Media", "dependencies": { "Admin": "1.0.0", + "Organization": "1.0.0", "Home": "1.0.0", "Tag": "1.0.0" }, diff --git a/tests/Models/MediaTypeL11nTest.php b/tests/Models/MediaTypeL11nTest.php deleted file mode 100755 index 2d99ef8..0000000 --- a/tests/Models/MediaTypeL11nTest.php +++ /dev/null @@ -1,87 +0,0 @@ -l11n = new MediaTypeL11n(); - } - - /** - * @covers Modules\Media\Models\MediaTypeL11n - * @group module - */ - public function testDefault() : void - { - self::assertEquals(0, $this->l11n->getId()); - self::assertEquals('', $this->l11n->title); - self::assertEquals(0, $this->l11n->type); - self::assertEquals(ISO639x1Enum::_EN, $this->l11n->getLanguage()); - } - - /** - * @covers Modules\Media\Models\MediaTypeL11n - * @group module - */ - public function testNameInputOutput() : void - { - $this->l11n->title = 'TestName'; - self::assertEquals('TestName', $this->l11n->title); - } - - /** - * @covers Modules\Media\Models\MediaTypeL11n - * @group module - */ - public function testLanguageInputOutput() : void - { - $this->l11n->setLanguage(ISO639x1Enum::_DE); - self::assertEquals(ISO639x1Enum::_DE, $this->l11n->getLanguage()); - } - - /** - * @covers Modules\Media\Models\MediaTypeL11n - * @group module - */ - public function testSerialize() : void - { - $this->l11n->title = 'Title'; - $this->l11n->type = 2; - $this->l11n->setLanguage(ISO639x1Enum::_DE); - - self::assertEquals( - [ - 'id' => 0, - 'title' => 'Title', - 'type' => 2, - 'language' => ISO639x1Enum::_DE, - ], - $this->l11n->jsonSerialize() - ); - } -} diff --git a/tests/Models/MediaTypeTest.php b/tests/Models/MediaTypeTest.php index bc1149e..0bb33a3 100755 --- a/tests/Models/MediaTypeTest.php +++ b/tests/Models/MediaTypeTest.php @@ -15,7 +15,7 @@ declare(strict_types=1); namespace Modules\Media\tests\Models; use Modules\Media\Models\MediaType; -use Modules\Media\Models\MediaTypeL11n; +use phpOMS\Localization\BaseStringL11n; /** * @internal @@ -51,7 +51,7 @@ final class MediaTypeTest extends \PHPUnit\Framework\TestCase $this->type->setL11n('Test1'); self::assertEquals('Test1', $this->type->getL11n()); - $this->type->setL11n(new MediaTypeL11n('Test2')); + $this->type->setL11n(new BaseStringL11n('Test2')); self::assertEquals('Test2', $this->type->getL11n()); } diff --git a/tests/Models/NullMediaTypeL11nTest.php b/tests/Models/NullMediaTypeL11nTest.php deleted file mode 100755 index c47602e..0000000 --- a/tests/Models/NullMediaTypeL11nTest.php +++ /dev/null @@ -1,42 +0,0 @@ -getId()); - } -}