From ea23ad2deb8c4ed9d8374ead051a1a77822e89d3 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 1 Nov 2020 21:25:09 +0100 Subject: [PATCH] impl. virtual path --- Admin/Install/db.json | 4 ++-- Controller/ApiController.php | 16 ++++++++++++++-- Controller/BackendController.php | 30 +++++++++--------------------- Models/EditorDoc.php | 10 +++++----- Models/EditorDocMapper.php | 23 ++++++++++++++++++++++- 5 files changed, 52 insertions(+), 31 deletions(-) diff --git a/Admin/Install/db.json b/Admin/Install/db.json index 6e5a126..3d2c8da 100755 --- a/Admin/Install/db.json +++ b/Admin/Install/db.json @@ -24,8 +24,8 @@ "type": "TEXT", "null": false }, - "editor_doc_path": { - "name": "editor_doc_path", + "editor_doc_virtual": { + "name": "editor_doc_virtual", "type": "VARCHAR(255)", "null": false }, diff --git a/Controller/ApiController.php b/Controller/ApiController.php index e9eb4a0..80bb113 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -24,6 +24,7 @@ use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\Model\Message\FormValidation; use phpOMS\Utils\Parser\Markdown\Markdown; +use phpOMS\Message\Http\HttpResponse; /** * Calendar controller class. @@ -106,11 +107,22 @@ final class ApiController extends Controller $doc->setTitle((string) ($request->getData('title') ?? '')); $doc->setPlain((string) ($request->getData('plain') ?? '')); $doc->setContent(Markdown::parse((string) ($request->getData('plain') ?? ''))); + $doc->setVirtualPath((string) ($request->getData('virtualpath') ?? '/')); $doc->setCreatedBy(new NullAccount($request->getHeader()->getAccount())); - if (!empty($tags = $request->getDataJson('tag'))) { + if (!empty($tags = $request->getDataJson('tags'))) { foreach ($tags as $tag) { - $doc->addTag(new NullTag((int) $tag)); + if (!isset($tag['id'])) { + $request->setData('title', $tag['title'], true); + $request->setData('color', $tag['color'], true); + $request->setData('language', $tag['language'], true); + + $internalResponse = new HttpResponse(); + $this->app->moduleManager->get('Tag')->apiTagCreate($request, $internalResponse, null); + $doc->addTag($internalResponse->get($request->getUri()->__toString())['response']); + } else { + $doc->addTag(new NullTag((int) $tag['id'])); + } } } diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 09531ac..3704674 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -24,6 +24,8 @@ use phpOMS\Message\Http\RequestStatusCode; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\Views\View; +use Modules\Media\Models\Collection; +use Modules\Admin\Models\Account; /** * Calendar controller class. @@ -100,29 +102,15 @@ final class BackendController extends Controller $view->setTemplate('/Modules/Editor/Theme/Backend/editor-list'); $view->addData('nav', $this->app->moduleManager->get('Navigation')->createNavigationMid(1005301001, $request, $response)); - $path = (string) ($request->getData('path') ?? '/'); - $collection = CollectionMapper::getByVirtualPath(\str_replace('+', ' ', $path)); - $parent = CollectionMapper::getParentCollection(\str_replace('+', ' ', $path)); + $path = \str_replace('+', ' ', (string) ($request->getData('path') ?? '/')); + $docs = EditorDocMapper::withConditional('language', $response->getHeader()->getL11n()->getLanguage())::getByVirtualPath($path, $request->getHeader()->getAccount()); + list($collection, $parent) = CollectionMapper::getCollectionsByPath($path); + + $view->addData('parent', $parent); $view->addData('collections', $collection); $view->addData('path', $path); - - if ($request->getData('ptype') === 'p') { - $view->setData('docs', - EditorDocMapper::withConditional('language', $response->getHeader()->getL11n()->getLanguage()) - ::getBeforePivot((int) ($request->getData('id') ?? 0), null, 25) - ); - } elseif ($request->getData('ptype') === 'n') { - $view->setData('docs', - EditorDocMapper::withConditional('language', $response->getHeader()->getL11n()->getLanguage()) - ::getAfterPivot((int) ($request->getData('id') ?? 0), null, 25) - ); - } else { - $view->setData('docs', - EditorDocMapper::withConditional('language', $response->getHeader()->getL11n()->getLanguage()) - ::getAfterPivot(0, null, 25) - ); - } + $view->addData('docs', $docs); return $view; } @@ -144,7 +132,7 @@ final class BackendController extends Controller $view = new View($this->app->l11nManager, $request, $response); /** @var \Modules\Editor\Models\EditorDoc $doc */ - $doc = EditorDocMapper::get((int) $request->getData('id')); + $doc = EditorDocMapper::withConditional('language', $response->getHeader()->getL11n()->getLanguage())::get((int) $request->getData('id')); $accountId = $request->getHeader()->getAccount(); if ($doc->getCreatedBy()->getId() !== $accountId diff --git a/Models/EditorDoc.php b/Models/EditorDoc.php index 4eff22e..2e8be2a 100755 --- a/Models/EditorDoc.php +++ b/Models/EditorDoc.php @@ -67,7 +67,7 @@ class EditorDoc implements \JsonSerializable, ArrayableInterface * @var string * @since 1.0.0 */ - private string $path = ''; + private string $virtualPath = '/'; /** * Created. @@ -237,9 +237,9 @@ class EditorDoc implements \JsonSerializable, ArrayableInterface * * @since 1.0.0 */ - public function getPath() : string + public function getVirtualPath() : string { - return $this->path; + return $this->virtualPath; } /** @@ -251,9 +251,9 @@ class EditorDoc implements \JsonSerializable, ArrayableInterface * * @since 1.0.0 */ - public function setPath(string $path) + public function setVirtualPath(string $path) { - $this->path = $path; + $this->virtualPath = $path; } /** diff --git a/Models/EditorDocMapper.php b/Models/EditorDocMapper.php index 8762b16..0815402 100755 --- a/Models/EditorDocMapper.php +++ b/Models/EditorDocMapper.php @@ -17,6 +17,7 @@ namespace Modules\Editor\Models; use Modules\Admin\Models\AccountMapper; use Modules\Tag\Models\TagMapper; use phpOMS\DataStorage\Database\DataMapperAbstract; +use phpOMS\DataStorage\Database\RelationType; /** * Editor doc mapper class. @@ -40,7 +41,7 @@ final class EditorDocMapper extends DataMapperAbstract '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_path' => ['name' => 'editor_doc_path', 'type' => 'string', 'internal' => 'path'], + '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], ]; @@ -95,4 +96,24 @@ final class EditorDocMapper extends DataMapperAbstract * @since 1.0.0 */ protected static string $createdAt = 'editor_doc_created_at'; + + /** + * Get editor doc based on virtual path. + * + * @param string $virtualPath Virtual path + * @param int $account Account id + * + * @return array + * + * @since 1.0.0 + */ + public static function getByVirtualPath(string $virtualPath = '/', int $account = 0) : array + { + $depth = 3; + $query = self::getQuery(); + $query->where(self::$table . '_' . $depth . '.editor_doc_virtual', '=', $virtualPath); + $query->where(self::$table . '_' . $depth . '.editor_doc_created_by', '=', $account); + + return self::getAllByQuery($query, RelationType::ALL, $depth); + } }