Merged in bugfix/PMC-2 (pull request #6697)

PMC-2

Approved-by: Julio Cesar Laura Avendaño <contact@julio-laura.com>
This commit is contained in:
Paula Quispe
2018-11-23 20:31:04 +00:00
committed by Julio Cesar Laura Avendaño
5 changed files with 210 additions and 111 deletions

View File

@@ -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("<br /><b>" . $labelAssignment . "</b><br />");
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 .= "<br />- " . $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 = "&nbsp;- " . nl2br(htmlentities($trigger["TRI_TITLE"], ENT_QUOTES)) . "<br/>";
$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 .= "<br />- " . $triTitle . ": " . $codeAndLine;
}
}
/*----------------------------------********---------------------------------*/
return $foundDisabledCode;
}
/**
* Get the trigger's names
* @name getTriggerNames

View File

@@ -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 . "<br /><b>" . $labelAssignment . "</b><br />";
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 . "&nbsp;- " . nl2br(htmlentities(
$trigger->getTriTitle(),
ENT_QUOTES
)) . "<br />";
$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;

View File

@@ -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]] : ''