diff --git a/workflow/engine/src/BusinessModel/Table.php b/workflow/engine/src/BusinessModel/Table.php index 303af39a0..9f70ac7ad 100644 --- a/workflow/engine/src/BusinessModel/Table.php +++ b/workflow/engine/src/BusinessModel/Table.php @@ -434,6 +434,64 @@ class Table } } + /** + * Save Data for PmTable + * @var string $pmt_uid. Uid for PmTable + * @var string $pmt_data. Data for rows of PmTable + * + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return array + */ + public function saveTableData ($pmt_uid, $pmt_data) + { + $pmt_uid = $this->validateTabUid($pmt_uid, false); + $rows = $pmt_data['pmt_rows']; + + $additionalTables = new AdditionalTables(); + $table = $additionalTables->load($pmt_uid, true); + $primaryKeys = $additionalTables->getPrimaryKeys(); + + $className = $table['ADD_TAB_CLASS_NAME']; + $classPeerName = $className . 'Peer'; + $row = (array)$rows; + + $row = array_merge( array_change_key_case( $row, CASE_LOWER ), array_change_key_case( $row, CASE_UPPER ) ); + $toSave = false; + + if (! file_exists( PATH_WORKSPACE . 'classes/' . $className . '.php' )) { + throw new Exception( 'Create::' . G::loadTranslation( 'ID_PMTABLE_CLASS_DOESNT_EXIST', $className ) ); + } + + require_once PATH_WORKSPACE . 'classes/' . $className . '.php'; + eval( '$obj = new ' . $className . '();' ); + eval( '$con = Propel::getConnection(' . $classPeerName . '::DATABASE_NAME);' ); + $obj->fromArray( $row, \BasePeer::TYPE_FIELDNAME ); + if ($obj->validate()) { + $affectedRows = $obj->save(); + if ($affectedRows == 0) { + throw (new \Exception("The value of key column is required")); + } + $toSave = true; + $primaryKeysValues = array (); + foreach ($primaryKeys as $primaryKey) { + $method = 'get' . AdditionalTables::getPHPName( $primaryKey['FLD_NAME'] ); + $primaryKeysValues[] = $obj->$method(); + } + } else { + $msg = ''; + foreach ($obj->getValidationFailures() as $objValidationFailure) { + $msg .= $objValidationFailure->getMessage() . "\n"; + } + throw new \Exception( G::LoadTranslation('ID_ERROR_TRYING_INSERT'). '"' . $table['ADD_TAB_NAME'] . "\"\n" . $msg ); + } + + $index = G::encrypt( implode( ',', $primaryKeysValues ), 'pmtable' ); + $rep = $this->getTableData($pmt_uid); + return $rep; + } + /** * Update Data for Table * @var string $tab_data. Data for table @@ -486,6 +544,67 @@ class Table $this->saveTable($dataValidate, $pro_uid, $reportFlag, false); } + /** + * Update Data for PmTable + * @var string $pmt_uid. Uid for PmTable + * @var string $pmt_data. Data for rows of PmTable + * + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return void + */ + public function updateTableData($pmt_uid, $pmt_data) + { + $pmt_uid = $this->validateTabUid($pmt_uid, false); + $rows = $pmt_data['pmt_rows']; + $rows = array_merge( array_change_key_case( $rows, CASE_LOWER ), array_change_key_case( $rows, CASE_UPPER ) ); + + $oAdditionalTables = new AdditionalTables(); + $table = $oAdditionalTables->load( $pmt_uid, true ); + $primaryKeys = $oAdditionalTables->getPrimaryKeys( 'keys' ); + + foreach ($primaryKeys as $value) { + if (!isset($rows[$value])) { + throw (new \Exception("The field for column '$value' is required")); + } else { + $params[] = is_numeric($rows[$value]) ? $rows[$value] : "'".$rows[$value]."'"; + } + } + $className = $table['ADD_TAB_CLASS_NAME']; + $classPeerName = $className . 'Peer'; + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + if (! file_exists( $sPath . $className . '.php' )) { + throw new \Exception( 'Update:: ' . G::loadTranslation( 'ID_PMTABLE_CLASS_DOESNT_EXIST', $this->className ) ); + } + require_once $sPath . $className . '.php'; + + $obj = null; + eval( '$obj = ' . $classPeerName . '::retrieveByPk(' . implode( ',', $params ) . ');' ); + if (is_object( $obj )) { + foreach ($rows as $key => $value) { + // validation, don't modify primary keys + if (in_array(G::toUpper($key), $primaryKeys ) || in_array( G::toLower($key), $primaryKeys )) { + unset($rows[$key]); + } + $action = 'set' . AdditionalTables::getPHPName( $key ); + $obj->$action( $value ); + } + if ($r = $obj->validate()) { + $obj->save(); + $result = true; + } else { + $msg = ''; + foreach ($obj->getValidationFailures() as $objValidationFailure) { + $msg .= $objValidationFailure->getMessage() . "\n"; + } + throw new \Exception( $msg ); + } + } else { + throw (new \Exception("The key " . implode(',', $params) . " not exist")); + } + } + /** * Delete Table * @var string $tab_uid. Uid for table @@ -518,6 +637,65 @@ class Table $at->deleteAll($tab_uid); } + /** + * Delete Data for PmTable + * @var string $pmt_uid. Uid for PmTable + * @var string $rows. Data for rows of PmTable + * + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @return void + */ + public function deleteTableData($pmt_uid, $rows) + { + $pmt_uid = $this->validateTabUid($pmt_uid, false); + $rows = array_merge( array_change_key_case( $rows, CASE_LOWER ), array_change_key_case( $rows, CASE_UPPER ) ); + + $oAdditionalTables = new AdditionalTables(); + $table = $oAdditionalTables->load( $pmt_uid, true ); + $primaryKeys = $oAdditionalTables->getPrimaryKeys( 'keys' ); + + foreach ($primaryKeys as $value) { + if (!isset($rows[$value])) { + throw (new \Exception("The field for column '$value' is required")); + } else { + $params[] = is_numeric($rows[$value]) ? $rows[$value] : "'".$rows[$value]."'"; + } + } + $className = $table['ADD_TAB_CLASS_NAME']; + $classPeerName = $className . 'Peer'; + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + if (! file_exists( $sPath . $className . '.php' )) { + throw new \Exception( 'Update:: ' . G::loadTranslation( 'ID_PMTABLE_CLASS_DOESNT_EXIST', $this->className ) ); + } + require_once $sPath . $className . '.php'; + + $obj = null; + eval( '$obj = ' . $classPeerName . '::retrieveByPk(' . implode( ',', $params ) . ');' ); + if (is_object( $obj )) { + foreach ($rows as $key => $value) { + // validation, don't modify primary keys + if (in_array(G::toUpper($key), $primaryKeys ) || in_array( G::toLower($key), $primaryKeys )) { + unset($rows[$key]); + } + $action = 'set' . AdditionalTables::getPHPName( $key ); + $obj->$action( $value ); + } + if ($r = $obj->validate()) { + $obj->delete(); + } else { + $msg = ''; + foreach ($obj->getValidationFailures() as $objValidationFailure) { + $msg .= $objValidationFailure->getMessage() . "\n"; + } + throw new \Exception( $msg ); + } + } else { + throw (new \Exception("The key " . implode(',', $params) . " not exist")); + } + } + /** * Get Fields of Dynaforms * @var string $pro_uid. Uid for Process diff --git a/workflow/engine/src/Services/Api/ProcessMaker/Pmtable.php b/workflow/engine/src/Services/Api/ProcessMaker/Pmtable.php index c9d9bb110..0f618c033 100644 --- a/workflow/engine/src/Services/Api/ProcessMaker/Pmtable.php +++ b/workflow/engine/src/Services/Api/ProcessMaker/Pmtable.php @@ -99,6 +99,33 @@ class Pmtable extends Api } } + /** + * @param string $pmt_uid {@min 1} {@max 32} + * + * @param array $request_data + * @param array $pmt_rows {@from body} {@required true} + * @return array + * + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @url POST /:pmt_uid/data + * @status 201 + */ + public function doPostPmTableData( + $pmt_uid, + $request_data, + $pmt_rows = '' + ) { + try { + $oReportTable = new \BusinessModel\Table(); + $response = $oReportTable->saveTableData($pmt_uid, $request_data); + return $response; + } catch (\Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } + /** * @param string $pmt_uid {@min 1} {@max 32} * @@ -125,6 +152,32 @@ class Pmtable extends Api } } + /** + * @param string $pmt_uid {@min 1} {@max 32} + * + * @param array $request_data + * @param array $pmt_rows {@from body} {@required true} + * @return array + * + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @url PUT /:pmt_uid/data + */ + public function doPutPmTableData( + $pmt_uid, + $request_data, + $pmt_rows = '' + ) { + try { + $oReportTable = new \BusinessModel\Table(); + $response = $oReportTable->updateTableData($pmt_uid, $request_data); + return $response; + } catch (\Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } + /** * @param string $pmt_uid {@min 1} {@max 32} * @@ -144,5 +197,41 @@ class Pmtable extends Api throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); } } + + /** + * @param string $pmt_uid {@min 1} {@max 32} + * @param string $key1 {@min 1} + * @param string $value1 {@min 1} + * @param string $key2 + * @param string $value2 + * @param string $key3 + * @param string $value3 + * + * @return array + * + * @author Brayan Pereyra (Cochalo) + * @copyright Colosa - Bolivia + * + * @url DELETE /:pmt_uid/data/:key1/:value1 + * @url DELETE /:pmt_uid/data/:key1/:value1/:key2/:value2 + * @url DELETE /:pmt_uid/data/:key1/:value1/:key2/:value2/:key3/:value3 + */ + public function doDeletePmTableData($pmt_uid, $key1, $value1, $key2 = '', $value2 = '', $key3 = '', $value3 = '') + { + try { + $rows = array($key1 => $value1); + if ($key2 != '') { + $rows[$key2] = $value2; + } + if ($key3 != '') { + $rows[$key3] = $value3; + } + $oReportTable = new \BusinessModel\Table(); + $response = $oReportTable->deleteTableData($pmt_uid, $rows); + return $response; + } catch (\Exception $e) { + throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage())); + } + } }