From 64c418dfe2e1142848ce4b52e0449dcd726acf26 Mon Sep 17 00:00:00 2001 From: Victor Saisa Lopez Date: Wed, 2 Mar 2016 09:27:08 -0400 Subject: [PATCH] HOR-172 "DbArray en ProcessMaker 3.x" SOLVED Issue: DbArray en ProcessMaker 3.x Cause: Nuevo requerimiento de funciones Solution: Se a implementado la nueva funcionalidad "datasource" para los fields de un DynaForm (Back-End) --- workflow/engine/classes/class.pmDynaform.php | 61 ++++++++--- .../ProcessMaker/BusinessModel/Variable.php | 103 ++++++++++++------ 2 files changed, 115 insertions(+), 49 deletions(-) diff --git a/workflow/engine/classes/class.pmDynaform.php b/workflow/engine/classes/class.pmDynaform.php index b555a067c..25940b193 100644 --- a/workflow/engine/classes/class.pmDynaform.php +++ b/workflow/engine/classes/class.pmDynaform.php @@ -176,22 +176,47 @@ class pmDynaform if (!isset($json->sql)) $json->sql = ""; $json->optionsSql = array(); - if ($json->dbConnection !== "" && $json->dbConnection !== "none" && $json->sql !== "") { - try { - $cnn = Propel::getConnection($json->dbConnection); - $stmt = $cnn->createStatement(); - $sql = G::replaceDataField($json->sql, $this->getValuesDependentFields($json)); - $rs = $stmt->executeQuery($sql, \ResultSet::FETCHMODE_NUM); - while ($rs->next()) { - $row = $rs->getRow(); - $option = new stdClass(); - $option->value = $row[0]; - $option->label = isset($row[1]) ? $row[1] : $row[0]; - array_push($json->optionsSql, $option); + + switch ((isset($json->datasource))? $json->datasource : 'database') { + case 'dataVariable': + $dataVariable = (preg_match('/^\s*@.(.+)\s*$/', $json->dataVariable, $arrayMatch))? + $arrayMatch[1] : $json->dataVariable; + + if (isset($this->fields['APP_DATA'][$dataVariable]) && + is_array($this->fields['APP_DATA'][$dataVariable]) && + !empty($this->fields['APP_DATA'][$dataVariable]) + ) { + foreach ($this->fields['APP_DATA'][$dataVariable] as $row) { + $option = new stdClass(); + $option->value = $row[0]; + $option->label = (isset($row[1]))? $row[1] : $row[0]; + + $json->optionsSql[] = $option; + } } - } catch (Exception $e) { - - } + break; + default: + //database + if ($json->dbConnection !== '' && $json->dbConnection !== 'none' && $json->sql !== '') { + try { + $cnn = Propel::getConnection($json->dbConnection); + $stmt = $cnn->createStatement(); + $sql = G::replaceDataField($json->sql, $this->getValuesDependentFields($json)); + $rs = $stmt->executeQuery($sql, ResultSet::FETCHMODE_NUM); + + while ($rs->next()) { + $row = $rs->getRow(); + + $option = new stdClass(); + $option->value = $row[0]; + $option->label = (isset($row[1]))? $row[1] : $row[0]; + + $json->optionsSql[] = $option; + } + } catch (Exception $e) { + } + } + break; } } //data @@ -565,7 +590,7 @@ class pmDynaform $data[$json->variable === "" ? $json->id : $json->variable] = $row[0]; } } catch (Exception $e) { - + } } } @@ -688,9 +713,9 @@ class pmDynaform " token: credentials,\n" . " submitRest: false\n" . " });\n" . - " $(document).find('form').find('button').on('click', function (e) {\n". + " $(document).find('form').find('button').on('click', function (e) {\n". " e.preventDefault();\n" . - " return false;\n". + " return false;\n". " });\n" . " $(document).find('form').submit(function (e) {\n" . " e.preventDefault();\n" . diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php index 69ea8d1ca..766957119 100755 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php @@ -717,44 +717,85 @@ class Variable \G::LoadClass('pmDynaform'); $pmDynaform = new \pmDynaform(); $field = $pmDynaform->searchField($arrayVariable["dyn_uid"], $variableName); - $dbConnection = "workflow"; - if ($field !== null && !empty($field->dbConnection)) { - $dbConnection = $field->dbConnection; - } - $variableSql = $field !== null ? $field->sql : ""; //Get data - $_SESSION["PROCESS"] = $processUid; + $filter = str_replace('\'', '\'\'', (isset($arrayVariable['filter']))? $arrayVariable['filter'] : ''); + $start = (isset($arrayVariable['start']))? $arrayVariable['start'] : 0; + $limit = (isset($arrayVariable['limit']))? $arrayVariable['limit'] : ''; - $cnn = \Propel::getConnection($dbConnection); - $stmt = $cnn->createStatement(); + switch (($field !== null && isset($field->datasource))? $field->datasource : 'database') { + case 'dataVariable': + if (!isset($arrayVariable['app_uid'])) { + return []; + } - $replaceFields = G::replaceDataField($variableSql, $arrayVariable); + $applicationUid = $arrayVariable['app_uid']; - $filter = ""; - if (isset($arrayVariable["filter"])) { - $filter = $arrayVariable["filter"]; - } - $start = 0; - if (isset($arrayVariable["start"])) { - $start = $arrayVariable["start"]; - } - $limit = ""; - if (isset($arrayVariable["limit"])) { - $limit = $arrayVariable["limit"]; - } - $parser = new \PHPSQLParser($replaceFields); - $filter = str_replace("'", "''", $filter); - $replaceFields = $this->queryModified($parser->parsed, $filter, "*searchtype*", $start, $limit, $dbConnection); - $rs = $stmt->executeQuery($replaceFields, \ResultSet::FETCHMODE_NUM); + $case = new \ProcessMaker\BusinessModel\Cases(); - while ($rs->next()) { - $row = $rs->getRow(); + $arrayApplicationData = $case->getApplicationRecordByPk( + $applicationUid, ['$applicationUid' => 'app_uid'] + ); - $arrayRecord[] = array( - strtolower("VALUE") => $row[0], - strtolower("TEXT") => isset($row[1]) ? $row[1] : $row[0] - ); + $case = new \Cases(); + + $arrayApplicationData['APP_DATA'] = $case->unserializeData($arrayApplicationData['APP_DATA']); + + $dataVariable = (preg_match('/^\s*@.(.+)\s*$/', $field->dataVariable, $arrayMatch))? + $arrayMatch[1] : $field->dataVariable; + + if (isset($arrayApplicationData['APP_DATA'][$dataVariable]) && + is_array($arrayApplicationData['APP_DATA'][$dataVariable]) && + !empty($arrayApplicationData['APP_DATA'][$dataVariable]) + ) { + foreach ($arrayApplicationData['APP_DATA'][$dataVariable] as $row) { + $value = $row[0]; + $text = (isset($row[1]))? $row[1] : $row[0]; + + if ($filter !== '') { + if (preg_match('/^.*' . $filter . '.*$/i', $text)) { + $arrayRecord[] = [strtolower('VALUE') => $value, strtolower('TEXT') => $text]; + } + } else { + $arrayRecord[] = [strtolower('VALUE') => $value, strtolower('TEXT') => $text]; + } + } + + $arrayRecord = array_slice( + $arrayRecord, + (int)($start), + ($limit !== '')? (int)($limit) : null + ); + } + break; + default: + //database + $dbConnection = ($field !== null && !empty($field->dbConnection))? $field->dbConnection : 'workflow'; + $variableSql = ($field !== null)? $field->sql : ''; + + $_SESSION['PROCESS'] = $processUid; + + $cnn = \Propel::getConnection($dbConnection); + $stmt = $cnn->createStatement(); + + $replaceFields = \G::replaceDataField($variableSql, $arrayVariable); + + $parser = new \PHPSQLParser($replaceFields); + + $replaceFields = $this->queryModified( + $parser->parsed, $filter, '*searchtype*', $start, $limit, $dbConnection + ); + $rs = $stmt->executeQuery($replaceFields, \ResultSet::FETCHMODE_NUM); + + while ($rs->next()) { + $row = $rs->getRow(); + + $value = $row[0]; + $text = (isset($row[1]))? $row[1] : $row[0]; + + $arrayRecord[] = [strtolower('VALUE') => $value, strtolower('TEXT') => $text]; + } + break; } //Return