PMCORE-1677 Dependent variable needs to search the data outside the grid if the prefix is @?

This commit is contained in:
Roly Rudy Gutierrez Pinto
2020-06-19 11:49:18 -04:00
committed by Paula Quispe
parent e5d42aa682
commit d9cc26ca37
6 changed files with 801 additions and 50 deletions

View File

@@ -22,6 +22,7 @@ class PmDynaform
private $lastQueryError = null;
private $propertiesToExclude = [];
private $sysSys = null;
private $fieldsAppData;
public $credentials = null;
public $displayMode = null;
public $fields = null;
@@ -91,6 +92,7 @@ class PmDynaform
if (is_array($this->fields) && !isset($this->fields["APP_UID"])) {
$this->fields["APP_UID"] = null;
}
$this->fieldsAppData = isset($this->fields["APP_DATA"]) ? $this->fields["APP_DATA"] : [];
//todo: compatibility checkbox
if ($this->record !== null && isset($this->record["DYN_CONTENT"]) && $this->record["DYN_CONTENT"] !== "") {
@@ -338,7 +340,7 @@ class PmDynaform
}
}
}
$sql = G::replaceDataField($json->sql, $dtFields, 'mysql', false);
$sql = $this->replaceDataField($json->sql, $dtFields);
if ($value === "suggest") {
$sql = $this->prepareSuggestSql($sql, $json);
}
@@ -686,6 +688,8 @@ class PmDynaform
$json->dataGridEnvironment = "onDataGridEnvironment";
if (isset($this->fields["APP_DATA"])) {
$dataGridEnvironment = $this->fields["APP_DATA"];
//Grids only access the global variables of 'ProcessMaker', other variables are removed.
$this->fields["APP_DATA"] = Cases::getGlobalVariables($this->fields["APP_DATA"]);
//restore AppData with dataVariable definition, only for columns control
foreach ($columnsDataVariable as $dge) {
if (isset($dataGridEnvironment[$dge])) {
@@ -833,7 +837,12 @@ class PmDynaform
}
}
private function getValuesDependentFields($json)
/**
* Get the values of the dependent references.
* @param object $json
* @return array
*/
private function getValuesDependentFields($json): array
{
if (!isset($this->record["DYN_CONTENT"])) {
return array();
@@ -848,7 +857,7 @@ class PmDynaform
}
if (isset($json->dbConnection) && isset($json->sql)) {
$result = array();
preg_match_all('/\@(?:([\@\%\#\=\!Qq])([a-zA-Z\_]\w*)|([a-zA-Z\_][\w\-\>\:]*)\(((?:[^\\\\\)]*?)*)\))/', $json->sql, $result, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE);
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) {
//Prevents an infinite cycle. If the name of the variable is used within its own dependent.
@@ -863,7 +872,7 @@ class PmDynaform
}
}
if ($json->dbConnection !== "" && $json->dbConnection !== "none" && $json->sql !== "") {
$sql = G::replaceDataField($json->sql, $data, 'mysql', false);
$sql = $this->replaceDataField($json->sql, $data);
$dt = $this->getCacheQueryData($json->dbConnection, $sql, $json->type);
$row = isset($dt[0]) ? $dt[0] : [];
$index = $json->variable === "" ? $json->id : $json->variable;
@@ -1780,12 +1789,11 @@ class PmDynaform
* @param string $dynUid
* @param string $fieldId
* @param string $proUid
*
* @param array $and
* @return object
*
* @see \ProcessMaker\BusinessModel\Variable::executeSqlControl()
*/
public function searchField($dynUid, $fieldId, $proUid = null)
*/
public function searchField($dynUid, $fieldId, $proUid = null, array $and = [])
{
//get pro_uid if empty
if (empty($proUid)) {
@@ -1828,8 +1836,8 @@ class PmDynaform
}
}
}
return $this->jsonsf($json, $fieldId);
$this->completeAdditionalHelpInformationOnControls($json);
return $this->jsonsf($json, $fieldId, "id", $and);
}
public function searchFieldByName($dyn_uid, $name)
@@ -1846,19 +1854,92 @@ class PmDynaform
return $this->jsonsf($json, $name, "name");
}
private function jsonsf(&$json, $id, $for = "id")
/**
* Replace data field with custom variables.
* @param string $sql
* @param array $data
* @return string
*/
private function replaceDataField(string $sql, array $data): string
{
$textParse = '';
$dbEngine = 'mysql';
$start = 0;
$prefix = '\?';
$pattern = '/\@(?:([' . $prefix . 'Qq\!])([a-zA-Z\_]\w*)|([a-zA-Z\_][\w\-\>\:]*)\(((?:[^\\\\\)]*(?:[\\\\][\w\W])?)*)\))((?:\s*\[[\'"]?\w+[\'"]?\])+|\-\>([a-zA-Z\_]\w*))?/';
$result = preg_match_all($pattern, $sql, $match, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE);
for ($r = 0; $result !== false && $r < $result; $r++) {
$dataGlobal = array_merge($this->fieldsAppData, $data);
if (!isset($dataGlobal[$match[2][$r][0]])) {
$dataGlobal[$match[2][$r][0]] = '';
}
if (!is_array($dataGlobal[$match[2][$r][0]])) {
$textParse = $textParse . substr($sql, $start, $match[0][$r][1] - $start);
$start = $match[0][$r][1] + strlen($match[0][$r][0]);
if (($match[1][$r][0] == '?') && (isset($dataGlobal[$match[2][$r][0]]))) {
$textParse = $textParse . $dataGlobal[$match[2][$r][0]];
continue;
}
}
}
$textParse = $textParse . substr($sql, $start);
$sqlResult = G::replaceDataField($textParse, $data, $dbEngine, false);
return $sqlResult;
}
/**
* complete additional help information on controls.
* @param object $json
*/
private function completeAdditionalHelpInformationOnControls(&$json)
{
foreach ($json as $key => $value) {
$sw1 = is_array($value);
$sw2 = is_object($value);
if ($sw1 || $sw2) {
$val = $this->jsonsf($value, $id, $for);
$this->completeAdditionalHelpInformationOnControls($value);
}
if (!$sw1 && !$sw2) {
if ($key === "type" && ($value === "grid")) {
foreach ($json->columns as $column) {
$column->gridName = $json->id;
}
}
}
}
}
/**
* Gets an element within an object that represents the dynaform. Search is
* done by 'id', 'property' and additional filters.
* @param object $json
* @param string $id
* @param string $for
* @param array $and
* @return mixed
*/
private function jsonsf(&$json, string $id, string $for = "id", array $and = [])
{
foreach ($json as $key => $value) {
$sw1 = is_array($value);
$sw2 = is_object($value);
if ($sw1 || $sw2) {
$val = $this->jsonsf($value, $id, $for, $and);
if ($val !== null) {
return $val;
}
}
if (!$sw1 && !$sw2) {
if ($key === $for && $id === $value) {
$filter = empty($and);
foreach ($and as $keyAnd => $valueAnd) {
$filter = isset($json->{$keyAnd}) && $json->{$keyAnd} === $valueAnd;
if ($filter === false) {
break;
}
}
if ($key === $for && $id === $value && $filter) {
return $json;
}
}