HOR-2421
This commit is contained in:
@@ -22,9 +22,17 @@ class pmDynaform
|
||||
public $isRTL = false;
|
||||
public $pathRTLCss = '';
|
||||
public $serverConf = null;
|
||||
private $cache = array();
|
||||
private $sysSys = null;
|
||||
private $context = array();
|
||||
private $dataSources = null;
|
||||
private $databaseProviders = null;
|
||||
|
||||
public function __construct($fields = array())
|
||||
{
|
||||
$this->sysSys = (defined("SYS_SYS")) ? SYS_SYS : "Undefined";
|
||||
$this->context = \Bootstrap::getDefaultContextLog();
|
||||
$this->dataSources = array("database", "dataVariable");
|
||||
$this->pathRTLCss = '/lib/pmdynaform/build/css/PMDynaform-rtl.css';
|
||||
$this->serverConf = &serverConf::getSingleton();
|
||||
$this->isRTL = ($this->serverConf->isRtl(SYS_LANG)) ? 'true' : 'false';
|
||||
@@ -163,11 +171,11 @@ class pmDynaform
|
||||
return $this->credentials;
|
||||
}
|
||||
|
||||
public function jsonr(&$json)
|
||||
public function jsonr(&$json, $clearCache = true)
|
||||
{
|
||||
$sysSys = (defined("SYS_SYS"))? SYS_SYS : "Undefined";
|
||||
$aContext = \Bootstrap::getDefaultContextLog();
|
||||
|
||||
if ($clearCache === true) {
|
||||
$this->cache = [];
|
||||
}
|
||||
if (empty($json)) {
|
||||
return;
|
||||
}
|
||||
@@ -175,7 +183,7 @@ class pmDynaform
|
||||
$sw1 = is_array($value);
|
||||
$sw2 = is_object($value);
|
||||
if ($sw1 || $sw2) {
|
||||
$this->jsonr($value);
|
||||
$this->jsonr($value, false);
|
||||
}
|
||||
if (!$sw1 && !$sw2) {
|
||||
//read event
|
||||
@@ -190,7 +198,7 @@ class pmDynaform
|
||||
if (isset($this->fields["APP_DATA"][$triggerValue])) {
|
||||
$json->{$key} = $this->fields["APP_DATA"][$triggerValue];
|
||||
} else {
|
||||
$json->{$key} = '';
|
||||
$json->{$key} = "";
|
||||
}
|
||||
}
|
||||
//set properties from 'formInstance' variable
|
||||
@@ -211,58 +219,114 @@ class pmDynaform
|
||||
}
|
||||
}
|
||||
}
|
||||
//options & query
|
||||
//options & query options
|
||||
if ($key === "type" && ($value === "text" || $value === "textarea" || $value === "hidden" || $value === "dropdown" || $value === "checkgroup" || $value === "radio" || $value === "suggest")) {
|
||||
if (!isset($json->dbConnection))
|
||||
if (!isset($json->dbConnection)) {
|
||||
$json->dbConnection = "none";
|
||||
if (!isset($json->sql))
|
||||
}
|
||||
if (!isset($json->sql)) {
|
||||
$json->sql = "";
|
||||
}
|
||||
if (!isset($json->datasource)) {
|
||||
$json->datasource = "database";
|
||||
}
|
||||
if (!in_array($json->datasource, $this->dataSources)) {
|
||||
$json->datasource = "database";
|
||||
}
|
||||
|
||||
$json->optionsSql = array();
|
||||
|
||||
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] : "";
|
||||
$json->optionsSql[] = $option;
|
||||
if ($json->datasource === "database" && $json->dbConnection !== "" && $json->dbConnection !== "none" && $json->sql !== "") {
|
||||
if (isset($json->queryField)) {
|
||||
$dtFields = $json->queryInputData;
|
||||
} else {
|
||||
$dtFields = $this->getValuesDependentFields($json);
|
||||
foreach ($dtFields as $keyF => $valueF) {
|
||||
if (isset($this->fields["APP_DATA"][$keyF])) {
|
||||
$dtFields[$keyF] = $this->fields["APP_DATA"][$keyF];
|
||||
}
|
||||
}
|
||||
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);
|
||||
//Logger
|
||||
$aContext['action'] = 'execute-sql';
|
||||
$aContext['sql'] = $sql;
|
||||
\Bootstrap::registerMonolog('sqlExecution', 200, 'Sql Execution', $aContext, $sysSys, 'processmaker.log');
|
||||
while ($rs->next()) {
|
||||
$row = $rs->getRow();
|
||||
$option = new stdClass();
|
||||
$option->value = $row[0];
|
||||
$option->label = isset($row[1]) ? $row[1] : "";
|
||||
$json->optionsSql[] = $option;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
//Logger
|
||||
$aContext['action'] = 'execute-sql';
|
||||
$aContext['exception'] = (array)$e;
|
||||
\Bootstrap::registerMonolog('sqlExecution', 400, 'Sql Execution', $aContext, $sysSys, 'processmaker.log');
|
||||
}
|
||||
$sql = G::replaceDataField($json->sql, $dtFields);
|
||||
if ($value === "suggest") {
|
||||
$sql = $this->sqlParse($sql, function($parsed, &$select, &$from, &$where, &$groupBy, &$having, &$orderBy, &$limit) use ($json) {
|
||||
$dt = $parsed["SELECT"];
|
||||
|
||||
$isWhere = empty($where);
|
||||
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 (isset($json->queryField) && isset($dt[0]["base_expr"])) {
|
||||
$col = isset($dt[1]["base_expr"]) ? $dt[1]["base_expr"] : $dt[0]["base_expr"];
|
||||
$qf = str_replace("'", "''", $json->queryFilter);
|
||||
$where = $isWhere ? "WHERE " . $col . " LIKE '%" . $qf . "%'" : $where . " AND " . $col . " LIKE '%" . $qf . "%'";
|
||||
}
|
||||
|
||||
$provider = $this->getDatabaseProvider($json->dbConnection);
|
||||
$start = 0;
|
||||
$end = 10;
|
||||
if (isset($json->queryStart)) {
|
||||
$start = $json->queryStart;
|
||||
}
|
||||
if (isset($json->queryLimit)) {
|
||||
$end = $json->queryLimit;
|
||||
}
|
||||
if (empty($limit) && $provider === "mysql") {
|
||||
$limit = "LIMIT " . $start . "," . $end . "";
|
||||
}
|
||||
if (empty($limit) && $provider === "pgsql") {
|
||||
$limit = "OFFSET " . $start . " LIMIT " . $end . "";
|
||||
}
|
||||
if ($provider === "mssql") {
|
||||
$limit = "";
|
||||
if (strpos(strtoupper($select), "TOP") === false) {
|
||||
$isDistinct = strpos(strtoupper($select), "DISTINCT");
|
||||
$isAll = strpos(strtoupper($select), "ALL");
|
||||
if ($isDistinct === false && $isAll === false) {
|
||||
$select = preg_replace("/SELECT/", "SELECT TOP(" . $end . ")", strtoupper($select), 1);
|
||||
}
|
||||
if ($isDistinct !== false) {
|
||||
$select = preg_replace("/DISTINCT/", "DISTINCT TOP(" . $end . ")", strtoupper($select), 1);
|
||||
}
|
||||
if ($isAll !== false) {
|
||||
$select = preg_replace("/DISTINCT/", "DISTINCT TOP(" . $end . ")", strtoupper($select), 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($provider === "oracle") {
|
||||
$limit = "";
|
||||
$rowNumber = "";
|
||||
if (strpos(strtoupper($where), "ROWNUM") === false) {
|
||||
$rowNumber = " AND " . $start . " <= ROWNUM AND ROWNUM <= " . $end;
|
||||
}
|
||||
$where = empty($where) ? "WHERE " . $start . " <= ROWNUM AND ROWNUM <= " . $end : $where . $rowNumber;
|
||||
}
|
||||
});
|
||||
}
|
||||
$dt = $this->getCacheQueryData($json->dbConnection, $sql, $json->type);
|
||||
foreach ($dt as $row) {
|
||||
$option = new stdClass();
|
||||
$option->value = $row[0];
|
||||
$option->label = isset($row[1]) ? $row[1] : "";
|
||||
$json->optionsSql[] = $option;
|
||||
}
|
||||
if (isset($json->queryField)) {
|
||||
$json->queryOutputData = $json->optionsSql;
|
||||
}
|
||||
}
|
||||
|
||||
if ($json->datasource === "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])) {
|
||||
foreach ($this->fields['APP_DATA'][$dataVariable] as $row) {
|
||||
$option = new stdClass();
|
||||
$option->value = $row[0];
|
||||
$option->label = isset($row[1]) ? $row[1] : "";
|
||||
$json->optionsSql[] = $option;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//data
|
||||
@@ -633,18 +697,11 @@ class pmDynaform
|
||||
}
|
||||
}
|
||||
if ($json->dbConnection !== "" && $json->dbConnection !== "none" && $json->sql !== "") {
|
||||
$cnn = Propel::getConnection($json->dbConnection);
|
||||
$stmt = $cnn->createStatement();
|
||||
try {
|
||||
$a = G::replaceDataField($json->sql, $data);
|
||||
$rs = $stmt->executeQuery($a, \ResultSet::FETCHMODE_NUM);
|
||||
$rs->next();
|
||||
$row = $rs->getRow();
|
||||
if (isset($row[0]) && $json->type !== "suggest" && $json->type !== "radio") {
|
||||
$data[$json->variable === "" ? $json->id : $json->variable] = $row[0];
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
|
||||
$a = G::replaceDataField($json->sql, $data);
|
||||
$dt = $this->getCacheQueryData($json->dbConnection, $a, $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];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -660,6 +717,241 @@ class pmDynaform
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function getCacheQueryData($connection, $sql, $type = "", $clearCache = false)
|
||||
{
|
||||
$data = [];
|
||||
if (!empty($type)) {
|
||||
$type = "-" . $type;
|
||||
}
|
||||
try {
|
||||
if ($clearCache === true) {
|
||||
unset($this->cache[md5($sql)]);
|
||||
}
|
||||
if (isset($this->cache[md5($sql)])) {
|
||||
$data = $this->cache[md5($sql)];
|
||||
} else {
|
||||
$cnn = Propel::getConnection($connection);
|
||||
$stmt = $cnn->createStatement();
|
||||
$rs = $stmt->executeQuery($sql, \ResultSet::FETCHMODE_NUM);
|
||||
while ($rs->next()) {
|
||||
$data[] = $rs->getRow();
|
||||
}
|
||||
$this->cache[md5($sql)] = $data;
|
||||
|
||||
$this->context["action"] = "execute-sql" . $type;
|
||||
$this->context["sql"] = $sql;
|
||||
\Bootstrap::registerMonolog("sqlExecution", 200, "Sql Execution", $this->context, $this->sysSys, "processmaker.log");
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->context["action"] = "execute-sql" . $type;
|
||||
$this->context["exception"] = (array) $e;
|
||||
\Bootstrap::registerMonolog("sqlExecution", 400, "Sql Execution", $this->context, $this->sysSys, "processmaker.log");
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getDatabaseProvider($dbConnection)
|
||||
{
|
||||
if ($this->databaseProviders === null) {
|
||||
$a = new Criteria("workflow");
|
||||
$a->addSelectColumn(DbSourcePeer::DBS_UID);
|
||||
$a->addSelectColumn(DbSourcePeer::DBS_TYPE);
|
||||
$ds = DbSourcePeer::doSelectRS($a);
|
||||
$ds->setFetchmode(ResultSet::FETCHMODE_ASSOC);
|
||||
$this->databaseProviders = [];
|
||||
while ($ds->next()) {
|
||||
$this->databaseProviders[] = $ds->getRow();
|
||||
}
|
||||
}
|
||||
foreach ($this->databaseProviders as $key => $value) {
|
||||
if ($value["DBS_UID"] === $dbConnection) {
|
||||
return $value["DBS_TYPE"];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function sqlParse($sql, $fn = null)
|
||||
{
|
||||
$sqlParser = new \PHPSQLParser($sql);
|
||||
$parsed = $sqlParser->parsed;
|
||||
if (!empty($parsed["SELECT"])) {
|
||||
$options = isset($parsed["OPTIONS"]) && count($parsed["OPTIONS"]) > 0 ? implode(" ", $parsed["OPTIONS"]) : "";
|
||||
if (!empty($options)) {
|
||||
$options = $options . " ";
|
||||
}
|
||||
$select = "SELECT " . $options;
|
||||
$dt = $parsed["SELECT"];
|
||||
foreach ($dt as $key => $value) {
|
||||
if ($key != 0) {
|
||||
$select .= ", ";
|
||||
}
|
||||
$sAlias = str_replace("`", "", $dt[$key]["alias"]);
|
||||
$sBaseExpr = $dt[$key]["base_expr"];
|
||||
if (strpos(strtoupper($sBaseExpr), "TOP") !== false) {
|
||||
$dt[$key]["expr_type"] = "";
|
||||
$sBaseExpr = trim($sBaseExpr) . " " . trim($sAlias);
|
||||
}
|
||||
switch ($dt[$key]["expr_type"]) {
|
||||
case "colref":
|
||||
if ($sAlias === $sBaseExpr) {
|
||||
$select .= $sAlias;
|
||||
} else {
|
||||
$select .= $sBaseExpr . " AS " . $sAlias;
|
||||
}
|
||||
break;
|
||||
case "expression":
|
||||
if ($sAlias === $sBaseExpr) {
|
||||
$select .= $sBaseExpr;
|
||||
} else {
|
||||
$select .= $sBaseExpr . " AS " . $sAlias;
|
||||
}
|
||||
break;
|
||||
case "subquery":
|
||||
if (strpos($sAlias, $sBaseExpr, 0) != 0) {
|
||||
$select .= $sAlias;
|
||||
} else {
|
||||
$select .= $sBaseExpr . " AS " . $sAlias;
|
||||
}
|
||||
break;
|
||||
case "operator":
|
||||
$select .= $sBaseExpr;
|
||||
break;
|
||||
default:
|
||||
$select .= $sBaseExpr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$select = trim($select);
|
||||
|
||||
$isOffsetWord = false;
|
||||
|
||||
$from = "";
|
||||
if (!empty($parsed["FROM"])) {
|
||||
$from = "FROM ";
|
||||
$dt = $parsed["FROM"];
|
||||
foreach ($dt as $key => $value) {
|
||||
//reserved word: OFFSET
|
||||
if ($dt[$key]["alias"] === "OFFSET") {
|
||||
$isOffsetWord = true;
|
||||
$dt[$key]["alias"] = "";
|
||||
}
|
||||
if ($key == 0) {
|
||||
$from .= $dt[$key]["table"]
|
||||
. ($dt[$key]["table"] == $dt[$key]["alias"] ? "" : " " . $dt[$key]["alias"]);
|
||||
} else {
|
||||
$from .= " "
|
||||
. ($dt[$key]["join_type"] == "JOIN" ? "INNER" : $dt[$key]["join_type"])
|
||||
. " JOIN "
|
||||
. $dt[$key]["table"]
|
||||
. ($dt[$key]["table"] == $dt[$key]["alias"] ? "" : " " . $dt[$key]["alias"]) . " "
|
||||
. $dt[$key]["ref_type"] . " "
|
||||
. $dt[$key]["ref_clause"];
|
||||
}
|
||||
}
|
||||
}
|
||||
$from = trim($from);
|
||||
|
||||
$where = "";
|
||||
if (!empty($parsed["WHERE"])) {
|
||||
$where = "WHERE ";
|
||||
$dt = $parsed["WHERE"];
|
||||
$nw = count($dt);
|
||||
//reserved word: OFFSET
|
||||
if ($dt[$nw - 2]["base_expr"] === "OFFSET") {
|
||||
$isOffsetWord = true;
|
||||
if ($dt[$nw - 2]["expr_type"] === "colref") {
|
||||
$dt[$nw - 2]["base_expr"] = "";
|
||||
}
|
||||
if ($dt[$nw - 1]["expr_type"] === "const") {
|
||||
if (isset($parsed["LIMIT"]["start"])) {
|
||||
$parsed["LIMIT"]["start"] = $dt[$nw - 1]["base_expr"];
|
||||
}
|
||||
$dt[$nw - 1]["base_expr"] = "";
|
||||
}
|
||||
}
|
||||
foreach ($dt as $key => $value) {
|
||||
$where .= $value["base_expr"] . " ";
|
||||
}
|
||||
}
|
||||
$where = trim($where);
|
||||
|
||||
$groupBy = "";
|
||||
if (!empty($parsed["GROUP"])) {
|
||||
$groupBy = "GROUP BY ";
|
||||
$dt = $parsed["GROUP"];
|
||||
foreach ($dt as $key => $value) {
|
||||
$groupBy .= $value["base_expr"] . ", ";
|
||||
}
|
||||
$groupBy = rtrim($groupBy, ", ");
|
||||
}
|
||||
$groupBy = trim($groupBy);
|
||||
|
||||
$having = "";
|
||||
if (!empty($parsed["HAVING"])) {
|
||||
$having = "HAVING ";
|
||||
$dt = $parsed["HAVING"];
|
||||
foreach ($dt as $key => $value) {
|
||||
$having .= $value["base_expr"] . " ";
|
||||
}
|
||||
}
|
||||
$having = trim($having);
|
||||
|
||||
$orderBy = "";
|
||||
if (!empty($parsed["ORDER"])) {
|
||||
$orderBy = "ORDER BY ";
|
||||
$dt = $parsed["ORDER"];
|
||||
foreach ($dt as $key => $value) {
|
||||
$orderBy .= $value["base_expr"] . ", ";
|
||||
}
|
||||
$orderBy = rtrim($orderBy, ", ");
|
||||
$orderBy .= " " . $value["direction"];
|
||||
}
|
||||
$orderBy = trim($orderBy);
|
||||
|
||||
$limit = "";
|
||||
if (!empty($parsed["LIMIT"])) {
|
||||
if ($isOffsetWord == false) {
|
||||
$limit = "LIMIT " . $parsed["LIMIT"]["start"] . ", " . $parsed["LIMIT"]["end"];
|
||||
}
|
||||
if ($isOffsetWord == true) {
|
||||
$limit = "OFFSET " . $parsed["LIMIT"]["start"] . " LIMIT " . $parsed["LIMIT"]["end"];
|
||||
}
|
||||
}
|
||||
|
||||
if ($fn !== null && (is_callable($fn) || function_exists($fn))) {
|
||||
$fn($parsed, $select, $from, $where, $groupBy, $having, $orderBy, $limit);
|
||||
}
|
||||
|
||||
$dt = [$select, $from, $where, $groupBy, $having, $orderBy, $limit];
|
||||
$query = "";
|
||||
foreach ($dt as $val) {
|
||||
$val = trim($val);
|
||||
if (!empty($val)) {
|
||||
$query = $query . $val . " ";
|
||||
}
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
if (!empty($parsed["CALL"])) {
|
||||
$sCall = "CALL ";
|
||||
$aCall = $parsed["CALL"];
|
||||
foreach ($aCall as $key => $value) {
|
||||
$sCall .= $value . " ";
|
||||
}
|
||||
return $sCall;
|
||||
}
|
||||
if (!empty($parsed["EXECUTE"])) {
|
||||
$sCall = "EXECUTE ";
|
||||
$aCall = $parsed["EXECUTE"];
|
||||
foreach ($aCall as $key => $value) {
|
||||
$sCall .= $value . " ";
|
||||
}
|
||||
return $sCall;
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
public function isResponsive()
|
||||
{
|
||||
return $this->record != null && $this->record["DYN_VERSION"] == 2 ? true : false;
|
||||
|
||||
@@ -1750,23 +1750,20 @@ class Cases
|
||||
$arrayCaseVariable = [];
|
||||
|
||||
if (!is_null($dynaFormUid)) {
|
||||
$dynaForm = new \ProcessMaker\BusinessModel\DynaForm();
|
||||
$arrayDynaFormData = $dynaForm->getDynaFormRecordByPk($dynaFormUid, ['$dynaFormUid' => '$dynaFormUid']);
|
||||
|
||||
$arrayDynContent = \G::json_decode($arrayDynaFormData['DYN_CONTENT']);
|
||||
|
||||
\G::LoadClass("pmDynaform");
|
||||
$data["CURRENT_DYNAFORM"] = $dynaFormUid;
|
||||
$pmDynaForm = new \pmDynaform($data);
|
||||
$arrayDynaFormData = $pmDynaForm->getDynaform();
|
||||
$arrayDynContent = \G::json_decode($arrayDynaFormData['DYN_CONTENT']);
|
||||
$pmDynaForm->jsonr($arrayDynContent);
|
||||
$arrayDynContent = \G::json_decode(\G::json_encode($arrayDynContent),true);
|
||||
|
||||
$arrayDynContent = \G::json_decode(\G::json_encode($arrayDynContent), true);
|
||||
|
||||
$arrayAppData = $fields['APP_DATA'];
|
||||
|
||||
$arrayCaseVariable = $this->__getFieldsAndValuesByDynaFormAndAppData(
|
||||
$arrayDynContent['items'][0], $arrayAppData, $arrayCaseVariable
|
||||
$arrayDynContent['items'][0], $arrayAppData, $arrayCaseVariable
|
||||
);
|
||||
|
||||
} else {
|
||||
$arrayCaseVariable = $fields['APP_DATA'];
|
||||
}
|
||||
|
||||
@@ -710,314 +710,29 @@ class Variable
|
||||
public function executeSqlSuggest($processUid, $variableName, array $arrayVariable = array())
|
||||
{
|
||||
try {
|
||||
$arrayRecord = array();
|
||||
$sysSys = (defined("SYS_SYS"))? SYS_SYS : "Undefined";
|
||||
$aContext = \Bootstrap::getDefaultContextLog();
|
||||
|
||||
//Verify data
|
||||
$process = new \ProcessMaker\BusinessModel\Process();
|
||||
|
||||
$process->throwExceptionIfNotExistsProcess($processUid, strtolower("PRJ_UID"));
|
||||
|
||||
//Set data
|
||||
\G::LoadClass('pmDynaform');
|
||||
$_SESSION["PROCESS"] = $processUid;
|
||||
\G::LoadClass("pmDynaform");
|
||||
$pmDynaform = new \pmDynaform();
|
||||
$field = $pmDynaform->searchField($arrayVariable["dyn_uid"], $variableName, $processUid);
|
||||
$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);
|
||||
|
||||
//Get data
|
||||
$filter = str_replace('\'', '\'\'', (isset($arrayVariable['filter']))? $arrayVariable['filter'] : '');
|
||||
$start = (isset($arrayVariable['start']))? $arrayVariable['start'] : 0;
|
||||
$limit = (isset($arrayVariable['limit']))? $arrayVariable['limit'] : '';
|
||||
|
||||
switch (($field !== null && isset($field->datasource))? $field->datasource : 'database') {
|
||||
case 'dataVariable':
|
||||
if (!isset($arrayVariable['app_uid'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$applicationUid = $arrayVariable['app_uid'];
|
||||
|
||||
$case = new \ProcessMaker\BusinessModel\Cases();
|
||||
|
||||
$arrayApplicationData = $case->getApplicationRecordByPk(
|
||||
$applicationUid, ['$applicationUid' => 'app_uid']
|
||||
);
|
||||
|
||||
$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);
|
||||
|
||||
//Logger
|
||||
$aContext['action'] = 'execute-sql-suggest';
|
||||
$aContext['sql'] = $replaceFields;
|
||||
\Bootstrap::registerMonolog('sqlExecution', 200, 'Sql Execution', $aContext, $sysSys, 'processmaker.log');
|
||||
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;
|
||||
$result = array();
|
||||
if (isset($field->queryOutputData) && is_array($field->queryOutputData)) {
|
||||
foreach ($field->queryOutputData as $item) {
|
||||
$result[] = ["value" => $item->value, "text" => $item->label];
|
||||
}
|
||||
}
|
||||
|
||||
//Return
|
||||
return $arrayRecord;
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
//Logger
|
||||
$aContext['action'] = 'execute-sql-suggest';
|
||||
$aContext['exception'] = (array)$e;
|
||||
\Bootstrap::registerMonolog('sqlExecution', 400, 'Sql Execution', $aContext, $sysSys, 'processmaker.log');
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public function queryModified($sqlParsed, $inputSel = "", $searchType = "*searchtype*", $start = 0, $limit = "", $dbConnection = "workflow")
|
||||
{
|
||||
if (!empty($sqlParsed['SELECT'])) {
|
||||
$sqlSelectOptions = (isset($sqlParsed["OPTIONS"]) && count($sqlParsed["OPTIONS"]) > 0) ? implode(" ", $sqlParsed["OPTIONS"]) : null;
|
||||
|
||||
$sqlSelect = "SELECT $sqlSelectOptions ";
|
||||
$aSelect = $sqlParsed["SELECT"];
|
||||
|
||||
$sFieldSel = (count($aSelect) > 1 ) ? $aSelect[1]['base_expr'] : $aSelect[0]['base_expr'];
|
||||
foreach ($aSelect as $key => $value) {
|
||||
if ($key != 0)
|
||||
$sqlSelect .= ", ";
|
||||
$sAlias = str_replace("`", "", $aSelect[$key]['alias']);
|
||||
$sBaseExpr = $aSelect[$key]['base_expr'];
|
||||
switch ($aSelect[$key]['expr_type']) {
|
||||
case 'colref' : if ($sAlias === $sBaseExpr)
|
||||
$sqlSelect .= $sAlias;
|
||||
else
|
||||
$sqlSelect .= $sBaseExpr . ' AS ' . $sAlias;
|
||||
break;
|
||||
case 'expression' : if ($sAlias === $sBaseExpr)
|
||||
$sqlSelect .= $sBaseExpr;
|
||||
else
|
||||
$sqlSelect .= $sBaseExpr . ' AS ' . $sAlias;
|
||||
break;
|
||||
case 'subquery' : if (strpos($sAlias, $sBaseExpr, 0) != 0)
|
||||
$sqlSelect .= $sAlias;
|
||||
else
|
||||
$sqlSelect .= $sBaseExpr . " AS " . $sAlias;
|
||||
break;
|
||||
case 'operator' : $sqlSelect .= $sBaseExpr;
|
||||
break;
|
||||
default : $sqlSelect .= $sBaseExpr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$sqlFrom = " FROM ";
|
||||
if (!empty($sqlParsed['FROM'])) {
|
||||
$aFrom = $sqlParsed['FROM'];
|
||||
if (count($aFrom) > 0) {
|
||||
foreach ($aFrom as $key => $value) {
|
||||
if ($key == 0) {
|
||||
$sqlFrom .= $aFrom[$key]['table'] . (($aFrom[$key]['table'] == $aFrom[$key]['alias']) ? "" : " " . $aFrom[$key]['alias']);
|
||||
} else {
|
||||
$sqlFrom .= " " . (($aFrom[$key]['join_type'] == 'JOIN') ? "INNER" : $aFrom[$key]['join_type']) . " JOIN " . $aFrom[$key]['table']
|
||||
. (($aFrom[$key]['table'] == $aFrom[$key]['alias']) ? "" : " " . $aFrom[$key]['alias']) . " " . $aFrom[$key]['ref_type'] . " " . $aFrom[$key]['ref_clause'];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sqlConditionLike = "LIKE '%" . $inputSel . "%'";
|
||||
|
||||
switch ($searchType) {
|
||||
case "searchtype*":
|
||||
$sqlConditionLike = "LIKE '" . $inputSel . "%'";
|
||||
break;
|
||||
case "*searchtype":
|
||||
$sqlConditionLike = "LIKE '%" . $inputSel . "'";
|
||||
break;
|
||||
}
|
||||
|
||||
if (!empty($sqlParsed['WHERE'])) {
|
||||
$sqlWhere = " WHERE ";
|
||||
$aWhere = $sqlParsed['WHERE'];
|
||||
foreach ($aWhere as $key => $value) {
|
||||
$sqlWhere .= $value['base_expr'] . " ";
|
||||
}
|
||||
$sqlWhere .= " AND " . $sFieldSel . " " . $sqlConditionLike;
|
||||
} else {
|
||||
$sqlWhere = " WHERE " . $sFieldSel . " " . $sqlConditionLike;
|
||||
}
|
||||
|
||||
$sqlGroupBy = "";
|
||||
if (!empty($sqlParsed['GROUP'])) {
|
||||
$sqlGroupBy = "GROUP BY ";
|
||||
$aGroup = $sqlParsed['GROUP'];
|
||||
foreach ($aGroup as $key => $value) {
|
||||
if ($key != 0)
|
||||
$sqlGroupBy .= ", ";
|
||||
if ($value['direction'] == 'ASC')
|
||||
$sqlGroupBy .= $value['base_expr'];
|
||||
else
|
||||
$sqlGroupBy .= $value['base_expr'] . " " . $value['direction'];
|
||||
}
|
||||
}
|
||||
|
||||
$sqlHaving = "";
|
||||
if (!empty($sqlParsed['HAVING'])) {
|
||||
$sqlHaving = "HAVING ";
|
||||
$aHaving = $sqlParsed['HAVING'];
|
||||
foreach ($aHaving as $key => $value) {
|
||||
$sqlHaving .= $value['base_expr'] . " ";
|
||||
}
|
||||
}
|
||||
|
||||
$sqlOrderBy = "";
|
||||
if (!empty($sqlParsed['ORDER'])) {
|
||||
$sqlOrderBy = "ORDER BY ";
|
||||
$aOrder = $sqlParsed['ORDER'];
|
||||
foreach ($aOrder as $key => $value) {
|
||||
if ($key != 0)
|
||||
$sqlOrderBy .= ", ";
|
||||
if ($value['direction'] == 'ASC')
|
||||
$sqlOrderBy .= $value['base_expr'];
|
||||
else
|
||||
$sqlOrderBy .= $value['base_expr'] . " " . $value['direction'];
|
||||
}
|
||||
} else {
|
||||
$sqlOrderBy = " ORDER BY " . $sFieldSel;
|
||||
}
|
||||
|
||||
$start = 0;
|
||||
$sqlLimit = "";
|
||||
if ($start >= 0) {
|
||||
$sqlLimit = " LIMIT " . $start;
|
||||
}
|
||||
if ($limit !== "") {
|
||||
$sqlLimit = " LIMIT " . $start . "," . $limit;
|
||||
}
|
||||
if (!empty($sqlParsed['LIMIT'])) {
|
||||
$sqlLimit = " LIMIT " . $sqlParsed['LIMIT']['start'] . ", " . $sqlParsed['LIMIT']['end'];
|
||||
}
|
||||
|
||||
//get database provider
|
||||
$a = new \Criteria("workflow");
|
||||
$a->addSelectColumn(\DbSourcePeer::DBS_TYPE);
|
||||
$a->add(\DbSourcePeer::DBS_UID, $dbConnection, \Criteria::EQUAL);
|
||||
$ds = \DbSourcePeer::doSelectRS($a);
|
||||
$ds->setFetchmode(\ResultSet::FETCHMODE_ASSOC);
|
||||
$ds->next();
|
||||
$row = $ds->getRow();
|
||||
if (isset($row["DBS_TYPE"])) {
|
||||
if ($row["DBS_TYPE"] === "pgsql") {
|
||||
if ($start >= 0) {
|
||||
$sqlLimit = " OFFSET " . $start;
|
||||
}
|
||||
if ($limit !== "") {
|
||||
$sqlLimit = $sqlLimit . " LIMIT " . $limit;
|
||||
}
|
||||
}
|
||||
if ($row["DBS_TYPE"] === "mssql") {
|
||||
$sqlLimit = "";
|
||||
if ($limit !== "") {
|
||||
$wordsSearch = [" DISTINCT ", " ALL "];
|
||||
$wordsSearchCount = count($wordsSearch);
|
||||
for ($i = 0; $i < $wordsSearchCount; $i++) {
|
||||
$stringSearch = $wordsSearch[$i];
|
||||
$stringPosition = strpos($sqlSelect, $stringSearch);
|
||||
if ($stringPosition !== false) {
|
||||
$stringLength = strlen($stringSearch);
|
||||
$string1 = substr($sqlSelect, 0, $stringPosition + $stringLength);
|
||||
$string2 = substr($sqlSelect, $stringPosition + $stringLength);
|
||||
$sqlSelect = $string1 . "TOP(" . $limit . ") " . $string2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($row["DBS_TYPE"] === "oracle") {
|
||||
$sqlLimit = "";
|
||||
if ($limit !== "") {
|
||||
if (strpos($sqlWhere, "WHERE ") === false) {
|
||||
$sqlWhere = " WHERE ROWNUM <= " . $limit;
|
||||
} else {
|
||||
$sqlWhere = $sqlWhere . " AND ROWNUM <= " . $limit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $sqlSelect . $sqlFrom . $sqlWhere . $sqlGroupBy . $sqlHaving . " " . $sqlOrderBy . $sqlLimit;
|
||||
}
|
||||
if (!empty($sqlParsed['CALL'])) {
|
||||
$sCall = "CALL ";
|
||||
$aCall = $sqlParsed['CALL'];
|
||||
foreach ($aCall as $key => $value) {
|
||||
$sCall .= $value . " ";
|
||||
}
|
||||
return $sCall;
|
||||
}
|
||||
if (!empty($sqlParsed['EXECUTE'])) {
|
||||
$sCall = "EXECUTE ";
|
||||
$aCall = $sqlParsed['EXECUTE'];
|
||||
foreach ($aCall as $key => $value) {
|
||||
$sCall .= $value . " ";
|
||||
}
|
||||
return $sCall;
|
||||
}
|
||||
if (!empty($sqlParsed[''])) {
|
||||
$sCall = "";
|
||||
$aCall = $sqlParsed[''];
|
||||
foreach ($aCall as $key => $value) {
|
||||
$sCall .= $value . " ";
|
||||
}
|
||||
return $sCall;
|
||||
}
|
||||
}
|
||||
|
||||
public function getVariableTypeByName($processUid, $variableName)
|
||||
{
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user