diff --git a/workflow/engine/classes/class.pmDynaform.php b/workflow/engine/classes/class.pmDynaform.php index 9feb9717c..348a9d710 100644 --- a/workflow/engine/classes/class.pmDynaform.php +++ b/workflow/engine/classes/class.pmDynaform.php @@ -196,8 +196,7 @@ class pmDynaform $fn($json, $key, $value); } //set properties from trigger - $prefixs = self::$prefixs; - if (is_string($value) && in_array(substr($value, 0, 2), $prefixs)) { + if (is_string($value) && in_array(substr($value, 0, 2), self::$prefixs)) { $triggerValue = substr($value, 2); if (isset($this->fields["APP_DATA"][$triggerValue])) { if (!in_array($key, $this->propertiesToExclude)) { @@ -255,7 +254,6 @@ class pmDynaform } } } - $dtFields = array_merge($this->fields['APP_DATA'], $dtFields); $sql = G::replaceDataField($json->sql, $dtFields); if ($value === "suggest") { $sql = $this->sqlParse($sql, function($parsed, &$select, &$from, &$where, &$groupBy, &$having, &$orderBy, &$limit) use ($json) { @@ -268,7 +266,9 @@ class pmDynaform if (!isset($json->queryField) && isset($dt[0]["base_expr"])) { $col = $dt[0]["base_expr"]; $dv = str_replace("'", "''", $json->defaultValue); - $where = $isWhere ? "WHERE " . $col . "='" . $dv . "'" : $where . " AND " . $col . "='" . $dv . "'"; + if ($dv !== "") { + $where = $isWhere ? "WHERE " . $col . "='" . $dv . "'" : $where . " AND " . $col . "='" . $dv . "'"; + } } if (isset($json->queryField) && isset($dt[0]["base_expr"])) { $col = isset($dt[1]["base_expr"]) ? $dt[1]["base_expr"] : $dt[0]["base_expr"]; @@ -723,9 +723,9 @@ class pmDynaform } $data = array(); if (isset($json->dbConnection) && isset($json->sql)) { - $salida = array(); - preg_match_all('/\@(?:([\@\%\#\=\!Qq])([a-zA-Z\_]\w*)|([a-zA-Z\_][\w\-\>\:]*)\(((?:[^\\\\\)]*?)*)\))/', $json->sql, $salida, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE); - $variables = isset($salida[2]) ? $salida[2] : array(); + $result = array(); + preg_match_all('/\@(?:([\@\%\#\=\!Qq])([a-zA-Z\_]\w*)|([a-zA-Z\_][\w\-\>\:]*)\(((?:[^\\\\\)]*?)*)\))/', $json->sql, $result, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE); + $variables = isset($result[2]) ? $result[2] : array(); foreach ($variables as $key => $value) { $jsonSearch = $this->jsonsf(G::json_decode($this->record["DYN_CONTENT"]), $value[0], $json->variable === "" ? "id" : "variable"); $a = $this->getValuesDependentFields($jsonSearch); @@ -734,8 +734,15 @@ class pmDynaform } } if ($json->dbConnection !== "" && $json->dbConnection !== "none" && $json->sql !== "") { - $a = G::replaceDataField($json->sql, $data); - $dt = $this->getCacheQueryData($json->dbConnection, $a, $json->type); + if (isset($this->fields["APP_DATA"])) { + foreach ($this->fields["APP_DATA"] as $keyA => $valueA) { + if (!isset($data[$keyA]) && !is_array($valueA)) { + $data[$keyA] = $valueA; + } + } + } + $sql = G::replaceDataField($json->sql, $data); + $dt = $this->getCacheQueryData($json->dbConnection, $sql, $json->type); $row = isset($dt[0]) ? $dt[0] : []; if (isset($row[0]) && $json->type !== "suggest" && $json->type !== "radio") { $data[$json->variable === "" ? $json->id : $json->variable] = $row[0]; diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php index fac3bf41e..bd7d249f5 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php @@ -552,49 +552,7 @@ class Variable public function executeSql($processUid, $variableName, array $arrayVariable = array()) { try { - $arrayRecord = array(); - - //Verify data - $process = new \ProcessMaker\BusinessModel\Process(); - - $process->throwExceptionIfNotExistsProcess($processUid, strtolower("PRJ_UID")); - - //Set data - \G::LoadClass('pmDynaform'); - $pmDynaform = new \pmDynaform(); - $field = $pmDynaform->searchField($arrayVariable["dyn_uid"], $arrayVariable["field_id"], $processUid); - $dbConnection = "workflow"; - if ($field !== null && !empty($field->dbConnection)) { - $dbConnection = $field->dbConnection; - } - $variableSql = $field !== null ? $field->sql : ""; - - //Get data - $_SESSION["PROCESS"] = $processUid; - - $cnn = \Propel::getConnection($dbConnection); - $stmt = $cnn->createStatement(); - if (isset($arrayVariable['app_uid']) && !empty($arrayVariable['app_uid'])) { - $delIndex = isset($arrayVariable["del_index"]) ? $arrayVariable["del_index"] : 0; - $case = new \Cases(); - $fields = $case->loadCase($arrayVariable['app_uid'], (int) $delIndex); - $arrayVariable = array_merge($fields['APP_DATA'], \ProcessMaker\BusinessModel\Cases::getGlobalVariables($fields['APP_DATA'])); - } - $sql = G::replaceDataField($variableSql, $arrayVariable); - - $rs = $stmt->executeQuery($sql, \ResultSet::FETCHMODE_NUM); - - while ($rs->next()) { - $row = $rs->getRow(); - - $arrayRecord[] = array( - strtolower("VALUE") => $row[0], - strtolower("TEXT") => isset($row[1]) ? $row[1] : $row[0] - ); - } - - //Return - return $arrayRecord; + return $this->executeSqlControl($processUid, $arrayVariable); } catch (\Exception $e) { throw $e; } @@ -715,33 +673,7 @@ class Variable public function executeSqlSuggest($processUid, $variableName, array $arrayVariable = array()) { try { - $appData = array(); - if (isset($arrayVariable['app_uid'])) { - $case = new \ProcessMaker\BusinessModel\Cases(); - $fields = $case->getApplicationRecordByPk($arrayVariable['app_uid'], ['$applicationUid' => 'app_uid']); - $case = new \Cases(); - $appData = $case->unserializeData($fields['APP_DATA']); - $appData = array_merge($appData, \ProcessMaker\BusinessModel\Cases::getGlobalVariables($appData)); - $arrayVariable = array_merge($appData, $arrayVariable); - } - $_SESSION["PROCESS"] = $processUid; - \G::LoadClass("pmDynaform"); - $pmDynaform = new \pmDynaform(array("APP_DATA" => $appData)); - $field = $pmDynaform->searchField($arrayVariable["dyn_uid"], $arrayVariable["field_id"], $processUid); - $field->queryField = true; - $field->queryInputData = $arrayVariable; - $field->queryFilter = isset($arrayVariable["filter"]) ? $arrayVariable["filter"] : ""; - $field->queryStart = isset($arrayVariable["start"]) ? $arrayVariable["start"] : 0; - $field->queryLimit = isset($arrayVariable["limit"]) ? $arrayVariable["limit"] : 10; - $pmDynaform->jsonr($field); - - $result = array(); - if (isset($field->queryOutputData) && is_array($field->queryOutputData)) { - foreach ($field->queryOutputData as $item) { - $result[] = ["value" => $item->value, "text" => $item->label]; - } - } - return $result; + return $this->executeSqlControl($processUid, $arrayVariable); } catch (\Exception $e) { throw $e; } @@ -825,4 +757,82 @@ class Variable } return $vType; } + + /** + * Executes the sql string of a control and returns the data in the queryOutputData + * property of the control. The control returned by the pmDynaform :: searchField + * function is the php representation of the json definition, which can be + * supported by the pmDynaform :: jsonr function. + * The params parameter must contain: dyn_uid, field_id and optionally + * app_uid, del_index, filter, start, limit, and so many related control variables + * to be sent and their corresponding value. + * The parameters: filter, start and limit, are only necessary for the suggest + * control. + * If app_uid is not sent you can not get the appData in an environment where + * only endPoint is used, it is always advisable to send the app_uid and _index. + * Note: You do not get triguer execution values where only endPoint is used. + * @param type $proUid + * @param array $params + * @return array + * @throws \Exception + */ + public function executeSqlControl($proUid, array $params = array()) + { + try { + //Get and clear vector data that does not correspond to variables + //related to a control. + $dynUid = $params["dyn_uid"]; + $fieldId = $params["field_id"]; + $filter = isset($params["filter"]) ? $params["filter"] : ""; + $start = isset($params["start"]) ? $params["start"] : 0; + $limit = isset($params["limit"]) ? $params["limit"] : 10; + $appUid = empty($params["app_uid"]) ? null : $params["app_uid"]; + $delIndex = (int) isset($params["del_index"]) ? $params["del_index"] : 0; + unset($params["dyn_uid"]); + unset($params["field_id"]); + unset($params["app_uid"]); + unset($params["del_index"]); + unset($params["filter"]); + unset($params["start"]); + unset($params["limit"]); + + //Get appData and system variables + if ($appUid !== null) { + $case = new \Cases(); + $fields = $case->loadCase($appUid, $delIndex); + $appData = $fields["APP_DATA"]; + $appData = array_merge($appData, \ProcessMaker\BusinessModel\Cases::getGlobalVariables($appData)); + $params = array_merge($params, $appData); + } + + //This value is required to be able to query the database. + $_SESSION["PROCESS"] = $proUid; + //The pmdynaform class is instantiated + \G::LoadClass("pmDynaform"); + $pmDynaform = new \pmDynaform(array("APP_DATA" => $params)); + + //Get control from dynaform. + //The parameters: queryFilter, queryStart, queryLimit, are only necessary + //for the suggest control, the rest of the controls are ignored. + $field = $pmDynaform->searchField($dynUid, $fieldId, $proUid); + $field->queryField = true; + $field->queryInputData = $params; + $field->queryFilter = $filter; + $field->queryStart = $start; + $field->queryLimit = $limit; + + //Populate control data + $pmDynaform->jsonr($field); + $result = array(); + if (isset($field->queryOutputData) && is_array($field->queryOutputData)) { + foreach ($field->queryOutputData as $item) { + $result[] = ["value" => $item->value, "text" => $item->label]; + } + } + return $result; + } catch (\Exception $e) { + throw $e; + } + } + }