Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/cvetdv.ru/bitrix/modules/bizproc/lib/automation/trigger/ |
Upload File : |
<?php namespace Bitrix\Bizproc\Automation\Trigger; use Bitrix\Bizproc\Automation\Engine\ConditionGroup; use Bitrix\Bizproc\Automation\Target\BaseTarget; use Bitrix\Bizproc\Workflow\Template\WorkflowTemplateSettingsTable; use Bitrix\Main; class BaseTrigger { protected $target; protected $returnValues; /** * @return string the fully qualified name of this class. */ public static function className() { return get_called_class(); } public static function isEnabled() { return true; } /** * @param BaseTarget $target * @return $this */ public function setTarget(BaseTarget $target) { $this->target = $target; return $this; } /** * @return BaseTarget * @throws Main\InvalidOperationException */ public function getTarget() { if ($this->target === null) { throw new Main\InvalidOperationException('Target must be set by setTarget method.'); } return $this->target; } /** * @return string Gets the alphanumeric trigger code. */ public static function getCode() { return 'BASE'; } /** * @return string Gets the trigger name. */ public static function getName() { return 'Base trigger'; } public function send() { $this->runWorkflowTriggers(); $applied = false; $triggers = $this->getPotentialTriggers(); if ($triggers) { foreach ($triggers as $trigger) { if ($this->checkApplyRules($trigger)) { $this->applyTrigger($trigger); $applied = true; break; } } } return $applied; } protected function runWorkflowTriggers(): void { if ( !\CBPRuntime::isFeatureEnabled() || !\Bitrix\Main\Config\Option::get('bizproc', 'bp_triggers', false) ) { return; } try { [$module, $entity, $documentType] = \CBPHelper::parseDocumentId($this->target->getDocumentType()); } catch (\CBPArgumentNullException $exception) { return; } $code = static::getCode(); $triggers = WorkflowTemplateSettingsTable::query() ->setSelect(['TEMPLATE', 'TEMPLATE_ID']) ->where('NAME', "TRIGGER_$code") ->where('VALUE', 'Y') ->where('TEMPLATE.MODULE_ID', $module) ->where('TEMPLATE.ENTITY', $entity) ->where('TEMPLATE.DOCUMENT_TYPE', $documentType) ->exec() ->fetchCollection() ; if ($triggers) { $complexDocumentId = $this->target->getComplexDocumentId(); foreach ($triggers as $trigger) { $errors = []; \CBPDocument::startWorkflow($trigger->getTemplateId(), $complexDocumentId, [], $errors); } } } protected function applyTrigger(array $trigger) { $statusId = $trigger['DOCUMENT_STATUS']; $target = $this->getTarget(); $target->setAppliedTrigger($trigger); $target->setDocumentStatus($statusId); $target->getRuntime()->onDocumentStatusChanged(); return true; } protected function getPotentialTriggers() { $triggers = []; $currentStatus = $this->getTarget()->getDocumentStatus(); $allStatuses = array_keys($this->getTarget()->getDocumentStatusList()); $needleKey = array_search($currentStatus, $allStatuses, false); if ($needleKey === false) { return $triggers; } $forwardStatuses = array_slice($allStatuses, $needleKey + 1); unset($allStatuses[$needleKey]); $code = static::getCode(); $rows = []; $targetTriggers = $this->getTarget()->getTriggers($allStatuses); foreach ($targetTriggers as $row) { if ($row['CODE'] !== $code) { continue; } if (!in_array($row['DOCUMENT_STATUS'], $forwardStatuses, false)) { if ( !isset($row['APPLY_RULES']['ALLOW_BACKWARDS']) || $row['APPLY_RULES']['ALLOW_BACKWARDS'] !== 'Y' ) { continue; } } $rows[$row['DOCUMENT_STATUS']][] = $row; } if ($rows) { foreach ($allStatuses as $needleStatus) { if (isset($rows[$needleStatus])) { $triggers[] = $rows[$needleStatus]; } } } return array_merge(...$triggers); } public function checkApplyRules(array $trigger) { $conditionRules = $trigger['APPLY_RULES']['Condition'] ?? null; if ($conditionRules) { $conditionGroup = new ConditionGroup($conditionRules); $target = $this->getTarget(); $result = $conditionGroup->evaluate($target); if ($result) { $target->setAppliedTriggerConditionResults($conditionGroup->getEvaluateResults()); } return $result; } return true; } public function getReturnValues(): ?array { return $this->returnValues; } /** * @param array $values * @return $this */ public function setReturnValues(array $values) { $this->returnValues = $values; return $this; } public static function getReturnProperties(): array { return []; } public static function toArray() { return [ 'NAME' => static::getName(), 'CODE' => static::getCode(), 'RETURN' => static::getReturnProperties(), 'DESCRIPTION' => static::getDescription(), 'GROUP' => static::getGroup(), 'SETTINGS' => static::getSettings(), ]; } public static function getDescription(): string { return ''; } public static function getGroup(): array { return []; } protected static function getSettings(): ?array { $map = static::getPropertiesMap(); if ($map) { return ['Properties' => array_values($map)]; } return null; } protected static function getPropertiesMap(): array { return []; } }