diff --git a/Admin/Hooks/Cli.php b/Admin/Hooks/Cli.php new file mode 100644 index 0000000..0a4a6f1 --- /dev/null +++ b/Admin/Hooks/Cli.php @@ -0,0 +1,20 @@ + [ + 'callback' => ['\Modules\Workflow\Controller\CliController:findWorkflow'], + ], +]; + diff --git a/Admin/Hooks/Web/Api.php b/Admin/Hooks/Web/Api.php new file mode 100644 index 0000000..55c301f --- /dev/null +++ b/Admin/Hooks/Web/Api.php @@ -0,0 +1,19 @@ + [ + 'callback' => ['\Modules\Workflow\Controller\ApiController:cliEventCall'], + ], +]; diff --git a/Admin/Install/db.json b/Admin/Install/db.json index a3c07d5..39a9954 100755 --- a/Admin/Install/db.json +++ b/Admin/Install/db.json @@ -1,38 +1,38 @@ { "workflow": { - "name": "workflow", + "name": "workflow_template", "fields": { - "workflow_id": { - "name": "workflow_id", + "workflow_template_id": { + "name": "workflow_template_id", "type": "INT", "null": false, "primary": true, "autoincrement": true }, - "workflow_name": { - "name": "workflow_name", + "workflow_template_name": { + "name": "workflow_template_name", "type": "VARCHAR(255)", "default": null, "null": true }, - "workflow_status": { - "name": "workflow_status", + "workflow_template_status": { + "name": "workflow_template_status", "type": "INT", "null": false }, - "workflow_desc": { - "name": "workflow_desc", + "workflow_template_desc": { + "name": "workflow_template_desc", "type": "TEXT", "default": null, "null": true }, - "workflow_created": { - "name": "workflow_created", + "workflow_template_created": { + "name": "workflow_template_created", "type": "DATETIME", "null": false }, - "workflow_created_by": { - "name": "workflow_created_by", + "workflow_template_created_by": { + "name": "workflow_template_created_by", "type": "INT", "null": false, "foreignTable": "account", @@ -40,139 +40,35 @@ } } }, - "workflow_media": { - "name": "workflow_media", + "workflow_template_media": { + "name": "workflow_template_media", "fields": { - "workflow_media_id": { - "name": "workflow_media_id", + "workflow_template_media_id": { + "name": "workflow_template_media_id", "type": "INT", "null": false, "primary": true, "autoincrement": true }, - "workflow_media_media": { - "name": "workflow_media_media", + "workflow_template_media_media": { + "name": "workflow_template_media_media", "type": "INT", "null": false, "foreignTable": "media", "foreignKey": "media_id" }, - "workflow_media_workflow": { - "name": "workflow_media_workflow", + "workflow_template_media_workflow": { + "name": "workflow_template_media_workflow", "type": "INT", "null": false, - "foreignTable": "workflow", - "foreignKey": "workflow_id" + "foreignTable": "workflow_template", + "foreignKey": "workflow_template_id" }, - "workflow_media_type": { - "name": "workflow_media_type", + "workflow_template_media_type": { + "name": "workflow_template_media_type", "type": "TINYINT", "null": false } } - }, - "workflow_element": { - "name": "workflow_element", - "fields": { - "workflow_element_id": { - "name": "workflow_element_id", - "type": "INT", - "null": false, - "primary": true, - "autoincrement": true - }, - "workflow_element_name": { - "name": "workflow_element_name", - "type": "VARCHAR(255)", - "null": false - }, - "workflow_element_status": { - "name": "workflow_element_status", - "type": "INT", - "null": false - }, - "workflow_element_data": { - "name": "workflow_element_data", - "type": "TEXT", - "null": false - }, - "workflow_element_desc": { - "name": "workflow_element_desc", - "type": "TEXT", - "default": null, - "null": true - }, - "workflow_element_created": { - "name": "workflow_element_created", - "type": "DATETIME", - "null": false - }, - "workflow_element_created_by": { - "name": "workflow_element_created_by", - "type": "INT", - "null": false, - "foreignTable": "account", - "foreignKey": "account_id" - }, - "workflow_element_workflow": { - "name": "workflow_element_workflow", - "type": "INT", - "null": false, - "foreignTable": "workflow", - "foreignKey": "workflow_id" - } - } - }, - "workflow_element_media": { - "name": "workflow_element_media", - "fields": { - "workflow_element_media_id": { - "name": "workflow_element_media_id", - "type": "INT", - "null": false, - "primary": true, - "autoincrement": true - }, - "workflow_element_media_media": { - "name": "workflow_element_media_media", - "type": "INT", - "null": false, - "foreignTable": "media", - "foreignKey": "media_id" - }, - "workflow_element_media_workflow_element": { - "name": "workflow_element_media_workflow_element", - "type": "INT", - "null": false, - "foreignTable": "workflow_element", - "foreignKey": "workflow_element_id" - } - } - }, - "workflow_element_task": { - "name": "workflow_element_task", - "fields": { - "workflow_element_task_id": { - "name": "workflow_element_task_id", - "type": "INT", - "null": false, - "primary": true, - "autoincrement": true - }, - "workflow_element_task_task": { - "name": "workflow_element_task_task", - "type": "INT", - "null": false, - "foreignTable": "task", - "foreignKey": "task_id" - }, - "workflow_element_task_workflow_element": { - "name": "workflow_element_task_workflow_element", - "type": "INT", - "null": false, - "foreignTable": "workflow_element", - "foreignKey": "workflow_element_id" - } - } } } \ No newline at end of file diff --git a/Controller/ApiController.php b/Controller/ApiController.php new file mode 100644 index 0000000..2037375 --- /dev/null +++ b/Controller/ApiController.php @@ -0,0 +1,56 @@ +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(); + } +} diff --git a/Models/Template.php b/Models/WorkflowState.php similarity index 58% rename from Models/Template.php rename to Models/WorkflowState.php index 81477d6..d809910 100755 --- a/Models/Template.php +++ b/Models/WorkflowState.php @@ -14,14 +14,27 @@ declare(strict_types=1); namespace Modules\Workflow\Models; +use phpOMS\Stdlib\Base\Enum; + /** - * Workflow template class. + * Workflow state enum. * * @package Modules\Workflow\Models * @license OMS License 1.0 * @link https://karaka.app * @since 1.0.0 */ -class Template +abstract class WorkflowState extends Enum { + public const OPEN = 1; + + public const WORKING = 2; + + public const SUSPENDED = 3; + + public const CANCELED = 4; + + public const DONE = 5; + + public const CLOSED = 6; } diff --git a/Models/WorkflowStatus.php b/Models/WorkflowStatus.php old mode 100755 new mode 100644 index 285a6c1..9e3cabf --- a/Models/WorkflowStatus.php +++ b/Models/WorkflowStatus.php @@ -26,15 +26,7 @@ use phpOMS\Stdlib\Base\Enum; */ abstract class WorkflowStatus extends Enum { - public const OPEN = 1; + public const ACTIVE = 1; - public const WORKING = 2; - - public const SUSPENDED = 3; - - public const CANCELED = 4; - - public const DONE = 5; - - public const CLOSED = 6; + public const INACTIVE = 2; } diff --git a/Models/WorkflowTemplate.php b/Models/WorkflowTemplate.php new file mode 100644 index 0000000..ab540c2 --- /dev/null +++ b/Models/WorkflowTemplate.php @@ -0,0 +1,76 @@ +createdBy = new NullAccount(); + $this->createdAt = new \DateTimeImmutable('now'); + } + + /** + * Get id + * + * @return int + * + * @since 1.0.0 + */ + public function getId() : int + { + return $this->id; + } +} diff --git a/Models/WorkflowTemplateMapper.php b/Models/WorkflowTemplateMapper.php new file mode 100644 index 0000000..f64e0a6 --- /dev/null +++ b/Models/WorkflowTemplateMapper.php @@ -0,0 +1,70 @@ + + * @since 1.0.0 + */ + public const COLUMNS = [ + 'workflow_template_id' => ['name' => 'workflow_template_id', 'type' => 'int', 'internal' => 'id'], + 'workflow_template_created_at' => ['name' => 'workflow_template_created_at', 'type' => 'DateTimeImmutable', 'internal' => 'createdAt', 'readonly' => true], + ]; + + /** + * Primary table. + * + * @var string + * @since 1.0.0 + */ + public const TABLE = 'workflow_template'; + + /** + * Created at. + * + * @var string + * @since 1.0.0 + */ + public const CREATED_AT = 'workflow_template_created_at'; + + /** + * Primary field name. + * + * @var string + * @since 1.0.0 + */ + public const PRIMARYFIELD ='workflow_template_id'; +}