From 854cad4c1d9ad5c37eebde602a49d7ae3671af60 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 4 Feb 2024 20:34:13 +0000 Subject: [PATCH] update --- Admin/Search.php | 43 ++++++++ Admin/SearchCommands.php | 32 ++++++ Controller/ApiController.php | 178 +++++++++++++++----------------- Controller/SearchController.php | 57 ++++++++-- Models/Elastic/Task.json | 6 ++ Models/TaskMapper.php | 1 - Theme/Backend/Lang/de.lang.php | 1 + Theme/Backend/Lang/en.lang.php | 1 + Theme/Backend/task-view.tpl.php | 37 ++++--- 9 files changed, 236 insertions(+), 120 deletions(-) create mode 100644 Admin/Search.php create mode 100644 Admin/SearchCommands.php create mode 100644 Models/Elastic/Task.json diff --git a/Admin/Search.php b/Admin/Search.php new file mode 100644 index 0000000..e2aa963 --- /dev/null +++ b/Admin/Search.php @@ -0,0 +1,43 @@ + __DIR__ . '/SearchCommands.php']); + } +} diff --git a/Admin/SearchCommands.php b/Admin/SearchCommands.php new file mode 100644 index 0000000..408d5d2 --- /dev/null +++ b/Admin/SearchCommands.php @@ -0,0 +1,32 @@ + [ + [ + 'dest' => '\Modules\Tasks\Controller\SearchController:searchGeneral', + 'verb' => RouteVerb::ANY, + 'permission' => [ + 'module' => SearchController::NAME, + 'type' => PermissionType::READ, + 'state' => PermissionCategory::TASK, + ], + ], + ], +]; diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 0daf12a..b50bb75 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -194,8 +194,10 @@ private function createTaskMedia(Task $task, RequestAbstract $request) : void /** @var \Modules\Admin\Models\Account $account */ $account = AccountMapper::get()->where('id', $request->header->account)->execute(); + $collection = null; + if (!empty($uploadedFiles = $request->files)) { - $uploaded = $this->app->moduleManager->get('Media')->uploadFiles( + $uploaded = $this->app->moduleManager->get('Media', 'Api')->uploadFiles( names: [], fileNames: [], files: $uploadedFiles, @@ -205,7 +207,6 @@ private function createTaskMedia(Task $task, RequestAbstract $request) : void pathSettings: PathSettings::FILE_PATH ); - $collection = null; foreach ($uploaded as $media) { $this->createModelRelation( $request->header->account, @@ -261,54 +262,51 @@ private function createTaskMedia(Task $task, RequestAbstract $request) : void } } - if (!empty($mediaFiles = $request->getDataJson('media'))) { - $collection = null; - - foreach ($mediaFiles as $file) { - /** @var \Modules\Media\Models\Media $media */ - $media = MediaMapper::get()->where('id', (int) $file)->limit(1)->execute(); + $mediaFiles = $request->getDataJson('media'); + foreach ($mediaFiles as $file) { + /** @var \Modules\Media\Models\Media $media */ + $media = MediaMapper::get()->where('id', (int) $file)->limit(1)->execute(); - $this->createModelRelation( - $request->header->account, - $task->id, - $media->id, - TaskMapper::class, - 'files', - '', - $request->getOrigin() - ); + $this->createModelRelation( + $request->header->account, + $task->id, + $media->id, + TaskMapper::class, + 'files', + '', + $request->getOrigin() + ); - $ref = new Reference(); - $ref->name = $media->name; - $ref->source = new NullMedia($media->id); - $ref->createdBy = new NullAccount($request->header->account); - $ref->setVirtualPath($path); + $ref = new Reference(); + $ref->name = $media->name; + $ref->source = new NullMedia($media->id); + $ref->createdBy = new NullAccount($request->header->account); + $ref->setVirtualPath($path); - $this->createModel($request->header->account, $ref, ReferenceMapper::class, 'media_reference', $request->getOrigin()); + $this->createModel($request->header->account, $ref, ReferenceMapper::class, 'media_reference', $request->getOrigin()); - if ($collection === null) { - /** @var \Modules\Media\Models\Collection $collection */ - $collection = MediaMapper::getParentCollection($path)->limit(1)->execute(); + if ($collection === null) { + /** @var \Modules\Media\Models\Collection $collection */ + $collection = MediaMapper::getParentCollection($path)->limit(1)->execute(); - if ($collection->id === 0) { - $collection = $this->app->moduleManager->get('Media')->createRecursiveMediaCollection( - $path, - $request->header->account, - __DIR__ . '/../../../Modules/Media/Files' . $path - ); - } + if ($collection->id === 0) { + $collection = $this->app->moduleManager->get('Media')->createRecursiveMediaCollection( + $path, + $request->header->account, + __DIR__ . '/../../../Modules/Media/Files' . $path + ); } - - $this->createModelRelation( - $request->header->account, - $collection->id, - $ref->id, - CollectionMapper::class, - 'sources', - '', - $request->getOrigin() - ); } + + $this->createModelRelation( + $request->header->account, + $collection->id, + $ref->id, + CollectionMapper::class, + 'sources', + '', + $request->getOrigin() + ); } } @@ -538,8 +536,10 @@ private function createTaskElementMedia(Task $task, TaskElement $element, Reques /** @var \Modules\Admin\Models\Account $account */ $account = AccountMapper::get()->where('id', $request->header->account)->execute(); + $collection = null; + if (!empty($uploadedFiles = $request->files)) { - $uploaded = $this->app->moduleManager->get('Media')->uploadFiles( + $uploaded = $this->app->moduleManager->get('Media', 'Api')->uploadFiles( [], [], $uploadedFiles, @@ -548,7 +548,6 @@ private function createTaskElementMedia(Task $task, TaskElement $element, Reques $path, ); - $collection = null; foreach ($uploaded as $media) { $this->createModelRelation( $request->header->account, @@ -574,16 +573,14 @@ private function createTaskElementMedia(Task $task, TaskElement $element, Reques $this->createModel($request->header->account, $ref, ReferenceMapper::class, 'media_reference', $request->getOrigin()); - if ($collection === null) { - $collection = $this->app->moduleManager->get('Media')->createRecursiveMediaCollection( - $accountPath, - $request->header->account, - __DIR__ . '/../../../Modules/Media/Files/Accounts/' . $account->id - . '/Tasks/' . $task->createdAt->format('Y') . '/' - . $task->createdAt->format('m') . '/' - . $task->id - ); - } + $collection ??= $this->app->moduleManager->get('Media')->createRecursiveMediaCollection( + $accountPath, + $request->header->account, + __DIR__ . '/../../../Modules/Media/Files/Accounts/' . $account->id + . '/Tasks/' . $task->createdAt->format('Y') . '/' + . $task->createdAt->format('m') . '/' + . $task->id + ); $this->createModelRelation( $request->header->account, @@ -597,49 +594,44 @@ private function createTaskElementMedia(Task $task, TaskElement $element, Reques } } - if (!empty($mediaFiles = $request->getDataJson('media'))) { - $collection = null; + $mediaFiles = $request->getDataJson('media'); + foreach ($mediaFiles as $file) { + /** @var \Modules\Media\Models\Media $media */ + $media = MediaMapper::get()->where('id', (int) $file)->limit(1)->execute(); - foreach ($mediaFiles as $file) { - /** @var \Modules\Media\Models\Media $media */ - $media = MediaMapper::get()->where('id', (int) $file)->limit(1)->execute(); - - $this->createModelRelation( - $request->header->account, - $element->id, - $media->id, - TaskElementMapper::class, - 'files', - '', - $request->getOrigin() - ); + $this->createModelRelation( + $request->header->account, + $element->id, + $media->id, + TaskElementMapper::class, + 'files', + '', + $request->getOrigin() + ); - $ref = new Reference(); - $ref->name = $media->name; - $ref->source = new NullMedia($media->id); - $ref->createdBy = new NullAccount($request->header->account); - $ref->setVirtualPath($path); + $ref = new Reference(); + $ref->name = $media->name; + $ref->source = new NullMedia($media->id); + $ref->createdBy = new NullAccount($request->header->account); + $ref->setVirtualPath($path); - $this->createModel($request->header->account, $ref, ReferenceMapper::class, 'media_reference', $request->getOrigin()); + $this->createModel($request->header->account, $ref, ReferenceMapper::class, 'media_reference', $request->getOrigin()); - if ($collection === null) { - $collection = $this->app->moduleManager->get('Media')->createRecursiveMediaCollection( - $path, - $request->header->account, - __DIR__ . '/../../../Modules/Media/Files' . $path - ); - } + $collection ??= $this->app->moduleManager->get('Media')->createRecursiveMediaCollection( + $path, + $request->header->account, + __DIR__ . '/../../../Modules/Media/Files' . $path + ); - $this->createModelRelation( - $request->header->account, - $collection->id, - $ref->id, - CollectionMapper::class, - 'sources', - '', - $request->getOrigin() - ); - } + $this->createModelRelation( + $request->header->account, + $collection->id, + $ref->id, + CollectionMapper::class, + 'sources', + '', + $request->getOrigin() + ); } } diff --git a/Controller/SearchController.php b/Controller/SearchController.php index 901e8a8..3f130f1 100755 --- a/Controller/SearchController.php +++ b/Controller/SearchController.php @@ -14,6 +14,8 @@ namespace Modules\Tasks\Controller; +use Modules\Tasks\Models\TaskMapper; +use phpOMS\DataStorage\Database\Query\OrderType; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\System\MimeType; @@ -41,16 +43,57 @@ final class SearchController extends Controller * * @since 1.0.0 */ - public function searchTag(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void + public function searchGeneral(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void { - // join tags with tag l11n - // join tags with tasks - // return where tag l11n matches X + // @performance Guaranteed <= 1 hasMany selects should behave like a join instead of creating sub-queries + // https://github.com/Karaka-Management/phpOMS/issues/363 - $tags = []; + // @bug limit(1, 'taskElements') applies to all taskElements not just taskElements per task! + // https://github.com/Karaka-Management/phpOMS/issues/362 - $response->header->set('Content-Type', MimeType::M_JSON . '; charset=utf-8', true); + /** @var \Modules\Tasks\Models\Task[] $tasks */ + $tasks = TaskMapper::getAll() + ->with('tags') + ->with('tags/title') + ->with('taskElements') + ->where('title', '%' . ($request->getDataString('search') ?? '') . '%', 'LIKE') + ->where('tags/title/language', $response->header->l11n->language) + ->sort('createdAt', OrderType::DESC) + ->sort('taskElements/createdAt', OrderType::ASC) + ->limit(25) + //->limit(1, 'taskElements') + ->execute(); + + $results = []; + $count = 0; + + foreach ($tasks as $task) { + if ($count >= 8) { + break; + } + + // @performance Check if this can be combined with the above getAll() + // https://github.com/Karaka-Management/oms-Tasks/issues/41 + if (!TaskMapper::hasReadingPermission($request->header->account, $task->id)) { + continue; + } - $response->set($request->uri->__toString(), $tags); + ++$count; + + $results[] = [ + 'title' => $task->title, + 'summary' => \substr(\trim($task->description), 0, 500), + 'link' => '{/base}/task/view?id=' . $task->id, + 'account' => '', + 'createdAt' => $task->createdAt, + 'image' => '', + 'tags' => $task->tags, + 'type' => 'list_links', + 'module' => 'Tasks', + ]; + } + + $response->header->set('Content-Type', MimeType::M_JSON . '; charset=utf-8', true); + $response->add($request->uri->__toString(), $results); } } diff --git a/Models/Elastic/Task.json b/Models/Elastic/Task.json new file mode 100644 index 0000000..d878d25 --- /dev/null +++ b/Models/Elastic/Task.json @@ -0,0 +1,6 @@ +{ + "id": "{id}", + "title": "{title}", + "content": "{content}", + "tags": ["{tags}"] +} \ No newline at end of file diff --git a/Models/TaskMapper.php b/Models/TaskMapper.php index 3c239c1..d2cdc3c 100755 --- a/Models/TaskMapper.php +++ b/Models/TaskMapper.php @@ -19,7 +19,6 @@ use Modules\Media\Models\MediaMapper; use Modules\Tag\Models\TagMapper; use Modules\Tasks\Models\Attribute\TaskAttributeMapper; -use phpOMS\DataStorage\Database\Mapper\DataMapperAbstract; use phpOMS\DataStorage\Database\Mapper\DataMapperFactory; use phpOMS\DataStorage\Database\Mapper\ReadMapper; use phpOMS\DataStorage\Database\Query\Builder; diff --git a/Theme/Backend/Lang/de.lang.php b/Theme/Backend/Lang/de.lang.php index 31a65be..ab2b2bc 100755 --- a/Theme/Backend/Lang/de.lang.php +++ b/Theme/Backend/Lang/de.lang.php @@ -21,6 +21,7 @@ 'Completion' => 'Abgeschlossen', 'Created' => 'Erstellt', 'Creator' => 'Ersteller', + 'Advanced' => 'Fortgeschritten', 'Due' => 'Fällig', 'Due/Priority' => 'Fällig / Priorität', 'For' => 'Für', diff --git a/Theme/Backend/Lang/en.lang.php b/Theme/Backend/Lang/en.lang.php index 2c5a67c..4a4136c 100755 --- a/Theme/Backend/Lang/en.lang.php +++ b/Theme/Backend/Lang/en.lang.php @@ -21,6 +21,7 @@ 'Completion' => 'Completion', 'Created' => 'Created', 'Creator' => 'Creator', + 'Advanced' => 'Advanced', 'Due' => 'Due', 'Due/Priority' => 'Due/Priority', 'For' => 'For', diff --git a/Theme/Backend/task-view.tpl.php b/Theme/Backend/task-view.tpl.php index 94a8cb4..723ef9a 100644 --- a/Theme/Backend/task-view.tpl.php +++ b/Theme/Backend/task-view.tpl.php @@ -376,28 +376,27 @@
-
-
- - -
-
- - -
+
+ + +
+ +
+ +