diff --git a/Admin/Install/Navigation.install.json b/Admin/Install/Navigation.install.json index 916c319..36e1255 100755 --- a/Admin/Install/Navigation.install.json +++ b/Admin/Install/Navigation.install.json @@ -10,7 +10,7 @@ "icon": null, "order": 40, "from": "Wokflow", - "permission": { "permission": 2, "type": null, "element": null }, + "permission": { "permission": 2, "category": null, "element": null }, "parent": 1003301001, "children": [ { @@ -24,7 +24,7 @@ "icon": null, "order": 1, "from": "Wokflow", - "permission": { "permission": 2, "type": null, "element": null }, + "permission": { "permission": 2, "category": null, "element": null }, "parent": 1005501001, "children": [] }, @@ -39,7 +39,7 @@ "icon": null, "order": 5, "from": "Wokflow", - "permission": { "permission": 2, "type": null, "element": null }, + "permission": { "permission": 2, "category": null, "element": null }, "parent": 1005501001, "children": [] }, @@ -54,7 +54,7 @@ "icon": null, "order": 10, "from": "Wokflow", - "permission": { "permission": 4, "type": null, "element": null }, + "permission": { "permission": 4, "category": null, "element": null }, "parent": 1005501001, "children": [] } diff --git a/Admin/Installer.php b/Admin/Installer.php index 5247af8..f69e067 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -74,12 +74,12 @@ final class Installer extends InstallerAbstract protected string $appName = 'Api'; }; - $apiApp->dbPool = $app->dbPool; - $apiApp->orgId = $app->orgId; + $apiApp->dbPool = $app->dbPool; + $apiApp->orgId = $app->orgId; $apiApp->accountManager = $app->accountManager; - $apiApp->appSettings = $app->appSettings; - $apiApp->moduleManager = $app->moduleManager; - $apiApp->eventManager = $app->eventManager; + $apiApp->appSettings = $app->appSettings; + $apiApp->moduleManager = $app->moduleManager; + $apiApp->eventManager = $app->eventManager; foreach ($workflowData as $workflow) { self::installWorkflow($apiApp, $workflow); @@ -112,6 +112,10 @@ final class Installer extends InstallerAbstract $tempPath = __DIR__ . '/../../../temp/'; $workflowFiles = \scandir(__DIR__ . '/../../..' . $data['path']); + if ($workflowFiles === false) { + return; + } + foreach ($workflowFiles as $filePath) { if (!\is_file(__DIR__ . '/../../..' . $data['path'] . '/' . $filePath) || $filePath === '..' || $filePath === '.') { continue; @@ -131,6 +135,6 @@ final class Installer extends InstallerAbstract ]); } - $module->apiTemplateCreate($request, $response); + $module->apiWorkflowTemplateCreate($request, $response); } } diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 5a6f74d..bd034f0 100644 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -23,6 +23,7 @@ use Modules\Workflow\Models\PermissionCategory; use Modules\Workflow\Models\WorkflowInstance; use Modules\Workflow\Models\WorkflowInstanceAbstract; use Modules\Workflow\Models\WorkflowInstanceMapper; +use Modules\Workflow\Models\WorkflowStatus; use Modules\Workflow\Models\WorkflowTemplate; use Modules\Workflow\Models\WorkflowTemplateMapper; use phpOMS\Account\PermissionType; @@ -50,6 +51,62 @@ use phpOMS\DataStorage\Database\Schema\Builder as SchemaBuilder; */ final class ApiController extends Controller { + /** + * Api method to make a call to the cli app + * + * @param mixed ...$data Generic data + * + * @return void + * + * @api + * + * @since 1.0.0 + */ + public function runWorkflowFromHook(...$data) : void + { + $workflows = WorkflowTemplateMapper::getAll()->where('status', WorkflowStatus::ACTIVE)->execute(); + foreach ($workflows as $workflow) { + $hooks = $workflow->getHooks(); + + foreach ($hooks as $hook) { + $triggerIsRegex = \stripos($data[':triggerGroup'], '/') === 0; + $matched = false; + + if ($triggerIsRegex) { + $matched = \preg_match($data[':triggerGroup'], $hook) === 1; + } else { + $matched = $data[':triggerGroup'] === $hook; + } + + if (!$matched && \stripos($hook, '/') === 0) { + $matched = \preg_match($hook, $data[':triggerGroup']) === 1; + } + + if ($matched) { + $this->runWorkflow($workflow, $hook, $data); + } + } + } + } + + /** + * Api method to make a call to the cli app + * + * @param WorkflowTemplate $workflow Workflow template + * @param string $hook Event hook + * @param array $data Event data + * + * @return void + * + * @api + * + * @since 1.0.0 + */ + public function runWorkflow(WorkflowTemplate $workflow, string $hook, array $data) : void + { + include $workflow->source->getAbsolutePath(); + } + /** * Routing end-point for application behaviour. * @@ -205,11 +262,11 @@ final class ApiController extends Controller } /** - * Create view from template + * Create view from template/instance * - * @param WorkflowInstanceAbstract $instance Instance to create view from - * @param RequestAbstract $request Request - * @param ResponseAbstract $response Response + * @param WorkflowInstanceAbstract $instance Instance to create view from + * @param RequestAbstract $request Request + * @param ResponseAbstract $response Response * * @return View * @@ -217,11 +274,13 @@ final class ApiController extends Controller * * @since 1.0.0 */ - private function createView(WorkflowInstanceAbstract $instance, RequestAbstract $request, ResponseAbstract $response) : View - { - /** @var array $tcoll */ + private function createView( + WorkflowInstanceAbstract $instance, + RequestAbstract $request, + ResponseAbstract $response + ) : View { $tcoll = []; - $files = $instance->source->getSources(); + $files = $instance->template->source->getSources(); /** @var \Modules\Media\Models\Media $tMedia */ foreach ($files as $tMedia) { @@ -345,7 +404,7 @@ final class ApiController extends Controller $files[] = $upload; } - /** @var Collection $collection */ + /** @var \Modules\Media\Models\Collection $collection */ $collection = $this->app->moduleManager->get('Media')->createMediaCollectionFromMedia( (string) ($request->getData('name') ?? ''), (string) ($request->getData('description') ?? ''), @@ -376,8 +435,12 @@ final class ApiController extends Controller continue; } - $path = $upload->getAbsolutePath(); + $path = $upload->getAbsolutePath(); $content = \file_get_contents($path); + if ($content === false) { + $content = ''; + } + $content = \str_replace('{workflow_id}', (string) $template->getId(), $content); \file_put_contents($path, $content); } @@ -385,6 +448,15 @@ final class ApiController extends Controller $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Template', 'Template successfully created', $template); } + /** + * Create media directory path + * + * @param WorkflowTemplate $template Workflow template + * + * @return string + * + * @since 1.0.0 + */ private function createTemplateDir(WorkflowTemplate $template) : string { return '/Modules/Workflow/' @@ -434,7 +506,6 @@ final class ApiController extends Controller } $workflowTemplate->createdBy = new NullAccount($request->header->account); - $workflowTemplate->virtualPath = (string) ($request->getData('virtualpath') ?? '/'); return $workflowTemplate; } @@ -475,6 +546,8 @@ final class ApiController extends Controller foreach ($definitions as $definition) { SchemaBuilder::createFromSchema($definition, $this->app->dbPool->get('schema'))->execute(); } + + return; } } @@ -553,6 +626,7 @@ final class ApiController extends Controller } } + /** @var \Modules\Workflow\Models\WorkflowControllerInterface $controller */ $instance = $controller->createInstanceFromRequest($request); return $instance; diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 6b8e25b..66b4599 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -16,9 +16,11 @@ namespace Modules\Workflow\Controller; use Modules\Media\Models\CollectionMapper; use Modules\Media\Models\NullMedia; +use Modules\Workflow\Models\WorkflowControllerInterface; use Modules\Workflow\Models\WorkflowInstanceMapper; use Modules\Workflow\Models\WorkflowStatus; use Modules\Workflow\Models\WorkflowTemplateMapper; +use Modules\Workflow\Models\WorkflowTemplateStatus; use phpOMS\Contract\RenderableInterface; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; @@ -34,60 +36,6 @@ use phpOMS\Views\View; */ final class BackendController extends Controller { - /** - * Api method to make a call to the cli app - * - * @param mixed $data Generic data - * - * @return void - * - * @api - * - * @since 1.0.0 - */ - public function runWorkflowFromHook(...$data) : void - { - $workflows = WorkflowTemplateMapper::getAll()->where('status', WorkflowStatus::ACTIVE)->execute(); - foreach ($workflows as $workflow) { - $hooks = $workflow->getHooks(); - - foreach ($hooks as $hook) { - $triggerIsRegex = \stripos($data[':triggerGroup'], '/') === 0; - $matched = false; - - if ($triggerIsRegex) { - $matched = \preg_match($data[':triggerGroup'], $hook) === 1; - } else { - $matched = $data[':triggerGroup'] === $hook; - } - - if (!$matched && \stripos($hook, '/') === 0) { - $matched = \preg_match($hook, $data[':triggerGroup']) === 1; - } - - if ($matched) { - $this->runWorkflow($workflow, $hook, $data); - } - } - } - } - - /** - * Api method to make a call to the cli app - * - * @param mixed $data Generic data - * - * @return void - * - * @api - * - * @since 1.0.0 - */ - public function runWorkflow(WorkflowTemplate $workflow, string $hook, array $data) : void - { - include $workflow->media->getAbsolutePath(); - } - /** * Routing end-point for application behaviour. * @@ -270,7 +218,7 @@ final class BackendController extends Controller /** @var WorkflowControllerInterface $controller */ $controller = new WorkflowController($this->app, $template); - $view->addData('instance', $controller->getInstanceFromRequest($request)); + $view->addData('instance', $controller->createInstanceFromRequest($request)); if (!(($instance = $template->findFile('instance-profile.tpl.php')) instanceof NullMedia)) { $view->setTemplate('/' . \substr($instance->getPath(), 0, -8)); diff --git a/Controller/CliController.php b/Controller/CliController.php index fbf07a8..c85c6e1 100644 --- a/Controller/CliController.php +++ b/Controller/CliController.php @@ -39,7 +39,7 @@ final class CliController extends Controller /** * Api method to make a call to the cli app * - * @param mixed $data Generic data + * @param mixed ...$data Generic data * * @return void * @@ -55,7 +55,7 @@ final class CliController extends Controller foreach ($hooks as $hook) { $triggerIsRegex = \stripos($data[':triggerGroup'], '/') === 0; - $matched = false; + $matched = false; if ($triggerIsRegex) { $matched = \preg_match($data[':triggerGroup'], $hook) === 1; @@ -77,7 +77,9 @@ final class CliController extends Controller /** * Api method to make a call to the cli app * - * @param mixed $data Generic data + * @param WorkflowTemplate $workflow Workflow template + * @param string $hook Event hook + * @param array $data Event data * * @return void * @@ -87,7 +89,7 @@ final class CliController extends Controller */ public function runWorkflow(WorkflowTemplate $workflow, string $hook, array $data) : void { - include $workflow->media->getAbsolutePath(); + include $workflow->source->getAbsolutePath(); } /** @@ -153,7 +155,6 @@ final class CliController extends Controller ->execute(); $controller = null; - $mapper = null; $files = $template->source->getSources(); foreach ($files as $tMedia) { @@ -168,6 +169,7 @@ final class CliController extends Controller } } + /** @var \Modules\Workflow\Models\WorkflowControllerInterface $controller */ $instance = $controller->createInstanceFromRequest($request); $controller->createInstanceDbModel($instance); diff --git a/Models/NullWorkflowInstance.php b/Models/NullWorkflowInstance.php deleted file mode 100644 index 9298605..0000000 --- a/Models/NullWorkflowInstance.php +++ /dev/null @@ -1,39 +0,0 @@ -id = $id; - parent::__construct(); - } -} diff --git a/Models/WorkflowControllerInterface.php b/Models/WorkflowControllerInterface.php index e16a3b6..2afb202 100644 --- a/Models/WorkflowControllerInterface.php +++ b/Models/WorkflowControllerInterface.php @@ -14,7 +14,6 @@ declare(strict_types=1); namespace Modules\Workflow\Models; -use phpOMS\Contract\RenderableInterface; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; @@ -53,9 +52,11 @@ interface WorkflowControllerInterface /** * Change workflow instance state * - * @param RequestAbstract $request Request + * @param RequestAbstract $request Request + * @param ResponseAbstract $response Response + * @param null|mixed $data Data * - * @return WorkflowInstanceAbstract + * @return void * * @since 1.0.0 */ diff --git a/Models/WorkflowInstanceAbstract.php b/Models/WorkflowInstanceAbstract.php index 044260d..b192395 100644 --- a/Models/WorkflowInstanceAbstract.php +++ b/Models/WorkflowInstanceAbstract.php @@ -25,7 +25,7 @@ use Modules\Admin\Models\NullAccount; * @link https://karaka.app * @since 1.0.0 */ -class WorkflowInstanceAbstract +abstract class WorkflowInstanceAbstract { /** * ID. @@ -74,7 +74,7 @@ class WorkflowInstanceAbstract */ public function __construct() { - $this->template = new NullWorkflowTemplate(); + $this->template = new NullWorkflowTemplate(); $this->createdBy = new NullAccount(); $this->createdAt = new \DateTimeImmutable('now'); } diff --git a/Models/WorkflowTemplate.php b/Models/WorkflowTemplate.php index d825f2d..0959f7a 100644 --- a/Models/WorkflowTemplate.php +++ b/Models/WorkflowTemplate.php @@ -67,7 +67,7 @@ class WorkflowTemplate /** * Status. * - * @var string + * @var int * @since 1.0.0 */ public int $status = WorkflowTemplateStatus::ACTIVE; @@ -120,6 +120,15 @@ class WorkflowTemplate return $this->id; } + /** + * Find file by file name + * + * @param string $name File name + * + * @return Media + * + * @since 1.0.0 + */ public function findFile(string $name) : Media { $files = $this->source->getSources();