diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php index 556730c4f..f56329cec 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php @@ -45,6 +45,21 @@ class Cases } } + /** + * Throw the exception "The Case doesn't exist" + * + * @param string $applicationUid Unique id of Case + * @param string $fieldNameForException Field name for the exception + * + * @return void + */ + private function throwExceptionCaseDoesNotExist($applicationUid, $fieldNameForException) + { + throw new \Exception(\G::LoadTranslation( + 'ID_CASE_DOES_NOT_EXIST2', [$fieldNameForException, $applicationUid] + )); + } + /** * Verify if does not exist the Case in table APPLICATION * @@ -68,13 +83,91 @@ class Cases } if ($flag) { - throw new \Exception(\G::LoadTranslation("ID_CASE_DOES_NOT_EXIST2", array($fieldNameForException, $applicationUid))); + $this->throwExceptionCaseDoesNotExist($applicationUid, $fieldNameForException); } } catch (\Exception $e) { throw $e; } } + /** + * Get Application record + * + * @param string $applicationUid Unique id of Case + * @param array $arrayVariableNameForException Variable name for exception + * @param bool $throwException Flag to throw the exception if the main parameters are invalid or do not exist + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return array Returns an array with Application record, ThrowTheException/FALSE otherwise + */ + public function getApplicationRecordByPk( + $applicationUid, + array $arrayVariableNameForException, + $throwException = true + ) { + try { + $obj = \ApplicationPeer::retrieveByPK($applicationUid); + + if (is_null($obj)) { + if ($throwException) { + $this->throwExceptionCaseDoesNotExist( + $applicationUid, $arrayVariableNameForException['$applicationUid'] + ); + } else { + return false; + } + } + + //Return + return $obj->toArray(\BasePeer::TYPE_FIELDNAME); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get AppDelegation record + * + * @param string $applicationUid Unique id of Case + * @param int $delIndex Delegation index + * @param array $arrayVariableNameForException Variable name for exception + * @param bool $throwException Flag to throw the exception if the main parameters are invalid or do not exist + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return array Returns an array with AppDelegation record, ThrowTheException/FALSE otherwise + */ + public function getAppDelegationRecordByPk( + $applicationUid, + $delIndex, + array $arrayVariableNameForException, + $throwException = true + ) { + try { + $obj = \AppDelegationPeer::retrieveByPK($applicationUid, $delIndex); + + if (is_null($obj)) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_CASE_DEL_INDEX_DOES_NOT_EXIST', + [ + $arrayVariableNameForException['$applicationUid'], + $applicationUid, + $arrayVariableNameForException['$delIndex'], + $delIndex + ] + )); + } else { + return false; + } + } + + //Return + return $obj->toArray(\BasePeer::TYPE_FIELDNAME); + } catch (\Exception $e) { + throw $e; + } + } + /** * Get list counters * diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Variable.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Variable.php new file mode 100644 index 000000000..cd5f831bf --- /dev/null +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases/Variable.php @@ -0,0 +1,804 @@ +runningWorkflow, $this->arrayFieldDefinition, $this->arrayVariableNameForException + ); + + $this->arrayFieldNameForException = $this->getArrayFieldNameForException(); + $this->arrayVariableNameForException = $this->getArrayVariableNameForException(); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Set runningWorkflow atributte + * + * @param bool $flag + * + * @return void + */ + public function setRunningWorkflow($flag) + { + try { + parent::setRunningWorkflow($flag); + + $this->runningWorkflow = $flag; + + $this->arrayFieldNameForException = $this->getArrayFieldNameForException(); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Set arrayVariableNameForException atributte by data + * + * @param array $arrayData + * + * @return void + */ + public function setArrayVariableNameForException(array $arrayData) + { + try { + foreach ($arrayData as $key => $value) { + $this->arrayVariableNameForException[$key] = $value; + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get Application, AppDelegation and Variable record by data + * + * @param string $applicationUid Unique id of Case + * @param int $delIndex Delegation index + * @param string $variableName Variable name + * @param bool $throwException Flag to throw the exception (This only if the parameters are invalid) + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return mixed Returns array with Application, AppDelegation and Variable record, + * ThrowTheException/FALSE otherwise + */ + private function __getApplicationAppDelegationAndVariableRecordByData( + $applicationUid, + $delIndex, + $variableName, + $throwException = true + ) { + try { + $case = new \ProcessMaker\BusinessModel\Cases(); + + $arrayApplicationData = $case->getApplicationRecordByPk( + $applicationUid, $this->arrayVariableNameForException, $throwException + ); + + if ($arrayApplicationData === false) { + return false; + } + + $arrayAppDelegationData = $case->getAppDelegationRecordByPk( + $applicationUid, $delIndex, $this->arrayVariableNameForException, $throwException + ); + + if ($arrayAppDelegationData === false) { + return false; + } + + $variable = new \ProcessMaker\BusinessModel\Variable(); + + $arrayVariableData = $variable->getVariableRecordByName( + $arrayApplicationData['PRO_UID'], $variableName, $this->arrayVariableNameForException, $throwException + ); + + if ($arrayVariableData === false) { + return false; + } + + $case = new \Cases(); + + $arrayApplicationData['APP_DATA'] = $case->unserializeData($arrayApplicationData['APP_DATA']); + + //Return + return [$arrayApplicationData, $arrayAppDelegationData, $arrayVariableData]; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get Fields of a Grid + * + * @param string $projectUid Unique id of Project + * @param string $gridName Grid name (Variable name) + * + * @return array Returns an array with Fields of a Grid + */ + private function __getGridFieldDefinitions($projectUid, $gridName) + { + try { + $arrayGridField = []; + + //Get data + $criteria = new \Criteria('workflow'); + + $criteria->addSelectColumn(\DynaformPeer::DYN_CONTENT); + + $criteria->add(\DynaformPeer::PRO_UID, $projectUid, \Criteria::EQUAL); + $criteria->add(\DynaformPeer::DYN_CONTENT, '%' . $gridName . '%', \Criteria::LIKE); + + $rsCriteria = \DynaformPeer::doSelectRS($criteria); + $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); + + while ($rsCriteria->next()) { + $row = $rsCriteria->getRow(); + + $dynaFormContent = \G::json_decode($row['DYN_CONTENT']); + + foreach ($dynaFormContent->items[0]->items as $value) { + $arrayField = $value; + + foreach ($arrayField as $value2) { + $fld = $value2; + + if ($fld->type == 'grid' && $fld->variable == $gridName) { + foreach ($fld->columns as $value3) { + $col = $value3; + + if (!isset($arrayGridField[$col->id])) { + $arrayGridField[$col->id] = $col; + } + } + break 2; + } + } + } + } + + //Return + return $arrayGridField; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Validate data + * + * @param array $arrayData Data + * @param array $arrayVariableData Variable data + * @param bool $throwException Flag to throw the exception (This only if the parameters are invalid) + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return bool Returns TRUE when array data is valid, ThrowTheException/FALSE otherwise + */ + private function __validateData(array $arrayData, array $arrayVariableData, $throwException = true) + { + try { + if (empty($arrayData)) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation('ID_INVALID_DATA')); + } else { + return false; + } + } + + if (isset($arrayVariableData['arrayGridField']) && empty($arrayVariableData['arrayGridField'])) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_GRID_DOES_NOT_HAVE_FIELDS', + [$this->arrayVariableNameForException['$variableName'], $arrayVariableData['VAR_NAME']] + )); + } else { + return false; + } + } + + $msgException = ''; + + switch ($arrayVariableData['VAR_FIELD_TYPE']) { + case 'grid': + foreach ($arrayData as $key => $value) { + $row = $value; + + if (is_array($row)) { + foreach ($arrayVariableData['arrayGridField'] as $value2) { + $field = $value2; + + if (isset($row[$field->id])) { + if (isset($row[$field->id . '_label'])) { + unset($row[$field->id], $row[$field->id . '_label']); + } else { + $msgException = $key . ': ' . $field->id . '_label' . ' ' . + \G::LoadTranslation('ID_DOES_NOT_EXIST'); + break 2; + } + } + } + + if (!empty($row)) { + $msgException = $key . ': ' . \G::LoadTranslation('ID_FIELD_INVALID') . + ' (' . implode(', ', array_keys($row)) . ')'; + break; + } + } else { + $msgException = $key . ': ' . \G::LoadTranslation('ID_INVALID_DATA'); + break; + } + } + break; + default: + $arrayFieldName = [ + $arrayVariableData['VAR_NAME'], + $arrayVariableData['VAR_NAME'] . '_label' + ]; + + foreach ($arrayFieldName as $value) { + if (!isset($arrayData[$value])) { + $msgException = $value . ' ' . \G::LoadTranslation('ID_DOES_NOT_EXIST'); + break; + } + } + break; + } + + if ($msgException != '') { + if ($throwException) { + throw new \Exception($msgException); + } else { + return false; + } + } + + //Return + return true; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Create Variable for the Case + * + * @param string $applicationUid Unique id of Case + * @param int $delIndex Delegation index + * @param string $variableName Variable name + * @param array $arrayData Data + * @param bool $throwException Flag to throw the exception (This only if the parameters are invalid) + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return array Returns data of the new Variable created, ThrowTheException/FALSE otherwise + */ + public function create($applicationUid, $delIndex, $variableName, array $arrayData, $throwException = true) + { + try { + //Verify data and Set variables + $result = $this->__getApplicationAppDelegationAndVariableRecordByData( + $applicationUid, $delIndex, $variableName, $throwException + ); + + if ($result === false) { + return false; + } + + $arrayApplicationData = $result[0]; + $arrayAppDelegationData = $result[1]; + $arrayVariableData = $result[2]; + + if ($arrayVariableData['VAR_FIELD_TYPE'] != 'grid' && + isset($arrayApplicationData['APP_DATA'][$variableName]) + ) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_CASE_VARIABLE_ALREADY_EXISTS', + [$this->arrayVariableNameForException['$variableName'], $variableName] + )); + } else { + return false; + } + } + + if ($arrayVariableData['VAR_FIELD_TYPE'] == 'grid') { + $arrayVariableData['arrayGridField'] = $this->__getGridFieldDefinitions( + $arrayVariableData['PRJ_UID'], $arrayVariableData['VAR_NAME'] + ); + } + + $result = $this->__validateData($arrayData, $arrayVariableData, $throwException); + + if ($result === false) { + return false; + } + + //Create + $arrayVariable = []; + + switch ($arrayVariableData['VAR_FIELD_TYPE']) { + case 'grid': + $arrayGridData = (isset($arrayApplicationData['APP_DATA'][$variableName]))? + $arrayApplicationData['APP_DATA'][$variableName] : []; + + $i1 = $i2 = count($arrayGridData); + + foreach ($arrayData as $value) { + $i1++; + $arrayGridData[$i1] = $value; + } + + $arrayVariable = array_slice($arrayGridData, $i2, null, true); + + $arrayApplicationData['APP_DATA'][$variableName] = $arrayGridData; + break; + default: + $arrayVariable = [ + $variableName => $arrayData[$variableName], + $variableName . '_label' => $arrayData[$variableName . '_label'] + ]; + + $arrayApplicationData['APP_DATA'] = array_merge($arrayApplicationData['APP_DATA'], $arrayVariable); + break; + } + + $case = new \Cases(); + + $result = $case->updateCase($applicationUid, $arrayApplicationData); + + //Return + return $arrayVariable; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Update Variable for the Case + * + * @param string $applicationUid Unique id of Case + * @param int $delIndex Delegation index + * @param string $variableName Variable name + * @param array $arrayData Data + * @param bool $throwException Flag to throw the exception (This only if the parameters are invalid) + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return bool Returns TRUE when Variable is updated, ThrowTheException/FALSE otherwise + */ + public function update($applicationUid, $delIndex, $variableName, array $arrayData, $throwException = true) + { + try { + //Verify data and Set variables + $result = $this->__getApplicationAppDelegationAndVariableRecordByData( + $applicationUid, $delIndex, $variableName, $throwException + ); + + if ($result === false) { + return false; + } + + $arrayApplicationData = $result[0]; + $arrayAppDelegationData = $result[1]; + $arrayVariableData = $result[2]; + + if (!isset($arrayApplicationData['APP_DATA'][$variableName])) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_CASE_VARIABLE_DOES_NOT_EXIST', + [$this->arrayVariableNameForException['$variableName'], $variableName] + )); + } else { + return false; + } + } + + if ($arrayVariableData['VAR_FIELD_TYPE'] == 'grid') { + $arrayVariableData['arrayGridField'] = $this->__getGridFieldDefinitions( + $arrayVariableData['PRJ_UID'], $arrayVariableData['VAR_NAME'] + ); + } + + $result = $this->__validateData($arrayData, $arrayVariableData, $throwException); + + if ($result === false) { + return false; + } + + if ($arrayVariableData['VAR_FIELD_TYPE'] == 'grid') { + foreach ($arrayData as $key => $value) { + if (!isset($arrayApplicationData['APP_DATA'][$variableName][$key])) { + if ($throwException) { + throw new \Exception($key . ': ' . \G::LoadTranslation('ID_NO_EXIST')); + } else { + return false; + } + } + } + } + + //Update + switch ($arrayVariableData['VAR_FIELD_TYPE']) { + case 'grid': + foreach ($arrayData as $key => $value) { + $arrayApplicationData['APP_DATA'][$variableName][$key] = array_merge( + $arrayApplicationData['APP_DATA'][$variableName][$key], $value + ); + } + break; + default: + $arrayApplicationData['APP_DATA'] = array_merge( + $arrayApplicationData['APP_DATA'], + [ + $variableName => $arrayData[$variableName], + $variableName . '_label' => $arrayData[$variableName . '_label'] + ] + ); + break; + } + + $case = new \Cases(); + + $result = $case->updateCase($applicationUid, $arrayApplicationData); + + //Return + return true; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Delete Variable of the Case + * + * @param string $applicationUid Unique id of Case + * @param int $delIndex Delegation index + * @param string $variableName Variable name + * @param array $arrayKey Keys to delete (Only for Grids) + * @param bool $throwException Flag to throw the exception (This only if the parameters are invalid) + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return bool Returns TRUE when Variable is deleted, ThrowTheException/FALSE otherwise + */ + public function delete($applicationUid, $delIndex, $variableName, array $arrayKey = null, $throwException = true) + { + try { + //Verify data and Set variables + $result = $this->__getApplicationAppDelegationAndVariableRecordByData( + $applicationUid, $delIndex, $variableName, $throwException + ); + + if ($result === false) { + return false; + } + + $arrayApplicationData = $result[0]; + $arrayAppDelegationData = $result[1]; + $arrayVariableData = $result[2]; + + if (!isset($arrayApplicationData['APP_DATA'][$variableName])) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_CASE_VARIABLE_DOES_NOT_EXIST', + [$this->arrayVariableNameForException['$variableName'], $variableName] + )); + } else { + return false; + } + } + + if ($arrayVariableData['VAR_FIELD_TYPE'] == 'grid' && !is_null($arrayKey)) { + $msgException = ''; + + if (!empty($arrayKey)) { + foreach ($arrayKey as $value) { + $key = $value; + + if (!isset($arrayApplicationData['APP_DATA'][$variableName][$key])) { + $msgException = $key . ': ' . \G::LoadTranslation('ID_NO_EXIST'); + break; + } + } + } else { + $msgException = \G::LoadTranslation( + 'ID_INVALID_VALUE_CAN_NOT_BE_EMPTY', [$this->arrayVariableNameForException['$arrayKey']] + ); + } + + if ($msgException != '') { + if ($throwException) { + throw new \Exception($msgException); + } else { + return false; + } + } + } + + //Delete + switch ($arrayVariableData['VAR_FIELD_TYPE']) { + case 'grid': + if (!is_null($arrayKey)) { + //Delete keys + foreach ($arrayKey as $value) { + $key = $value; + + unset($arrayApplicationData['APP_DATA'][$variableName][$key]); + } + + //Reset keys + $arrayGridData = []; + $i = 1; + + foreach ($arrayApplicationData['APP_DATA'][$variableName] as $value) { + $arrayGridData[$i] = $value; + $i++; + } + + $arrayApplicationData['APP_DATA'][$variableName] = $arrayGridData; + } else { + unset($arrayApplicationData['APP_DATA'][$variableName]); + } + break; + default: + unset( + $arrayApplicationData['APP_DATA'][$variableName], + $arrayApplicationData['APP_DATA'][$variableName . '_label'] + ); + break; + } + + $case = new \Cases(); + + $result = $case->updateCase($applicationUid, $arrayApplicationData); + + //Return + return true; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get Variable of a Case + * + * @param string $applicationUid Unique id of Case + * @param int $delIndex Delegation index + * @param string $variableName Variable name + * @param bool $throwException Flag to throw the exception (This only if the parameters are invalid) + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return array Returns an array with Variable of a Case, ThrowTheException/FALSE otherwise + */ + public function getVariableByName( + $applicationUid, + $delIndex, + $variableName, + $throwException = true + ) { + try { + $arrayVariable = []; + + //Verify data and Set variables + $result = $this->__getApplicationAppDelegationAndVariableRecordByData( + $applicationUid, $delIndex, $variableName, $throwException + ); + + if ($result === false) { + return false; + } + + $arrayApplicationData = $result[0]; + $arrayAppDelegationData = $result[1]; + $arrayVariableData = $result[2]; + + if (!isset($arrayApplicationData['APP_DATA'][$variableName])) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_CASE_VARIABLE_DOES_NOT_EXIST', + [$this->arrayVariableNameForException['$variableName'], $variableName] + )); + } else { + return false; + } + } + + //Get Variable + switch ($arrayVariableData['VAR_FIELD_TYPE']) { + case 'grid': + $arrayVariable = $arrayApplicationData['APP_DATA'][$variableName]; + break; + default: + $arrayVariable = [ + $variableName => $arrayApplicationData['APP_DATA'][$variableName], + $variableName . '_label' => isset($arrayApplicationData['APP_DATA'][$variableName . '_label'])? + $arrayApplicationData['APP_DATA'][$variableName . '_label'] : '' + ]; + break; + } + + //Return + return $arrayVariable; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get Variable of a Case (Only for Grids) + * + * @param string $applicationUid Unique id of Case + * @param int $delIndex Delegation index + * @param string $variableName Variable name + * @param array $arrayFilterData Data of the filters + * @param int $start Start + * @param int $limit Limit + * @param bool $throwException Flag to throw the exception (This only if the parameters are invalid) + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return array Returns an array with Variable of a Case, ThrowTheException/FALSE otherwise + */ + public function getVariableByNamePaged( + $applicationUid, + $delIndex, + $variableName, + $arrayFilterData = null, + $start = null, + $limit = null, + $throwException = true + ) { + try { + $arrayVariable = []; + + $numRecTotal = 0; + + //Verify data and Set variables + $flagFilter = !is_null($arrayFilterData) && is_array($arrayFilterData) && isset($arrayFilterData['filter']); + + $result = \ProcessMaker\BusinessModel\Validator::validatePagerDataByPagerDefinition( + [ + $this->arrayVariableNameForException['$start'] => $start, + $this->arrayVariableNameForException['$limit'] => $limit + ], + $this->arrayVariableNameForException + ); + + if ($result !== true) { + if ($throwException) { + throw new \Exception($result); + } else { + return false; + } + } + + $result = $this->__getApplicationAppDelegationAndVariableRecordByData( + $applicationUid, $delIndex, $variableName, $throwException + ); + + if ($result === false) { + return false; + } + + $arrayApplicationData = $result[0]; + $arrayAppDelegationData = $result[1]; + $arrayVariableData = $result[2]; + + if (!isset($arrayApplicationData['APP_DATA'][$variableName])) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_CASE_VARIABLE_DOES_NOT_EXIST', + [$this->arrayVariableNameForException['$variableName'], $variableName] + )); + } else { + return false; + } + } + + if ($arrayVariableData['VAR_FIELD_TYPE'] != 'grid') { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_VARIABLE_NO_IS_GRID', + [$this->arrayVariableNameForException['$variableName'], $variableName] + )); + } else { + return false; + } + } + + //Set variables + $filterName = 'filter'; + + if ($flagFilter) { + $arrayAux = [ + '' => 'filter', + 'LEFT' => 'lfilter', + 'RIGHT' => 'rfilter' + ]; + + $filterName = $arrayAux[ + (isset($arrayFilterData['filterOption']))? $arrayFilterData['filterOption'] : '' + ]; + } + + //Get Variable + if (!is_null($limit) && (string)($limit) == '0') { + return [ + 'total' => $numRecTotal, + 'start' => (int)((!is_null($start))? $start : 0), + 'limit' => (int)((!is_null($limit))? $limit : 0), + $filterName => ($flagFilter)? $arrayFilterData['filter'] : '', + 'data' => $arrayVariable + ]; + } + + $arraySearch = [ + '' => '.*' . $arrayFilterData['filter'] . '.*', + 'LEFT' => $arrayFilterData['filter'] . '.*', + 'RIGHT' => '.*' . $arrayFilterData['filter'] + ]; + + $search = $arraySearch[ + (isset($arrayFilterData['filterOption']))? $arrayFilterData['filterOption'] : '' + ]; + + foreach ($arrayApplicationData['APP_DATA'][$variableName] as $key => $value) { + if ($flagFilter && trim($arrayFilterData['filter']) != '') { + foreach ($value as $key2 => $value2) { + if (preg_match('/^' . $search . '$/i', $value2)) { + $arrayVariable[$key] = $value; + $numRecTotal++; + break; + } + } + } else { + $arrayVariable[$key] = $value; + $numRecTotal++; + } + } + + $arrayVariable = array_slice( + $arrayVariable, + (!is_null($start))? (int)($start) : 0, + (!is_null($limit))? (int)($limit) : null, + true + ); + + //Return + return [ + 'total' => $numRecTotal, + 'start' => (int)((!is_null($start))? $start : 0), + 'limit' => (int)((!is_null($limit))? $limit : 0), + $filterName => ($flagFilter)? $arrayFilterData['filter'] : '', + 'data' => $arrayVariable + ]; + } catch (\Exception $e) { + throw $e; + } + } +} + diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm.php b/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm.php index bc3c58435..0b891f7c2 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm.php @@ -268,6 +268,21 @@ class DynaForm } } + /** + * Throw the exception "The DynaForm doesn't exist" + * + * @param string $dynaFormUid Unique id of DynaForm + * @param string $fieldNameForException Field name for the exception + * + * @return void + */ + private function throwExceptionDynaFormDoesNotExist($dynaFormUid, $fieldNameForException) + { + throw new \Exception(\G::LoadTranslation( + 'ID_DYNAFORM_DOES_NOT_EXIST', [$fieldNameForException, $dynaFormUid] + )); + } + /** * Verify if doesn't exists the DynaForm in table DYNAFORM * @@ -293,7 +308,7 @@ class DynaForm $rsCriteria = \DynaformPeer::doSelectRS($criteria); if (!$rsCriteria->next()) { - throw new \Exception(\G::LoadTranslation("ID_DYNAFORM_DOES_NOT_EXIST", array($fieldNameForException, $dynaFormUid))); + $this->throwExceptionDynaFormDoesNotExist($dynaFormUid, $fieldNameForException); } } catch (\Exception $e) { throw $e; @@ -345,6 +360,38 @@ class DynaForm } } + /** + * Get DynaForm record + * + * @param string $dynaFormUid Unique id of DynaForm + * @param array $arrayVariableNameForException Variable name for exception + * @param bool $throwException Flag to throw the exception if the main parameters are invalid or do not exist + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return array Returns an array with DynaForm record, ThrowTheException/FALSE otherwise + */ + public function getDynaFormRecordByPk($dynaFormUid, array $arrayVariableNameForException, $throwException = true) + { + try { + $obj = \DynaformPeer::retrieveByPK($dynaFormUid); + + if (is_null($obj)) { + if ($throwException) { + $this->throwExceptionDynaFormDoesNotExist( + $dynaFormUid, $arrayVariableNameForException['$dynaFormUid'] + ); + } else { + return false; + } + } + + //Return + return $obj->toArray(\BasePeer::TYPE_FIELDNAME); + } catch (\Exception $e) { + throw $e; + } + } + /** * Create DynaForm for a Process * @@ -553,7 +600,7 @@ class DynaForm $this->throwExceptionIfNotExistsDynaForm($dynaFormUidCopyImport, $processUidCopyImport, $this->getFieldNameByFormatFieldName("COPY_IMPORT.DYN_UID")); //Copy/Import - + //Copy content if version is 2 if ($arrayData["DYN_VERSION"] === 2) { $dynaFormOld = new \Dynaform(); @@ -562,7 +609,7 @@ class DynaForm $arrayData["DYN_CONTENT"] = $arrayDynaFormData["DYN_CONTENT"]; } - + //Create $arrayData = $this->create($processUid, $arrayData); @@ -976,7 +1023,7 @@ class DynaForm if ($record["DYN_VERSION"] == 0) { $record["DYN_VERSION"] = 1; } - + $record["DYN_CONTENT"] = preg_replace("/\\\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", $record["DYN_CONTENT"]); return array( @@ -1147,7 +1194,7 @@ class DynaForm throw $e; } } - + /** * Get data of a DynaForm History * diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm/Grid.php b/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm/Grid.php new file mode 100644 index 000000000..318c88517 --- /dev/null +++ b/workflow/engine/src/ProcessMaker/BusinessModel/DynaForm/Grid.php @@ -0,0 +1,193 @@ +runningWorkflow, $this->arrayFieldDefinition, $this->arrayVariableNameForException + ); + + $this->arrayFieldNameForException = $this->getArrayFieldNameForException(); + $this->arrayVariableNameForException = $this->getArrayVariableNameForException(); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Set runningWorkflow atributte + * + * @param bool $flag + * + * @return void + */ + public function setRunningWorkflow($flag) + { + try { + parent::setRunningWorkflow($flag); + + $this->runningWorkflow = $flag; + + $this->arrayFieldNameForException = $this->getArrayFieldNameForException(); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Set arrayVariableNameForException atributte by data + * + * @param array $arrayData + * + * @return void + */ + public function setArrayVariableNameForException(array $arrayData) + { + try { + foreach ($arrayData as $key => $value) { + $this->arrayVariableNameForException[$key] = $value; + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get Fields of a Grid + * + * @param string $dynaFormUid Unique id of DynaForm + * @param string $gridName Grid name (Variable name) + * @param bool $throwException Flag to throw the exception if the main parameters are invalid or do not exist + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return array Returns an array with Fields of a Grid, ThrowTheException/FALSE otherwise + */ + public function getGridFieldDefinitions($dynaFormUid, $gridName, $throwException = true) + { + try { + //Verify data and Set variables + $dynaForm = new \ProcessMaker\BusinessModel\DynaForm(); + + $arrayDynaFormData = $dynaForm->getDynaFormRecordByPk( + $dynaFormUid, $this->arrayVariableNameForException, $throwException + ); + + if ($arrayDynaFormData === false) { + return false; + } + + //Get data + $dynaFormContent = \G::json_decode($arrayDynaFormData['DYN_CONTENT']); + + $arrayGridField = []; + $flagFound = false; + + foreach ($dynaFormContent->items[0]->items as $value) { + $arrayField = $value; + + foreach ($arrayField as $value2) { + $fld = $value2; + + if ($fld->type == 'grid' && $fld->variable == $gridName) { + $arrayGridField = $fld->columns; + $flagFound = true; + break 2; + } + } + } + + if (!$flagFound) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_GRID_DOES_NOT_EXIST_IN_DYNAFORM', + [$this->arrayVariableNameForException['$gridName'], $gridName] + )); + } else { + return false; + } + } + + //Return + return $arrayGridField; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get Field of a Grid + * + * @param string $dynaFormUid Unique id of DynaForm + * @param string $gridName Grid name (Variable name) + * @param string $fieldId Field id + * @param bool $throwException Flag to throw the exception if the main parameters are invalid or do not exist + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return array Returns an array with Field of a Grid, ThrowTheException/FALSE otherwise + */ + public function getGridFieldDefinition($dynaFormUid, $gridName, $fieldId, $throwException = true) + { + try { + //Verify data and Set variables + $arrayGridField = $this->getGridFieldDefinitions($dynaFormUid, $gridName, $throwException); + + if ($arrayGridField === false) { + return false; + } + + //Get data + $field = null; + $flagFound = false; + + foreach ($arrayGridField as $value) { + $fld = $value; + + if ($fld->id == $fieldId) { + $field = $fld; + $flagFound = true; + break; + } + } + + if (!$flagFound) { + if ($throwException) { + throw new \Exception(\G::LoadTranslation( + 'ID_GRID_FIELD_DOES_NOT_EXIST', + [$this->arrayVariableNameForException['$fieldId'], $fieldId] + )); + } else { + return false; + } + } + + //Return + return $field; + } catch (\Exception $e) { + throw $e; + } + } +} + diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Process.php b/workflow/engine/src/ProcessMaker/BusinessModel/Process.php index 5352373b9..991d9776d 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Process.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Process.php @@ -325,12 +325,12 @@ class Process public function throwExceptionIfDataNotMetPagerVarDefinition($arrayData, $arrayFieldNameForException) { try { - foreach ($arrayData as $key => $value) { - $nameForException = (isset($arrayFieldNameForException[$key]))? $arrayFieldNameForException[$key] : $key; + $result = \ProcessMaker\BusinessModel\Validator::validatePagerDataByPagerDefinition( + $arrayData, $arrayFieldNameForException + ); - if (!is_null($value) && ($value . "" == "" || !preg_match("/^(?:\+|\-)?(?:0|[1-9]\d*)$/", $value . "") || (int)($value) < 0)) { - throw new \Exception(\G::LoadTranslation('ID_INVALID_VALUE_EXPECTING_POSITIVE_INTEGER', [$nameForException])); - } + if ($result !== true) { + throw new \Exception($result); } } catch (\Exception $e) { throw $e; diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Util/Attribute.php b/workflow/engine/src/ProcessMaker/BusinessModel/Util/Attribute.php new file mode 100644 index 000000000..ed48ab343 --- /dev/null +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Util/Attribute.php @@ -0,0 +1,141 @@ +runningWorkflow = $runningWorkflow; + $this->arrayFieldDefinition = $arrayFieldDefinition; + + foreach ($arrayFieldDefinition as $key => $value) { + if (isset($value['fieldNameAux'])) { + $this->arrayFieldNameForException[$value['fieldNameAux']] = $key; + } + } + + foreach ($arrayVariableNameForException as $value) { + $this->arrayVariableNameForException[$value] = $value; + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Set runningWorkflow atributte + * + * @param bool $flag + * + * @return void + */ + public function setRunningWorkflow($flag) + { + try { + $this->runningWorkflow = $flag; + + $this->setArrayFieldNameForException($this->arrayFieldNameForException); + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Set arrayFieldNameForException atributte by data + * + * @param array $arrayData + * + * @return void + */ + public function setArrayFieldNameForException(array $arrayData) + { + try { + foreach ($arrayData as $key => $value) { + $this->arrayFieldNameForException[$key] = $this->convertFieldNameByRunningWorkflow($value); + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Set arrayVariableNameForException atributte by data + * + * @param array $arrayData + * + * @return void + */ + public function setArrayVariableNameForException(array $arrayData) + { + try { + foreach ($arrayData as $key => $value) { + $this->arrayVariableNameForException[$key] = $value; + } + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get arrayFieldNameForException atributte + * + * @return array + */ + public function getArrayFieldNameForException() + { + try { + return $this->arrayFieldNameForException; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Get arrayVariableNameForException atributte + * + * @return array + */ + public function getArrayVariableNameForException() + { + try { + return $this->arrayVariableNameForException; + } catch (\Exception $e) { + throw $e; + } + } + + /** + * Convert field name by runningWorkflow + * + * @param string $fieldName + * + * @return string + */ + public function convertFieldNameByRunningWorkflow($fieldName) + { + try { + return ($this->runningWorkflow)? strtoupper($fieldName) : strtolower($fieldName); + } catch (\Exception $e) { + throw $e; + } + } +} + diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Validator.php b/workflow/engine/src/ProcessMaker/BusinessModel/Validator.php index bb622755c..5c61055ae 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Validator.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Validator.php @@ -374,5 +374,38 @@ class Validator throw $e; } } + + /** + * Validate pager data + * + * @param array $arrayData Data + * @param array $arrayVariableNameForException Variable name for exception + * + * @return mixed Returns TRUE when pager data is valid, Message Error otherwise + */ + public static function validatePagerDataByPagerDefinition($arrayPagerData, $arrayVariableNameForException) + { + try { + foreach ($arrayPagerData as $key => $value) { + $nameForException = (isset($arrayVariableNameForException[$key]))? + $arrayVariableNameForException[$key] : $key; + + if (!is_null($value) && + ( + (string)($value) == '' || + !preg_match('/^(?:\+|\-)?(?:0|[1-9]\d*)$/', $value . '') || + (int)($value) < 0 + ) + ) { + return \G::LoadTranslation('ID_INVALID_VALUE_EXPECTING_POSITIVE_INTEGER', [$nameForException]); + } + } + + //Return + return true; + } catch (\Exception $e) { + throw $e; + } + } } diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php index d8bd70c03..69ea8d1ca 100755 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Variable.php @@ -730,7 +730,7 @@ class Variable $stmt = $cnn->createStatement(); $replaceFields = G::replaceDataField($variableSql, $arrayVariable); - + $filter = ""; if (isset($arrayVariable["filter"])) { $filter = $arrayVariable["filter"]; @@ -763,7 +763,7 @@ class Variable throw $e; } } - + public function queryModified($sqlParsed, $inputSel = "", $searchType = "*searchtype*", $start = 0, $limit = "", $dbConnection = "workflow") { if (!empty($sqlParsed['SELECT'])) { @@ -876,7 +876,7 @@ class Variable } else { $sqlOrderBy = " ORDER BY " . $sFieldSel; } - + $sqlLimit = ""; if ($start >= 0) { $sqlLimit = " LIMIT " . $start; @@ -887,7 +887,7 @@ class Variable if (!empty($sqlParsed['LIMIT'])) { $sqlLimit = " LIMIT " . $sqlParsed['LIMIT']['start'] . ", " . $sqlParsed['LIMIT']['end']; } - + //get database provider $a = new \Criteria("workflow"); $a->addSelectColumn(\DbSourcePeer::DBS_TYPE); @@ -935,7 +935,7 @@ class Variable return $sCall; } } - + public function limitPgsql($start = 0, $limit = "") { $sqlLimit = ""; @@ -990,4 +990,49 @@ class Variable } } + /** + * Get Variable record by name + * + * @param string $projectUid Unique id of Project + * @param string $variableName Variable name + * @param array $arrayVariableNameForException Variable name for exception + * @param bool $throwException Flag to throw the exception if the main parameters are invalid or do not exist + * (TRUE: throw the exception; FALSE: returns FALSE) + * + * @return array Returns an array with Variable record, ThrowTheException/FALSE otherwise + */ + public function getVariableRecordByName( + $projectUid, + $variableName, + array $arrayVariableNameForException, + $throwException = true + ) { + try { + $criteria = new \Criteria('workflow'); + + $criteria->add(\ProcessVariablesPeer::PRJ_UID, $projectUid, \Criteria::EQUAL); + $criteria->add(\ProcessVariablesPeer::VAR_NAME, $variableName, \Criteria::EQUAL); + + $rsCriteria = \ProcessVariablesPeer::doSelectRS($criteria); + $rsCriteria->setFetchmode(\ResultSet::FETCHMODE_ASSOC); + + if ($rsCriteria->next()) { + $arrayVariableData = $rsCriteria->getRow(); + } else { + if ($throwException) { + throw new \Exception( + $arrayVariableNameForException['$variableName'] . ': ' . $variableName. ' ' . + \G::LoadTranslation('ID_DOES_NOT_EXIST') + ); + } else { + return false; + } + } + + //Return + return $arrayVariableData; + } catch (\Exception $e) { + throw $e; + } + } } diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Cases/Variable.php b/workflow/engine/src/ProcessMaker/Services/Api/Cases/Variable.php new file mode 100644 index 000000000..61620f006 --- /dev/null +++ b/workflow/engine/src/ProcessMaker/Services/Api/Cases/Variable.php @@ -0,0 +1,147 @@ +variable = new \ProcessMaker\BusinessModel\Cases\Variable(); + $this->variable->setRunningWorkflow(false); + $this->variable->setArrayVariableNameForException([ + '$applicationUid' => 'app_uid', + '$delIndex' => 'del_index', + '$variableName' => 'var_name', + '$filter' => 'filter', + '$start' => 'start', + '$limit' => 'limit', + '$arrayKey' => 'keys' + ]); + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } + + /** + * @url GET /:app_uid/:del_index/variable/:var_name/paged + * @url GET /:app_uid/:del_index/variable/:var_name + * + * @param string $app_uid {@min 32}{@max 32} + * @param int $del_index {@min 1} + * @param string $var_name + */ + public function doGetVariable( + $app_uid, + $del_index, + $var_name, + $filter = null, + $lfilter = null, + $rfilter = null, + $start = null, + $limit = null + ) { + try { + if (preg_match("/^.*\/paged.*$/", $this->restler->url)) { + $arrayFilterData = [ + 'filter' => (!is_null($filter))? + $filter : ((!is_null($lfilter))? $lfilter : ((!is_null($rfilter))? $rfilter : null)), + 'filterOption' => (!is_null($filter))? + '' : ((!is_null($lfilter))? 'LEFT' : ((!is_null($rfilter))? 'RIGHT' : '')) + ]; + + $response = $this->variable->getVariableByNamePaged( + $app_uid, $del_index, $var_name, $arrayFilterData, $start, $limit + ); + } else { + $response = $this->variable->getVariableByName($app_uid, $del_index, $var_name); + } + + return \ProcessMaker\Util\DateTime::convertUtcToIso8601($response); + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } + + /** + * @url POST /:app_uid/:del_index/variable/:var_name + * + * @param string $app_uid {@min 32}{@max 32} + * @param int $del_index {@min 1} + * @param string $var_name + * @param array $request_data + * + * @status 201 + */ + public function doPostVariable($app_uid, $del_index, $var_name, array $request_data) + { + try { + $response = $this->variable->create($app_uid, $del_index, $var_name, $request_data); + + return \ProcessMaker\Util\DateTime::convertUtcToIso8601($response); + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } + + /** + * @url PUT /:app_uid/:del_index/variable/:var_name + * + * @param string $app_uid {@min 32}{@max 32} + * @param int $del_index {@min 1} + * @param string $var_name + * @param array $request_data + * + * @status 204 + */ + public function doPutVariable($app_uid, $del_index, $var_name, array $request_data) + { + try { + $response = $this->variable->update($app_uid, $del_index, $var_name, $request_data); + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } + + /** + * @url DELETE /:app_uid/:del_index/variable/:var_name + * + * @param string $app_uid {@min 32}{@max 32} + * @param int $del_index {@min 1} + * @param string $var_name + */ + public function doDeleteVariable($app_uid, $del_index, $var_name, $keys = null) + { + try { + $arrayKey = null; + + if (!is_null($keys)) { + $keys = trim($keys, ' ,'); + $arrayKey = explode(',', $keys); + + if ($keys == '' || !is_array($arrayKey)) { + throw new \Exception(\G::LoadTranslation('ID_INVALID_VALUE', ['keys'])); + } + } + + $response = $this->variable->delete($app_uid, $del_index, $var_name, $arrayKey); + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } +} + diff --git a/workflow/engine/src/ProcessMaker/Services/Api/Project/DynaForm/Grid.php b/workflow/engine/src/ProcessMaker/Services/Api/Project/DynaForm/Grid.php new file mode 100644 index 000000000..e24ff4854 --- /dev/null +++ b/workflow/engine/src/ProcessMaker/Services/Api/Project/DynaForm/Grid.php @@ -0,0 +1,75 @@ +grid = new \ProcessMaker\BusinessModel\DynaForm\Grid(); + $this->grid->setRunningWorkflow(false); + $this->grid->setArrayVariableNameForException([ + '$projectUid' => 'prj_uid', + '$dynaFormUid' => 'dyn_uid', + '$gridName' => 'grd_name', + '$fieldId' => 'fld_id' + ]); + + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } + + /** + * @url GET /:prj_uid/dynaform/:dyn_uid/grid/:grd_name/field-definitions + * + * @param string $prj_uid {@min 32}{@max 32} + * @param string $dyn_uid {@min 32}{@max 32} + * @param string $grd_name + */ + public function doGetDynaFormGridFieldDefinitions($prj_uid, $dyn_uid, $grd_name) + { + try { + $response = $this->grid->getGridFieldDefinitions($dyn_uid, $grd_name); + + return $response; + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } + + /** + * @url GET /:prj_uid/dynaform/:dyn_uid/grid/:grd_name/field-definition/:fld_id + * + * @param string $prj_uid {@min 32}{@max 32} + * @param string $dyn_uid {@min 32}{@max 32} + * @param string $grd_name + * @param string $fld_id + */ + public function doGetDynaFormGridFieldDefinition($prj_uid, $dyn_uid, $grd_name, $fld_id) + { + try { + $response = $this->grid->getGridFieldDefinition($dyn_uid, $grd_name, $fld_id); + + return $response; + } catch (\Exception $e) { + throw new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()); + } + } +} + diff --git a/workflow/engine/src/ProcessMaker/Services/api.ini b/workflow/engine/src/ProcessMaker/Services/api.ini index 6eedd3c3a..9c05af1ba 100644 --- a/workflow/engine/src/ProcessMaker/Services/api.ini +++ b/workflow/engine/src/ProcessMaker/Services/api.ini @@ -21,6 +21,7 @@ debug = 1 trigger2 = "ProcessMaker\Services\Api\Project\Trigger" event = "ProcessMaker\Services\Api\Project\Event" dynaform = "ProcessMaker\Services\Api\Project\DynaForm" + grid = "ProcessMaker\Services\Api\Project\DynaForm\Grid" input-document = "ProcessMaker\Services\Api\Project\InputDocument" output-documents = "ProcessMaker\Services\Api\Project\OutputDocuments" supervisors = "ProcessMaker\Services\Api\Project\ProcessSupervisors" @@ -72,6 +73,7 @@ debug = 1 [alias: case] case = "ProcessMaker\Services\Api\Cases" + variable = "ProcessMaker\Services\Api\Cases\Variable" [alias: cases] case = "ProcessMaker\Services\Api\Cases"