Initial commit
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/***********************************************************************************
|
||||
* The contents of this file are subject to the Extension License Agreement
|
||||
* ("Agreement") which can be viewed at
|
||||
* https://www.espocrm.com/extension-license-agreement/.
|
||||
* By copying, installing downloading, or using this file, You have unconditionally
|
||||
* agreed to the terms and conditions of the Agreement, and You may not use this
|
||||
* file except in compliance with the Agreement. Under the terms of the Agreement,
|
||||
* You shall not license, sublicense, sell, resell, rent, lease, lend, distribute,
|
||||
* redistribute, market, publish, commercialize, or otherwise transfer rights or
|
||||
* usage to the software or any modified version or derivative work of the software
|
||||
* created by or for you.
|
||||
*
|
||||
* Copyright (C) 2015-2025 EspoCRM, Inc.
|
||||
*
|
||||
* License ID: 19bc86a68a7bb01f458cb391d43a9212
|
||||
************************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Advanced\Tools\Workflow\Jobs;
|
||||
|
||||
use Espo\Core\Exceptions\BadRequest;
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Core\Exceptions\Forbidden;
|
||||
use Espo\Core\Exceptions\NotFound;
|
||||
use Espo\Core\Formula\Exceptions\Error as FormulaError;
|
||||
use Espo\Core\Job\Job;
|
||||
use Espo\Core\Job\Job\Data;
|
||||
use Espo\Core\Job\JobSchedulerFactory;
|
||||
use Espo\Modules\Advanced\Entities\Workflow;
|
||||
use Espo\Modules\Advanced\Tools\Report\ListType\RunParams as ListRunParams;
|
||||
use Espo\Modules\Advanced\Tools\Report\Service as ReportService;
|
||||
use Espo\Modules\Advanced\Tools\Workflow\Service;
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
class RunScheduledWorkflow implements Job
|
||||
{
|
||||
public function __construct(
|
||||
private ReportService $reportService,
|
||||
private EntityManager $entityManager,
|
||||
private Service $service,
|
||||
private JobSchedulerFactory $jobSchedulerFactory,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws BadRequest
|
||||
* @throws Forbidden
|
||||
* @throws Error
|
||||
* @throws NotFound
|
||||
*/
|
||||
public function run(Data $data): void
|
||||
{
|
||||
$workflowId = $data->getTargetId() ?? $data->get('workflowId');
|
||||
|
||||
if (!$workflowId) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
$workflow = $this->entityManager->getRDBRepositoryByClass(Workflow::class)->getById($workflowId);
|
||||
|
||||
if (!$workflow) {
|
||||
throw new RuntimeException("Workflow $workflowId not found.");
|
||||
}
|
||||
|
||||
if (!$workflow->isActive()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targetReport = $this->entityManager
|
||||
->getRDBRepository(Workflow::ENTITY_TYPE)
|
||||
->getRelation($workflow, 'targetReport')
|
||||
->findOne();
|
||||
|
||||
if (!$targetReport) {
|
||||
throw new RuntimeException("Workflow $workflowId: Target report not found.");
|
||||
}
|
||||
|
||||
$result = $this->reportService->runList(
|
||||
id: $targetReport->getId(),
|
||||
runParams: ListRunParams::create()->withReturnSthCollection(),
|
||||
);
|
||||
|
||||
foreach ($result->getCollection() as $entity) {
|
||||
try {
|
||||
$this->runScheduledWorkflowForEntity(
|
||||
$workflow->getId(),
|
||||
$entity->getEntityType(),
|
||||
$entity->getId()
|
||||
);
|
||||
} catch (Exception) {
|
||||
// @todo Revise.
|
||||
|
||||
$this->jobSchedulerFactory
|
||||
->create()
|
||||
->setClassName(RunScheduledWorkflowForEntity::class)
|
||||
->setGroup('scheduled-workflows')
|
||||
->setData([
|
||||
'workflowId' => $workflow->getId(),
|
||||
'entityType' => $entity->getEntityType(),
|
||||
'entityId' => $entity->getId(),
|
||||
])
|
||||
->schedule();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FormulaError
|
||||
* @throws Error
|
||||
*/
|
||||
private function runScheduledWorkflowForEntity(string $workflowId, string $entityType, string $id): void
|
||||
{
|
||||
// @todo Create jobs if a parameter is enabled.
|
||||
|
||||
$entity = $this->entityManager->getEntityById($entityType, $id);
|
||||
|
||||
if (!$entity) {
|
||||
throw new RuntimeException("Workflow $workflowId: Entity $entityType $id not found.");
|
||||
}
|
||||
|
||||
$this->service->triggerWorkflow($entity, $workflowId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/***********************************************************************************
|
||||
* The contents of this file are subject to the Extension License Agreement
|
||||
* ("Agreement") which can be viewed at
|
||||
* https://www.espocrm.com/extension-license-agreement/.
|
||||
* By copying, installing downloading, or using this file, You have unconditionally
|
||||
* agreed to the terms and conditions of the Agreement, and You may not use this
|
||||
* file except in compliance with the Agreement. Under the terms of the Agreement,
|
||||
* You shall not license, sublicense, sell, resell, rent, lease, lend, distribute,
|
||||
* redistribute, market, publish, commercialize, or otherwise transfer rights or
|
||||
* usage to the software or any modified version or derivative work of the software
|
||||
* created by or for you.
|
||||
*
|
||||
* Copyright (C) 2015-2025 EspoCRM, Inc.
|
||||
*
|
||||
* License ID: 19bc86a68a7bb01f458cb391d43a9212
|
||||
************************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Advanced\Tools\Workflow\Jobs;
|
||||
|
||||
use Espo\Core\Job\Job;
|
||||
use Espo\Core\Job\Job\Data;
|
||||
use Espo\Modules\Advanced\Tools\Workflow\Service;
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class RunScheduledWorkflowForEntity implements Job
|
||||
{
|
||||
private EntityManager $entityManager;
|
||||
private Service $service;
|
||||
|
||||
public function __construct(
|
||||
EntityManager $entityManager,
|
||||
Service $service
|
||||
) {
|
||||
$this->entityManager = $entityManager;
|
||||
$this->service = $service;
|
||||
}
|
||||
|
||||
public function run(Data $data): void
|
||||
{
|
||||
$data = $data->getRaw();
|
||||
|
||||
$entityType = $data->entityType;
|
||||
$id = $data->entityId;
|
||||
$workflowId = $data->workflowId;
|
||||
|
||||
$entity = $this->entityManager->getEntityById($entityType, $id);
|
||||
|
||||
if (!$entity) {
|
||||
throw new RuntimeException("Workflow $workflowId: Entity $entityType $id not found.");
|
||||
}
|
||||
|
||||
$this->service->triggerWorkflow($entity, $workflowId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/***********************************************************************************
|
||||
* The contents of this file are subject to the Extension License Agreement
|
||||
* ("Agreement") which can be viewed at
|
||||
* https://www.espocrm.com/extension-license-agreement/.
|
||||
* By copying, installing downloading, or using this file, You have unconditionally
|
||||
* agreed to the terms and conditions of the Agreement, and You may not use this
|
||||
* file except in compliance with the Agreement. Under the terms of the Agreement,
|
||||
* You shall not license, sublicense, sell, resell, rent, lease, lend, distribute,
|
||||
* redistribute, market, publish, commercialize, or otherwise transfer rights or
|
||||
* usage to the software or any modified version or derivative work of the software
|
||||
* created by or for you.
|
||||
*
|
||||
* Copyright (C) 2015-2025 EspoCRM, Inc.
|
||||
*
|
||||
* License ID: 19bc86a68a7bb01f458cb391d43a9212
|
||||
************************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Advanced\Tools\Workflow\Jobs;
|
||||
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Core\Job\Job;
|
||||
use Espo\Core\Job\Job\Data;
|
||||
use Espo\Core\Mail\Exceptions\NoSmtp;
|
||||
use Espo\Modules\Advanced\Tools\Workflow\SendEmailService;
|
||||
|
||||
class SendEmail implements Job
|
||||
{
|
||||
private SendEmailService $service;
|
||||
|
||||
public function __construct(SendEmailService $service)
|
||||
{
|
||||
$this->service = $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Error
|
||||
* @throws NoSmtp
|
||||
*/
|
||||
public function run(Data $data): void
|
||||
{
|
||||
$this->service->send($data->getRaw());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/***********************************************************************************
|
||||
* The contents of this file are subject to the Extension License Agreement
|
||||
* ("Agreement") which can be viewed at
|
||||
* https://www.espocrm.com/extension-license-agreement/.
|
||||
* By copying, installing downloading, or using this file, You have unconditionally
|
||||
* agreed to the terms and conditions of the Agreement, and You may not use this
|
||||
* file except in compliance with the Agreement. Under the terms of the Agreement,
|
||||
* You shall not license, sublicense, sell, resell, rent, lease, lend, distribute,
|
||||
* redistribute, market, publish, commercialize, or otherwise transfer rights or
|
||||
* usage to the software or any modified version or derivative work of the software
|
||||
* created by or for you.
|
||||
*
|
||||
* Copyright (C) 2015-2025 EspoCRM, Inc.
|
||||
*
|
||||
* License ID: 19bc86a68a7bb01f458cb391d43a9212
|
||||
************************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Advanced\Tools\Workflow\Jobs;
|
||||
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Core\Job\Job;
|
||||
use Espo\Core\Job\Job\Data;
|
||||
use Espo\Modules\Advanced\Tools\Workflow\Service;
|
||||
use Espo\ORM\EntityManager;
|
||||
|
||||
class TriggerWorkflow implements Job
|
||||
{
|
||||
public function __construct(
|
||||
private Service $service,
|
||||
private EntityManager $entityManager
|
||||
) {}
|
||||
|
||||
public function run(Data $data): void
|
||||
{
|
||||
$data = $data->getRaw();
|
||||
|
||||
if (
|
||||
empty($data->entityId) ||
|
||||
empty($data->entityType) ||
|
||||
empty($data->nextWorkflowId)
|
||||
) {
|
||||
throw new Error("Workflow[$data->workflowId][triggerWorkflow]: Not sufficient job data.");
|
||||
}
|
||||
|
||||
$entityId = $data->entityId;
|
||||
$entityType = $data->entityType;
|
||||
|
||||
$entity = $this->entityManager->getEntityById($entityType, $entityId);
|
||||
|
||||
if (!$entity) {
|
||||
throw new Error("Workflow[$data->workflowId][triggerWorkflow]: Entity not found.");
|
||||
}
|
||||
|
||||
$values = $data->values ?? null;
|
||||
|
||||
if (is_object($values)) {
|
||||
$values = get_object_vars($values);
|
||||
|
||||
foreach ($values as $attribute => $value) {
|
||||
$entity->setFetched($attribute, $value);
|
||||
}
|
||||
}
|
||||
|
||||
$this->service->triggerWorkflow($entity, $data->nextWorkflowId, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/***********************************************************************************
|
||||
* The contents of this file are subject to the Extension License Agreement
|
||||
* ("Agreement") which can be viewed at
|
||||
* https://www.espocrm.com/extension-license-agreement/.
|
||||
* By copying, installing downloading, or using this file, You have unconditionally
|
||||
* agreed to the terms and conditions of the Agreement, and You may not use this
|
||||
* file except in compliance with the Agreement. Under the terms of the Agreement,
|
||||
* You shall not license, sublicense, sell, resell, rent, lease, lend, distribute,
|
||||
* redistribute, market, publish, commercialize, or otherwise transfer rights or
|
||||
* usage to the software or any modified version or derivative work of the software
|
||||
* created by or for you.
|
||||
*
|
||||
* Copyright (C) 2015-2025 EspoCRM, Inc.
|
||||
*
|
||||
* License ID: 19bc86a68a7bb01f458cb391d43a9212
|
||||
************************************************************************************/
|
||||
|
||||
namespace Espo\Modules\Advanced\Tools\Workflow\Jobs;
|
||||
|
||||
use Espo\Core\Exceptions\Error;
|
||||
use Espo\Core\Job\Job;
|
||||
use Espo\Core\Job\Job\Data;
|
||||
use Espo\Core\Utils\Log;
|
||||
use Espo\Modules\Advanced\Entities\Workflow;
|
||||
use Espo\Modules\Advanced\Tools\Workflow\Core\TargetProvider;
|
||||
use Espo\Modules\Advanced\Tools\Workflow\Service;
|
||||
use Espo\ORM\EntityManager;
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
class TriggerWorkflowMany implements Job
|
||||
{
|
||||
public function __construct(
|
||||
private TargetProvider $targetProvider,
|
||||
private EntityManager $entityManager,
|
||||
private Service $service,
|
||||
private Log $log
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws Error
|
||||
*/
|
||||
public function run(Data $data): void
|
||||
{
|
||||
$workflowId = $data->get('nextWorkflowId');
|
||||
$entityId = $data->get('entityId');
|
||||
$entityType = $data->get('entityType');
|
||||
$target = $data->get('target');
|
||||
|
||||
if (!is_string($target)) {
|
||||
throw new RuntimeException("No target.");
|
||||
}
|
||||
|
||||
if (!is_string($workflowId)) {
|
||||
throw new RuntimeException("No nextWorkflowId.");
|
||||
}
|
||||
|
||||
if (!is_string($entityId)) {
|
||||
throw new RuntimeException("No entityId.");
|
||||
}
|
||||
|
||||
if (!is_string($entityType)) {
|
||||
throw new RuntimeException("No entityType.");
|
||||
}
|
||||
|
||||
$entity = $this->entityManager->getEntityById($entityType, $entityId);
|
||||
|
||||
if (!$entity) {
|
||||
return;
|
||||
}
|
||||
|
||||
$workflow = $this->entityManager->getRDBRepositoryByClass(Workflow::class)->getById($workflowId);
|
||||
|
||||
if (!$workflow) {
|
||||
throw new RuntimeException("No workflow $workflowId.");
|
||||
}
|
||||
|
||||
$targetEntityList = $this->targetProvider->get($entity, $target);
|
||||
|
||||
foreach ($targetEntityList as $targetEntity) {
|
||||
try {
|
||||
$this->service->triggerWorkflow($targetEntity, $workflowId);
|
||||
}
|
||||
catch (Exception $e) {
|
||||
$this->log->error("Trigger workflow $workflowId for entity $entityId: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user