Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Eichhorn committed Feb 4, 2024
1 parent 7ddc44f commit 854cad4
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 120 deletions.
43 changes: 43 additions & 0 deletions Admin/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package Modules\Tasks\Admin\Install
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);

namespace Modules\Tasks\Admin\Install;

use phpOMS\Application\ApplicationAbstract;

/**
* Search class.
*
* @package Modules\Tasks\Admin\Install
* @license OMS License 2.0
* @link https://jingga.app
* @since 1.0.0
*/
final class Search
{
/**
* Install navigation providing
*
* @param ApplicationAbstract $app Application
* @param string $path Module path
*
* @return void
*
* @since 1.0.0
*/
public static function install(ApplicationAbstract $app, string $path) : void
{
\Modules\Search\Admin\Installer::installExternal($app, ['path' => __DIR__ . '/SearchCommands.php']);
}
}
32 changes: 32 additions & 0 deletions Admin/SearchCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Jingga
*
* PHP Version 8.1
*
* @package Modules\Tasks
* @copyright Dennis Eichhorn
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);

use Modules\Tasks\Controller\SearchController;
use Modules\Tasks\Models\PermissionCategory;
use phpOMS\Account\PermissionType;
use phpOMS\Router\RouteVerb;

return [
'^(?!:).+.*?' => [
[
'dest' => '\Modules\Tasks\Controller\SearchController:searchGeneral',
'verb' => RouteVerb::ANY,
'permission' => [
'module' => SearchController::NAME,
'type' => PermissionType::READ,
'state' => PermissionCategory::TASK,
],
],
],
];
178 changes: 85 additions & 93 deletions Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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()
);
}
}

Expand Down Expand Up @@ -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,
Expand All @@ -548,7 +548,6 @@ private function createTaskElementMedia(Task $task, TaskElement $element, Reques
$path,
);

$collection = null;
foreach ($uploaded as $media) {
$this->createModelRelation(
$request->header->account,
Expand All @@ -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,
Expand All @@ -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()
);
}
}

Expand Down
57 changes: 50 additions & 7 deletions Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions Models/Elastic/Task.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"id": "{id}",
"title": "{title}",
"content": "{content}",
"tags": ["{tags}"]
}
Loading

0 comments on commit 854cad4

Please sign in to comment.