From 7fff95b0f6a150db6592d11b5109f4d2c7f4519a Mon Sep 17 00:00:00 2001 From: Paula Quispe Date: Wed, 21 Nov 2018 16:32:48 -0400 Subject: [PATCH] PMC-2 --- workflow/engine/classes/Cases.php | 167 +++++++++++++++--- workflow/engine/classes/WsBase.php | 111 +++++------- workflow/engine/classes/class.pmScript.php | 29 ++- .../engine/methods/cases/debug_triggers.php | 10 +- workflow/engine/methods/cases/debug_vars.php | 4 +- 5 files changed, 210 insertions(+), 111 deletions(-) diff --git a/workflow/engine/classes/Cases.php b/workflow/engine/classes/Cases.php index f3b0a64c8..125df6d32 100644 --- a/workflow/engine/classes/Cases.php +++ b/workflow/engine/classes/Cases.php @@ -8,7 +8,6 @@ use ProcessMaker\ChangeLog\ChangeLog; use ProcessMaker\Core\System; use ProcessMaker\Plugins\PluginRegistry; - /** * A Cases object where you can do start, load, update, refresh about cases * This object is applied to Task @@ -20,6 +19,7 @@ class Cases public $dir = 'ASC'; public $sort = 'APP_MSG_DATE'; public $arrayTriggerExecutionTime = []; + private $triggerMessageExecution = ''; public function __construct() { @@ -29,6 +29,28 @@ class Cases } } + /** + * Get the triggerMessageExecution + * + * @return string + */ + public function getTriggerMessageExecution() + { + return $this->triggerMessageExecution; + } + + /** + * Add messages related to the trigger execution + * + * @param string $v + * + * @return void + */ + public function addTriggerMessageExecution($v) + { + $this->triggerMessageExecution .= $v; + } + /** * Ask if an user can start a case * @param string $sUIDUser @@ -3438,61 +3460,86 @@ class Cases */ public function executeTriggers($tasUid, $stepType, $stepUidObj, $triggerType, $fieldsCase = []) { + //Load the triggers assigned in the step + $triggersList = $this->loadTriggers($tasUid, $stepType, $stepUidObj, $triggerType); + + //Execute the trigger defined in the step + $lastFields = $this->executeTriggerFromList($triggersList, $fieldsCase, $stepType, $stepUidObj, $triggerType); + /*----------------------------------********---------------------------------*/ ChangeLog::getChangeLog() ->setObjectUid($stepUidObj) ->getExecutedAtIdByTriggerType($triggerType); /*----------------------------------********---------------------------------*/ - $listTriggers = $this->loadTriggers($tasUid, $stepType, $stepUidObj, $triggerType); + return $lastFields; + } - if (count($listTriggers) > 0) { + /** + * This method executes the triggers send in an array + * + * @param array $triggersList + * @param array $fieldsCase + * @param string $stepType + * @param string $stepUidObj + * @param string $triggerType + * @param string $labelAssignment + * + * @return array + */ + public function executeTriggerFromList( + array $triggersList, + array $fieldsCase, + $stepType, + $stepUidObj, + $triggerType, + $labelAssignment = '' + ) + { + if (count($triggersList) > 0) { global $oPMScript; - $oPMScript = new PMScript(); + $this->addTriggerMessageExecution("
" . $labelAssignment . "
"); + if (!isset($oPMScript)) { + $oPMScript = new PMScript(); + } $oPMScript->setFields($fieldsCase); /*----------------------------------********---------------------------------*/ $cs = new CodeScanner(config("system.workspace")); - $strFoundDisabledCode = ""; + $foundDisabledCode = ""; /*----------------------------------********---------------------------------*/ - foreach ($listTriggers as $trigger) { - /*----------------------------------********---------------------------------*/ - if (PMLicensedFeatures::getSingleton()->verifyfeature("B0oWlBLY3hHdWY0YUNpZEtFQm5CeTJhQlIwN3IxMEkwaG4=")) { - //Check disabled code - $arrayFoundDisabledCode = $cs->checkDisabledCode("SOURCE", $trigger["TRI_WEBBOT"]); - - if (!empty($arrayFoundDisabledCode)) { - $strCodeAndLine = ""; - - foreach ($arrayFoundDisabledCode["source"] as $key => $value) { - $strCodeAndLine .= (($strCodeAndLine != "") ? ", " : "") . G::LoadTranslation("ID_DISABLED_CODE_CODE_AND_LINE", array($key, implode(", ", $value))); - } - - $strFoundDisabledCode .= "
- " . $trigger["TRI_TITLE"] . ": " . $strCodeAndLine; - continue; - } + $fieldsTrigger = []; + foreach ($triggersList as $trigger) { + //Scan the code + $disabledCode = $this->codeScannerReview($cs, $trigger["TRI_WEBBOT"], $trigger["TRI_TITLE"]); + if (!empty($disabledCode)) { + $foundDisabledCode .= $disabledCode; + continue; } - /*----------------------------------********---------------------------------*/ - //Execute $execute = true; - - if ($trigger['ST_CONDITION'] !== '') { + //Check if the trigger has conditions for the execution + if (!empty($trigger['ST_CONDITION'])) { $oPMScript->setDataTrigger($trigger); $oPMScript->setScript($trigger['ST_CONDITION']); $oPMScript->setExecutedOn(PMScript::CONDITION); $execute = $oPMScript->evaluate(); } + //Execute the trigger if ($execute) { $oPMScript->setDataTrigger($trigger); $oPMScript->setScript($trigger['TRI_WEBBOT']); $executedOn = $oPMScript->getExecutionOriginForAStep($stepType, $stepUidObj, $triggerType); $oPMScript->setExecutedOn($executedOn); $oPMScript->execute(); - $fieldsTrigger = $oPMScript->aFields; + $varsChanged = $oPMScript->getVarsChanged(); + $appDataAfterTrigger = $oPMScript->aFields; + + //Get the key and values changed + $fieldsTrigger = $this->findKeysAndValues($appDataAfterTrigger, $varsChanged); //We will be load the last appData if ($oPMScript->executedOn() === $oPMScript::AFTER_ROUTING) { @@ -3503,21 +3550,85 @@ class Cases } } + //Register the time execution $this->arrayTriggerExecutionTime[$trigger['TRI_UID']] = $oPMScript->scriptExecutionTime; + //Register the message of execution + $varTriggers = " - " . nl2br(htmlentities($trigger["TRI_TITLE"], ENT_QUOTES)) . "
"; + $this->addTriggerMessageExecution($varTriggers); } } + /*----------------------------------********---------------------------------*/ - if ($strFoundDisabledCode != "") { - G::SendTemporalMessage(G::LoadTranslation("ID_DISABLED_CODE_TRIGGER_TO_EXECUTE", array($strFoundDisabledCode)), "", "string"); + if (!empty($foundDisabledCode)) { + G::SendTemporalMessage( + G::LoadTranslation("ID_DISABLED_CODE_TRIGGER_TO_EXECUTE", [$foundDisabledCode]), + "", + "string" + ); } /*----------------------------------********---------------------------------*/ + //The Code Scanner can be interrupt the execution + if (empty($fieldsTrigger)) { + return $fieldsCase; + } + return $fieldsTrigger; } else { return $fieldsCase; } } + /** + * Find keys and values into the appData + * + * @param array $appData + * @param array $keyToSearch + * + * @return array + */ + private function findKeysAndValues(array $appData, array $keyToSearch) + { + $keysAndValues = []; + foreach ($keyToSearch as $key) { + $keysAndValues[$key] = $appData[$key]; + } + + return $keysAndValues; + } + + /** + * Review the code in the trigger if the feature is enable + * + * @param CodeScanner $cs + * @param string $code + * @param string $triTitle + * + * @return string + * + */ + private function codeScannerReview(CodeScanner $cs, $code, $triTitle) + { + $foundDisabledCode = ""; + /*----------------------------------********---------------------------------*/ + if (PMLicensedFeatures::getSingleton()->verifyfeature("B0oWlBLY3hHdWY0YUNpZEtFQm5CeTJhQlIwN3IxMEkwaG4=")) { + //Check disabled code + $arrayFoundDisabledCode = $cs->checkDisabledCode("SOURCE", $code); + + if (!empty($arrayFoundDisabledCode)) { + $codeAndLine = ""; + foreach ($arrayFoundDisabledCode["source"] as $key => $value) { + $codeAndLine .= (($codeAndLine != "") ? ", " : "") . G::LoadTranslation("ID_DISABLED_CODE_CODE_AND_LINE", + [$key, implode(", ", $value)]); + } + $foundDisabledCode .= "
- " . $triTitle . ": " . $codeAndLine; + } + } + /*----------------------------------********---------------------------------*/ + + return $foundDisabledCode; + } + /** * Get the trigger's names * @name getTriggerNames diff --git a/workflow/engine/classes/WsBase.php b/workflow/engine/classes/WsBase.php index 489fdd533..ae157522c 100644 --- a/workflow/engine/classes/WsBase.php +++ b/workflow/engine/classes/WsBase.php @@ -2169,7 +2169,7 @@ class WsBase * Execute the trigger defined in the steps * This function is used when the case is derived from abe, Soap, PMFDerivateCase * - * @param string $caseId , Uid related to the case + * @param string $appUid , Uid related to the case * @param array $appData , contain all the information about the case related to the index [APP_DATA] * @param string $tasUid , Uid related to the task * @param string $stepType , before or after step @@ -2180,7 +2180,7 @@ class WsBase * @return string $varTriggers updated */ public function executeTriggerFromDerivate( - $caseId, + $appUid, &$appData, $tasUid, $stepType, @@ -2189,80 +2189,49 @@ class WsBase $labelAssignment = '' ) { - $varTriggers = ""; - $oCase = new Cases(); + $caseInstance = new Cases(); - //Load the triggers assigned in the $triggerType - $aTriggers = $oCase->loadTriggers($tasUid, $stepType, $stepUidObj, $triggerType); - - if (count($aTriggers) > 0) { - $varTriggers = $varTriggers . "
" . $labelAssignment . "
"; - - global $oPMScript; - - if (!isset($oPMScript)) { - $oPMScript = new PMScript(); - } - - foreach ($aTriggers as $aTrigger) { - //Set variables - $params = new stdClass(); - $params->appData = $appData; - - if ($this->stored_system_variables) { - $params->option = "STORED SESSION"; - $params->SID = $this->wsSessionId; - } - - //We can set the index APP_DATA - $appFields["APP_DATA"] = array_merge($appData, G::getSystemConstants($params)); - - //PMScript - $oPMScript->setFields($appFields['APP_DATA']); - $bExecute = true; - - if ($aTrigger['ST_CONDITION'] !== '') { - $oPMScript->setScript($aTrigger['ST_CONDITION']); - $oPMScript->setExecutedOn(PMScript::CONDITION); - $bExecute = $oPMScript->evaluate(); - } - - if ($bExecute) { - $oPMScript->setDataTrigger($aTrigger); - $oPMScript->setScript($aTrigger['TRI_WEBBOT']); - $executedOn = $oPMScript->getExecutionOriginForAStep($stepType, $stepUidObj, $triggerType); - $oPMScript->setExecutedOn($executedOn); - $oPMScript->execute(); - - $trigger = TriggersPeer::retrieveByPk($aTrigger["TRI_UID"]); - $varTriggers = $varTriggers . " - " . nl2br(htmlentities( - $trigger->getTriTitle(), - ENT_QUOTES - )) . "
"; - - $appFields['APP_DATA'] = $oPMScript->aFields; - unset($appFields['APP_STATUS']); - unset($appFields['APP_PROC_STATUS']); - unset($appFields['APP_PROC_CODE']); - unset($appFields['APP_PIN']); - $oCase->updateCase($caseId, $appFields); - - //We need to update the variable $appData for use the new variables in the next trigger - $appData = array_merge($appData, $appFields['APP_DATA']); - } - } + //Set new variables in the APP_DATA + $params = new stdClass(); + $params->appData = $appData; + if ($this->stored_system_variables) { + $params->option = "STORED SESSION"; + $params->SID = $this->wsSessionId; } + $appFields["APP_DATA"] = array_merge($appData, G::getSystemConstants($params)); + + //Load the triggers assigned in the step + $triggersList = $caseInstance->loadTriggers($tasUid, $stepType, $stepUidObj, $triggerType); + + //Execute the trigger defined in the step + $lastFields = $caseInstance->executeTriggerFromList($triggersList, $appFields["APP_DATA"], $stepType, $stepUidObj, $triggerType, $labelAssignment); + + //Get message of the execution + $varTriggers = $caseInstance->getTriggerMessageExecution(); + + //Define the new APP_DATA + $appFields['APP_DATA'] = $lastFields; + unset($appFields['APP_STATUS']); + unset($appFields['APP_PROC_STATUS']); + unset($appFields['APP_PROC_CODE']); + unset($appFields['APP_PIN']); + + //Update the APP_DATA + $caseInstance->updateCase($appUid, $appFields); + + //We need to update the variable $appData for use the new variables in the next trigger + $appData = array_merge($appData, $appFields['APP_DATA']); /*----------------------------------********---------------------------------*/ ChangeLog::getChangeLog() - ->setDate('now') - ->setAppNumber($appData['APP_NUMBER']) - ->setDelIndex($appData['INDEX']) - ->getProIdByProUid($appData['PROCESS']) - ->getTasIdByTasUid($appData['TASK']) - ->setData(serialize($appData)) - ->getExecutedAtIdByTriggerType($triggerType) - ->getObjectIdByUidAndObjType($stepUidObj, $stepType); + ->setDate('now') + ->setAppNumber($appData['APP_NUMBER']) + ->setDelIndex($appData['INDEX']) + ->getProIdByProUid($appData['PROCESS']) + ->getTasIdByTasUid($appData['TASK']) + ->setData(serialize($appData)) + ->getExecutedAtIdByTriggerType($triggerType) + ->getObjectIdByUidAndObjType($stepUidObj, $stepType); /*----------------------------------********---------------------------------*/ return $varTriggers; diff --git a/workflow/engine/classes/class.pmScript.php b/workflow/engine/classes/class.pmScript.php index 94c68265b..83585e2d5 100644 --- a/workflow/engine/classes/class.pmScript.php +++ b/workflow/engine/classes/class.pmScript.php @@ -117,6 +117,11 @@ class PMScript */ protected $executedOn = self::UNDEFINED_ORIGIN; + /** + * Variables changed in the trigger execution + */ + private $varsChanged = []; + /** * Constructor of the class PMScript * @@ -127,6 +132,28 @@ class PMScript $this->aFields['__ERROR__'] = 'none'; } + /** + * Set the fields changed in the trigger execution + * + * @param array $v + * + * @return void + */ + public function setVarsChanged(array $v) + { + $this->varsChanged = $v; + } + + /** + * Get the fields changed in the trigger execution + * + * @return array + */ + public function getVarsChanged() + { + return $this->varsChanged; + } + /** * Set the fields to use * @@ -437,7 +464,7 @@ class PMScript $sScript = "try {\n" . $sScript . "\n} catch (Exception \$oException) {\n " . " \$this->aFields['__ERROR__'] = utf8_encode(\$oException->getMessage());\n}"; $this->executeAndCatchErrors($sScript, $this->sScript); - + $this->setVarsChanged($this->affected_fields); $this->aFields["__VAR_CHANGED__"] = implode(",", $this->affected_fields); for ($i = 0; $i < count($this->affected_fields); $i ++) { $_SESSION['TRIGGER_DEBUG']['DATA'][] = Array('key' => $this->affected_fields[$i], 'value' => isset($this->aFields[$this->affected_fields[$i]]) ? $this->aFields[$this->affected_fields[$i]] : '' diff --git a/workflow/engine/methods/cases/debug_triggers.php b/workflow/engine/methods/cases/debug_triggers.php index 3866748d7..6fa6a9df3 100644 --- a/workflow/engine/methods/cases/debug_triggers.php +++ b/workflow/engine/methods/cases/debug_triggers.php @@ -29,7 +29,7 @@ foreach ($aTriggers as $aTrigger) { $triggersList[$i]['code'] = $geshi->parse_code(); //$aTrigger['TRIGGERS_VALUES'][$index]['TRI_WEBBOT']; $triggerUid = $aTrigger['TRIGGERS_VALUES'][$index]['TRI_UID']; - $triggersList[$i]['script_execution_time'] = $aTrigger['TRIGGERS_EXECUTION_TIME'][$triggerUid]; + $triggersList[$i]['script_execution_time'] = isset($aTrigger['TRIGGERS_EXECUTION_TIME'][$triggerUid]) ? $aTrigger['TRIGGERS_EXECUTION_TIME'][$triggerUid] : ''; $i ++; } @@ -58,14 +58,6 @@ foreach ($DEBUG_ERRORS as $error) { $i ++; } } - -/*echo '{total:5, data:[ - {name:"trigger1", execution_time:"after"}, - {name:"trigger2", execution_time:"before"}, - {name:"trigger13", execution_time:"before"}, - ]}'; - - */ $triggersRet = new StdClass(); $triggersRet->total = count( $triggersList ); $triggersRet->data = $triggersList; diff --git a/workflow/engine/methods/cases/debug_vars.php b/workflow/engine/methods/cases/debug_vars.php index 1c4751eed..e340b19f4 100644 --- a/workflow/engine/methods/cases/debug_vars.php +++ b/workflow/engine/methods/cases/debug_vars.php @@ -81,8 +81,8 @@ switch ($request) { $systemConstants = G::getSystemConstants(); //Add missing items - $systemConstants['PIN'] = $aVars['PIN']; - $systemConstants['APP_NUMBER'] = $aVars['APP_NUMBER']; + $systemConstants['PIN'] = isset($aVars['PIN']) ? $aVars['PIN'] : ''; + $systemConstants['APP_NUMBER'] = isset($aVars['APP_NUMBER']) ? $aVars['APP_NUMBER'] : ''; //when a case with dynaform is started there are no changed variables, this event is validated if (isset($aVars['__VAR_CHANGED__'])) { $systemConstants['__VAR_CHANGED__'] = $aVars['__VAR_CHANGED__'];