diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm.php b/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm.php index 4e22170c5..9499036ef 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm.php @@ -967,7 +967,7 @@ class DynaForm $this->getFieldNameByFormatFieldName("DYN_DESCRIPTION") => $record["DYN_DESCRIPTION"] . "", $this->getFieldNameByFormatFieldName("DYN_TYPE") => $record["DYN_TYPE"] . "", $this->getFieldNameByFormatFieldName("DYN_CONTENT") => $record["DYN_CONTENT"] . "", - $this->getFieldNameByFormatFieldName("DYN_VERSION") => $record["DYN_VERSION"] . "" + $this->getFieldNameByFormatFieldName("DYN_VERSION") => (int)($record["DYN_VERSION"]) ); } catch (\Exception $e) { throw $e; diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/FilesManager.php b/workflow/engine/src/ProcessMaker/BusinessModel/FilesManager.php index a7d68b1f5..e10995b0e 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/FilesManager.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/FilesManager.php @@ -191,7 +191,7 @@ class FilesManager } $content = $aData['prf_content']; if (is_string($content)) { - if (file_exists(PATH_SEP.$sDirectory)) { + if (file_exists($sDirectory)) { $directory = $sMainDirectory. PATH_SEP . $sSubDirectory . $aData['prf_filename']; throw new \Exception(\G::LoadTranslation("ID_EXISTS_FILE", array($directory))); } @@ -393,7 +393,7 @@ class FilesManager * @param string $prfUid {@min 32} {@max 32} * * - * @access public + * @access public */ public function deleteProcessFilesManager($sProcessUID, $prfUid) { diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php index 90a3237e8..68dd86890 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php @@ -354,5 +354,120 @@ class Variable throw $e; } } + + /** + * Get required variables in the SQL + * + * @param string $sql SQL + * + * return array Return an array with required variables in the SQL + */ + public function sqlGetRequiredVariables($sql) + { + try { + $arrayVariableRequired = array(); + + preg_match_all("/@[@%#\?\x24\=]([A-Za-z_]\w*)/", $sql, $arrayMatch, PREG_SET_ORDER); + + foreach ($arrayMatch as $value) { + $arrayVariableRequired[] = $value[1]; + } + + return $arrayVariableRequired; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Verify if some required variable in the SQL is missing in the variables + * + * @param string $variableName Variable name + * @param string $variableSql SQL + * @param array $arrayVariable The variables + * + * return void Throw exception if some required variable in the SQL is missing in the variables + */ + public function throwExceptionIfSomeRequiredVariableSqlIsMissingInVariables($variableName, $variableSql, array $arrayVariable) + { + try { + $arrayResult = array_diff(array_unique($this->sqlGetRequiredVariables($variableSql)), array_keys($arrayVariable)); + + if (count($arrayResult) > 0) { + throw new \Exception(\G::LoadTranslation("ID_PROCESS_VARIABLE_REQUIRED_VARIABLES_FOR_QUERY", array($variableName, implode(", ", $arrayResult)))); + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get all records by execute SQL + * + * @param string $processUid Unique id of Process + * @param string $variableName Variable name + * @param array $arrayVariable The variables + * + * return array Return an array with all records + */ + 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 + $variableDbConnectionUid = ""; + $variableSql = ""; + + $criteria = new \Criteria("workflow"); + + $criteria->addSelectColumn(\ProcessVariablesPeer::VAR_DBCONNECTION); + $criteria->addSelectColumn(\ProcessVariablesPeer::VAR_SQL); + $criteria->add(\ProcessVariablesPeer::PRJ_UID, $processUid, \Criteria::EQUAL); + $criteria->add(\ProcessVariablesPeer::VAR_NAME, $variableName, \Criteria::EQUAL); + + $rsCriteria = \ProcessVariablesPeer::doSelectRS($criteria); + $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); + + if ($rsCriteria->next()) { + $row = $rsCriteria->getRow(); + + $variableDbConnectionUid = $row["VAR_DBCONNECTION"]; + $variableSql = $row["VAR_SQL"]; + } else { + throw new \Exception(\G::LoadTranslation("ID_PROCESS_VARIABLE_DOES_NOT_EXIST", array(strtolower("VAR_NAME"), $variableName))); + } + + //Verify data + $this->throwExceptionIfSomeRequiredVariableSqlIsMissingInVariables($variableName, $variableSql, $arrayVariable); + + //Get data + $_SESSION["PROCESS"] = $processUid; + + $cnn = \Propel::getConnection(($variableDbConnectionUid . "" != "")? $variableDbConnectionUid : "workflow"); + $stmt = $cnn->createStatement(); + + $rs = $stmt->executeQuery(\G::replaceDataField($variableSql, $arrayVariable), \ResultSet::FETCHMODE_NUM); + + while ($rs->next()) { + $row = $rs->getRow(); + + $arrayRecord[] = array( + strtolower("VALUE") => $row[0], + strtolower("TEXT") => $row[1] + ); + } + + //Return + return $arrayRecord; + } catch (\Exception $e) { + throw $e; + } + } } diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Project/Variable.php b/workflow/engine/src/ProcessMaker/Services/Api/Project/Variable.php index ff8ad5e94..d1479fc83 100644 --- a/workflow/engine/src/ProcessMaker/Services/Api/Project/Variable.php +++ b/workflow/engine/src/ProcessMaker/Services/Api/Project/Variable.php @@ -107,4 +107,27 @@ class Variable extends Api throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); } } -} \ No newline at end of file + + /** + * @url POST /:prj_uid/process-variable/:var_name/execute-query + * + * @param string $prj_uid {@min 32}{@max 32} + * @param string $var_name + * @param array $request_data + */ + public function doPostVariableExecuteSql($prj_uid, $var_name, $request_data) + { + try { + $variable = new \ProcessMaker\BusinessModel\Variable(); + + $arrayData = ($request_data != null)? $variable->executeSql($prj_uid, $var_name, $request_data) : $variable->executeSql($prj_uid, $var_name); + + $response = $arrayData; + + return $response; + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } +} +