diff --git a/workflow/engine/classes/model/AdditionalTables.php b/workflow/engine/classes/model/AdditionalTables.php index 0bc3f7285..2f971d1ea 100755 --- a/workflow/engine/classes/model/AdditionalTables.php +++ b/workflow/engine/classes/model/AdditionalTables.php @@ -1,11 +1,11 @@ toArray(BasePeer::TYPE_FIELDNAME); - $this->fromArray($aFields, BasePeer::TYPE_FIELDNAME); - - if ($bFields) { - $aFields['FIELDS'] = $this->getFields(); - } - - return $aFields; - } - - public function getFields() - { - if (count($this->fields) > 0) { - return $this->fields; - } - - require_once 'classes/model/Fields.php'; - $oCriteria = new Criteria('workflow'); - - $oCriteria->addSelectColumn(FieldsPeer::FLD_UID); - $oCriteria->addSelectColumn(FieldsPeer::FLD_INDEX); - $oCriteria->addSelectColumn(FieldsPeer::FLD_NAME); - $oCriteria->addSelectColumn(FieldsPeer::FLD_DESCRIPTION); - $oCriteria->addSelectColumn(FieldsPeer::FLD_TYPE); - $oCriteria->addSelectColumn(FieldsPeer::FLD_SIZE); - $oCriteria->addSelectColumn(FieldsPeer::FLD_NULL); - $oCriteria->addSelectColumn(FieldsPeer::FLD_AUTO_INCREMENT); - $oCriteria->addSelectColumn(FieldsPeer::FLD_KEY); - $oCriteria->addSelectColumn(FieldsPeer::FLD_FOREIGN_KEY); - $oCriteria->addSelectColumn(FieldsPeer::FLD_FOREIGN_KEY_TABLE); - $oCriteria->addSelectColumn(FieldsPeer::FLD_DYN_NAME); - $oCriteria->addSelectColumn(FieldsPeer::FLD_DYN_UID); - $oCriteria->addSelectColumn(FieldsPeer::FLD_FILTER); - $oCriteria->add(FieldsPeer::ADD_TAB_UID, $this->getAddTabUid()); - $oCriteria->addAscendingOrderByColumn(FieldsPeer::FLD_INDEX); - - $oDataset = FieldsPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - - while ($oDataset->next()) { - $this->fields[] = $oDataset->getRow(); - } - - return $this->fields; - } - - public function getPrimaryKeys($type = 'complete') - { - $this->primaryKeys = array(); - foreach ($this->fields as $field) { - if ($field['FLD_KEY'] == '1') { - if ($type == 'complete') { - $this->primaryKeys[] = $field; + if (is_null($oAdditionalTables)) { + return null; } - else { // just field names - $this->primaryKeys[] = $field['FLD_NAME']; + + $aFields = $oAdditionalTables->toArray(BasePeer::TYPE_FIELDNAME); + $this->fromArray($aFields, BasePeer::TYPE_FIELDNAME); + + if ($bFields) { + $aFields['FIELDS'] = $this->getFields(); } - } - } - return $this->primaryKeys; - } - public function loadByName($name) { - try { - $oCriteria = new Criteria('workflow'); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_UID); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_NAME); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_CLASS_NAME); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_DESCRIPTION); - //DEPRECATED! $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_LOG_INSERT); - // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_LOG_UPDATE); - // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_LOG_DELETE); - // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_LOG_SELECT); - // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_MAX_LENGTH); - // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_AUTO_DELETE); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_PLG_UID); - $oCriteria->addSelectColumn(AdditionalTablesPeer::DBS_UID); - $oCriteria->addSelectColumn(AdditionalTablesPeer::PRO_UID); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_TYPE); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_GRID); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_TAG); - - // AdditionalTablesPeer::ADD_TAB_NAME is unique - $oCriteria->add(AdditionalTablesPeer::ADD_TAB_NAME, $name, Criteria::EQUAL); - - $oDataset = AdditionalTablesPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - - $oDataset->next(); - - return $oDataset->getRow(); - } - catch (Exception $oError) { - throw($oError); - } - } - - /** - * Create & Update function - */ - function create($aData, $aFields = array()) - { - if (!isset($aData['ADD_TAB_UID']) || (isset($aData['ADD_TAB_UID']) && $aData['ADD_TAB_UID'] == '')) { - $aData['ADD_TAB_UID'] = G::generateUniqueID(); + return $aFields; } - $oConnection = Propel::getConnection(AdditionalTablesPeer::DATABASE_NAME); - - try { - $oAdditionalTables = new AdditionalTables(); - $oAdditionalTables->fromArray($aData, BasePeer::TYPE_FIELDNAME); - - if ($oAdditionalTables->validate()) { - $oConnection->begin(); - $iResult = $oAdditionalTables->save(); - $oConnection->commit(); - /****DEPRECATED - require_once 'classes/model/ShadowTable.php'; - $oShadowTable = new ShadowTable(); - $oShadowTable->create(array('ADD_TAB_UID' => $aData['ADD_TAB_UID'], - 'SHD_ACTION' => 'CREATE', - 'SHD_DETAILS' => serialize($aFields), - 'USR_UID' => (isset($_SESSION['USER_LOGGED']) ? $_SESSION['USER_LOGGED'] : ''), - 'APP_UID' => '', - 'SHD_DATE' => date('Y-m-d H:i:s'))); - */ - return $aData['ADD_TAB_UID']; - } else { - $sMessage = ''; - $aValidationFailures = $oAdditionalTables->getValidationFailures(); - foreach($aValidationFailures as $oValidationFailure) { - $sMessage .= $oValidationFailure->getMessage() . '
'; + public function getFields() + { + if (count($this->fields) > 0) { + return $this->fields; } - throw(new Exception('The registry cannot be created!
' . $sMessage)); - } - } - catch (Exception $oError) { - $oConnection->rollback(); - throw($oError); - } - } - function update($aData, $aFields = array()) { - $oConnection = Propel::getConnection(AdditionalTablesPeer::DATABASE_NAME); - try { - $oAdditionalTables = AdditionalTablesPeer::retrieveByPK($aData['ADD_TAB_UID']); - if (!is_null($oAdditionalTables)) { - $oAdditionalTables->fromArray($aData, BasePeer::TYPE_FIELDNAME); - if ($oAdditionalTables->validate()) { - $oConnection->begin(); - $iResult = $oAdditionalTables->save(); - $oConnection->commit(); - /*** DEPRECATED - require_once 'classes/model/ShadowTable.php'; - $oShadowTable = new ShadowTable(); - $oShadowTable->create(array('ADD_TAB_UID' => $aData['ADD_TAB_UID'], - 'SHD_ACTION' => 'ALTER', - 'SHD_DETAILS' => serialize($aFields), - 'USR_UID' => (isset($_SESSION['USER_LOGGED']) ? $_SESSION['USER_LOGGED'] : ''), - 'APP_UID' => '', - 'SHD_DATE' => date('Y-m-d H:i:s'))); - return $iResult;*/ + require_once 'classes/model/Fields.php'; + $oCriteria = new Criteria('workflow'); + + $oCriteria->addSelectColumn(FieldsPeer::FLD_UID); + $oCriteria->addSelectColumn(FieldsPeer::FLD_INDEX); + $oCriteria->addSelectColumn(FieldsPeer::FLD_NAME); + $oCriteria->addSelectColumn(FieldsPeer::FLD_DESCRIPTION); + $oCriteria->addSelectColumn(FieldsPeer::FLD_TYPE); + $oCriteria->addSelectColumn(FieldsPeer::FLD_SIZE); + $oCriteria->addSelectColumn(FieldsPeer::FLD_NULL); + $oCriteria->addSelectColumn(FieldsPeer::FLD_AUTO_INCREMENT); + $oCriteria->addSelectColumn(FieldsPeer::FLD_KEY); + $oCriteria->addSelectColumn(FieldsPeer::FLD_FOREIGN_KEY); + $oCriteria->addSelectColumn(FieldsPeer::FLD_FOREIGN_KEY_TABLE); + $oCriteria->addSelectColumn(FieldsPeer::FLD_DYN_NAME); + $oCriteria->addSelectColumn(FieldsPeer::FLD_DYN_UID); + $oCriteria->addSelectColumn(FieldsPeer::FLD_FILTER); + $oCriteria->add(FieldsPeer::ADD_TAB_UID, $this->getAddTabUid()); + $oCriteria->addAscendingOrderByColumn(FieldsPeer::FLD_INDEX); + + $oDataset = FieldsPeer::doSelectRS($oCriteria); + $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + + while ($oDataset->next()) { + $this->fields[] = $oDataset->getRow(); } - else { - $sMessage = ''; - $aValidationFailures = $oAdditionalTables->getValidationFailures(); - foreach($aValidationFailures as $oValidationFailure) { - $sMessage .= $oValidationFailure->getMessage() . '
'; - } - throw(new Exception('The registry cannot be updated!
'.$sMessage)); - } - } - else { - throw(new Exception('This row doesn\'t exist!')); - } - } - catch (Exception $oError) { - $oConnection->rollback(); - throw($oError); - } - } - function remove($sUID) { - $oConnection = Propel::getConnection(AdditionalTablesPeer::DATABASE_NAME); - try { - $oAdditionalTables = AdditionalTablesPeer::retrieveByPK($sUID); - if (!is_null($oAdditionalTables)) { - $aAdditionalTables = $oAdditionalTables->toArray(BasePeer::TYPE_FIELDNAME); - $oConnection->begin(); - $iResult = $oAdditionalTables->delete(); - $oConnection->commit(); - - return $iResult; - } - else { - throw(new Exception('This row doesn\'t exist!')); - } - } - catch (Exception $oError) { - $oConnection->rollback(); - throw($oError); - } - } - - function deleteAll($id) - { - //deleting pm table - $additionalTable = AdditionalTables::load($id); - AdditionalTables::remove($id); - - //deleting fields - require_once 'classes/model/Fields.php'; - $criteria = new Criteria('workflow'); - $criteria->add(FieldsPeer::ADD_TAB_UID, $id); - FieldsPeer::doDelete($criteria); - - //remove all related to pmTable - G::loadClass('pmTable'); - $pmTable = new pmTable($additionalTable['ADD_TAB_NAME']); - $pmTable->setDataSource($additionalTable['DBS_UID']); - $pmTable->remove(); - } - - function getPHPName($sName) { - $sName = trim($sName); - $aAux = explode('_', $sName); - foreach ($aAux as $iKey => $sPart) { - $aAux[$iKey] = ucwords(strtolower($sPart)); - } - return implode('', $aAux); - } - - function deleteMultiple($arrUID){ - $arrUIDs = explode(",",$arrUID); - foreach ($arrUIDs as $UID){ - $this->deleteAll($UID); - } - } - - function getDataCriteria($sUID) { - try { - $aData = $this->load($sUID, true); - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' ? $aData['ADD_TAB_CLASS_NAME'] : $this->getPHPName($aData['ADD_TAB_NAME'])); - - if (file_exists ($sPath . $sClassName . '.php') ) { - require_once $sPath . $sClassName . '.php'; - } else { - return null; - } - - $sClassPeerName = $sClassName . 'Peer'; - $con = Propel::getConnection($aData['DBS_UID']); - $oCriteria = new Criteria($aData['DBS_UID']); - - //eval('$oCriteria->addSelectColumn(' . $sClassPeerName . '::PM_UNIQUE_ID);'); - eval('$oCriteria->addSelectColumn("\'1\' AS DUMMY");'); - foreach ($aData['FIELDS'] as $aField) { - eval('$oCriteria->addSelectColumn(' . $sClassPeerName . '::' . $aField['FLD_NAME'] . ');'); - } - - switch ($aField['FLD_TYPE']) { - case 'VARCHAR': - case 'TEXT': - case 'DATE': -// if($aField['FLD_NULL']!=1) -// eval('$oCriteria->add(' . $sClassPeerName . '::' . $aField['FLD_NAME'] . ', \'(�_�_�)\', Criteria::NOT_EQUAL);'); - break; - case 'INT'; - case 'FLOAT': - eval('$oCriteria->add(' . $sClassPeerName . '::' . $aField['FLD_NAME'] . ', -99999999999, Criteria::NOT_EQUAL);'); - break; - } - //eval('$oCriteria->addAscendingOrderByColumn(' . $sClassPeerName . '::PM_UNIQUE_ID);'); - //echo $oCriteria->toString(); - return $oCriteria; - } - catch (Exception $oError) { - throw($oError); - } - } - - function getAllData($sUID, $start=NULL, $limit=NULL, $keyOrderUppercase = true) - { - $addTab = new AdditionalTables(); - $aData = $addTab->load($sUID, true); - if (! isset($_SESSION['PROCESS'])) { - $_SESSION["PROCESS"] = $aData['PRO_UID']; - } - $aData['DBS_UID'] = $aData['DBS_UID'] ? $aData['DBS_UID'] : 'workflow'; - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' ? $aData['ADD_TAB_CLASS_NAME'] : $this->getPHPName($aData['ADD_TAB_NAME'])); - - if (file_exists ($sPath . $sClassName . '.php') ) { - require_once $sPath . $sClassName . '.php'; - } else { - return null; + return $this->fields; } - $sClassPeerName = $sClassName . 'Peer'; - $con = Propel::getConnection($aData['DBS_UID']); - $oCriteria = new Criteria($aData['DBS_UID']); - - //eval('$oCriteria->addSelectColumn("\'1\' AS DUMMY");'); - if($keyOrderUppercase==true){ - foreach ($aData['FIELDS'] as $aField) { - eval('$oCriteria->addSelectColumn(' . $sClassPeerName . '::' . $aField['FLD_NAME'] . ');'); - if ($aField['FLD_KEY'] == '1') { - eval('$oCriteria->addAscendingOrderByColumn(' . $sClassPeerName . '::' . $aField['FLD_NAME'] . ');'); - } - } - } - $oCriteriaCount = clone $oCriteria; - //$count = $sClassPeerName::doCount($oCriteria); - eval('$count = '.$sClassPeerName.'::doCount($oCriteria);'); - - if (isset($limit)) { - $oCriteria->setLimit($limit); - } - if (isset($start)) { - $oCriteria->setOffset($start); - } - //$rs = $sClassPeerName::doSelectRS($oCriteria); - eval('$rs = '.$sClassPeerName.'::doSelectRS($oCriteria);'); - $rs->setFetchmode (ResultSet::FETCHMODE_ASSOC); - - $rows = Array(); - while ($rs->next()) { - $rows[] = $rs->getRow(); - } - - return array('rows' => $rows, 'count' => $count); - } - - function checkClassNotExist($sUID) { - try { - $aData = $this->load($sUID, true); - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' ? $aData['ADD_TAB_CLASS_NAME'] : $this->getPHPName($aData['ADD_TAB_NAME'])); - - if (file_exists ($sPath . $sClassName . '.php') ) { - return $sClassName; - } else { - return ''; - } - - } catch (Exception $oError) { - throw($oError); - } - } - - function saveDataInTable($sUID, $aFields) { - try { - $aData = $this->load($sUID, true); - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' ? $aData['ADD_TAB_CLASS_NAME'] : $this->getPHPName($aData['ADD_TAB_NAME'])); - // $oConnection = Propel::getConnection(FieldsPeer::DATABASE_NAME); - $oConnection = Propel::getConnection($aData['DBS_UID']); - $stmt = $oConnection->createStatement(); - require_once $sPath . $sClassName . '.php'; - $sKeys = ''; - foreach ($aData['FIELDS'] as $aField) { - if ($aField['FLD_KEY'] == 1) { - $vValue = $aFields[$aField['FLD_NAME']]; - eval('$' . $aField['FLD_NAME'] . ' = $vValue;'); - $sKeys .= '$' . $aField['FLD_NAME'] . ','; - } - } - $sKeys = substr($sKeys, 0, -1); - $oClass = new $sClassName; - foreach ($aFields as $sKey => $sValue) { - if(!preg_match("/\(?\)/", $sKey)) - eval('$oClass->set' . $this->getPHPName($sKey) . '($aFields["' . $sKey . '"]);'); - } - if ($oClass->validate()) { - $iResult = $oClass->save(); - } - return true; - } - catch (Exception $oError) { - throw($oError); - } - } - - function getDataTable($sUID, $aKeys) { - try { - $aData = $this->load($sUID, true); - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' ? $aData['ADD_TAB_CLASS_NAME'] : $this->getPHPName($aData['ADD_TAB_NAME'])); - require_once $sPath . $sClassName . '.php'; - $sKeys = ''; - foreach ($aKeys as $sName => $vValue) { - eval('$' . $sName . ' = $vValue;'); - $sKeys .= '$' . $sName . ','; - } - $sKeys = substr($sKeys, 0, -1); - eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK(' . $sKeys . ');'); - //eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK($sPMUID);'); - if (!is_null($oClass)) { - return $oClass->toArray(BasePeer::TYPE_FIELDNAME); - } - else { - return false; - } - } - catch (Exception $oError) { - throw($oError); - } - } - - function updateDataInTable($sUID, $aFields) { - try { - //$sPMUID = $aFields['PM_UNIQUE_ID']; - $aData = $this->load($sUID, true); - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' ? $aData['ADD_TAB_CLASS_NAME'] : $this->getPHPName($aData['ADD_TAB_NAME'])); - $oConnection = Propel::getConnection(FieldsPeer::DATABASE_NAME); - require_once $sPath . $sClassName . '.php'; - $sKeys = ''; - foreach ($aData['FIELDS'] as $aField) {//$sName => $vValue - if ($aField['FLD_KEY'] == 1) { - $vValue = $aFields[$aField['FLD_NAME']]; - eval('$' . $aField['FLD_NAME'] . ' = $vValue;'); - $sKeys .= '$' . $aField['FLD_NAME'] . ','; - } - } - $sKeys = substr($sKeys, 0, -1); - eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK(' . $sKeys . ');'); - //eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK($sPMUID);'); - if (!is_null($oClass)) { - $oClass->fromArray($aFields, BasePeer::TYPE_FIELDNAME); - if ($oClass->validate()) { - $oConnection->begin(); - $iResult = $oClass->save(); - $oConnection->commit(); - return $iResult; - } - } - else { - $sMessage = ''; - if ($oClass) { - $aValidationFailures = $oClass->getValidationFailures(); - foreach($aValidationFailures as $oValidationFailure) { - $sMessage .= $oValidationFailure->getMessage() . '
'; - } - } - else { - $sMessage = 'Error, row cannot updated'; - return false; - } - throw(new Exception('The registry cannot be updated!
' . $sMessage)); - } - } - catch (Exception $oError) { - throw($oError); - } - } - - function deleteDataInTable($sUID, $aKeys) { - try { - $aData = $this->load($sUID, true); - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' ? $aData['ADD_TAB_CLASS_NAME'] : $this->getPHPName($aData['ADD_TAB_NAME'])); - $oConnection = Propel::getConnection(FieldsPeer::DATABASE_NAME); - require_once $sPath . $sClassName . '.php'; - $sKeys = ''; - foreach ($aKeys as $sName => $vValue) { - eval('$' . $sName . ' = $vValue;'); - $sKeys .= '$' . $sName . ','; - } - $sKeys = substr($sKeys, 0, -1); - eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK(' . $sKeys . ');'); - //eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK($sPMUID);'); - if (!is_null($oClass)) { - if ($oClass->validate()) { - $oConnection->begin(); - $iResult = $oClass->delete(); - $oConnection->commit(); - return $iResult; - } - } - else { - $sMessage = ''; - $aValidationFailures = $oConnection-->getValidationFailures(); - foreach($aValidationFailures as $oValidationFailure) { - $sMessage .= $oValidationFailure->getMessage() . '
'; - } - throw(new Exception('The registry cannot be updated!
' . $sMessage)); - } - } - catch (Exception $oError) { - throw($oError); - } - } - - - /** - * Populate the report table with all case data - * @param string $sType - * @param string $sProcessUid - * @param string $sGrid - * @return number - */ - public function populateReportTable($tableName, $sConnection = 'rp', $type = 'NORMAL', $processUid = '', $gridKey = '') - { - require_once "classes/model/Application.php"; - - $this->className = $this->getPHPName($tableName); - $this->classPeerName = $this->className . 'Peer'; - - if (!file_exists (PATH_WORKSPACE . 'classes/' . $this->className . '.php') ) { - throw new Exception("ERROR: ".PATH_WORKSPACE . 'classes/' . $this->className . '.php'." class file doesn't exit!"); - } - - require_once PATH_WORKSPACE . 'classes/' . $this->className . '.php'; - - //select cases for this Process, ordered by APP_NUMBER - $con = Propel::getConnection($sConnection); - $stmt = $con->createStatement(); - $criteria = new Criteria('workflow'); - $criteria->add(ApplicationPeer::PRO_UID, $processUid); - $criteria->addAscendingOrderByColumn(ApplicationPeer::APP_NUMBER); - $dataset = ApplicationPeer::doSelectRS($criteria); - $dataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - - while ($dataset->next()) { - $row = $dataset->getRow(); - //remove old applications references - $deleteSql = "DELETE FROM $tableName WHERE APP_UID = '".$row['APP_UID']."'"; - $rs = $stmt->executeQuery($deleteSql); - // getting the case data - $caseData = unserialize($row['APP_DATA']); - - // quick fix - // map all empty values as NULL for Database - foreach ($caseData as $dKey => $dValue) { - if (!is_array($dValue)) { // normal fields - if (trim($dValue) === '') { - $caseData[$dKey] = NULL; - } - } - else { // grids - foreach ($caseData[$dKey] as $dIndex => $dRow) { - foreach ($dRow as $k => $v) { - if (trim($v) === '') { - $caseData[$dKey][$dIndex][$k] = NULL; - } - } - } - } - } - - if ($type == 'GRID') { - list($gridName, $gridUid) = explode('-', $gridKey); - $gridData = isset($caseData[$gridName]) ? $caseData[$gridName] : array(); - - foreach ($gridData as $i => $gridRow) { - eval('$obj = new ' .$this->className. '();'); - $obj->fromArray($caseData, BasePeer::TYPE_FIELDNAME); - $obj->setAppUid($row['APP_UID']); - $obj->setAppNumber($row['APP_NUMBER']); - $obj->fromArray(array_change_key_case($gridRow, CASE_UPPER), BasePeer::TYPE_FIELDNAME); - $obj->setRow($i); - $obj->save(); - eval('$obj = new ' .$this->className. '();'); - } - } else { - eval('$obj = new ' .$this->className. '();'); - $obj->fromArray(array_change_key_case($caseData, CASE_UPPER), BasePeer::TYPE_FIELDNAME); - $obj->setAppUid($row['APP_UID']); - $obj->setAppNumber($row['APP_NUMBER']); - $obj->save(); - $obj = null; - } - } - } - - /** - * Update the report table with a determinated case data - * @param string $proUid - * @param string $appUid - * @param string $appNumber - * @param string $caseData - */ - public function updateReportTables($proUid, $appUid, $appNumber, $caseData) - { - G::loadClass('pmTable'); - //get all Active Report Tables - $criteria = new Criteria('workflow'); - $criteria->add(AdditionalTablesPeer::PRO_UID, $proUid); - $dataset = AdditionalTablesPeer::doSelectRS($criteria); - $dataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - - // accomplish all related report tables for this process that contain case data for the target ($appUid) application - while ($dataset->next()) { - $row = $dataset->getRow(); - $className = $row['ADD_TAB_CLASS_NAME']; - // verify if the report table class exists - if (!file_exists (PATH_WORKSPACE . 'classes/' . $className . '.php') ) { - continue; - } - // the class exists then load it. - require_once PATH_WORKSPACE . 'classes/' . $className . '.php'; // - - // create a criteria object of report table class - $c = new Criteria(pmTable::resolveDbSource($row['DBS_UID'])); - // select all related records with this $appUid - eval('$c->add(' . $className . 'Peer::APP_UID, \'' . $appUid . '\');'); - eval('$records = ' . $className . 'Peer::doSelect($c);'); - - switch ($row['ADD_TAB_TYPE']) { //switching by report table type - case 'NORMAL': - // parsing empty values to null - foreach ($caseData as $i => $v) { - $caseData[$i] = $v === '' ? NULL : $v; - } - - if (is_array($records) && count($records) > 0) { // if the record already exists on the report table - foreach ($records as $record) { //update all records - $record->fromArray(array_change_key_case($caseData, CASE_UPPER), BasePeer::TYPE_FIELDNAME); - if ($record->validate()) { - $record->save(); - } - } - } - else { // there are not any record for this application on the table, then create it - eval('$obj = new ' . $className . '();'); - $obj->fromArray(array_change_key_case($caseData, CASE_UPPER), BasePeer::TYPE_FIELDNAME); - $obj->setAppUid($appUid); - $obj->setAppNumber($appNumber); - $obj->save(); - } - break; - - case 'GRID': - list($gridName, $gridUid) = explode('-', $row['ADD_TAB_GRID']); - $gridData = isset($caseData[$gridName]) ? $caseData[$gridName] : array(); - - // delete old records - if (is_array($records) && count($records) > 0) { - foreach ($records as $record) { - $record->delete(); - } - } - // save all grid rows on grid type report table - foreach ($gridData as $i => $gridRow) { - eval('$obj = new ' . $className . '();'); - $obj->fromArray(array_change_key_case($gridRow, CASE_UPPER), BasePeer::TYPE_FIELDNAME); - $obj->setAppUid($appUid); - $obj->setAppNumber($appNumber); - $obj->setRow($i); - $obj->save(); - } - break; - } - } - } - - public function getTableVars($uid, $bWhitType = false) - { - require_once 'classes/model/Fields.php'; - try { - $oCriteria = new Criteria('workflow'); - $oCriteria->addSelectColumn(FieldsPeer::ADD_TAB_UID); - $oCriteria->addSelectColumn(FieldsPeer::FLD_NAME); - $oCriteria->addSelectColumn(FieldsPeer::FLD_TYPE); - $oCriteria->addSelectColumn(FieldsPeer::FLD_DYN_NAME); - $oCriteria->add(FieldsPeer::ADD_TAB_UID, $uid); - $oDataset = ReportVarPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - $aVars = array(); - $aImportedVars = array();//This array will help to control if the variable already exist - while ($aRow = $oDataset->getRow()) { - if ($bWhitType) { - if (!in_array($aRow['FLD_NAME'], $aImportedVars)) { - $aImportedVars[]=$aRow['FLD_NAME']; - $aVars[] = array('sFieldName' => $aRow['FLD_NAME'], 'sFieldDynName' => $aRow['FLD_DYN_NAME'], 'sType' => $aRow['FLD_TYPE']); + public function getPrimaryKeys($type='complete') + { + $this->primaryKeys = array(); + foreach ($this->fields as $field) { + if ($field['FLD_KEY'] == '1') { + if ($type == 'complete') { + $this->primaryKeys[] = $field; + } else { + // just field names + $this->primaryKeys[] = $field['FLD_NAME']; } - }else { - $aVars[] = $aRow['FLD_NAME']; } - $oDataset->next(); - } - return $aVars; - } - catch (Exception $oError) { - throw($oError); - } - } - - public function getAll($start = 0, $limit = 20, $filter = '', $process = null) - { - $oCriteria = new Criteria('workflow'); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_UID); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_NAME); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_DESCRIPTION); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_TYPE); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_TAG); - $oCriteria->addSelectColumn(AdditionalTablesPeer::PRO_UID); - $oCriteria->addSelectColumn(AdditionalTablesPeer::DBS_UID); - - if (isset($process)) { - foreach ($process as $key => $pro_uid) { - if ($key == 'equal') - $oCriteria->add(AdditionalTablesPeer::PRO_UID, $pro_uid, Criteria::EQUAL); - else - $oCriteria->add(AdditionalTablesPeer::PRO_UID, $pro_uid, Criteria::NOT_EQUAL); - } + } + return $this->primaryKeys; } - if ($filter != '' && is_string($filter)) { - $oCriteria->add( - $oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_NAME, '%'.$filter.'%',Criteria::LIKE)->addOr( - $oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_DESCRIPTION, '%'.$filter.'%',Criteria::LIKE)) - ); + public function loadByName($name) + { + try { + $oCriteria = new Criteria('workflow'); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_UID); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_NAME); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_CLASS_NAME); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_DESCRIPTION); + //DEPRECATED! $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_LOG_INSERT); + // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_LOG_UPDATE); + // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_LOG_DELETE); + // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_LOG_SELECT); + // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_MAX_LENGTH); + // $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_SDW_AUTO_DELETE); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_PLG_UID); + $oCriteria->addSelectColumn(AdditionalTablesPeer::DBS_UID); + $oCriteria->addSelectColumn(AdditionalTablesPeer::PRO_UID); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_TYPE); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_GRID); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_TAG); + + // AdditionalTablesPeer::ADD_TAB_NAME is unique + $oCriteria->add(AdditionalTablesPeer::ADD_TAB_NAME, $name, Criteria::EQUAL); + + $oDataset = AdditionalTablesPeer::doSelectRS($oCriteria); + $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + + $oDataset->next(); + + return $oDataset->getRow(); + } catch (Exception $oError) { + throw($oError); + } } - $criteriaCount = clone $oCriteria; - $count = AdditionalTablesPeer::doCount($criteriaCount); + /** + * Create & Update function + */ + public function create($aData, $aFields=array()) + { + if (!isset($aData['ADD_TAB_UID']) || (isset($aData['ADD_TAB_UID']) && $aData['ADD_TAB_UID'] == '')) { + $aData['ADD_TAB_UID'] = G::generateUniqueID(); + } - $oCriteria->setLimit($limit); - $oCriteria->setOffset($start); + $oConnection = Propel::getConnection(AdditionalTablesPeer::DATABASE_NAME); - $oDataset = AdditionalTablesPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + try { + $oAdditionalTables = new AdditionalTables(); + $oAdditionalTables->fromArray($aData, BasePeer::TYPE_FIELDNAME); - $addTables = Array(); - $proUids = Array(); - - while( $oDataset->next() ) { - $row = $oDataset->getRow(); - $row['PRO_TITLE'] = $row['PRO_DESCRIPTION'] = ''; - $addTables[] = $row; - if ($row['PRO_UID'] != '') { - $proUids[] = $row['PRO_UID']; - } + if ($oAdditionalTables->validate()) { + $oConnection->begin(); + $iResult = $oAdditionalTables->save(); + $oConnection->commit(); + /* * **DEPRECATED + require_once 'classes/model/ShadowTable.php'; + $oShadowTable = new ShadowTable(); + $oShadowTable->create(array('ADD_TAB_UID' => $aData['ADD_TAB_UID'], + 'SHD_ACTION' => 'CREATE', + 'SHD_DETAILS' => serialize($aFields), + 'USR_UID' => (isset($_SESSION['USER_LOGGED']) ? $_SESSION['USER_LOGGED'] : ''), + 'APP_UID' => '', + 'SHD_DATE' => date('Y-m-d H:i:s'))); + */ + return $aData['ADD_TAB_UID']; + } else { + $sMessage = ''; + $aValidationFailures = $oAdditionalTables->getValidationFailures(); + foreach ($aValidationFailures as $oValidationFailure) { + $sMessage .= $oValidationFailure->getMessage() . '
'; + } + throw(new Exception('The registry cannot be created!
' . $sMessage)); + } + } catch (Exception $oError) { + $oConnection->rollback(); + throw($oError); + } } - //process details will have the info about the processes - $procDetails = Array(); - - if (count($proUids) > 0) { - //now get the labels for all process, using an array of Uids, - $c = new Criteria('workflow'); - //$c->add ( ContentPeer::CON_CATEGORY, 'PRO_TITLE', Criteria::EQUAL ); - $c->add(ContentPeer::CON_LANG, defined('SYS_LANG')? SYS_LANG: 'en', Criteria::EQUAL); - $c->add(ContentPeer::CON_ID, $proUids, Criteria::IN); - - $dt = ContentPeer::doSelectRS ($c); - $dt->setFetchmode(ResultSet::FETCHMODE_ASSOC); - - while ($dt->next()) { - $row = $dt->getRow(); - $procDetails[$row['CON_ID']][$row['CON_CATEGORY']] = $row['CON_VALUE']; - } - - foreach ($addTables as $i => $addTable) { - if (isset($procDetails[$addTable['PRO_UID']]['PRO_TITLE'])) - $addTables[$i]['PRO_TITLE'] = $procDetails[$addTable['PRO_UID']]['PRO_TITLE']; - - if (isset($procDetails[$addTable['PRO_UID']]['PRO_DESCRIPTION'])) - $addTables[$i]['PRO_DESCRIPTION'] = $procDetails[$addTable['PRO_UID']]['PRO_DESCRIPTION']; - } + public function update($aData, $aFields=array()) + { + $oConnection = Propel::getConnection(AdditionalTablesPeer::DATABASE_NAME); + try { + $oAdditionalTables = AdditionalTablesPeer::retrieveByPK($aData['ADD_TAB_UID']); + if (!is_null($oAdditionalTables)) { + $oAdditionalTables->fromArray($aData, BasePeer::TYPE_FIELDNAME); + if ($oAdditionalTables->validate()) { + $oConnection->begin(); + $iResult = $oAdditionalTables->save(); + $oConnection->commit(); + /* * * DEPRECATED + require_once 'classes/model/ShadowTable.php'; + $oShadowTable = new ShadowTable(); + $oShadowTable->create(array('ADD_TAB_UID' => $aData['ADD_TAB_UID'], + 'SHD_ACTION' => 'ALTER', + 'SHD_DETAILS' => serialize($aFields), + 'USR_UID' => (isset($_SESSION['USER_LOGGED']) ? $_SESSION['USER_LOGGED'] : ''), + 'APP_UID' => '', + 'SHD_DATE' => date('Y-m-d H:i:s'))); + return $iResult; */ + } else { + $sMessage = ''; + $aValidationFailures = $oAdditionalTables->getValidationFailures(); + foreach ($aValidationFailures as $oValidationFailure) { + $sMessage .= $oValidationFailure->getMessage() . '
'; + } + throw(new Exception('The registry cannot be updated!
' . $sMessage)); + } + } else { + throw(new Exception('This row doesn\'t exist!')); + } + } catch (Exception $oError) { + $oConnection->rollback(); + throw($oError); + } } - // // fltering by proces title - // if(isset($filter['process'])) { - // foreach ($addTables as $i => $addTable) { - // if (strpos($addTable['PRO_TITLE'], $filter['process']) === false) - // unset($addTables[$i]); - // } - // } + public function remove($sUID) + { + $oConnection = Propel::getConnection(AdditionalTablesPeer::DATABASE_NAME); + try { + $oAdditionalTables = AdditionalTablesPeer::retrieveByPK($sUID); + if (!is_null($oAdditionalTables)) { + $aAdditionalTables = $oAdditionalTables->toArray(BasePeer::TYPE_FIELDNAME); + $oConnection->begin(); + $iResult = $oAdditionalTables->delete(); + $oConnection->commit(); - return array('rows'=>$addTables, 'count'=>$count); - } + return $iResult; + } else { + throw(new Exception('This row doesn\'t exist!')); + } + } catch (Exception $oError) { + $oConnection->rollback(); + throw($oError); + } + } - /** - * DEPRECATED createPropelClasses() - * - * Don't use this method, it was left only for backward compatibility - * for some external plugins that still is using it - */ + public function deleteAll($id) + { + //deleting pm table + $additionalTable = AdditionalTables::load($id); + AdditionalTables::remove($id); - function createPropelClasses($sTableName, $sClassName, $aFields, $sAddTabUid, $connection='workflow') - { - try { - /*$aUID = array('FLD_NAME' => 'PM_UNIQUE_ID', - 'FLD_TYPE' => 'INT', - 'FLD_KEY' => 'on', - 'FLD_SIZE' => '11', - 'FLD_NULL' => '', - 'FLD_AUTO_INCREMENT' => 'on'); - array_unshift($aFields, $aUID);*/ - $aTypes = array( - 'VARCHAR' => 'string', - 'TEXT' => 'string', - 'DATE' => 'int', - 'INT' => 'int', - 'FLOAT' => 'double' - ); - $aCreoleTypes = array( - 'VARCHAR' => 'VARCHAR', - 'TEXT' => 'LONGVARCHAR', - 'DATE' => 'TIMESTAMP', - 'INT' => 'INTEGER', - 'FLOAT' => 'DOUBLE' - ); - if ($sClassName == '') { - $sClassName = $this->getPHPName($sTableName); - } + //deleting fields + require_once 'classes/model/Fields.php'; + $criteria = new Criteria('workflow'); + $criteria->add(FieldsPeer::ADD_TAB_UID, $id); + FieldsPeer::doDelete($criteria); - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - if (!file_exists($sPath)) { - G::mk_dir($sPath); - } - if (!file_exists($sPath . 'map')) { - G::mk_dir($sPath . 'map'); - } - if (!file_exists($sPath . 'om')) { - G::mk_dir($sPath . 'om'); - } - $aData = array(); - $aData['pathClasses'] = substr(PATH_DB, 0, -1); - $aData['tableName'] = $sTableName; - $aData['className'] = $sClassName; - $aData['connection'] = $connection; - $aData['GUID'] = $sAddTabUid; - - $aData['firstColumn'] = isset($aFields[0])? strtoupper($aFields[0]['FLD_NAME']) : ($aFields[1]['FLD_NAME']); - $aData['totalColumns'] = count($aFields); - $aData['useIdGenerator'] = 'false'; - $oTP1 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'Table.tpl'); - $oTP1->prepare(); - $oTP1->assignGlobal($aData); - file_put_contents($sPath . $sClassName . '.php', $oTP1->getOutputContent()); - $oTP2 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'TablePeer.tpl'); - $oTP2->prepare(); - $oTP2->assignGlobal($aData); - file_put_contents($sPath . $sClassName . 'Peer.php', $oTP2->getOutputContent()); - $aColumns = array(); - $aPKs = array(); - $aNotPKs = array(); - $i = 0; - foreach($aFields as $iKey => $aField) { - $aField['FLD_NAME'] = strtoupper($aField['FLD_NAME']); - if ($aField['FLD_TYPE']=='DATE') $aField['FLD_NULL'] = ''; - $aColumn = array( - 'name' => $aField['FLD_NAME'], - 'phpName' => $this->getPHPName($aField['FLD_NAME']), - 'type' => $aTypes[$aField['FLD_TYPE']], - 'creoleType' => $aCreoleTypes[$aField['FLD_TYPE']], - 'notNull' => ($aField['FLD_NULL'] == 'on' ? 'true' : 'false'), - 'size' => (($aField['FLD_TYPE'] == 'VARCHAR') || ($aField['FLD_TYPE'] == 'INT') || ($aField['FLD_TYPE'] == 'FLOAT') ? $aField['FLD_SIZE'] : 'null'), - 'var' => strtolower($aField['FLD_NAME']), - 'attribute' => (($aField['FLD_TYPE'] == 'VARCHAR') || ($aField['FLD_TYPE'] == 'TEXT') || ($aField['FLD_TYPE'] == 'DATE') ? '$' . strtolower($aField['FLD_NAME']) . " = ''" : '$' . strtolower($aField['FLD_NAME']) . ' = 0'), - 'index' => $i, - ); - if ($aField['FLD_TYPE'] == 'DATE') { - $aColumn['getFunction'] = '/** + //remove all related to pmTable + G::loadClass('pmTable'); + $pmTable = new pmTable($additionalTable['ADD_TAB_NAME']); + $pmTable->setDataSource($additionalTable['DBS_UID']); + $pmTable->remove(); + } + + public function getPHPName($sName) + { + $sName = trim($sName); + $aAux = explode('_', $sName); + foreach ($aAux as $iKey => $sPart) { + $aAux[$iKey] = ucwords(strtolower($sPart)); + } + return implode('', $aAux); + } + + public function deleteMultiple($arrUID) + { + $arrUIDs = explode(",", $arrUID); + foreach ($arrUIDs as $UID) { + $this->deleteAll($UID); + } + } + + public function getDataCriteria($sUID) + { + try { + $aData = $this->load($sUID, true); + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' + ? $aData['ADD_TAB_CLASS_NAME'] + : $this->getPHPName($aData['ADD_TAB_NAME'])); + + if (file_exists($sPath . $sClassName . '.php')) { + require_once $sPath . $sClassName . '.php'; + } else { + return null; + } + + $sClassPeerName = $sClassName . 'Peer'; + $con = Propel::getConnection($aData['DBS_UID']); + $oCriteria = new Criteria($aData['DBS_UID']); + + //eval('$oCriteria->addSelectColumn(' . $sClassPeerName . '::PM_UNIQUE_ID);'); + eval('$oCriteria->addSelectColumn("\'1\' AS DUMMY");'); + foreach ($aData['FIELDS'] as $aField) { + eval('$oCriteria->addSelectColumn(' . $sClassPeerName . '::' . $aField['FLD_NAME'] . ');'); + } + + switch ($aField['FLD_TYPE']) { + case 'VARCHAR': + case 'TEXT': + case 'DATE': + // if($aField['FLD_NULL']!=1) + // eval('$oCriteria->add(' . $sClassPeerName . '::' . $aField['FLD_NAME'] + //. ', \'(�_�_�)\', Criteria::NOT_EQUAL);'); + break; + case 'INT'; + case 'FLOAT': + eval('$oCriteria->add(' . $sClassPeerName . '::' . $aField['FLD_NAME'] + .', -99999999999, Criteria::NOT_EQUAL);'); + break; + } + //eval('$oCriteria->addAscendingOrderByColumn(' . $sClassPeerName . '::PM_UNIQUE_ID);'); + //echo $oCriteria->toString(); + return $oCriteria; + } catch (Exception $oError) { + throw($oError); + } + } + + public function getAllData($sUID, $start=null, $limit=null, $keyOrderUppercase=true) + { + $addTab = new AdditionalTables(); + $aData = $addTab->load($sUID, true); + if (!isset($_SESSION['PROCESS'])) { + $_SESSION["PROCESS"] = $aData['PRO_UID']; + } + $aData['DBS_UID'] = $aData['DBS_UID'] ? $aData['DBS_UID'] : 'workflow'; + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' + ? $aData['ADD_TAB_CLASS_NAME'] + : $this->getPHPName($aData['ADD_TAB_NAME'])); + + if (file_exists($sPath . $sClassName . '.php')) { + require_once $sPath . $sClassName . '.php'; + } else { + return null; + } + + $sClassPeerName = $sClassName . 'Peer'; + $con = Propel::getConnection($aData['DBS_UID']); + $oCriteria = new Criteria($aData['DBS_UID']); + + //eval('$oCriteria->addSelectColumn("\'1\' AS DUMMY");'); + if ($keyOrderUppercase == true) { + foreach ($aData['FIELDS'] as $aField) { + eval('$oCriteria->addSelectColumn(' . $sClassPeerName . '::' . $aField['FLD_NAME'] . ');'); + if ($aField['FLD_KEY'] == '1') { + eval('$oCriteria->addAscendingOrderByColumn('. $sClassPeerName . '::' . $aField['FLD_NAME'] . ');'); + } + } + } + $oCriteriaCount = clone $oCriteria; + //$count = $sClassPeerName::doCount($oCriteria); + eval('$count = ' . $sClassPeerName . '::doCount($oCriteria);'); + + if (isset($limit)) { + $oCriteria->setLimit($limit); + } + if (isset($start)) { + $oCriteria->setOffset($start); + } + //$rs = $sClassPeerName::doSelectRS($oCriteria); + eval('$rs = ' . $sClassPeerName . '::doSelectRS($oCriteria);'); + $rs->setFetchmode(ResultSet::FETCHMODE_ASSOC); + + $rows = Array(); + while ($rs->next()) { + $rows[] = $rs->getRow(); + } + + return array('rows' => $rows, 'count' => $count); + } + + public function checkClassNotExist($sUID) + { + try { + $aData = $this->load($sUID, true); + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' + ? $aData['ADD_TAB_CLASS_NAME'] + : $this->getPHPName($aData['ADD_TAB_NAME'])); + + if (file_exists($sPath . $sClassName . '.php')) { + return $sClassName; + } else { + return ''; + } + } catch (Exception $oError) { + throw($oError); + } + } + + public function saveDataInTable($sUID, $aFields) + { + try { + $aData = $this->load($sUID, true); + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' + ? $aData['ADD_TAB_CLASS_NAME'] + : $this->getPHPName($aData['ADD_TAB_NAME'])); + // $oConnection = Propel::getConnection(FieldsPeer::DATABASE_NAME); + $oConnection = Propel::getConnection($aData['DBS_UID']); + $stmt = $oConnection->createStatement(); + require_once $sPath . $sClassName . '.php'; + $sKeys = ''; + foreach ($aData['FIELDS'] as $aField) { + if ($aField['FLD_KEY'] == 1) { + $vValue = $aFields[$aField['FLD_NAME']]; + eval('$' . $aField['FLD_NAME'] . ' = $vValue;'); + $sKeys .= '$' . $aField['FLD_NAME'] . ','; + } + } + $sKeys = substr($sKeys, 0, -1); + $oClass = new $sClassName; + foreach ($aFields as $sKey => $sValue) { + if (!preg_match("/\(?\)/", $sKey)) { + eval('$oClass->set' . $this->getPHPName($sKey) . '($aFields["' . $sKey . '"]);'); + } + } + if ($oClass->validate()) { + $iResult = $oClass->save(); + } + return true; + } catch (Exception $oError) { + throw($oError); + } + } + + public function getDataTable($sUID, $aKeys) + { + try { + $aData = $this->load($sUID, true); + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' + ? $aData['ADD_TAB_CLASS_NAME'] + : $this->getPHPName($aData['ADD_TAB_NAME'])); + require_once $sPath . $sClassName . '.php'; + $sKeys = ''; + foreach ($aKeys as $sName => $vValue) { + eval('$' . $sName . ' = $vValue;'); + $sKeys .= '$' . $sName . ','; + } + $sKeys = substr($sKeys, 0, -1); + eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK(' . $sKeys . ');'); + //eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK($sPMUID);'); + if (!is_null($oClass)) { + return $oClass->toArray(BasePeer::TYPE_FIELDNAME); + } else { + return false; + } + } catch (Exception $oError) { + throw($oError); + } + } + + public function updateDataInTable($sUID, $aFields) + { + try { + //$sPMUID = $aFields['PM_UNIQUE_ID']; + $aData = $this->load($sUID, true); + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' + ? $aData['ADD_TAB_CLASS_NAME'] + : $this->getPHPName($aData['ADD_TAB_NAME'])); + $oConnection = Propel::getConnection(FieldsPeer::DATABASE_NAME); + require_once $sPath . $sClassName . '.php'; + $sKeys = ''; + foreach ($aData['FIELDS'] as $aField) { + if ($aField['FLD_KEY'] == 1) { + $vValue = $aFields[$aField['FLD_NAME']]; + eval('$' . $aField['FLD_NAME'] . ' = $vValue;'); + $sKeys .= '$' . $aField['FLD_NAME'] . ','; + } + } + $sKeys = substr($sKeys, 0, -1); + eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK(' . $sKeys . ');'); + //eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK($sPMUID);'); + if (!is_null($oClass)) { + $oClass->fromArray($aFields, BasePeer::TYPE_FIELDNAME); + if ($oClass->validate()) { + $oConnection->begin(); + $iResult = $oClass->save(); + $oConnection->commit(); + return $iResult; + } + } else { + $sMessage = ''; + if ($oClass) { + $aValidationFailures = $oClass->getValidationFailures(); + foreach ($aValidationFailures as $oValidationFailure) { + $sMessage .= $oValidationFailure->getMessage() . '
'; + } + } else { + $sMessage = 'Error, row cannot updated'; + return false; + } + throw(new Exception('The registry cannot be updated!
' . $sMessage)); + } + } catch (Exception $oError) { + throw($oError); + } + } + + public function deleteDataInTable($sUID, $aKeys) + { + try { + $aData = $this->load($sUID, true); + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + $sClassName = ($aData['ADD_TAB_CLASS_NAME'] != '' + ? $aData['ADD_TAB_CLASS_NAME'] + : $this->getPHPName($aData['ADD_TAB_NAME'])); + $oConnection = Propel::getConnection(FieldsPeer::DATABASE_NAME); + require_once $sPath . $sClassName . '.php'; + $sKeys = ''; + foreach ($aKeys as $sName => $vValue) { + eval('$' . $sName . ' = $vValue;'); + $sKeys .= '$' . $sName . ','; + } + $sKeys = substr($sKeys, 0, -1); + eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK(' . $sKeys . ');'); + //eval('$oClass = ' . $sClassName . 'Peer::retrieveByPK($sPMUID);'); + if (!is_null($oClass)) { + if ($oClass->validate()) { + $oConnection->begin(); + $iResult = $oClass->delete(); + $oConnection->commit(); + return $iResult; + } + } else { + $sMessage = ''; + $aValidationFailures = $oConnection-- > getValidationFailures(); + foreach ($aValidationFailures as $oValidationFailure) { + $sMessage .= $oValidationFailure->getMessage() . '
'; + } + throw(new Exception('The registry cannot be updated!
' . $sMessage)); + } + } catch (Exception $oError) { + throw($oError); + } + } + + /** + * Populate the report table with all case data + * @param string $sType + * @param string $sProcessUid + * @param string $sGrid + * @return number + */ + public function populateReportTable($tableName, $sConnection='rp', $type='NORMAL', $processUid='', $gridKey='') + { + require_once "classes/model/Application.php"; + + $this->className = $this->getPHPName($tableName); + $this->classPeerName = $this->className . 'Peer'; + + if (!file_exists(PATH_WORKSPACE . 'classes/' . $this->className . '.php')) { + throw new Exception( "ERROR: " . PATH_WORKSPACE . 'classes/' . $this->className . '.php' + . " class file doesn't exit!"); + } + + require_once PATH_WORKSPACE . 'classes/' . $this->className . '.php'; + + //select cases for this Process, ordered by APP_NUMBER + $con = Propel::getConnection($sConnection); + $stmt = $con->createStatement(); + $criteria = new Criteria('workflow'); + $criteria->add(ApplicationPeer::PRO_UID, $processUid); + $criteria->addAscendingOrderByColumn(ApplicationPeer::APP_NUMBER); + $dataset = ApplicationPeer::doSelectRS($criteria); + $dataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + + while ($dataset->next()) { + $row = $dataset->getRow(); + //remove old applications references + $deleteSql = "DELETE FROM $tableName WHERE APP_UID = '" . $row['APP_UID'] . "'"; + $rs = $stmt->executeQuery($deleteSql); + // getting the case data + $caseData = unserialize($row['APP_DATA']); + + // quick fix + // map all empty values as NULL for Database + foreach ($caseData as $dKey => $dValue) { + if (!is_array($dValue)) { + // normal fields + if (trim($dValue) === '') { + $caseData[$dKey] = null; + } + } else { + // grids + foreach ($caseData[$dKey] as $dIndex => $dRow) { + foreach ($dRow as $k => $v) { + if (trim($v) === '') { + $caseData[$dKey][$dIndex][$k] = null; + } + } + } + } + } + + if ($type == 'GRID') { + list($gridName, $gridUid) = explode('-', $gridKey); + $gridData = isset($caseData[$gridName]) ? $caseData[$gridName] : array(); + + foreach ($gridData as $i => $gridRow) { + eval('$obj = new ' . $this->className . '();'); + $obj->fromArray($caseData, BasePeer::TYPE_FIELDNAME); + $obj->setAppUid($row['APP_UID']); + $obj->setAppNumber($row['APP_NUMBER']); + $obj->fromArray(array_change_key_case($gridRow, CASE_UPPER), BasePeer::TYPE_FIELDNAME); + $obj->setRow($i); + $obj->save(); + eval('$obj = new ' . $this->className . '();'); + } + } else { + eval('$obj = new ' . $this->className . '();'); + $obj->fromArray(array_change_key_case($caseData, CASE_UPPER), BasePeer::TYPE_FIELDNAME); + $obj->setAppUid($row['APP_UID']); + $obj->setAppNumber($row['APP_NUMBER']); + $obj->save(); + $obj = null; + } + } + } + + /** + * Update the report table with a determinated case data + * @param string $proUid + * @param string $appUid + * @param string $appNumber + * @param string $caseData + */ + public function updateReportTables($proUid, $appUid, $appNumber, $caseData) + { + G::loadClass('pmTable'); + //get all Active Report Tables + $criteria = new Criteria('workflow'); + $criteria->add(AdditionalTablesPeer::PRO_UID, $proUid); + $dataset = AdditionalTablesPeer::doSelectRS($criteria); + $dataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + + // accomplish all related report tables for this process that contain case data + // for the target ($appUid) application + while ($dataset->next()) { + $row = $dataset->getRow(); + $className = $row['ADD_TAB_CLASS_NAME']; + // verify if the report table class exists + if (!file_exists(PATH_WORKSPACE . 'classes/' . $className . '.php')) { + continue; + } + // the class exists then load it. + require_once PATH_WORKSPACE . 'classes/' . $className . '.php'; + // create a criteria object of report table class + $c = new Criteria(pmTable::resolveDbSource($row['DBS_UID'])); + // select all related records with this $appUid + eval('$c->add(' . $className . 'Peer::APP_UID, \'' . $appUid . '\');'); + eval('$records = ' . $className . 'Peer::doSelect($c);'); + + switch ($row['ADD_TAB_TYPE']) { + //switching by report table type + case 'NORMAL': + // parsing empty values to null + foreach ($caseData as $i => $v) { + $caseData[$i] = $v === '' ? null : $v; + } + + if (is_array($records) && count($records) > 0) { + // if the record already exists on the report table + foreach ($records as $record) { + //update all records + $record->fromArray(array_change_key_case($caseData, CASE_UPPER), BasePeer::TYPE_FIELDNAME); + if ($record->validate()) { + $record->save(); + } + } + } else { + // there are not any record for this application on the table, then create it + eval('$obj = new ' . $className . '();'); + $obj->fromArray(array_change_key_case($caseData, CASE_UPPER), BasePeer::TYPE_FIELDNAME); + $obj->setAppUid($appUid); + $obj->setAppNumber($appNumber); + $obj->save(); + } + break; + case 'GRID': + list($gridName, $gridUid) = explode('-', $row['ADD_TAB_GRID']); + $gridData = isset($caseData[$gridName]) ? $caseData[$gridName] : array(); + + // delete old records + if (is_array($records) && count($records) > 0) { + foreach ($records as $record) { + $record->delete(); + } + } + // save all grid rows on grid type report table + foreach ($gridData as $i => $gridRow) { + eval('$obj = new ' . $className . '();'); + $obj->fromArray(array_change_key_case($gridRow, CASE_UPPER), BasePeer::TYPE_FIELDNAME); + $obj->setAppUid($appUid); + $obj->setAppNumber($appNumber); + $obj->setRow($i); + $obj->save(); + } + break; + } + } + } + + public function getTableVars($uid, $bWhitType=false) + { + require_once 'classes/model/Fields.php'; + try { + $oCriteria = new Criteria('workflow'); + $oCriteria->addSelectColumn(FieldsPeer::ADD_TAB_UID); + $oCriteria->addSelectColumn(FieldsPeer::FLD_NAME); + $oCriteria->addSelectColumn(FieldsPeer::FLD_TYPE); + $oCriteria->addSelectColumn(FieldsPeer::FLD_DYN_NAME); + $oCriteria->add(FieldsPeer::ADD_TAB_UID, $uid); + $oDataset = ReportVarPeer::doSelectRS($oCriteria); + $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $oDataset->next(); + $aVars = array(); + $aImportedVars = array(); //This array will help to control if the variable already exist + while ($aRow = $oDataset->getRow()) { + if ($bWhitType) { + if (!in_array($aRow['FLD_NAME'], $aImportedVars)) { + $aImportedVars[] = $aRow['FLD_NAME']; + $aVars[] = array('sFieldName' => $aRow['FLD_NAME'], + 'sFieldDynName' => $aRow['FLD_DYN_NAME'], + 'sType' => $aRow['FLD_TYPE']); + } + } else { + $aVars[] = $aRow['FLD_NAME']; + } + $oDataset->next(); + } + return $aVars; + } catch (Exception $oError) { + throw($oError); + } + } + + public function getAll($start=0, $limit=20, $filter='', $process=null) + { + $oCriteria = new Criteria('workflow'); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_UID); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_NAME); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_DESCRIPTION); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_TYPE); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_TAG); + $oCriteria->addSelectColumn(AdditionalTablesPeer::PRO_UID); + $oCriteria->addSelectColumn(AdditionalTablesPeer::DBS_UID); + + if (isset($process)) { + foreach ($process as $key => $pro_uid) { + if ($key == 'equal') { + $oCriteria->add(AdditionalTablesPeer::PRO_UID, $pro_uid, Criteria::EQUAL); + } else { + $oCriteria->add(AdditionalTablesPeer::PRO_UID, $pro_uid, Criteria::NOT_EQUAL); + } + } + } + + if ($filter != '' && is_string($filter)) { + $oCriteria->add( + $oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_NAME, '%' . $filter . '%', Criteria::LIKE)->addOr( + $oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_DESCRIPTION, '%' . $filter . '%', Criteria::LIKE)) + ); + } + + $criteriaCount = clone $oCriteria; + $count = AdditionalTablesPeer::doCount($criteriaCount); + + $oCriteria->setLimit($limit); + $oCriteria->setOffset($start); + + $oDataset = AdditionalTablesPeer::doSelectRS($oCriteria); + $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + + $addTables = Array(); + $proUids = Array(); + + while ($oDataset->next()) { + $row = $oDataset->getRow(); + $row['PRO_TITLE'] = $row['PRO_DESCRIPTION'] = ''; + $addTables[] = $row; + if ($row['PRO_UID'] != '') { + $proUids[] = $row['PRO_UID']; + } + } + + //process details will have the info about the processes + $procDetails = Array(); + + if (count($proUids) > 0) { + //now get the labels for all process, using an array of Uids, + $c = new Criteria('workflow'); + //$c->add ( ContentPeer::CON_CATEGORY, 'PRO_TITLE', Criteria::EQUAL ); + $c->add(ContentPeer::CON_LANG, defined('SYS_LANG') ? SYS_LANG : 'en', Criteria::EQUAL); + $c->add(ContentPeer::CON_ID, $proUids, Criteria::IN); + + $dt = ContentPeer::doSelectRS($c); + $dt->setFetchmode(ResultSet::FETCHMODE_ASSOC); + + while ($dt->next()) { + $row = $dt->getRow(); + $procDetails[$row['CON_ID']][$row['CON_CATEGORY']] = $row['CON_VALUE']; + } + + foreach ($addTables as $i => $addTable) { + if (isset($procDetails[$addTable['PRO_UID']]['PRO_TITLE'])) { + $addTables[$i]['PRO_TITLE'] = $procDetails[$addTable['PRO_UID']]['PRO_TITLE']; + } + + if (isset($procDetails[$addTable['PRO_UID']]['PRO_DESCRIPTION'])) { + $addTables[$i]['PRO_DESCRIPTION'] = $procDetails[$addTable['PRO_UID']]['PRO_DESCRIPTION']; + } + } + } + + // // fltering by proces title + // if(isset($filter['process'])) { + // foreach ($addTables as $i => $addTable) { + // if (strpos($addTable['PRO_TITLE'], $filter['process']) === false) + // unset($addTables[$i]); + // } + // } + + return array('rows' => $addTables, 'count' => $count); + } + + /** + * DEPRECATED createPropelClasses() + * + * Don't use this method, it was left only for backward compatibility + * for some external plugins that still is using it + */ + public function createPropelClasses($sTableName, $sClassName, $aFields, $sAddTabUid, $connection='workflow') + { + try { + /* $aUID = array('FLD_NAME' => 'PM_UNIQUE_ID', + 'FLD_TYPE' => 'INT', + 'FLD_KEY' => 'on', + 'FLD_SIZE' => '11', + 'FLD_NULL' => '', + 'FLD_AUTO_INCREMENT' => 'on'); + array_unshift($aFields, $aUID); */ + $aTypes = array( + 'VARCHAR' => 'string', + 'TEXT' => 'string', + 'DATE' => 'int', + 'INT' => 'int', + 'FLOAT' => 'double' + ); + $aCreoleTypes = array( + 'VARCHAR' => 'VARCHAR', + 'TEXT' => 'LONGVARCHAR', + 'DATE' => 'TIMESTAMP', + 'INT' => 'INTEGER', + 'FLOAT' => 'DOUBLE' + ); + if ($sClassName == '') { + $sClassName = $this->getPHPName($sTableName); + } + + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + if (!file_exists($sPath)) { + G::mk_dir($sPath); + } + if (!file_exists($sPath . 'map')) { + G::mk_dir($sPath . 'map'); + } + if (!file_exists($sPath . 'om')) { + G::mk_dir($sPath . 'om'); + } + $aData = array(); + $aData['pathClasses'] = substr(PATH_DB, 0, -1); + $aData['tableName'] = $sTableName; + $aData['className'] = $sClassName; + $aData['connection'] = $connection; + $aData['GUID'] = $sAddTabUid; + + $aData['firstColumn'] = isset($aFields[0]) + ? strtoupper($aFields[0]['FLD_NAME']) + : ($aFields[1]['FLD_NAME']); + $aData['totalColumns'] = count($aFields); + $aData['useIdGenerator'] = 'false'; + $oTP1 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'Table.tpl'); + $oTP1->prepare(); + $oTP1->assignGlobal($aData); + file_put_contents($sPath . $sClassName . '.php', $oTP1->getOutputContent()); + $oTP2 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'TablePeer.tpl'); + $oTP2->prepare(); + $oTP2->assignGlobal($aData); + file_put_contents($sPath . $sClassName . 'Peer.php', $oTP2->getOutputContent()); + $aColumns = array(); + $aPKs = array(); + $aNotPKs = array(); + $i = 0; + foreach ($aFields as $iKey => $aField) { + $aField['FLD_NAME'] = strtoupper($aField['FLD_NAME']); + if ($aField['FLD_TYPE'] == 'DATE') { + $aField['FLD_NULL'] = ''; + } + $aColumn = array( + 'name' => $aField['FLD_NAME'], + 'phpName' => $this->getPHPName($aField['FLD_NAME']), + 'type' => $aTypes[$aField['FLD_TYPE']], + 'creoleType' => $aCreoleTypes[$aField['FLD_TYPE']], + 'notNull' => ($aField['FLD_NULL'] == 'on' ? 'true' : 'false'), + 'size' => (($aField['FLD_TYPE'] == 'VARCHAR') + || ($aField['FLD_TYPE'] == 'INT') + || ($aField['FLD_TYPE'] == 'FLOAT') ? $aField['FLD_SIZE'] : 'null'), + 'var' => strtolower($aField['FLD_NAME']), + 'attribute' => (($aField['FLD_TYPE'] == 'VARCHAR') + || ($aField['FLD_TYPE'] == 'TEXT') + || ($aField['FLD_TYPE'] == 'DATE') + ? '$' . strtolower($aField['FLD_NAME']) . " = ''" + : '$' . strtolower($aField['FLD_NAME']) . ' = 0'), + 'index' => $i, + ); + if ($aField['FLD_TYPE'] == 'DATE') { + $aColumn['getFunction'] = '/** * Get the [optionally formatted] [' . $aColumn['var'] . '] column value. * * @param string $format The date/time format string (either date()-style or strftime()-style). @@ -909,14 +937,16 @@ class AdditionalTables extends BaseAdditionalTables { return null; } elseif (!is_int($this->' . $aColumn['var'] . ')) { // a non-timestamp value was set externally, so we convert it - if (($this->' . $aColumn['var'] . ' == "0000-00-00 00:00:00") || ($this->' . $aColumn['var'] . ' == "0000-00-00") || !$this->' . $aColumn['var'] . ') { + if (($this->' . $aColumn['var'] . ' == "0000-00-00 00:00:00") + || ($this->' . $aColumn['var'] . ' == "0000-00-00") || !$this->' . $aColumn['var'] . ') { $ts = "0"; } else { $ts = strtotime($this->' . $aColumn['var'] . '); } if ($ts === -1 || $ts === false) { // in PHP 5.1 return value changes to FALSE - throw new PropelException("Unable to parse value of [' . $aColumn['var'] . '] as date/time value: " . var_export($this->' . $aColumn['var'] . ', true)); + throw new PropelException("Unable to parse value of [' . $aColumn['var'] . '] as date/time value: " + . var_export($this->' . $aColumn['var'] . ', true)); } } else { $ts = $this->' . $aColumn['var'] . '; @@ -929,9 +959,8 @@ class AdditionalTables extends BaseAdditionalTables { return date($format, $ts); } }'; - } - else { - $aColumn['getFunction'] = '/** + } else { + $aColumn['getFunction'] = '/** * Get the [' . $aColumn['var'] . '] column value. * * @return string @@ -941,11 +970,11 @@ class AdditionalTables extends BaseAdditionalTables { return $this->' . $aColumn['var'] . '; }'; - } - switch ($aField['FLD_TYPE']) { - case 'VARCHAR': - case 'TEXT': - $aColumn['setFunction'] = '// Since the native PHP type for this column is string, + } + switch ($aField['FLD_TYPE']) { + case 'VARCHAR': + case 'TEXT': + $aColumn['setFunction'] = '// Since the native PHP type for this column is string, // we will cast the input to a string (if it is not). if ($v !== null && !is_string($v)) { $v = (string) $v; @@ -955,15 +984,16 @@ class AdditionalTables extends BaseAdditionalTables { $this->' . $aColumn['var'] . ' = $v; $this->modifiedColumns[] = ' . $aData['className'] . 'Peer::' . $aColumn['name'] . '; }'; - break; - case 'DATE': - $aColumn['setFunction'] = 'if ($v !== null && !is_int($v)) { + break; + case 'DATE': + $aColumn['setFunction'] = 'if ($v !== null && !is_int($v)) { // if($v == \'\') // $ts = null; // else $ts = strtotime($v); if ($ts === -1 || $ts === false) { // in PHP 5.1 return value changes to FALSE - //throw new PropelException("Unable to parse date/time value for [' . $aColumn['var'] . '] from input: " . var_export($v, true)); + //throw new PropelException("Unable to parse date/time value for [' . $aColumn['var'] . '] from input: " + // . var_export($v, true)); } } else { $ts = $v; @@ -972,9 +1002,9 @@ class AdditionalTables extends BaseAdditionalTables { $this->' . $aColumn['var'] . ' = $ts; $this->modifiedColumns[] = ' . $aData['className'] . 'Peer::' . $aColumn['name'] . '; }'; - break; - case 'INT': - $aColumn['setFunction'] = '// Since the native PHP type for this column is integer, + break; + case 'INT': + $aColumn['setFunction'] = '// Since the native PHP type for this column is integer, // we will cast the input value to an int (if it is not). if ($v !== null && !is_int($v) && is_numeric($v)) { $v = (int) $v; @@ -983,225 +1013,230 @@ class AdditionalTables extends BaseAdditionalTables { $this->' . $aColumn['var'] . ' = $v; $this->modifiedColumns[] = ' . $aData['className'] . 'Peer::' . $aColumn['name'] . '; }'; - break; - case 'FLOAT': - $aColumn['setFunction'] = 'if ($this->' . $aColumn['var'] . ' !== $v || $v === 0) { + break; + case 'FLOAT': + $aColumn['setFunction'] = 'if ($this->' . $aColumn['var'] . ' !== $v || $v === 0) { $this->' . $aColumn['var'] . ' = $v; $this->modifiedColumns[] = ' . $aData['className'] . 'Peer::' . $aColumn['name'] . '; }'; - break; - } - $aColumns[] = $aColumn; - if ($aField['FLD_KEY'] == 1 || $aField['FLD_KEY'] === 'on') { - $aPKs[] = $aColumn; - } - else { - $aNotPKs[] = $aColumn; - } - if ($aField['FLD_AUTO_INCREMENT'] == 1 || $aField['FLD_AUTO_INCREMENT'] === 'on') { - $aData['useIdGenerator'] = 'true'; + break; + } + $aColumns[] = $aColumn; + if ($aField['FLD_KEY'] == 1 || $aField['FLD_KEY'] === 'on') { + $aPKs[] = $aColumn; + } else { + $aNotPKs[] = $aColumn; + } + if ($aField['FLD_AUTO_INCREMENT'] == 1 || $aField['FLD_AUTO_INCREMENT'] === 'on') { + $aData['useIdGenerator'] = 'true'; + } + $i++; + } + $oTP3 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'map' + . PATH_SEP . 'TableMapBuilder.tpl'); + $oTP3->prepare(); + $oTP3->assignGlobal($aData); + foreach ($aPKs as $iIndex => $aColumn) { + $oTP3->newBlock('primaryKeys'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP3->assign($sKey, $aColumn[$sKey]); + } + } + $oTP3->gotoBlock('_ROOT'); + foreach ($aNotPKs as $iIndex => $aColumn) { + $oTP3->newBlock('columnsWhitoutKeys'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP3->assign($sKey, $aColumn[$sKey]); + } + } + file_put_contents($sPath . PATH_SEP . 'map' . PATH_SEP . $sClassName + . 'MapBuilder.php', $oTP3->getOutputContent()); + $oTP4 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'om' . PATH_SEP . 'BaseTable.tpl'); + $oTP4->prepare(); + switch (count($aPKs)) { + case 0: + $aData['getPrimaryKeyFunction'] = 'return null;'; + $aData['setPrimaryKeyFunction'] = ''; + break; + case 1: + $aData['getPrimaryKeyFunction'] = 'return $this->get' . $aPKs[0]['phpName'] . '();'; + $aData['setPrimaryKeyFunction'] = '$this->set' . $aPKs[0]['phpName'] . '($key);'; + break; + default: + $aData['getPrimaryKeyFunction'] = '$pks = array();' . "\n"; + $aData['setPrimaryKeyFunction'] = ''; + foreach ($aPKs as $iIndex => $aColumn) { + $aData['getPrimaryKeyFunction'] .= '$pks[' . $iIndex . '] = $this->get' + . $aColumn['phpName'] . '();' . "\n"; + $aData['setPrimaryKeyFunction'] .= '$this->set' . $aColumn['phpName'] + . '($keys[' . $iIndex . ']);' . "\n"; + } + $aData['getPrimaryKeyFunction'] .= 'return $pks;' . "\n"; + break; + } + $oTP4->assignGlobal($aData); + foreach ($aColumns as $iIndex => $aColumn) { + $oTP4->newBlock('allColumns1'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + $oTP4->newBlock('allColumns2'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + $oTP4->newBlock('allColumns3'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + $oTP4->newBlock('allColumns4'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + $oTP4->newBlock('allColumns5'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + $oTP4->newBlock('allColumns6'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + $oTP4->newBlock('allColumns7'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + $oTP4->newBlock('allColumns8'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + $oTP4->newBlock('allColumns9'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + } + $oTP4->gotoBlock('_ROOT'); + foreach ($aPKs as $iIndex => $aColumn) { + $oTP4->newBlock('primaryKeys1'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + } + $oTP4->gotoBlock('_ROOT'); + foreach ($aPKs as $iIndex => $aColumn) { + $oTP4->newBlock('primaryKeys2'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + } + $oTP4->gotoBlock('_ROOT'); + foreach ($aNotPKs as $iIndex => $aColumn) { + $oTP4->newBlock('columnsWhitoutKeys'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP4->assign($sKey, $aColumn[$sKey]); + } + } + file_put_contents($sPath . PATH_SEP . 'om' . PATH_SEP . 'Base' + . $sClassName . '.php', $oTP4->getOutputContent()); + $oTP5 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'om' . PATH_SEP . 'BaseTablePeer.tpl'); + $oTP5->prepare(); + $sKeys = ''; + foreach ($aPKs as $iIndex => $aColumn) { + $sKeys .= '$' . $aColumn['var'] . ', '; + } + $sKeys = substr($sKeys, 0, -2); + //$sKeys = '$pm_unique_id'; + if ($sKeys != '') { + $aData['sKeys'] = $sKeys; + } else { + $aData['sKeys'] = '$DUMMY'; + } + $oTP5->assignGlobal($aData); + foreach ($aColumns as $iIndex => $aColumn) { + $oTP5->newBlock('allColumns1'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + $oTP5->newBlock('allColumns2'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + $oTP5->newBlock('allColumns3'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + $oTP5->newBlock('allColumns4'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + $oTP5->newBlock('allColumns5'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + $oTP5->newBlock('allColumns6'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + $oTP5->newBlock('allColumns7'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + $oTP5->newBlock('allColumns8'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + $oTP5->newBlock('allColumns9'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + $oTP5->newBlock('allColumns10'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + } + $oTP5->gotoBlock('_ROOT'); + foreach ($aPKs as $iIndex => $aColumn) { + $oTP5->newBlock('primaryKeys1'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + } + foreach ($aPKs as $iIndex => $aColumn) { + $oTP5->newBlock('primaryKeys2'); + $aKeys = array_keys($aColumn); + foreach ($aKeys as $sKey) { + $oTP5->assign($sKey, $aColumn[$sKey]); + } + } + file_put_contents($sPath . PATH_SEP . 'om' . PATH_SEP . 'Base' + . $sClassName . 'Peer.php', $oTP5->getOutputContent()); + } catch (Exception $oError) { + throw($oError); } - $i++; - } - $oTP3 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'map' . PATH_SEP . 'TableMapBuilder.tpl'); - $oTP3->prepare(); - $oTP3->assignGlobal($aData); - foreach ($aPKs as $iIndex => $aColumn) { - $oTP3->newBlock('primaryKeys'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP3->assign($sKey, $aColumn[$sKey]); - } - } - $oTP3->gotoBlock('_ROOT'); - foreach ($aNotPKs as $iIndex => $aColumn) { - $oTP3->newBlock('columnsWhitoutKeys'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP3->assign($sKey, $aColumn[$sKey]); - } - } - file_put_contents($sPath . PATH_SEP . 'map' . PATH_SEP . $sClassName . 'MapBuilder.php', $oTP3->getOutputContent()); - $oTP4 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'om' . PATH_SEP . 'BaseTable.tpl'); - $oTP4->prepare(); - switch (count($aPKs)) { - case 0: - $aData['getPrimaryKeyFunction'] = 'return null;'; - $aData['setPrimaryKeyFunction'] = ''; - break; - case 1: - $aData['getPrimaryKeyFunction'] = 'return $this->get' . $aPKs[0]['phpName'] . '();'; - $aData['setPrimaryKeyFunction'] = '$this->set' . $aPKs[0]['phpName'] . '($key);'; - break; - default: - $aData['getPrimaryKeyFunction'] = '$pks = array();' . "\n"; - $aData['setPrimaryKeyFunction'] = ''; - foreach ($aPKs as $iIndex => $aColumn) { - $aData['getPrimaryKeyFunction'] .= '$pks[' . $iIndex . '] = $this->get' . $aColumn['phpName'] . '();' . "\n"; - $aData['setPrimaryKeyFunction'] .= '$this->set' . $aColumn['phpName'] . '($keys[' . $iIndex . ']);' . "\n"; - } - $aData['getPrimaryKeyFunction'] .= 'return $pks;' . "\n"; - break; - } - $oTP4->assignGlobal($aData); - foreach ($aColumns as $iIndex => $aColumn) { - $oTP4->newBlock('allColumns1'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - $oTP4->newBlock('allColumns2'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - $oTP4->newBlock('allColumns3'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - $oTP4->newBlock('allColumns4'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - $oTP4->newBlock('allColumns5'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - $oTP4->newBlock('allColumns6'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - $oTP4->newBlock('allColumns7'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - $oTP4->newBlock('allColumns8'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - $oTP4->newBlock('allColumns9'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - } - $oTP4->gotoBlock('_ROOT'); - foreach ($aPKs as $iIndex => $aColumn) { - $oTP4->newBlock('primaryKeys1'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - } - $oTP4->gotoBlock('_ROOT'); - foreach ($aPKs as $iIndex => $aColumn) { - $oTP4->newBlock('primaryKeys2'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - } - $oTP4->gotoBlock('_ROOT'); - foreach ($aNotPKs as $iIndex => $aColumn) { - $oTP4->newBlock('columnsWhitoutKeys'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP4->assign($sKey, $aColumn[$sKey]); - } - } - file_put_contents($sPath . PATH_SEP . 'om' . PATH_SEP . 'Base' . $sClassName . '.php', $oTP4->getOutputContent()); - $oTP5 = new TemplatePower(PATH_TPL . 'additionalTables' . PATH_SEP . 'om' . PATH_SEP . 'BaseTablePeer.tpl'); - $oTP5->prepare(); - $sKeys = ''; - foreach ($aPKs as $iIndex => $aColumn) { - $sKeys .= '$' . $aColumn['var'] . ', '; - } - $sKeys = substr($sKeys, 0, -2); - //$sKeys = '$pm_unique_id'; - if ($sKeys != '') { - $aData['sKeys'] = $sKeys; - } - else { - $aData['sKeys'] = '$DUMMY'; - } - $oTP5->assignGlobal($aData); - foreach ($aColumns as $iIndex => $aColumn) { - $oTP5->newBlock('allColumns1'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - $oTP5->newBlock('allColumns2'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - $oTP5->newBlock('allColumns3'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - $oTP5->newBlock('allColumns4'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - $oTP5->newBlock('allColumns5'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - $oTP5->newBlock('allColumns6'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - $oTP5->newBlock('allColumns7'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - $oTP5->newBlock('allColumns8'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - $oTP5->newBlock('allColumns9'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - $oTP5->newBlock('allColumns10'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - } - $oTP5->gotoBlock('_ROOT'); - foreach ($aPKs as $iIndex => $aColumn) { - $oTP5->newBlock('primaryKeys1'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - } - foreach ($aPKs as $iIndex => $aColumn) { - $oTP5->newBlock('primaryKeys2'); - $aKeys = array_keys($aColumn); - foreach ($aKeys as $sKey) { - $oTP5->assign($sKey, $aColumn[$sKey]); - } - } - file_put_contents($sPath . PATH_SEP . 'om' . PATH_SEP . 'Base' . $sClassName . 'Peer.php', $oTP5->getOutputContent()); } - catch (Exception $oError) { - throw($oError); - } - } -} // AdditionalTables +} +// AdditionalTables + \ No newline at end of file diff --git a/workflow/engine/controllers/pmTablesProxy.php b/workflow/engine/controllers/pmTablesProxy.php index 732b086ba..d79b5fa9e 100755 --- a/workflow/engine/controllers/pmTablesProxy.php +++ b/workflow/engine/controllers/pmTablesProxy.php @@ -1,1556 +1,1567 @@ * @inherits HttpProxyController * @access public */ - require_once 'classes/model/AdditionalTables.php'; class pmTablesProxy extends HttpProxyController { - protected $className; - protected $classPeerName; - protected $dynUid; + protected $className; + protected $classPeerName; + protected $dynUid; - /** - * get pmtables list - * @param string $httpData->start - * @param string $httpData->limit - * @param string $httpData->textFilter - */ - public function getList($httpData) - { - G::LoadClass('configuration'); - G::LoadClass('processMap'); - G::LoadClass('pmTable'); - $configurations = new Configurations(); - $processMap = new processMap(); - - // setting parameters - $config = $configurations->getConfiguration('additionalTablesList', 'pageSize','',$_SESSION['USER_LOGGED']); - $env = $configurations->getConfiguration('ENVIRONMENT_SETTINGS', ''); - $limit_size = isset($config->pageSize) ? $config['pageSize'] : 20; - $start = isset($httpData->start) ? $httpData->start : 0; - $limit = isset($httpData->limit) ? $httpData->limit : $limit_size; - $filter = isset($httpData->textFilter) ? $httpData->textFilter : ''; - $pro_uid = isset($httpData->pro_uid) ? $httpData->pro_uid : null; + /** + * get pmtables list + * @param string $httpData->start + * @param string $httpData->limit + * @param string $httpData->textFilter + */ + public function getList($httpData) + { + G::LoadClass('configuration'); + G::LoadClass('processMap'); + G::LoadClass('pmTable'); + $configurations = new Configurations(); + $processMap = new processMap(); - if ($pro_uid !== null) { - $process = $pro_uid == '' ? array('not_equal'=>$pro_uid) : array('equal'=>$pro_uid); - $addTables = AdditionalTables::getAll($start, $limit, $filter, $process); - - $c = $processMap->getReportTablesCriteria($pro_uid); - $oDataset = RoutePeer::doSelectRS($c); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $reportTablesOldList = array(); - while($oDataset->next()) { - $reportTablesOldList[] = $oDataset->getRow(); - } - $addTables['count'] += count($reportTablesOldList); - - foreach ($reportTablesOldList as $i => $oldRepTab) { - $addTables['rows'][] = array( - 'ADD_TAB_UID' => $oldRepTab['REP_TAB_UID'], - 'PRO_UID' => $oldRepTab['PRO_UID'], - 'DBS_UID' => ($oldRepTab['REP_TAB_CONNECTION']=='wf'? 'workflow' : 'rp'), - 'ADD_TAB_DESCRIPTION' => $oldRepTab['REP_TAB_TITLE'], - 'ADD_TAB_NAME' => $oldRepTab['REP_TAB_NAME'], - 'ADD_TAB_TYPE' => $oldRepTab['REP_TAB_TYPE'], - 'TYPE' => 'CLASSIC' + // setting parameters + $config = $configurations->getConfiguration('additionalTablesList', 'pageSize', '', $_SESSION['USER_LOGGED']); + $env = $configurations->getConfiguration('ENVIRONMENT_SETTINGS', ''); + $limit_size = isset($config->pageSize) ? $config['pageSize'] : 20; + $start = isset($httpData->start) ? $httpData->start : 0; + $limit = isset($httpData->limit) ? $httpData->limit : $limit_size; + $filter = isset($httpData->textFilter) ? $httpData->textFilter : ''; + $pro_uid = isset($httpData->pro_uid) ? $httpData->pro_uid : null; + + if ($pro_uid !== null) { + $process = $pro_uid == '' ? array('not_equal' => $pro_uid) : array('equal' => $pro_uid); + $addTables = AdditionalTables::getAll($start, $limit, $filter, $process); + + $c = $processMap->getReportTablesCriteria($pro_uid); + $oDataset = RoutePeer::doSelectRS($c); + $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $reportTablesOldList = array(); + while ($oDataset->next()) { + $reportTablesOldList[] = $oDataset->getRow(); + } + $addTables['count'] += count($reportTablesOldList); + + foreach ($reportTablesOldList as $i => $oldRepTab) { + $addTables['rows'][] = array( + 'ADD_TAB_UID' => $oldRepTab['REP_TAB_UID'], + 'PRO_UID' => $oldRepTab['PRO_UID'], + 'DBS_UID' => ($oldRepTab['REP_TAB_CONNECTION'] == 'wf' ? 'workflow' : 'rp'), + 'ADD_TAB_DESCRIPTION' => $oldRepTab['REP_TAB_TITLE'], + 'ADD_TAB_NAME' => $oldRepTab['REP_TAB_NAME'], + 'ADD_TAB_TYPE' => $oldRepTab['REP_TAB_TYPE'], + 'TYPE' => 'CLASSIC' + ); + } + } else { + $addTables = AdditionalTables::getAll($start, $limit, $filter); + } + + foreach ($addTables['rows'] as $i => $table) { + try { + $con = Propel::getConnection(pmTable::resolveDbSource($table['DBS_UID'])); + $stmt = $con->createStatement(); + $rs = $stmt->executeQuery('SELECT COUNT(*) AS NUM_ROWS from ' . $table['ADD_TAB_NAME']); + if ($rs->next()) { + $r = $rs->getRow(); + $addTables['rows'][$i]['NUM_ROWS'] = $r['NUM_ROWS']; + } else { + $addTables['rows'][$i]['NUM_ROWS'] = 0; + } + + //removing the prefix "PMT" to allow alphabetical order (just in view) + if (substr($addTables['rows'][$i]['ADD_TAB_NAME'], 0, 4) == 'PMT_') { + $addTables['rows'][$i]['ADD_TAB_NAME'] = substr($addTables['rows'][$i]['ADD_TAB_NAME'], 4); + } + } catch (Exception $e) { + $addTables['rows'][$i]['NUM_ROWS'] = G::LoadTranslation('ID_TABLE_NOT_FOUND'); + } + } + + return $addTables; + } + + /** + * get processesList + */ + public function getProcessList() + { + require_once 'classes/model/Process.php'; + + $process = new Process(); + return $process->getAll(); + } + + /** + * get database connection list + */ + public function getDbConnectionsList() + { + if (!isset($_SESSION['PROCESS'])) { + $_SESSION['PROCESS'] = $_POST['PRO_UID']; + } + G::LoadClass('dbConnections'); + $proUid = $_POST['PRO_UID']; + $dbConn = new DbConnections(); + $dbConnections = $dbConn->getConnectionsProUid($proUid); + $defaultConnections = array( + array('DBS_UID' => 'workflow', 'DBS_NAME' => 'Workflow'), + array('DBS_UID' => 'rp', 'DBS_NAME' => 'REPORT') ); - } - } - else { - $addTables = AdditionalTables::getAll($start, $limit, $filter); - } - - foreach ($addTables['rows'] as $i => $table) { - try { - $con = Propel::getConnection(pmTable::resolveDbSource($table['DBS_UID'])); - $stmt = $con->createStatement(); - $rs = $stmt->executeQuery('SELECT COUNT(*) AS NUM_ROWS from ' . $table['ADD_TAB_NAME']); - if ($rs->next()) { - $r = $rs->getRow(); - $addTables['rows'][$i]['NUM_ROWS'] = $r['NUM_ROWS']; - } - else { - $addTables['rows'][$i]['NUM_ROWS'] = 0; - } - //removing the prefix "PMT" to allow alphabetical order (just in view) - if (substr($addTables['rows'][$i]['ADD_TAB_NAME'], 0, 4) == 'PMT_') { - $addTables['rows'][$i]['ADD_TAB_NAME'] = substr($addTables['rows'][$i]['ADD_TAB_NAME'], 4); - } - } - catch (Exception $e) { - $addTables['rows'][$i]['NUM_ROWS'] = G::LoadTranslation('ID_TABLE_NOT_FOUND'); - } + $dbConnections = array_merge($defaultConnections, $dbConnections); + + return $dbConnections; } - return $addTables; - } + /** + * get dynaform fields + * @param string $httpData->PRO_UID + * @param string $httpData->TYPE + * @param string $httpData->GRID_UID + */ + public function getDynafields($httpData) + { + G::LoadClass('reportTables'); - /** - * get processesList - */ - public function getProcessList() - { - require_once 'classes/model/Process.php'; - - $process = new Process(); - return $process->getAll(); - } + $aFields['FIELDS'] = array(); + $aFields['PRO_UID'] = $httpData->PRO_UID; + $dynFields = array(); - /** - * get database connection list - */ - public function getDbConnectionsList() - { - if (! isset($_SESSION['PROCESS'])) { - $_SESSION['PROCESS'] = $_POST['PRO_UID']; - } - G::LoadClass ( 'dbConnections'); - $proUid = $_POST['PRO_UID']; - $dbConn = new DbConnections(); - $dbConnections = $dbConn->getConnectionsProUid($proUid); - $defaultConnections = array ( - array('DBS_UID'=>'workflow', 'DBS_NAME'=>'Workflow'), - array('DBS_UID'=>'rp', 'DBS_NAME'=>'REPORT') - ); + if (isset($httpData->TYPE) && $httpData->TYPE == 'GRID') { + $aProcessGridFields = Array(); + if (isset($httpData->GRID_UID)) { + list($gridName, $gridId) = explode('-', $httpData->GRID_UID); + $this->dynUid = $gridId; - $dbConnections = array_merge($defaultConnections, $dbConnections); + $httpData->textFilter = isset($httpData->textFilter) ? $httpData->textFilter : null; + $dynFields = $this->_getDynafields($aFields['PRO_UID'], 'grid', $httpData->start, + $httpData->limit, $httpData->textFilter); + } else { + if (isset($_SESSION['_cache_pmtables'])) { + unset($_SESSION['_cache_pmtables']); + } + $gridFields = $this->_getGridFields($aFields['PRO_UID']); - return $dbConnections; - } - - /** - * get dynaform fields - * @param string $httpData->PRO_UID - * @param string $httpData->TYPE - * @param string $httpData->GRID_UID - */ - public function getDynafields($httpData) - { - G::LoadClass('reportTables'); - - $aFields['FIELDS'] = array(); - $aFields['PRO_UID'] = $httpData->PRO_UID; - $dynFields = array(); - - if(isset($httpData->TYPE) && $httpData->TYPE == 'GRID') { - $aProcessGridFields = Array(); - if (isset($httpData->GRID_UID)) { - list($gridName, $gridId) = explode('-', $httpData->GRID_UID); - $this->dynUid = $gridId; - - $httpData->textFilter = isset($httpData->textFilter) ? $httpData->textFilter : null; - $dynFields = $this->_getDynafields($aFields['PRO_UID'], 'grid', $httpData->start, $httpData->limit, $httpData->textFilter); - } - else { - if (isset($_SESSION['_cache_pmtables'])) { - unset($_SESSION['_cache_pmtables']); + foreach ($gridFields as $gfield) { + $dynFields[] = array( + 'FIELD_UID' => $gfield['name'] . '-' . $gfield['xmlform'], + 'FIELD_NAME' => $gfield['name'] + ); + } + } + } else { + // normal dynaform + $httpData->textFilter = isset($httpData->textFilter) ? $httpData->textFilter : null; + $dynFields = $this->_getDynafields($aFields['PRO_UID'], 'xmlform', $httpData->start, + $httpData->limit, $httpData->textFilter); } - $gridFields = $this->_getGridFields($aFields['PRO_UID']); - foreach ($gridFields as $gfield) { - $dynFields[] = array( - 'FIELD_UID' => $gfield['name'] . '-' . $gfield['xmlform'], - 'FIELD_NAME' => $gfield['name'] - ); + return $dynFields; + } + + public function updateAvDynafields($httpData) + { + $indexes = explode(',', $httpData->indexes); + $fields = array(); + $httpData->isset = $httpData->isset == 'true' ? true : false; + + if (isset($_SESSION['_cache_pmtables']) && $_SESSION['_cache_pmtables']['pro_uid'] == $httpData->PRO_UID) { + foreach ($indexes as $i) { + if (is_numeric($i)) { + if (isset($_SESSION['_cache_pmtables']['rows'][$i])) { + $_SESSION['_cache_pmtables']['rows'][$i]['_isset'] = $httpData->isset; + if ($httpData->isset) { + $_SESSION['_cache_pmtables']['count']++; + } else { + $_SESSION['_cache_pmtables']['count']--; + } + + $fields[] = $_SESSION['_cache_pmtables']['rows'][$i]['FIELD_NAME']; + } + } else { + + $index = $_SESSION['_cache_pmtables']['indexes'][$i]; + $_SESSION['_cache_pmtables']['rows'][$index]['_isset'] = $httpData->isset; + } + } } - } + return $fields; } - else { // normal dynaform - $httpData->textFilter = isset($httpData->textFilter) ? $httpData->textFilter : null; - $dynFields = $this->_getDynafields($aFields['PRO_UID'], 'xmlform', $httpData->start, $httpData->limit, $httpData->textFilter); - } - - return $dynFields; - } - public function updateAvDynafields($httpData) - { - $indexes = explode(',', $httpData->indexes); - $fields = array(); - $httpData->isset = $httpData->isset=='true'? true: false; - - if (isset($_SESSION['_cache_pmtables']) && $_SESSION['_cache_pmtables']['pro_uid']== $httpData->PRO_UID) { - foreach ($indexes as $i) { - if (is_numeric($i)) { - if (isset($_SESSION['_cache_pmtables']['rows'][$i])) { - $_SESSION['_cache_pmtables']['rows'][$i]['_isset'] = $httpData->isset; - if ($httpData->isset) { - $_SESSION['_cache_pmtables']['count']++; - } else { - $_SESSION['_cache_pmtables']['count']--; + /** + * save pm table + */ + public function save($httpData, $alterTable=true) + { + require_once 'classes/model/AdditionalTables.php'; + require_once 'classes/model/Fields.php'; + + try { + ob_start(); + $data = (array) $httpData; + $data['PRO_UID'] = trim($data['PRO_UID']); + $data['columns'] = G::json_decode(stripslashes($httpData->columns)); //decofing data columns + + $isReportTable = $data['PRO_UID'] != '' ? true : false; + $oAdditionalTables = new AdditionalTables(); + $oFields = new Fields(); + $repTabClassName = $oAdditionalTables->getPHPName($data['REP_TAB_NAME']); + $columns = $data['columns']; + + // Reserved Words + $reservedWords = array( + 'ALTER', 'CLOSE', 'COMMIT', 'CREATE', 'DECLARE', 'DELETE', 'DROP', 'FETCH', 'FUNCTION', 'GRANT', + 'INDEX', 'INSERT', 'OPEN', 'REVOKE', 'ROLLBACK', 'SELECT', 'SYNONYM', 'TABLE', 'UPDATE', 'VIEW', + 'APP_UID', 'ROW', 'PMTABLE' + ); + + // verify if exists. + if ($data['REP_TAB_UID'] == '' || (isset($httpData->forceUid) && $httpData->forceUid)) { + //new report table + if ($isReportTable && $alterTable) { + //setting default columns + $defaultColumns = $this->_getReportTableDefaultColumns($data['REP_TAB_TYPE']); + $columns = array_merge($defaultColumns, $columns); + } + + /** validations * */ + if (is_array($oAdditionalTables->loadByName($data['REP_TAB_NAME']))) { + throw new Exception(G::loadTranslation('ID_PMTABLE_ALREADY_EXISTS', array($data['REP_TAB_NAME']))); + } + + if (in_array(strtoupper($data['REP_TAB_NAME']), $reservedWords)) { + throw new Exception(G::loadTranslation('ID_PMTABLE_INVALID_NAME', array($data['REP_TAB_NAME']))); + } } - $fields[] = $_SESSION['_cache_pmtables']['rows'][$i]['FIELD_NAME']; - } - } else { - - $index = $_SESSION['_cache_pmtables']['indexes'][$i]; - $_SESSION['_cache_pmtables']['rows'][$index]['_isset'] = $httpData->isset; - } - } + //backward compatility + foreach ($columns as $i => $column) { + switch ($column->field_type) { + case 'INT': $columns[$i]->field_type = 'INTEGER'; + break; + case 'TEXT': $columns[$i]->field_type = 'LONGVARCHAR'; + break; + // propel DATETIME equivalent is TIMESTAMP + case 'DATETIME': $columns[$i]->field_type = 'TIMESTAMP'; + break; + } + + // VALIDATIONS + if ($columns[$i]->field_autoincrement) { + if ($columns[$i]->field_type !== 'INTEGER') { + $columns[$i]->field_autoincrement = false; + } + } + } + + G::loadClass('pmTable'); + + $pmTable = new pmTable($data['REP_TAB_NAME']); + $pmTable->setDataSource($data['REP_TAB_CONNECTION']); + $pmTable->setColumns($columns); + $pmTable->setAlterTable($alterTable); + $pmTable->build(); + + $buildResult = ob_get_contents(); + ob_end_clean(); + + // Updating additional table struture information + $addTabData = array( + 'ADD_TAB_UID' => $data['REP_TAB_UID'], + 'ADD_TAB_NAME' => $data['REP_TAB_NAME'], + 'ADD_TAB_CLASS_NAME' => $repTabClassName, + 'ADD_TAB_DESCRIPTION' => $data['REP_TAB_DSC'], + 'ADD_TAB_PLG_UID' => '', + 'DBS_UID' => ($data['REP_TAB_CONNECTION'] ? $data['REP_TAB_CONNECTION'] : 'workflow'), + 'PRO_UID' => $data['PRO_UID'], + 'ADD_TAB_TYPE' => $data['REP_TAB_TYPE'], + 'ADD_TAB_GRID' => $data['REP_TAB_GRID'] + ); + if ($data['REP_TAB_UID'] == '' || (isset($httpData->forceUid) && $httpData->forceUid)) { + //new report table + //create record + $addTabUid = $oAdditionalTables->create($addTabData); + } else { + //editing report table + //updating record + $addTabUid = $data['REP_TAB_UID']; + $oAdditionalTables->update($addTabData); + + //removing old data fields references + $oCriteria = new Criteria('workflow'); + $oCriteria->add(FieldsPeer::ADD_TAB_UID, $data['REP_TAB_UID']); + FieldsPeer::doDelete($oCriteria); + } + + // Updating pmtable fields + foreach ($columns as $i => $column) { + $field = array( + 'FLD_UID' => $column->uid, + 'FLD_INDEX' => $i, + 'ADD_TAB_UID' => $addTabUid, + 'FLD_NAME' => $column->field_name, + 'FLD_DESCRIPTION' => $column->field_label, + 'FLD_TYPE' => $column->field_type, + 'FLD_SIZE' => $column->field_size == '' ? null : $column->field_size, + 'FLD_NULL' => $column->field_null ? 1 : 0, + 'FLD_AUTO_INCREMENT' => $column->field_autoincrement ? 1 : 0, + 'FLD_KEY' => $column->field_key ? 1 : 0, + 'FLD_FOREIGN_KEY' => 0, + 'FLD_FOREIGN_KEY_TABLE' => '', + 'FLD_DYN_NAME' => $column->field_dyn, + 'FLD_DYN_UID' => $column->field_uid, + 'FLD_FILTER' => (isset($column->field_filter) && $column->field_filter) ? 1 : 0 + ); + $oFields->create($field); + } + + if ($isReportTable && $alterTable) { + // the table was create successfully but we're catching problems while populating table + try { + $oAdditionalTables->populateReportTable($data['REP_TAB_NAME'], + $pmTable->getDataSource(), + $data['REP_TAB_TYPE'], + $data['PRO_UID'], + $data['REP_TAB_GRID']); + } catch (Exception $e) { + $result->message = $result->msg = $e->getMessage(); + } + } + + $result->success = true; + $result->message = $result->msg = $buildResult; + } catch (Exception $e) { + $buildResult = ob_get_contents(); + ob_end_clean(); + $result->success = false; + + // if it is a propel exception message + if (preg_match('/(.*)\s\[(.*):\s(.*)\]\s\[(.*):\s(.*)\]/', $e->getMessage(), $match)) { + $result->message = $result->msg = $match[3]; + $result->type = ucfirst($pmTable->getDbConfig()->adapter); + } else { + $result->message = $result->msg = $e->getMessage(); + $result->type = G::loadTranslation('ID_EXCEPTION'); + } + + $result->trace = $e->getTraceAsString(); + } + + return $result; } - return $fields; - } + /** + * delete pm table + * @param string $httpData->rows + */ + public function delete($httpData) + { + $rows = G::json_decode(stripslashes($httpData->rows)); + $errors = ''; + $count = 0; - /** - * save pm table - */ - public function save($httpData, $alterTable = true) - { - require_once 'classes/model/AdditionalTables.php'; - require_once 'classes/model/Fields.php'; + foreach ($rows as $row) { + try { + $at = new AdditionalTables(); + $table = $at->load($row->id); - try { - ob_start(); - $data = (array) $httpData; - $data['PRO_UID'] = trim($data['PRO_UID']); - $data['columns'] = G::json_decode(stripslashes($httpData->columns)); //decofing data columns + if (!isset($table)) { + throw new Exception("Table does not exist... skipped!\n"); + } - $isReportTable = $data['PRO_UID'] != '' ? true : false; - $oAdditionalTables = new AdditionalTables(); - $oFields = new Fields(); - $repTabClassName = $oAdditionalTables->getPHPName($data['REP_TAB_NAME']); - $columns = $data['columns']; - - // Reserved Words - $reservedWords = array( - 'ALTER', 'CLOSE', 'COMMIT', 'CREATE', 'DECLARE', 'DELETE', 'DROP', 'FETCH', 'FUNCTION', 'GRANT', - 'INDEX', 'INSERT', 'OPEN', 'REVOKE', 'ROLLBACK', 'SELECT', 'SYNONYM', 'TABLE', 'UPDATE', 'VIEW', - 'APP_UID', 'ROW', 'PMTABLE' - ); - - // verify if exists. - if ($data['REP_TAB_UID'] == '' || (isset($httpData->forceUid) && $httpData->forceUid)) { //new report table - if ($isReportTable && $alterTable) { //setting default columns - $defaultColumns = $this->_getReportTableDefaultColumns($data['REP_TAB_TYPE']); - $columns = array_merge($defaultColumns, $columns); - } - - /** validations **/ - if (is_array($oAdditionalTables->loadByName($data['REP_TAB_NAME']))) { - throw new Exception(G::loadTranslation('ID_PMTABLE_ALREADY_EXISTS', array($data['REP_TAB_NAME']))); + if ($row->type == 'CLASSIC') { + G::LoadClass('reportTables'); + $rp = new reportTables(); + $rp->deleteReportTable($row->id); + $count++; + } else { + $at->deleteAll($row->id); + $count++; + } + } catch (Exception $e) { + $tableName = isset($table['ADD_TAB_NAME']) ? $table['ADD_TAB_NAME'] : $row->id; + $errors .= $e->getMessage() . "\n"; + continue; + } } - if (in_array(strtoupper($data['REP_TAB_NAME']), $reservedWords) ) { - throw new Exception(G::loadTranslation('ID_PMTABLE_INVALID_NAME', array($data['REP_TAB_NAME']))); - } - } - - //backward compatility - foreach ($columns as $i => $column) { - switch ($column->field_type) { - case 'INT': $columns[$i]->field_type = 'INTEGER'; break; - case 'TEXT': $columns[$i]->field_type = 'LONGVARCHAR'; break; - // propel DATETIME equivalent is TIMESTAMP - case 'DATETIME': $columns[$i]->field_type = 'TIMESTAMP'; break; + if ($errors == '') { + $result->success = true; + $result->message = "$count tables removed Successfully."; + } else { + $result->success = false; + $result->message = "$count tables removed but with errors.\n$errors"; } - // VALIDATIONS - if ($columns[$i]->field_autoincrement) { - if ($columns[$i]->field_type !== 'INTEGER') { - $columns[$i]->field_autoincrement = false; - } + $result->errors = $errors; + + return $result; + } + + /** + * get pm tables data + * @param string $httpData->id + * @param string $httpData->start + * @param string $httpData->limit + */ + public function dataView($httpData) + { + require_once 'classes/model/AdditionalTables.php'; + + G::LoadClass('configuration'); + $co = new Configurations(); + $config = $co->getConfiguration('additionalTablesData', 'pageSize', '', $_SESSION['USER_LOGGED']); + $limit_size = isset($config['pageSize']) ? $config['pageSize'] : 20; + $start = isset($httpData->start) ? $httpData->start : 0; + $limit = isset($httpData->limit) ? $httpData->limit : $limit_size; + + $additionalTables = new AdditionalTables(); + $table = $additionalTables->load($httpData->id, true); + $result = $additionalTables->getAllData($httpData->id, $start, $limit); + + $primaryKeys = $additionalTables->getPrimaryKeys(); + + foreach ($result['rows'] as $i => $row) { + $primaryKeysValues = array(); + foreach ($primaryKeys as $key) { + $primaryKeysValues[] = isset($row[$key['FLD_NAME']]) ? $row[$key['FLD_NAME']] : ''; + } + + $result['rows'][$i]['__index__'] = G::encrypt(implode('-', $primaryKeysValues), 'pmtable'); } - } - G::loadClass('pmTable'); - - $pmTable = new pmTable($data['REP_TAB_NAME']); - $pmTable->setDataSource($data['REP_TAB_CONNECTION']); - $pmTable->setColumns($columns); - $pmTable->setAlterTable($alterTable); - $pmTable->build(); - - $buildResult = ob_get_contents(); - ob_end_clean(); - - // Updating additional table struture information - $addTabData = array( - 'ADD_TAB_UID' => $data['REP_TAB_UID'], - 'ADD_TAB_NAME' => $data['REP_TAB_NAME'], - 'ADD_TAB_CLASS_NAME' => $repTabClassName, - 'ADD_TAB_DESCRIPTION' => $data['REP_TAB_DSC'], - 'ADD_TAB_PLG_UID' => '', - 'DBS_UID' => ($data['REP_TAB_CONNECTION'] ? $data['REP_TAB_CONNECTION'] : 'workflow'), - 'PRO_UID' => $data['PRO_UID'], - 'ADD_TAB_TYPE' => $data['REP_TAB_TYPE'], - 'ADD_TAB_GRID' => $data['REP_TAB_GRID'] - ); - if ($data['REP_TAB_UID'] == '' || (isset($httpData->forceUid) && $httpData->forceUid)) { //new report table - //create record - $addTabUid = $oAdditionalTables->create($addTabData); - } else { //editing report table - //updating record - $addTabUid = $data['REP_TAB_UID']; - $oAdditionalTables->update($addTabData); + return $result; + } + + /** + * create pm tables record + * @param string $httpData->rows + */ + public function dataCreate($httpData, $codification='json') + { + if ($codification == 'base64') { + $rows = unserialize(base64_decode($httpData->rows)); + } else { + $rows = G::json_decode(stripslashes($httpData->rows)); + } - //removing old data fields references - $oCriteria = new Criteria('workflow'); - $oCriteria->add(FieldsPeer::ADD_TAB_UID, $data['REP_TAB_UID']); - FieldsPeer::doDelete($oCriteria); - } - - // Updating pmtable fields - foreach ($columns as $i => $column) { - $field = array( - 'FLD_UID' => $column->uid, - 'FLD_INDEX' => $i, - 'ADD_TAB_UID' => $addTabUid, - 'FLD_NAME' => $column->field_name, - 'FLD_DESCRIPTION' => $column->field_label, - 'FLD_TYPE' => $column->field_type, - 'FLD_SIZE' => $column->field_size=='' ? NULL : $column->field_size, - 'FLD_NULL' => $column->field_null ? 1 : 0, - 'FLD_AUTO_INCREMENT' => $column->field_autoincrement ? 1 : 0, - 'FLD_KEY' => $column->field_key ? 1 : 0, - 'FLD_FOREIGN_KEY' => 0, - 'FLD_FOREIGN_KEY_TABLE' => '', - 'FLD_DYN_NAME' => $column->field_dyn, - 'FLD_DYN_UID' => $column->field_uid, - 'FLD_FILTER' => (isset($column->field_filter) && $column->field_filter) ? 1 : 0 - ); - $oFields->create($field); - } - - if ($isReportTable && $alterTable) { - // the table was create successfully but we're catching problems while populating table try { - $oAdditionalTables->populateReportTable($data['REP_TAB_NAME'], $pmTable->getDataSource(), $data['REP_TAB_TYPE'], $data['PRO_UID'], $data['REP_TAB_GRID']); - } - catch (Exception $e) { - $result->message = $result->msg = $e->getMessage(); - } - } + require_once 'classes/model/AdditionalTables.php'; + $additionalTables = new AdditionalTables(); + $table = $additionalTables->load($httpData->id, true); + $primaryKeys = $additionalTables->getPrimaryKeys(); - $result->success = true; - $result->message = $result->msg = $buildResult; + $this->className = $table['ADD_TAB_CLASS_NAME']; + $this->classPeerName = $this->className . 'Peer'; + $row = (array) $rows; - } - catch (Exception $e) { - $buildResult = ob_get_contents(); - ob_end_clean(); - $result->success = false; - - // if it is a propel exception message - if (preg_match('/(.*)\s\[(.*):\s(.*)\]\s\[(.*):\s(.*)\]/', $e->getMessage(), $match)) { - $result->message = $result->msg = $match[3]; - $result->type = ucfirst($pmTable->getDbConfig()->adapter); - } - else { - $result->message = $result->msg = $e->getMessage(); - $result->type = G::loadTranslation('ID_EXCEPTION'); - } - - $result->trace = $e->getTraceAsString(); - } + $row = array_merge(array_change_key_case($row, CASE_LOWER), array_change_key_case($row, CASE_UPPER)); + $toSave = false; - return $result; - } - - /** - * delete pm table - * @param string $httpData->rows - */ - public function delete($httpData) - { - $rows = G::json_decode(stripslashes($httpData->rows)); - $errors = ''; - $count = 0; + if (!file_exists(PATH_WORKSPACE . 'classes/' . $this->className . '.php')) { + throw new Exception('Create::' . G::loadTranslation('ID_PMTABLE_CLASS_DOESNT_EXIST', $this->className)); + } - foreach ($rows as $row ) { - try { - $at = new AdditionalTables(); - $table = $at->load($row->id); + require_once PATH_WORKSPACE . 'classes/' . $this->className . '.php'; + eval('$obj = new ' . $this->className . '();'); - if (!isset($table)) { - throw new Exception("Table does not exist... skipped!\n"); + if (count($row) > 0) { + eval('$con = Propel::getConnection(' . $this->classPeerName . '::DATABASE_NAME);'); + $obj->fromArray($row, BasePeer::TYPE_FIELDNAME); + + if ($obj->validate()) { + $obj->save(); + $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('Error trying insert into "' . $table['ADD_TAB_NAME'] . "\"\n" . $msg); + } + + $index = G::encrypt(implode('-', $primaryKeysValues), 'pmtable'); + } else { + $toSave = false; + } + + if ($toSave) { + $result->success = true; + $result->message = 'Record saved successfully'; + $result->rows = $obj->toArray(BasePeer::TYPE_FIELDNAME); + $result->rows['__index__'] = $index; + } else { + $result->success = false; + $result->rows = array(); + $result->message = '$$'; + } + } catch (Exception $e) { + $result->success = false; + $result->rows = array(); + $result->message = $e->getMessage(); } - if($row->type == 'CLASSIC') { - G::LoadClass('reportTables'); - $rp = new reportTables(); - $rp->deleteReportTable($row->id); - $count++; - } - else { - $at->deleteAll($row->id); - $count++; - } - } catch(Exception $e) { - $tableName = isset($table['ADD_TAB_NAME']) ? $table['ADD_TAB_NAME'] : $row->id; - $errors .= $e->getMessage() . "\n"; - continue; - } - } - - if ($errors == '') { - $result->success = true; - $result->message = "$count tables removed Successfully."; - } - else { - $result->success = false; - $result->message = "$count tables removed but with errors.\n$errors" ; + return $result; } - $result->errors = $errors; - - return $result; - } - - /** - * get pm tables data - * @param string $httpData->id - * @param string $httpData->start - * @param string $httpData->limit - */ - public function dataView($httpData) - { - require_once 'classes/model/AdditionalTables.php'; - - G::LoadClass('configuration'); - $co = new Configurations(); - $config = $co->getConfiguration('additionalTablesData', 'pageSize','',$_SESSION['USER_LOGGED']); - $limit_size = isset($config['pageSize']) ? $config['pageSize'] : 20; - $start = isset($httpData->start) ? $httpData->start : 0; - $limit = isset($httpData->limit) ? $httpData->limit : $limit_size; - - $additionalTables = new AdditionalTables(); - $table = $additionalTables->load($httpData->id, true); - $result = $additionalTables->getAllData($httpData->id, $start, $limit); - - $primaryKeys = $additionalTables->getPrimaryKeys(); - - foreach ($result['rows'] as $i => $row) { - $primaryKeysValues = array(); - foreach ($primaryKeys as $key) { - $primaryKeysValues[] = isset($row[$key['FLD_NAME']]) ? $row[$key['FLD_NAME']] : ''; - } - - $result['rows'][$i]['__index__'] = G::encrypt(implode('-', $primaryKeysValues), 'pmtable'); - } - - return $result; - } - - /** - * create pm tables record - * @param string $httpData->rows - */ - public function dataCreate($httpData , $codification = 'json') - { - if($codification=='base64') { - $rows = unserialize(base64_decode($httpData->rows)); - } - else { - $rows = G::json_decode(stripslashes($httpData->rows)); - } - - try { - require_once 'classes/model/AdditionalTables.php'; - $additionalTables = new AdditionalTables(); - $table = $additionalTables->load($httpData->id, true); - $primaryKeys = $additionalTables->getPrimaryKeys(); - - $this->className = $table['ADD_TAB_CLASS_NAME']; - $this->classPeerName = $this->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/' . $this->className . '.php') ) { - throw new Exception('Create::' . G::loadTranslation('ID_PMTABLE_CLASS_DOESNT_EXIST', $this->className)); - } - - require_once PATH_WORKSPACE . 'classes/' . $this->className . '.php'; - eval('$obj = new ' .$this->className. '();'); - - if (count($row) > 0) { - eval('$con = Propel::getConnection('.$this->classPeerName.'::DATABASE_NAME);'); - $obj->fromArray($row, BasePeer::TYPE_FIELDNAME); - - if ($obj->validate()) { - $obj->save(); - $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('Error trying insert into "'.$table['ADD_TAB_NAME'] ."\"\n". $msg); - } - - $index = G::encrypt(implode('-', $primaryKeysValues), 'pmtable'); - } - else { - $toSave = false; - } - - if ($toSave) { - $result->success = true; - $result->message = 'Record saved successfully'; - $result->rows = $obj->toArray(BasePeer::TYPE_FIELDNAME); - $result->rows['__index__'] = $index; - } - else { - $result->success = false; - $result->rows = array(); - $result->message = '$$'; - } - } - catch (Exception $e) { - $result->success = false; - $result->rows = array(); - $result->message = $e->getMessage(); - } - - return $result; - } - - /** - * update pm tables record - * @param string $httpData->id - */ - public function dataUpdate($httpData) - { - require_once 'classes/model/AdditionalTables.php'; - $oAdditionalTables = new AdditionalTables(); - $table = $oAdditionalTables->load($httpData->id, true); - $primaryKeys = $oAdditionalTables->getPrimaryKeys('keys'); - $this->className = $table['ADD_TAB_CLASS_NAME']; - $this->classPeerName = $this->className . 'Peer'; - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - - if (!file_exists ($sPath . $this->className . '.php') ) { - throw new Exception('Update:: '.G::loadTranslation('ID_PMTABLE_CLASS_DOESNT_EXIST', $this->className)); - } - - require_once $sPath . $this->className . '.php'; - - $rows = G::json_decode(stripslashes($httpData->rows)); - - if (is_array($rows)) { - foreach($rows as $row) { - $row = (array) $row; - $result = $this->_dataUpdate($row, $primaryKeys); - } - } - else { //then is object - $row = (array) $rows; - $result = $this->_dataUpdate($row, $primaryKeys); - } - - $this->success = $result; - $this->message = $result ? G::loadTranslation('ID_UPDATED_SUCCESSFULLY') : G::loadTranslation('ID_UPDATE_FAILED'); - } - - /** - * remove a pm tables record - * @param string $httpData->id - */ - public function dataDestroy($httpData) - { - require_once 'classes/model/AdditionalTables.php'; - $oAdditionalTables = new AdditionalTables(); - $table = $oAdditionalTables->load($httpData->id, true); - $this->className = $table['ADD_TAB_CLASS_NAME']; - $this->classPeerName = $this->className . 'Peer'; - $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - - if (!file_exists ($sPath . $this->className . '.php') ) { - throw new Exception('Destroy:: ' . G::loadTranslation('ID_PMTABLE_CLASS_DOESNT_EXIST', $this->className)); - } - - require_once $sPath . $this->className . '.php'; - - $this->success = $this->_dataDestroy($httpData->rows); - $this->message = $this->success ? G::loadTranslation('ID_DELETED_SUCCESSFULLY') : G::loadTranslation('ID_DELETE_FAILED'); - } - - /** - * import a CSV to pm tables record - * @param string $httpData->id - */ - public function importCSV($httpData) - { - if (preg_match('/[\x00-\x08\x0b-\x0c\x0e\x1f]/', file_get_contents($_FILES['form']['tmp_name']['CSV_FILE'])) === 0) { - $filename = $_FILES['form']['name']['CSV_FILE']; - if ($oFile = fopen($_FILES['form']['tmp_name']['CSV_FILE'], 'r')) { + /** + * update pm tables record + * @param string $httpData->id + */ + public function dataUpdate($httpData) + { require_once 'classes/model/AdditionalTables.php'; $oAdditionalTables = new AdditionalTables(); - $aAdditionalTables = $oAdditionalTables->load($_POST['form']['ADD_TAB_UID'], true); - $sErrorMessages = ''; - $i = 1; - $swHead = false; - while (($aAux = fgetcsv($oFile, 4096, $_POST['form']['CSV_DELIMITER'])) !== false) { - if(!is_null($aAux[0])) { - if(count($aAdditionalTables['FIELDS']) > count($aAux)){ - $this->success = false; - $this->message = G::LoadTranslation('INVALID_FILE'); - return 0; - } - if($i == 1) { - $j = 0; - foreach ($aAdditionalTables['FIELDS'] as $aField) { - if($aField['FLD_NAME'] === $aAux[$j]) $swHead = true; - $j++; - } - } + $table = $oAdditionalTables->load($httpData->id, true); + $primaryKeys = $oAdditionalTables->getPrimaryKeys('keys'); + $this->className = $table['ADD_TAB_CLASS_NAME']; + $this->classPeerName = $this->className . 'Peer'; + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; - if ($swHead == false) { - $aData = array(); - $j = 0; - foreach ($aAdditionalTables['FIELDS'] as $aField) { - $aData[$aField['FLD_NAME']] = (isset($aAux[$j]) ? $aAux[$j] : ''); - $j++; - } - try { - if (!$oAdditionalTables->saveDataInTable($_POST['form']['ADD_TAB_UID'], $aData)) { - $sErrorMessages .= G::LoadTranslation('ID_DUPLICATE_ENTRY_PRIMARY_KEY') . ', ' . G::LoadTranslation('ID_LINE') . ' ' . $i . '. '; - } - } - catch (Exception $oError) { - $sErrorMessages .= G::LoadTranslation('ID_ERROR_INSERT_LINE') . ': ' . G::LoadTranslation('ID_LINE') . ' ' . $i . '. '; - } - } else { - $swHead = false; - } - $i++; - } - } - fclose($oFile); - } - if ($sErrorMessages != '') { - $this->success = false; - $this->message = $sErrorMessages; - } else { - $this->success = true; - $this->message = G::loadTranslation('ID_FILE_IMPORTED_SUCCESSFULLY', array($filename)); - } - } - else { - $sMessage = G::LoadTranslation('ID_UPLOAD_VALID_CSV_FILE'); - $this->success = false; - $this->message = $sMessage; - - } - } - - /** - * export a pm tables record to CSV - * @param string $httpData->id - */ - public function exportCSV($httpData) - { - - try{ - - $link = ''; - $size = ''; - $META = 'Content'; - $bytesSaved = 0; - - require_once 'classes/model/AdditionalTables.php'; - $oAdditionalTables = new AdditionalTables(); - $aAdditionalTables = $oAdditionalTables->load($_POST['ADD_TAB_UID'], true); - $sErrorMessages = ''; - $sDelimiter = $_POST['CSV_DELIMITER']; - - $resultData = $oAdditionalTables->getAllData($_POST['ADD_TAB_UID'], NULL, NULL, false); - $rows = $resultData['rows']; - $count = $resultData['count']; - - $PUBLIC_ROOT_PATH = PATH_DATA . 'sites' . PATH_SEP . SYS_SYS . PATH_SEP . 'public' . PATH_SEP; - $filenameOnly = strtolower( $aAdditionalTables['ADD_TAB_NAME'] ."_".date("Y-m-d").'_'.date("Hi").".csv"); - $filename = $PUBLIC_ROOT_PATH . $filenameOnly; - $fp = fopen( $filename, "wb"); - - foreach($rows as $keyCol => $cols ){ - $SDATA = ""; - $cnt = count($cols); - foreach($cols as $key => $val){ - $SDATA .= $val; - if(--$cnt > 0 ) $SDATA .= $sDelimiter; - } - $SDATA .= "\n"; - $bytesSaved += fwrite($fp, $SDATA); - } - - - fclose ($fp); - - - // $filenameLink = "pmTables/streamExported?f=$filenameOnly"; - $filenameLink = "streamExported?f=$filenameOnly"; - $size = round(($bytesSaved/1024), 2)." Kb"; - $filename = $filenameOnly; - $link = $filenameLink; - - - $result->success = true; - $result->filename = $filenameOnly; - $result->link = $link; - $result->message = "Generated file: $filenameOnly, size: $size"; - } - catch (Exception $e) { - $result->success = false; - $result->message = $e->getMessage(); - } - - return $result; - - } - - /** - * import a pm table - * @param string $httpData->id - */ - public function import($httpData) - { - require_once 'classes/model/AdditionalTables.php'; - try { - $errors = ''; - - $overWrite = isset($_POST['form']['OVERWRITE'])? true: false; - - //save the file - if ($_FILES['form']['error']['FILENAME'] !== 0) { - throw new Exception(G::loadTranslation('ID_PMTABLE_UPLOADING_FILE_PROBLEM')); - } - - $oAdditionalTables = new AdditionalTables(); - $tableNameMap = array(); - $processQueue = array(); - $processQueueTables = array(); - - $PUBLIC_ROOT_PATH = PATH_DATA.'sites'.PATH_SEP.SYS_SYS.PATH_SEP.'public'.PATH_SEP; - $filename = $_FILES['form']['name']['FILENAME']; - $tempName = $_FILES['form']['tmp_name']['FILENAME']; - G::uploadFile($tempName, $PUBLIC_ROOT_PATH, $filename ); - - $fileContent = file_get_contents($PUBLIC_ROOT_PATH.$filename); - - if(strpos($fileContent, '-----== ProcessMaker Open Source Private Tables ==-----') === false) { - throw new Exception(G::loadTranslation('ID_PMTABLE_INVALID_FILE')); - } - - $fp = fopen($PUBLIC_ROOT_PATH.$filename, "rb"); - $fsData = intval(fread($fp, 9)); //reading the metadata - $sType = fread($fp, $fsData); - - // first create the tables structures - while (!feof($fp)) { - switch($sType) { - case '@META': - $fsData = intval(fread($fp, 9)); - $METADATA = fread($fp, $fsData); - break; - - case '@SCHEMA': - $fsUid = intval(fread($fp, 9)); - $uid = fread($fp, $fsUid); - $fsData = intval(fread($fp, 9)); - $schema = fread($fp, $fsData); - $contentSchema = unserialize($schema); - $additionalTable = new additionalTables(); - $tableExists = $additionalTable->loadByName($contentSchema['ADD_TAB_NAME']); - $tableNameMap[$contentSchema['ADD_TAB_NAME']] = $contentSchema['ADD_TAB_NAME']; - - if ($overWrite) { - if ($tableExists !== false) { - $additionalTable->deleteAll($tableExists['ADD_TAB_UID']); - } - } - else { - if ($tableExists !== false) {// some table exists with the same name - // renaming... - $tNameOld = $contentSchema['ADD_TAB_NAME']; - $newTableName = $contentSchema['ADD_TAB_NAME'] . '_' . date('YmdHis'); - $contentSchema['ADD_TAB_UID'] = G::generateUniqueID(); - $contentSchema['ADD_TAB_NAME'] = $newTableName; - $contentSchema['ADD_TAB_CLASS_NAME'] = additionalTables::getPHPName($newTableName); - //mapping the table name for posterior uses - $tableNameMap[$tNameOld] = $contentSchema['ADD_TAB_NAME']; - } - } - - // validating invalid bds_uid in old tables definition -> mapped to workflow - if (!$contentSchema['DBS_UID'] || $contentSchema['DBS_UID'] == '0' || !$contentSchema['DBS_UID']) { - $contentSchema['DBS_UID'] = 'workflow'; - } - - $columns = array(); - foreach ($contentSchema['FIELDS'] as $field){ - $column = array( - 'uid' => '', - 'field_uid' => '', - 'field_name' => $field['FLD_NAME'], - 'field_dyn' => isset($field['FLD_DYN_NAME']) ? $field['FLD_DYN_NAME'] : '', - 'field_label'=> isset($field['FLD_DESCRIPTION']) ? $field['FLD_DESCRIPTION'] : '', - 'field_type' => $field['FLD_TYPE'], - 'field_size' => $field['FLD_SIZE'], - 'field_key' => isset($field['FLD_KEY']) ? $field['FLD_KEY'] : 0, - 'field_null' => isset($field['FLD_NULL']) ? $field['FLD_NULL'] : 1, - 'field_autoincrement' => isset($field['FLD_AUTO_INCREMENT']) ? $field['FLD_AUTO_INCREMENT'] : 0 - ); - $columns[] = $column; - } - - $tableData = new stdClass(); - $tableData->REP_TAB_UID = $contentSchema['ADD_TAB_UID']; - $tableData->REP_TAB_NAME = $contentSchema['ADD_TAB_NAME']; - $tableData->REP_TAB_DSC = $contentSchema['ADD_TAB_DESCRIPTION']; - $tableData->REP_TAB_CONNECTION= $contentSchema['DBS_UID']; - $tableData->PRO_UID = isset($contentSchema['PRO_UID'])? $contentSchema['PRO_UID'] : ''; - $tableData->REP_TAB_TYPE = isset($contentSchema['ADD_TAB_TYPE'])? $contentSchema['ADD_TAB_TYPE'] : ''; - $tableData->REP_TAB_GRID = isset($contentSchema['ADD_TAB_GRID'])? $contentSchema['ADD_TAB_GRID'] : ''; - $tableData->columns = G::json_encode($columns); - $tableData->forceUid = true; - - //save the table - $alterTable = false; - $result = $this->save($tableData, $alterTable); - - if ($result->success) { - $processQueueTables[$contentSchema['DBS_UID']][] = $contentSchema['ADD_TAB_NAME']; - } - else { - $errors .= 'Error creating table: '.$tableData->REP_TAB_NAME.'-> '.$result->message . "\n\n"; - } - - break; - - case '@DATA': - $fstName = intval(fread($fp, 9)); - $tableName = fread($fp, $fstName); - $fsData = intval(fread($fp, 9)); - if ($fsData > 0) $data = fread($fp, $fsData); - break; + if (!file_exists($sPath . $this->className . '.php')) { + throw new Exception('Update:: ' . G::loadTranslation('ID_PMTABLE_CLASS_DOESNT_EXIST', $this->className)); } - $fsData = intval(fread($fp, 9)); //reading the metadata - if($fsData > 0){ // reading next block type - $sType = fread($fp, $fsData); - } - else { - break; - } + require_once $sPath . $this->className . '.php'; - } + $rows = G::json_decode(stripslashes($httpData->rows)); - fclose($fp); - G::loadClass('pmTable'); - - foreach ($processQueueTables as $dbsUid => $tables) { - $pmTable = new pmTable(); - ob_start(); - $pmTable->buildModelFor($dbsUid, $tables); - $buildResult = ob_get_contents(); - ob_end_clean(); - $errors .= $pmTable->upgradeDatabaseFor($pmTable->getDataSource(), $tables); - } - - $fp = fopen($PUBLIC_ROOT_PATH.$filename, "rb"); - $fsData = intval(fread($fp, 9)); - $sType = fread($fp, $fsData); - // data processing - - while (!feof($fp)) { - - switch($sType) { - case '@META': - $fsData = intval(fread($fp, 9)); - $METADATA = fread($fp, $fsData); - break; - - case '@SCHEMA': - $fsUid = intval(fread($fp, 9)); - $uid = fread($fp, $fsUid); - $fsData = intval(fread($fp, 9)); - $schema = fread($fp, $fsData); - $contentSchema = unserialize($schema); - $additionalTable = new additionalTables(); - $table = $additionalTable->loadByName($tableNameMap[$contentSchema['ADD_TAB_NAME']]); - if ($table['PRO_UID'] != '') { // is a report table, try populate it - $additionalTable->populateReportTable($table['ADD_TAB_NAME'], pmTable::resolveDbSource($table['DBS_UID']), $table['ADD_TAB_TYPE'], $table['PRO_UID'], $table['ADD_TAB_GRID']); + if (is_array($rows)) { + foreach ($rows as $row) { + $row = (array) $row; + $result = $this->_dataUpdate($row, $primaryKeys); } - break; - - case '@DATA': - $fstName = intval(fread($fp, 9)); - $tableName = fread($fp, $fstName); - $fsData = intval(fread($fp, 9)); - - if ($fsData > 0) { - $data = fread($fp, $fsData); - $contentData = unserialize($data); - $tableName = $tableNameMap[$tableName]; - - $oAdditionalTables = new AdditionalTables(); - $table = $oAdditionalTables->loadByName($tableName); - $isReport = $table['PRO_UID'] !== '' ? true : false; - - if ($table !== false) { - if (!$isReport) { - if (count($contentData) > 0) { - foreach ($contentData as $row) { - $data = new StdClass(); - $data->id = $table['ADD_TAB_UID']; - $data->rows = base64_encode(serialize($row)); - $res = $this->dataCreate($data , 'base64'); - if (!$res->success) { - $errors .= $res->message; - } - } - } - } - } - } - break; + } else { + //then is object + $row = (array) $rows; + $result = $this->_dataUpdate($row, $primaryKeys); } - $fsData = intval(fread($fp, 9)); - if($fsData > 0){ - $sType = fread($fp, $fsData); - } - else { - break; - } - } - - //////////// - - - if ($errors == '') { - $result->success = true; - $msg = G::loadTranslation('ID_PMTABLE_IMPORT_SUCCESS', array($filename)); - } - else { - $result->success = false; - $result->errorType = 'warning'; - $msg = G::loadTranslation('ID_PMTABLE_IMPORT_WITH_ERRORS', array($filename)) . "\n\n" . $errors; - } - - $result->message = $msg; - - } - catch(Exception $e) { - $result->errorType = 'error'; - $result->buildResult = ob_get_contents(); - ob_end_clean(); - $result->success = false; - - // if it is a propel exception message - if (preg_match('/(.*)\s\[(.*):\s(.*)\]\s\[(.*):\s(.*)\]/', $e->getMessage(), $match)) { - $result->message = $match[3]; - $result->type = G::loadTranslation('ID_ERROR'); - } - else { - $result->message = $e->getMessage(); - $result->type = G::loadTranslation('ID_EXCEPTION'); - } - //$result->trace = $e->getTraceAsString(); + $this->success = $result; + $this->message = $result + ? G::loadTranslation('ID_UPDATED_SUCCESSFULLY') + : G::loadTranslation('ID_UPDATE_FAILED'); } - return $result; - } - - /** - * Export PM tables - * @author: Erik Amaru Ortiz - */ - public function export($httpData) - { - require_once 'classes/model/AdditionalTables.php'; - $at = new AdditionalTables(); - $tablesToExport = G::json_decode(stripslashes($httpData->rows)); - - try{ - G::LoadCLass('net'); - $net = new NET(G::getIpAddress()); - - G::LoadClass("system"); - - $META = " \n-----== ProcessMaker Open Source Private Tables ==-----\n". - " @Ver: 1.0 Oct-2009\n". - " @Processmaker version: ".System::getVersion()."\n". - " -------------------------------------------------------\n". - " @Export Date: ".date("l jS \of F Y h:i:s A")."\n". - " @Server address: ".getenv('SERVER_NAME')." (".getenv('SERVER_ADDR').")\n". - " @Client address: ".$net->hostname."\n". - " @Workspace: ".SYS_SYS."\n". - " @Export trace back:\n\n"; - - $EXPORT_TRACEBACK = Array(); - $c = 0; - foreach ($tablesToExport as $table) { - $tableRecord = $at->load($table->ADD_TAB_UID); - $tableData = $at->getAllData($table->ADD_TAB_UID, NULL, NULL, false); - $table->ADD_TAB_NAME = $tableRecord['ADD_TAB_NAME']; - $rows = $tableData['rows']; - $count = $tableData['count']; - - array_push($EXPORT_TRACEBACK, Array( - 'uid' => $table->ADD_TAB_UID, - 'name' => $table->ADD_TAB_NAME, - 'num_regs' => $tableData['count'], - 'schema' => $table->_SCHEMA ? 'yes': 'no', - 'data' => $table->_DATA ? 'yes': 'no' - )); - } - - $sTrace = "TABLE UID TABLE NAME\tREGS\tSCHEMA\tDATA\n"; - - foreach($EXPORT_TRACEBACK as $row){ - $sTrace .= "{$row['uid']}\t{$row['name']}\t\t{$row['num_regs']}\t{$row['schema']}\t{$row['data']}\n"; - } - - $META .= $sTrace; - - ///////////////EXPORT PROCESS - $PUBLIC_ROOT_PATH = PATH_DATA . 'sites' . PATH_SEP . SYS_SYS . PATH_SEP . 'public' . PATH_SEP; - - $filenameOnly = strtolower('SYS-'.SYS_SYS."_".date("Y-m-d").'_'.date("Hi").".pmt"); - - $filename = $PUBLIC_ROOT_PATH . $filenameOnly; - $fp = fopen( $filename, "wb"); - - $bytesSaved = 0; - $bufferType = '@META'; - $fsData = sprintf("%09d", strlen($META)); - $fsbufferType = sprintf("%09d", strlen($bufferType)); - $bytesSaved += fwrite($fp, $fsbufferType); //writing the size of $oData - $bytesSaved += fwrite($fp, $bufferType); //writing the $oData - $bytesSaved += fwrite($fp, $fsData); //writing the size of $oData - $bytesSaved += fwrite($fp, $META); //writing the $oData - - foreach($tablesToExport as $table){ - - if ($table->_SCHEMA) { - $oAdditionalTables = new AdditionalTables(); - $aData = $oAdditionalTables->load($table->ADD_TAB_UID, true); - - $bufferType = '@SCHEMA'; - $SDATA = serialize($aData); - $fsUid = sprintf("%09d", strlen($table->ADD_TAB_UID)); - $fsData = sprintf("%09d", strlen ($SDATA)); - $fsbufferType = sprintf("%09d", strlen($bufferType)); - - $bytesSaved += fwrite($fp, $fsbufferType); //writing the size of $oData - $bytesSaved += fwrite($fp, $bufferType); //writing the $oData - $bytesSaved += fwrite($fp, $fsUid ); //writing the size of xml file - $bytesSaved += fwrite($fp, $table->ADD_TAB_UID); //writing the xmlfile - $bytesSaved += fwrite($fp, $fsData); //writing the size of xml file - $bytesSaved += fwrite($fp, $SDATA); //writing the xmlfile - } - - if ($table->_DATA) { - //export data - $oAdditionalTables = new additionalTables(); - $tableData = $oAdditionalTables->getAllData($table->ADD_TAB_UID, NULL, NULL, false); - - $SDATA = serialize($tableData['rows']); - $bufferType = '@DATA'; - - $fsbufferType = sprintf("%09d", strlen($bufferType)); - $fsTableName = sprintf("%09d", strlen($table->ADD_TAB_NAME)); - $fsData = sprintf("%09d", strlen ($SDATA)); - - $bytesSaved += fwrite($fp, $fsbufferType); //writing type size - $bytesSaved += fwrite($fp, $bufferType); //writing type - $bytesSaved += fwrite($fp, $fsTableName ); //writing the size of xml file - $bytesSaved += fwrite($fp, $table->ADD_TAB_NAME); //writing the xmlfile - $bytesSaved += fwrite($fp, $fsData); //writing the size of xml file - $bytesSaved += fwrite($fp, $SDATA); //writing the xmlfile - } - } - - fclose ($fp); - - $filenameLink = "pmTables/streamExported?f=$filenameOnly"; - $size = round(($bytesSaved/1024), 2)." Kb"; - $meta = "
".$META."
"; - $filename = $filenameOnly; - $link = $filenameLink; - - $result->success = true; - $result->filename = $filenameOnly; - $result->link = $link; - $result->message = "Generated file: $filenameOnly, size: $size"; - } - catch (Exception $e) { - $result->success = false; - $result->message = $e->getMessage(); - } - - return $result; - } - - public function exportList() - { - require_once 'classes/model/AdditionalTables.php'; - - $oCriteria = new Criteria('workflow'); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_UID); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_NAME); - $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_DESCRIPTION); - $oCriteria->addSelectColumn("'".G::LoadTranslation('ID_ACTION_EXPORT')."' as 'CH_SCHEMA'"); - $oCriteria->addSelectColumn("'".G::LoadTranslation('ID_ACTION_EXPORT')."' as 'CH_DATA'"); - - $uids = explode(',',$_GET['id']); - - foreach ($uids as $UID){ - if (!isset($CC)){ - $CC = $oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_UID, $UID ,Criteria::EQUAL); - }else{ - $CC->addOr($oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_UID, $UID ,Criteria::EQUAL)); - } - } - $oCriteria->add($CC); - $oCriteria->addAnd($oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_UID, '', Criteria::NOT_EQUAL)); - - $oDataset = AdditionalTablesPeer::doSelectRS ( $oCriteria ); - $oDataset->setFetchmode ( ResultSet::FETCHMODE_ASSOC ); - - $addTables = Array(); - while( $oDataset->next() ) { - $addTables[] = $oDataset->getRow(); - } - - return $addTables; - } - - public function updateTag($httpData) - { - require_once 'classes/model/AdditionalTables.php'; - $oAdditionalTables = new AdditionalTables(); - $uid = $_REQUEST['ADD_TAB_UID']; - $value = $_REQUEST['value']; - - $repTabData = array( - 'ADD_TAB_UID' => $uid, - 'ADD_TAB_TAG' => $value - ); - $oAdditionalTables->update($repTabData); - } - - /** - * - protected functions (non callable from controller outside) - - */ - - /** - * Update data from a addTable record - * @param $row - */ - function _dataUpdate($row, $primaryKeys) - { - $keys = G::decrypt($row['__index__'], 'pmtable'); - $keys = explode('-', $keys); - unset($row['__index__']); - - $params = array(); - - foreach ($keys as $key) { - $params[] = is_numeric($key) ? $key : "'$key'"; - } - - $obj = null; - eval('$obj = '.$this->classPeerName.'::retrieveByPk('.implode(',', $params).');'); - - if (is_object($obj)) { - foreach ($row as $key => $value) { - // validation, don't modify primary keys - if (in_array($key, $primaryKeys)) { - throw new Exception(G::loadTranslation('ID_DONT_MODIFY_PK_VALUE', array($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 { - $result = false; - } - - return $result; - } - - /** - * Update data from a addTable record - * @param $row - */ - function _dataDestroy($row) - { - $row = G::decrypt($row, 'pmtable'); - $row = str_replace('"', '', $row); - $keys = explode('-', $row); - $params = array(); - foreach ($keys as $key) { - $params[] = is_numeric($key) ? $key : "'$key'"; - } - - $obj = null; - eval('$obj = '.$this->classPeerName.'::retrieveByPk('.implode(',', $params).');'); - - if (is_object($obj)) { - $obj->delete(); - return true; - } else { - return false; - } - } - - public function genDataReport($httpData) - { - G::loadClass('pmTable'); - require_once 'classes/model/AdditionalTables.php'; - $result->message = ''; - $result->success = true; - - $additionalTables = new AdditionalTables(); - $table = $additionalTables->load($httpData->id); - if ($table['PRO_UID'] != '') { - $additionalTables->populateReportTable( - $table['ADD_TAB_NAME'], - pmTable::resolveDbSource($table['DBS_UID']), - $table['ADD_TAB_TYPE'], - $table['PRO_UID'], - $table['ADD_TAB_GRID'] - ); - $result->message = 'generated for table '.$table['ADD_TAB_NAME']; - } - - return $result; - } - - /** - * Get report table default columns - * @param $type - */ - protected function _getReportTableDefaultColumns($type='NORMAL') - { - $defaultColumns = array(); - $application = new stdClass(); //APPLICATION KEY - $application->uid = ''; - $application->field_dyn = ''; - $application->field_uid = ''; - $application->field_name = 'APP_UID'; - $application->field_label = 'APP_UID'; - $application->field_type = 'VARCHAR'; - $application->field_size = 32; - $application->field_dyn = ''; - $application->field_key = 1; - $application->field_null = 0; - $application->field_filter = false; - $application->field_autoincrement = false; - array_push($defaultColumns, $application); - - $application = new stdClass(); //APP_NUMBER - $application->uid = ''; - $application->field_dyn = ''; - $application->field_uid = ''; - $application->field_name = 'APP_NUMBER'; - $application->field_label = 'APP_NUMBER'; - $application->field_type = 'INTEGER'; - $application->field_size = 11; - $application->field_dyn = ''; - $application->field_key = 0; - $application->field_null = 0; - $application->field_filter = false; - $application->field_autoincrement = false; - array_push($defaultColumns, $application); - - //if it is a grid report table - if ($type == 'GRID') { //GRID INDEX - $gridIndex = new stdClass(); - $gridIndex->uid = ''; - $gridIndex->field_dyn = ''; - $gridIndex->field_uid = ''; - $gridIndex->field_name = 'ROW'; - $gridIndex->field_label = 'ROW'; - $gridIndex->field_type = 'INTEGER'; - $gridIndex->field_size = '11'; - $gridIndex->field_dyn = ''; - $gridIndex->field_key = 1; - $gridIndex->field_null = 0; - $gridIndex->field_filter = false; - $gridIndex->field_autoincrement = false; - array_push($defaultColumns, $gridIndex); - } - - return $defaultColumns; - } - - /** - * Get all dynaform fields from a process (without grid fields) - * @param $proUid - * @param $type [values:xmlform/grid] - */ - function _getDynafields2($proUid, $type = 'xmlform') - { - require_once 'classes/model/Dynaform.php'; - $fields = array(); - $fieldsNames = array(); - - $oCriteria = new Criteria('workflow'); - $oCriteria->addSelectColumn(DynaformPeer::DYN_FILENAME); - $oCriteria->add(DynaformPeer::PRO_UID, $proUid); - $oCriteria->add(DynaformPeer::DYN_TYPE, $type); - $oDataset = DynaformPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - - $excludeFieldsList = array('title', 'subtitle', 'link', 'file', 'button', 'reset', 'submit', - 'listbox', 'checkgroup', 'grid', 'javascript'); - - $labelFieldsTypeList = array('dropdown', 'checkbox', 'radiogroup', 'yesno'); - - while ($aRow = $oDataset->getRow()) { - if (file_exists(PATH_DYNAFORM . PATH_SEP . $aRow['DYN_FILENAME'] . '.xml')) { - $G_FORM = new Form($aRow['DYN_FILENAME'], PATH_DYNAFORM, SYS_LANG); - - if ($G_FORM->type == 'xmlform' || $G_FORM->type == '') { - foreach($G_FORM->fields as $fieldName => $fieldNode) { - if (!in_array($fieldNode->type, $excludeFieldsList) && !in_array($fieldName, $fieldsNames)) { - $fields[] = array('name' => $fieldName, 'type' => $fieldNode->type, 'label'=> $fieldNode->label); - $fieldsNames[] = $fieldName; - - if (in_array($fieldNode->type, $labelFieldsTypeList) && !in_array($fieldName.'_label', $fieldsNames)) { - $fields[] = array('name' => $fieldName . '_label', 'type' => $fieldNode->type, 'label'=>$fieldNode->label . '_label'); - $fieldsNames[] = $fieldName; - } - } - } - } - } - $oDataset->next(); - } - - return $fields; - } - - function _getDynafields($proUid, $type = 'xmlform', $start=null, $limit=null, $filter=null) - { - $cache = 1; - if (!isset($_SESSION['_cache_pmtables']) || - (isset($_SESSION['_cache_pmtables']) && $_SESSION['_cache_pmtables']['pro_uid'] != $proUid) || - (isset($_SESSION['_cache_pmtables']) && $_SESSION['_cache_pmtables']['dyn_uid'] != $this->dynUid) - ) { - - require_once 'classes/model/Dynaform.php'; - $cache = 0; - $fields = array(); - $fieldsNames = array(); - - $oCriteria = new Criteria('workflow'); - $oCriteria->addSelectColumn(DynaformPeer::DYN_FILENAME); - $oCriteria->add(DynaformPeer::PRO_UID, $proUid); - $oCriteria->add(DynaformPeer::DYN_TYPE, $type); - - - if (isset($this->dynUid)) { - $oCriteria->add(DynaformPeer::DYN_UID, $this->dynUid); - } - - $oDataset = DynaformPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - - $excludeFieldsList = array('title', 'subtitle', 'link', 'file', 'button', 'reset', 'submit', - 'listbox', 'checkgroup', 'grid', 'javascript'); - - $labelFieldsTypeList = array('dropdown', 'radiogroup'); - G::loadSystem('dynaformhandler'); - $index = 0; - - while ($aRow = $oDataset->getRow()) { - if (file_exists(PATH_DYNAFORM . PATH_SEP . $aRow['DYN_FILENAME'] . '.xml')) { - $dynaformHandler = new dynaformHandler(PATH_DYNAFORM . $aRow['DYN_FILENAME']. '.xml'); - $nodeFieldsList = $dynaformHandler->getFields(); - - foreach($nodeFieldsList as $node) { - $arrayNode = $dynaformHandler->getArray($node); - $fieldName = $arrayNode['__nodeName__']; - $fieldType = $arrayNode['type']; - - if (!in_array($fieldType, $excludeFieldsList) && !in_array($fieldName, $fieldsNames)) { - $fields[] = array( - 'FIELD_UID' => $fieldName . '-' . $fieldType, - 'FIELD_NAME' => $fieldName, - '_index' => $index++, - '_isset' => true - ); - $fieldsNames[] = $fieldName; - - if (in_array($fieldType, $labelFieldsTypeList) && !in_array($fieldName . '_label', $fieldsNames)) { - $fields[] = array( - 'FIELD_UID' => $fieldName . '_label' . '-' . $fieldType, - 'FIELD_NAME' => $fieldName . '_label', - '_index' => $index++, - '_isset' => true - ); - $fieldsNames[] = $fieldName; - } - } - } - - } - $oDataset->next(); - } - - sort($fields); - - // if is a editing - $fieldsEdit = array(); - if (isset($_SESSION['ADD_TAB_UID'])) { + /** + * remove a pm tables record + * @param string $httpData->id + */ + public function dataDestroy($httpData) + { require_once 'classes/model/AdditionalTables.php'; - + $oAdditionalTables = new AdditionalTables(); + $table = $oAdditionalTables->load($httpData->id, true); + $this->className = $table['ADD_TAB_CLASS_NAME']; + $this->classPeerName = $this->className . 'Peer'; + $sPath = PATH_DB . SYS_SYS . PATH_SEP . 'classes' . PATH_SEP; + + if (!file_exists($sPath . $this->className . '.php')) { + throw new Exception('Destroy:: ' . G::loadTranslation('ID_PMTABLE_CLASS_DOESNT_EXIST', $this->className)); + } + + require_once $sPath . $this->className . '.php'; + + $this->success = $this->_dataDestroy($httpData->rows); + $this->message = $this->success + ? G::loadTranslation('ID_DELETED_SUCCESSFULLY') + : G::loadTranslation('ID_DELETE_FAILED'); + } + + /** + * import a CSV to pm tables record + * @param string $httpData->id + */ + public function importCSV($httpData) + { + if (preg_match('/[\x00-\x08\x0b-\x0c\x0e\x1f]/', + file_get_contents($_FILES['form']['tmp_name']['CSV_FILE'])) === 0) { + $filename = $_FILES['form']['name']['CSV_FILE']; + if ($oFile = fopen($_FILES['form']['tmp_name']['CSV_FILE'], 'r')) { + require_once 'classes/model/AdditionalTables.php'; + $oAdditionalTables = new AdditionalTables(); + $aAdditionalTables = $oAdditionalTables->load($_POST['form']['ADD_TAB_UID'], true); + $sErrorMessages = ''; + $i = 1; + $swHead = false; + while (($aAux = fgetcsv($oFile, 4096, $_POST['form']['CSV_DELIMITER'])) !== false) { + if (!is_null($aAux[0])) { + if (count($aAdditionalTables['FIELDS']) > count($aAux)) { + $this->success = false; + $this->message = G::LoadTranslation('INVALID_FILE'); + return 0; + } + if ($i == 1) { + $j = 0; + foreach ($aAdditionalTables['FIELDS'] as $aField) { + if ($aField['FLD_NAME'] === $aAux[$j]) { + $swHead = true; + } + $j++; + } + } + + if ($swHead == false) { + $aData = array(); + $j = 0; + foreach ($aAdditionalTables['FIELDS'] as $aField) { + $aData[$aField['FLD_NAME']] = (isset($aAux[$j]) ? $aAux[$j] : ''); + $j++; + } + try { + if (!$oAdditionalTables->saveDataInTable($_POST['form']['ADD_TAB_UID'], $aData)) { + $sErrorMessages .= G::LoadTranslation('ID_DUPLICATE_ENTRY_PRIMARY_KEY') + . ', ' . G::LoadTranslation('ID_LINE') . ' ' . $i . '. '; + } + } catch (Exception $oError) { + $sErrorMessages .= G::LoadTranslation('ID_ERROR_INSERT_LINE') + . ': ' . G::LoadTranslation('ID_LINE') . ' ' . $i . '. '; + } + } else { + $swHead = false; + } + $i++; + } + } + fclose($oFile); + } + if ($sErrorMessages != '') { + $this->success = false; + $this->message = $sErrorMessages; + } else { + $this->success = true; + $this->message = G::loadTranslation('ID_FILE_IMPORTED_SUCCESSFULLY', array($filename)); + } + } else { + $sMessage = G::LoadTranslation('ID_UPLOAD_VALID_CSV_FILE'); + $this->success = false; + $this->message = $sMessage; + } + } + + /** + * export a pm tables record to CSV + * @param string $httpData->id + */ + public function exportCSV($httpData) + { + + try { + + $link = ''; + $size = ''; + $META = 'Content'; + $bytesSaved = 0; + + require_once 'classes/model/AdditionalTables.php'; + $oAdditionalTables = new AdditionalTables(); + $aAdditionalTables = $oAdditionalTables->load($_POST['ADD_TAB_UID'], true); + $sErrorMessages = ''; + $sDelimiter = $_POST['CSV_DELIMITER']; + + $resultData = $oAdditionalTables->getAllData($_POST['ADD_TAB_UID'], null, null, false); + $rows = $resultData['rows']; + $count = $resultData['count']; + + $PUBLIC_ROOT_PATH = PATH_DATA . 'sites' . PATH_SEP . SYS_SYS . PATH_SEP . 'public' . PATH_SEP; + $filenameOnly = strtolower($aAdditionalTables['ADD_TAB_NAME'] . "_" . date("Y-m-d") + . '_' . date("Hi") . ".csv"); + $filename = $PUBLIC_ROOT_PATH . $filenameOnly; + $fp = fopen($filename, "wb"); + + foreach ($rows as $keyCol => $cols) { + $SDATA = ""; + $cnt = count($cols); + foreach ($cols as $key => $val) { + $SDATA .= $val; + if (--$cnt > 0) { + $SDATA .= $sDelimiter; + } + } + $SDATA .= "\n"; + $bytesSaved += fwrite($fp, $SDATA); + } + + + fclose($fp); + + + // $filenameLink = "pmTables/streamExported?f=$filenameOnly"; + $filenameLink = "streamExported?f=$filenameOnly"; + $size = round(($bytesSaved / 1024), 2) . " Kb"; + $filename = $filenameOnly; + $link = $filenameLink; + + + $result->success = true; + $result->filename = $filenameOnly; + $result->link = $link; + $result->message = "Generated file: $filenameOnly, size: $size"; + } catch (Exception $e) { + $result->success = false; + $result->message = $e->getMessage(); + } + + return $result; + } + + /** + * import a pm table + * @param string $httpData->id + */ + public function import($httpData) + { + require_once 'classes/model/AdditionalTables.php'; + try { + $errors = ''; + + $overWrite = isset($_POST['form']['OVERWRITE']) ? true : false; + + //save the file + if ($_FILES['form']['error']['FILENAME'] !== 0) { + throw new Exception(G::loadTranslation('ID_PMTABLE_UPLOADING_FILE_PROBLEM')); + } + + $oAdditionalTables = new AdditionalTables(); + $tableNameMap = array(); + $processQueue = array(); + $processQueueTables = array(); + + $PUBLIC_ROOT_PATH = PATH_DATA . 'sites' . PATH_SEP . SYS_SYS . PATH_SEP . 'public' . PATH_SEP; + $filename = $_FILES['form']['name']['FILENAME']; + $tempName = $_FILES['form']['tmp_name']['FILENAME']; + G::uploadFile($tempName, $PUBLIC_ROOT_PATH, $filename); + + $fileContent = file_get_contents($PUBLIC_ROOT_PATH . $filename); + + if (strpos($fileContent, '-----== ProcessMaker Open Source Private Tables ==-----') === false) { + throw new Exception(G::loadTranslation('ID_PMTABLE_INVALID_FILE')); + } + + $fp = fopen($PUBLIC_ROOT_PATH . $filename, "rb"); + $fsData = intval(fread($fp, 9)); //reading the metadata + $sType = fread($fp, $fsData); + + // first create the tables structures + while (!feof($fp)) { + switch ($sType) { + case '@META': + $fsData = intval(fread($fp, 9)); + $METADATA = fread($fp, $fsData); + break; + case '@SCHEMA': + $fsUid = intval(fread($fp, 9)); + $uid = fread($fp, $fsUid); + $fsData = intval(fread($fp, 9)); + $schema = fread($fp, $fsData); + $contentSchema = unserialize($schema); + $additionalTable = new additionalTables(); + $tableExists = $additionalTable->loadByName($contentSchema['ADD_TAB_NAME']); + $tableNameMap[$contentSchema['ADD_TAB_NAME']] = $contentSchema['ADD_TAB_NAME']; + + if ($overWrite) { + if ($tableExists !== false) { + $additionalTable->deleteAll($tableExists['ADD_TAB_UID']); + } + } else { + if ($tableExists !== false) { + // some table exists with the same name + // renaming... + $tNameOld = $contentSchema['ADD_TAB_NAME']; + $newTableName = $contentSchema['ADD_TAB_NAME'] . '_' . date('YmdHis'); + $contentSchema['ADD_TAB_UID'] = G::generateUniqueID(); + $contentSchema['ADD_TAB_NAME'] = $newTableName; + $contentSchema['ADD_TAB_CLASS_NAME'] = additionalTables::getPHPName($newTableName); + //mapping the table name for posterior uses + $tableNameMap[$tNameOld] = $contentSchema['ADD_TAB_NAME']; + } + } + + // validating invalid bds_uid in old tables definition -> mapped to workflow + if (!$contentSchema['DBS_UID'] + || $contentSchema['DBS_UID'] == '0' || !$contentSchema['DBS_UID']) { + $contentSchema['DBS_UID'] = 'workflow'; + } + + $columns = array(); + foreach ($contentSchema['FIELDS'] as $field) { + $column = array( + 'uid' => '', + 'field_uid' => '', + 'field_name' => $field['FLD_NAME'], + 'field_dyn' => isset($field['FLD_DYN_NAME']) ? $field['FLD_DYN_NAME'] : '', + 'field_label' => isset($field['FLD_DESCRIPTION']) ? $field['FLD_DESCRIPTION'] : '', + 'field_type' => $field['FLD_TYPE'], + 'field_size' => $field['FLD_SIZE'], + 'field_key' => isset($field['FLD_KEY']) ? $field['FLD_KEY'] : 0, + 'field_null' => isset($field['FLD_NULL']) ? $field['FLD_NULL'] : 1, + 'field_autoincrement' => isset($field['FLD_AUTO_INCREMENT']) + ? $field['FLD_AUTO_INCREMENT'] : 0 + ); + $columns[] = $column; + } + + $tableData = new stdClass(); + $tableData->REP_TAB_UID = $contentSchema['ADD_TAB_UID']; + $tableData->REP_TAB_NAME = $contentSchema['ADD_TAB_NAME']; + $tableData->REP_TAB_DSC = $contentSchema['ADD_TAB_DESCRIPTION']; + $tableData->REP_TAB_CONNECTION = $contentSchema['DBS_UID']; + $tableData->PRO_UID = isset($contentSchema['PRO_UID']) ? $contentSchema['PRO_UID'] : ''; + $tableData->REP_TAB_TYPE = isset($contentSchema['ADD_TAB_TYPE']) + ? $contentSchema['ADD_TAB_TYPE'] : ''; + $tableData->REP_TAB_GRID = isset($contentSchema['ADD_TAB_GRID']) + ? $contentSchema['ADD_TAB_GRID'] : ''; + $tableData->columns = G::json_encode($columns); + $tableData->forceUid = true; + + //save the table + $alterTable = false; + $result = $this->save($tableData, $alterTable); + + if ($result->success) { + $processQueueTables[$contentSchema['DBS_UID']][] = $contentSchema['ADD_TAB_NAME']; + } else { + $errors .= 'Error creating table: ' . $tableData->REP_TAB_NAME + . '-> ' . $result->message . "\n\n"; + } + + break; + case '@DATA': + $fstName = intval(fread($fp, 9)); + $tableName = fread($fp, $fstName); + $fsData = intval(fread($fp, 9)); + if ($fsData > 0) { + $data = fread($fp, $fsData); + } + break; + } + + $fsData = intval(fread($fp, 9)); //reading the metadata + if ($fsData > 0) { + // reading next block type + $sType = fread($fp, $fsData); + } else { + break; + } + } + + fclose($fp); + G::loadClass('pmTable'); + + foreach ($processQueueTables as $dbsUid => $tables) { + $pmTable = new pmTable(); + ob_start(); + $pmTable->buildModelFor($dbsUid, $tables); + $buildResult = ob_get_contents(); + ob_end_clean(); + $errors .= $pmTable->upgradeDatabaseFor($pmTable->getDataSource(), $tables); + } + + $fp = fopen($PUBLIC_ROOT_PATH . $filename, "rb"); + $fsData = intval(fread($fp, 9)); + $sType = fread($fp, $fsData); + // data processing + + while (!feof($fp)) { + + switch ($sType) { + case '@META': + $fsData = intval(fread($fp, 9)); + $METADATA = fread($fp, $fsData); + break; + case '@SCHEMA': + $fsUid = intval(fread($fp, 9)); + $uid = fread($fp, $fsUid); + $fsData = intval(fread($fp, 9)); + $schema = fread($fp, $fsData); + $contentSchema = unserialize($schema); + $additionalTable = new additionalTables(); + $table = $additionalTable->loadByName($tableNameMap[$contentSchema['ADD_TAB_NAME']]); + if ($table['PRO_UID'] != '') { + // is a report table, try populate it + $additionalTable->populateReportTable($table['ADD_TAB_NAME'], + pmTable::resolveDbSource($table['DBS_UID']), + $table['ADD_TAB_TYPE'], + $table['PRO_UID'], + $table['ADD_TAB_GRID']); + } + break; + case '@DATA': + $fstName = intval(fread($fp, 9)); + $tableName = fread($fp, $fstName); + $fsData = intval(fread($fp, 9)); + + if ($fsData > 0) { + $data = fread($fp, $fsData); + $contentData = unserialize($data); + $tableName = $tableNameMap[$tableName]; + + $oAdditionalTables = new AdditionalTables(); + $table = $oAdditionalTables->loadByName($tableName); + $isReport = $table['PRO_UID'] !== '' ? true : false; + + if ($table !== false) { + if (!$isReport) { + if (count($contentData) > 0) { + foreach ($contentData as $row) { + $data = new StdClass(); + $data->id = $table['ADD_TAB_UID']; + $data->rows = base64_encode(serialize($row)); + $res = $this->dataCreate($data, 'base64'); + if (!$res->success) { + $errors .= $res->message; + } + } + } + } + } + } + break; + } + + $fsData = intval(fread($fp, 9)); + if ($fsData > 0) { + $sType = fread($fp, $fsData); + } else { + break; + } + } + + //////////// + + + if ($errors == '') { + $result->success = true; + $msg = G::loadTranslation('ID_PMTABLE_IMPORT_SUCCESS', array($filename)); + } else { + $result->success = false; + $result->errorType = 'warning'; + $msg = G::loadTranslation('ID_PMTABLE_IMPORT_WITH_ERRORS', array($filename)) . "\n\n" . $errors; + } + + $result->message = $msg; + } catch (Exception $e) { + $result->errorType = 'error'; + $result->buildResult = ob_get_contents(); + ob_end_clean(); + $result->success = false; + + // if it is a propel exception message + if (preg_match('/(.*)\s\[(.*):\s(.*)\]\s\[(.*):\s(.*)\]/', $e->getMessage(), $match)) { + $result->message = $match[3]; + $result->type = G::loadTranslation('ID_ERROR'); + } else { + $result->message = $e->getMessage(); + $result->type = G::loadTranslation('ID_EXCEPTION'); + } + //$result->trace = $e->getTraceAsString(); + } + + return $result; + } + + /** + * Export PM tables + * @author: Erik Amaru Ortiz + */ + public function export($httpData) + { + require_once 'classes/model/AdditionalTables.php'; + $at = new AdditionalTables(); + $tablesToExport = G::json_decode(stripslashes($httpData->rows)); + + try { + G::LoadCLass('net'); + $net = new NET(G::getIpAddress()); + + G::LoadClass("system"); + + $META = " \n-----== ProcessMaker Open Source Private Tables ==-----\n" . + " @Ver: 1.0 Oct-2009\n" . + " @Processmaker version: " . System::getVersion() . "\n" . + " -------------------------------------------------------\n" . + " @Export Date: " . date("l jS \of F Y h:i:s A") . "\n" . + " @Server address: " . getenv('SERVER_NAME') . " (" . getenv('SERVER_ADDR') . ")\n" . + " @Client address: " . $net->hostname . "\n" . + " @Workspace: " . SYS_SYS . "\n" . + " @Export trace back:\n\n"; + + $EXPORT_TRACEBACK = Array(); + $c = 0; + foreach ($tablesToExport as $table) { + $tableRecord = $at->load($table->ADD_TAB_UID); + $tableData = $at->getAllData($table->ADD_TAB_UID, null, null, false); + $table->ADD_TAB_NAME = $tableRecord['ADD_TAB_NAME']; + $rows = $tableData['rows']; + $count = $tableData['count']; + + array_push($EXPORT_TRACEBACK, Array( + 'uid' => $table->ADD_TAB_UID, + 'name' => $table->ADD_TAB_NAME, + 'num_regs' => $tableData['count'], + 'schema' => $table->_SCHEMA ? 'yes' : 'no', + 'data' => $table->_DATA ? 'yes' : 'no' + )); + } + + $sTrace = "TABLE UID TABLE NAME\tREGS\tSCHEMA\tDATA\n"; + + foreach ($EXPORT_TRACEBACK as $row) { + $sTrace .= "{$row['uid']}\t{$row['name']}\t\t{$row['num_regs']}\t{$row['schema']}\t{$row['data']}\n"; + } + + $META .= $sTrace; + + ///////////////EXPORT PROCESS + $PUBLIC_ROOT_PATH = PATH_DATA . 'sites' . PATH_SEP . SYS_SYS . PATH_SEP . 'public' . PATH_SEP; + + $filenameOnly = strtolower('SYS-' . SYS_SYS . "_" . date("Y-m-d") . '_' . date("Hi") . ".pmt"); + + $filename = $PUBLIC_ROOT_PATH . $filenameOnly; + $fp = fopen($filename, "wb"); + + $bytesSaved = 0; + $bufferType = '@META'; + $fsData = sprintf("%09d", strlen($META)); + $fsbufferType = sprintf("%09d", strlen($bufferType)); + $bytesSaved += fwrite($fp, $fsbufferType); //writing the size of $oData + $bytesSaved += fwrite($fp, $bufferType); //writing the $oData + $bytesSaved += fwrite($fp, $fsData); //writing the size of $oData + $bytesSaved += fwrite($fp, $META); //writing the $oData + + foreach ($tablesToExport as $table) { + + if ($table->_SCHEMA) { + $oAdditionalTables = new AdditionalTables(); + $aData = $oAdditionalTables->load($table->ADD_TAB_UID, true); + + $bufferType = '@SCHEMA'; + $SDATA = serialize($aData); + $fsUid = sprintf("%09d", strlen($table->ADD_TAB_UID)); + $fsData = sprintf("%09d", strlen($SDATA)); + $fsbufferType = sprintf("%09d", strlen($bufferType)); + + $bytesSaved += fwrite($fp, $fsbufferType); //writing the size of $oData + $bytesSaved += fwrite($fp, $bufferType); //writing the $oData + $bytesSaved += fwrite($fp, $fsUid); //writing the size of xml file + $bytesSaved += fwrite($fp, $table->ADD_TAB_UID); //writing the xmlfile + $bytesSaved += fwrite($fp, $fsData); //writing the size of xml file + $bytesSaved += fwrite($fp, $SDATA); //writing the xmlfile + } + + if ($table->_DATA) { + //export data + $oAdditionalTables = new additionalTables(); + $tableData = $oAdditionalTables->getAllData($table->ADD_TAB_UID, null, null, false); + + $SDATA = serialize($tableData['rows']); + $bufferType = '@DATA'; + + $fsbufferType = sprintf("%09d", strlen($bufferType)); + $fsTableName = sprintf("%09d", strlen($table->ADD_TAB_NAME)); + $fsData = sprintf("%09d", strlen($SDATA)); + + $bytesSaved += fwrite($fp, $fsbufferType); //writing type size + $bytesSaved += fwrite($fp, $bufferType); //writing type + $bytesSaved += fwrite($fp, $fsTableName); //writing the size of xml file + $bytesSaved += fwrite($fp, $table->ADD_TAB_NAME); //writing the xmlfile + $bytesSaved += fwrite($fp, $fsData); //writing the size of xml file + $bytesSaved += fwrite($fp, $SDATA); //writing the xmlfile + } + } + + fclose($fp); + + $filenameLink = "pmTables/streamExported?f=$filenameOnly"; + $size = round(($bytesSaved / 1024), 2) . " Kb"; + $meta = "
" . $META . "
"; + $filename = $filenameOnly; + $link = $filenameLink; + + $result->success = true; + $result->filename = $filenameOnly; + $result->link = $link; + $result->message = "Generated file: $filenameOnly, size: $size"; + } catch (Exception $e) { + $result->success = false; + $result->message = $e->getMessage(); + } + + return $result; + } + + public function exportList() + { + require_once 'classes/model/AdditionalTables.php'; + + $oCriteria = new Criteria('workflow'); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_UID); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_NAME); + $oCriteria->addSelectColumn(AdditionalTablesPeer::ADD_TAB_DESCRIPTION); + $oCriteria->addSelectColumn("'" . G::LoadTranslation('ID_ACTION_EXPORT') . "' as 'CH_SCHEMA'"); + $oCriteria->addSelectColumn("'" . G::LoadTranslation('ID_ACTION_EXPORT') . "' as 'CH_DATA'"); + + $uids = explode(',', $_GET['id']); + + foreach ($uids as $UID) { + if (!isset($CC)) { + $CC = $oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_UID, $UID, Criteria::EQUAL); + } else { + $CC->addOr($oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_UID, $UID, Criteria::EQUAL)); + } + } + $oCriteria->add($CC); + $oCriteria->addAnd($oCriteria->getNewCriterion(AdditionalTablesPeer::ADD_TAB_UID, '', Criteria::NOT_EQUAL)); + + $oDataset = AdditionalTablesPeer::doSelectRS($oCriteria); + $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + + $addTables = Array(); + while ($oDataset->next()) { + $addTables[] = $oDataset->getRow(); + } + + return $addTables; + } + + public function updateTag($httpData) + { + require_once 'classes/model/AdditionalTables.php'; + $oAdditionalTables = new AdditionalTables(); + $uid = $_REQUEST['ADD_TAB_UID']; + $value = $_REQUEST['value']; + + $repTabData = array( + 'ADD_TAB_UID' => $uid, + 'ADD_TAB_TAG' => $value + ); + $oAdditionalTables->update($repTabData); + } + + /** + * - protected functions (non callable from controller outside) - + */ + + /** + * Update data from a addTable record + * @param $row + */ + public function _dataUpdate($row, $primaryKeys) + { + $keys = G::decrypt($row['__index__'], 'pmtable'); + $keys = explode('-', $keys); + unset($row['__index__']); + + $params = array(); + + foreach ($keys as $key) { + $params[] = is_numeric($key) ? $key : "'$key'"; + } + + $obj = null; + eval('$obj = ' . $this->classPeerName . '::retrieveByPk(' . implode(',', $params) . ');'); + + if (is_object($obj)) { + foreach ($row as $key => $value) { + // validation, don't modify primary keys + if (in_array($key, $primaryKeys)) { + throw new Exception(G::loadTranslation('ID_DONT_MODIFY_PK_VALUE', array($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 { + $result = false; + } + + return $result; + } + + /** + * Update data from a addTable record + * @param $row + */ + public function _dataDestroy($row) + { + $row = G::decrypt($row, 'pmtable'); + $row = str_replace('"', '', $row); + $keys = explode('-', $row); + $params = array(); + foreach ($keys as $key) { + $params[] = is_numeric($key) ? $key : "'$key'"; + } + + $obj = null; + eval('$obj = ' . $this->classPeerName . '::retrieveByPk(' . implode(',', $params) . ');'); + + if (is_object($obj)) { + $obj->delete(); + return true; + } else { + return false; + } + } + + public function genDataReport($httpData) + { + G::loadClass('pmTable'); + require_once 'classes/model/AdditionalTables.php'; + $result->message = ''; + $result->success = true; + $additionalTables = new AdditionalTables(); - $table = $additionalTables->load($_SESSION['ADD_TAB_UID'], true); - - foreach ($table['FIELDS'] as $i=>$field) { - array_push($fieldsEdit, $field['FLD_DYN_NAME']); + $table = $additionalTables->load($httpData->id); + if ($table['PRO_UID'] != '') { + $additionalTables->populateReportTable( + $table['ADD_TAB_NAME'], + pmTable::resolveDbSource($table['DBS_UID']), + $table['ADD_TAB_TYPE'], + $table['PRO_UID'], + $table['ADD_TAB_GRID'] + ); + $result->message = 'generated for table ' . $table['ADD_TAB_NAME']; } - }//end editing - foreach($fields as $i => $field) { - $fields[$i]['_index'] = $i; - $indexes[$field['FIELD_NAME']] = $i; - - if(in_array($field['FIELD_NAME'], $fieldsEdit)) { - $fields[$i]['_isset'] = false; - } - } - - $_SESSION['_cache_pmtables']['pro_uid'] = $proUid; - $_SESSION['_cache_pmtables']['dyn_uid'] = $this->dynUid; - $_SESSION['_cache_pmtables']['rows'] = $fields; - $_SESSION['_cache_pmtables']['count'] = count($fields); - $_SESSION['_cache_pmtables']['indexes'] = $indexes; - } //end reload - - $fields = array(); - $tmp = array(); - - foreach ($_SESSION['_cache_pmtables']['rows'] as $i => $row) { - if (isset($filter) && $filter != '') { - if ($row['_isset'] && stripos($row['FIELD_NAME'], $filter) !== false) { - $tmp[] = $row; - } - } - else { - if ($row['_isset']) { - $tmp[] = $row; - } - } + return $result; } - $fields = array_slice($tmp, $start, $limit); - - return array('cache'=>$cache, 'count'=>count($tmp), 'rows'=>$fields); - } + /** + * Get report table default columns + * @param $type + */ + protected function _getReportTableDefaultColumns($type='NORMAL') + { + $defaultColumns = array(); + $application = new stdClass(); //APPLICATION KEY + $application->uid = ''; + $application->field_dyn = ''; + $application->field_uid = ''; + $application->field_name = 'APP_UID'; + $application->field_label = 'APP_UID'; + $application->field_type = 'VARCHAR'; + $application->field_size = 32; + $application->field_dyn = ''; + $application->field_key = 1; + $application->field_null = 0; + $application->field_filter = false; + $application->field_autoincrement = false; + array_push($defaultColumns, $application); - /** - * Get all dynaform grid fields from a process - * @param $proUid - * @param $gridId - */ - function _getGridDynafields($proUid, $gridId) - { - $fields = array(); - $fieldsNames = array(); - $excludeFieldsList = array('title', 'subtitle', 'link', 'file', 'button', 'reset', 'submit', - 'listbox', 'checkgroup', 'grid', 'javascript'); - - $labelFieldsTypeList = array('dropdown', 'checkbox', 'radiogroup', 'yesno'); + $application = new stdClass(); //APP_NUMBER + $application->uid = ''; + $application->field_dyn = ''; + $application->field_uid = ''; + $application->field_name = 'APP_NUMBER'; + $application->field_label = 'APP_NUMBER'; + $application->field_type = 'INTEGER'; + $application->field_size = 11; + $application->field_dyn = ''; + $application->field_key = 0; + $application->field_null = 0; + $application->field_filter = false; + $application->field_autoincrement = false; + array_push($defaultColumns, $application); - $G_FORM = new Form($proUid . '/' . $gridId, PATH_DYNAFORM, SYS_LANG, false); - - if ($G_FORM->type == 'grid') { - foreach($G_FORM->fields as $fieldName => $fieldNode) { - if (!in_array($fieldNode->type, $excludeFieldsList) && !in_array($fieldName, $fieldsNames)) { - $fields[] = array('name' => $fieldName, 'type' => $fieldNode->type, 'label'=> $fieldNode->label); - $fieldsNames[] = $fieldName; - - if (in_array($fieldNode->type, $labelFieldsTypeList) && !in_array($fieldName.'_label', $fieldsNames)) { - $fields[] = array('name' => $fieldName . '_label', 'type' => $fieldNode->type, 'label'=>$fieldNode->label . '_label'); - $fieldsNames[] = $fieldName; - } + //if it is a grid report table + if ($type == 'GRID') { + //GRID INDEX + $gridIndex = new stdClass(); + $gridIndex->uid = ''; + $gridIndex->field_dyn = ''; + $gridIndex->field_uid = ''; + $gridIndex->field_name = 'ROW'; + $gridIndex->field_label = 'ROW'; + $gridIndex->field_type = 'INTEGER'; + $gridIndex->field_size = '11'; + $gridIndex->field_dyn = ''; + $gridIndex->field_key = 1; + $gridIndex->field_null = 0; + $gridIndex->field_filter = false; + $gridIndex->field_autoincrement = false; + array_push($defaultColumns, $gridIndex); } - } + + return $defaultColumns; } - - return $fields; - } - - /** - * Get all dynaform fields inside all grids from a process - * @param $proUid - */ - function _getGridFields($proUid) - { - require_once 'classes/model/Dynaform.php'; - G::loadSystem('dynaformhandler'); - $aFields = array(); - $aFieldsNames = array(); - - $oCriteria = new Criteria('workflow'); - $oCriteria->addSelectColumn(DynaformPeer::DYN_FILENAME); - $oCriteria->add(DynaformPeer::PRO_UID, $proUid); - $oCriteria->add(DynaformPeer::DYN_TYPE, 'xmlform'); - $oDataset = DynaformPeer::doSelectRS($oCriteria); - $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); - $oDataset->next(); - while ($aRow = $oDataset->getRow()) { - //$G_FORM = new Form($aRow['DYN_FILENAME'], PATH_DYNAFORM, SYS_LANG); - $dynaformHandler = new dynaformHandler(PATH_DYNAFORM . $aRow['DYN_FILENAME']. '.xml'); - $nodeFieldsList = $dynaformHandler->getFields(); + /** + * Get all dynaform fields from a process (without grid fields) + * @param $proUid + * @param $type [values:xmlform/grid] + */ + public function _getDynafields2($proUid, $type='xmlform') + { + require_once 'classes/model/Dynaform.php'; + $fields = array(); + $fieldsNames = array(); - foreach($nodeFieldsList as $node) { - $arrayNode = $dynaformHandler->getArray($node); - $fieldName = $arrayNode['__nodeName__']; - $fieldType = $arrayNode['type']; + $oCriteria = new Criteria('workflow'); + $oCriteria->addSelectColumn(DynaformPeer::DYN_FILENAME); + $oCriteria->add(DynaformPeer::PRO_UID, $proUid); + $oCriteria->add(DynaformPeer::DYN_TYPE, $type); + $oDataset = DynaformPeer::doSelectRS($oCriteria); + $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $oDataset->next(); - if ($fieldType == 'grid') { + $excludeFieldsList = array('title', 'subtitle', 'link', 'file', 'button', 'reset', 'submit', + 'listbox', 'checkgroup', 'grid', 'javascript'); - if (!in_array($fieldName, $aFieldsNames)) { - $aFields[] = array('name' => $fieldName, 'xmlform' => str_replace($proUid . '/', '', $arrayNode['xmlgrid'])); - $aFieldsNames[] = $fieldName; - } + $labelFieldsTypeList = array('dropdown', 'checkbox', 'radiogroup', 'yesno'); + + while ($aRow = $oDataset->getRow()) { + if (file_exists(PATH_DYNAFORM . PATH_SEP . $aRow['DYN_FILENAME'] . '.xml')) { + $G_FORM = new Form($aRow['DYN_FILENAME'], PATH_DYNAFORM, SYS_LANG); + + if ($G_FORM->type == 'xmlform' || $G_FORM->type == '') { + foreach ($G_FORM->fields as $fieldName => $fieldNode) { + if (!in_array($fieldNode->type, $excludeFieldsList) && !in_array($fieldName, $fieldsNames)) { + $fields[] = array('name' => $fieldName, + 'type' => $fieldNode->type, + 'label' => $fieldNode->label); + $fieldsNames[] = $fieldName; + + if (in_array($fieldNode->type, $labelFieldsTypeList) + && !in_array($fieldName . '_label', $fieldsNames)) { + $fields[] = array('name' => $fieldName . '_label', + 'type' => $fieldNode->type, + 'label' => $fieldNode->label . '_label'); + $fieldsNames[] = $fieldName; + } + } + } + } + } + $oDataset->next(); } - } - - $oDataset->next(); + + return $fields; + } + + public function _getDynafields($proUid, $type='xmlform', $start=null, $limit=null, $filter=null) + { + $cache = 1; + if (!isset($_SESSION['_cache_pmtables']) || + (isset($_SESSION['_cache_pmtables']) && $_SESSION['_cache_pmtables']['pro_uid'] != $proUid) || + (isset($_SESSION['_cache_pmtables']) && $_SESSION['_cache_pmtables']['dyn_uid'] != $this->dynUid) + ) { + + require_once 'classes/model/Dynaform.php'; + $cache = 0; + $fields = array(); + $fieldsNames = array(); + + $oCriteria = new Criteria('workflow'); + $oCriteria->addSelectColumn(DynaformPeer::DYN_FILENAME); + $oCriteria->add(DynaformPeer::PRO_UID, $proUid); + $oCriteria->add(DynaformPeer::DYN_TYPE, $type); + + + if (isset($this->dynUid)) { + $oCriteria->add(DynaformPeer::DYN_UID, $this->dynUid); + } + + $oDataset = DynaformPeer::doSelectRS($oCriteria); + $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $oDataset->next(); + + $excludeFieldsList = array('title', 'subtitle', 'link', 'file', 'button', 'reset', 'submit', + 'listbox', 'checkgroup', 'grid', 'javascript'); + + $labelFieldsTypeList = array('dropdown', 'radiogroup'); + G::loadSystem('dynaformhandler'); + $index = 0; + + while ($aRow = $oDataset->getRow()) { + if (file_exists(PATH_DYNAFORM . PATH_SEP . $aRow['DYN_FILENAME'] . '.xml')) { + $dynaformHandler = new dynaformHandler(PATH_DYNAFORM . $aRow['DYN_FILENAME'] . '.xml'); + $nodeFieldsList = $dynaformHandler->getFields(); + + foreach ($nodeFieldsList as $node) { + $arrayNode = $dynaformHandler->getArray($node); + $fieldName = $arrayNode['__nodeName__']; + $fieldType = $arrayNode['type']; + + if (!in_array($fieldType, $excludeFieldsList) && !in_array($fieldName, $fieldsNames)) { + $fields[] = array( + 'FIELD_UID' => $fieldName . '-' . $fieldType, + 'FIELD_NAME' => $fieldName, + '_index' => $index++, + '_isset' => true + ); + $fieldsNames[] = $fieldName; + + if (in_array($fieldType, $labelFieldsTypeList) + && !in_array($fieldName . '_label', $fieldsNames)) { + $fields[] = array( + 'FIELD_UID' => $fieldName . '_label' . '-' . $fieldType, + 'FIELD_NAME' => $fieldName . '_label', + '_index' => $index++, + '_isset' => true + ); + $fieldsNames[] = $fieldName; + } + } + } + } + $oDataset->next(); + } + + sort($fields); + + // if is a editing + $fieldsEdit = array(); + if (isset($_SESSION['ADD_TAB_UID'])) { + require_once 'classes/model/AdditionalTables.php'; + + $additionalTables = new AdditionalTables(); + $table = $additionalTables->load($_SESSION['ADD_TAB_UID'], true); + + foreach ($table['FIELDS'] as $i => $field) { + array_push($fieldsEdit, $field['FLD_DYN_NAME']); + } + }//end editing + + foreach ($fields as $i => $field) { + $fields[$i]['_index'] = $i; + $indexes[$field['FIELD_NAME']] = $i; + + if (in_array($field['FIELD_NAME'], $fieldsEdit)) { + $fields[$i]['_isset'] = false; + } + } + + $_SESSION['_cache_pmtables']['pro_uid'] = $proUid; + $_SESSION['_cache_pmtables']['dyn_uid'] = $this->dynUid; + $_SESSION['_cache_pmtables']['rows'] = $fields; + $_SESSION['_cache_pmtables']['count'] = count($fields); + $_SESSION['_cache_pmtables']['indexes'] = $indexes; + } //end reload + + $fields = array(); + $tmp = array(); + + foreach ($_SESSION['_cache_pmtables']['rows'] as $i => $row) { + if (isset($filter) && $filter != '') { + if ($row['_isset'] && stripos($row['FIELD_NAME'], $filter) !== false) { + $tmp[] = $row; + } + } else { + if ($row['_isset']) { + $tmp[] = $row; + } + } + } + + $fields = array_slice($tmp, $start, $limit); + + return array('cache' => $cache, 'count' => count($tmp), 'rows' => $fields); + } + + /** + * Get all dynaform grid fields from a process + * @param $proUid + * @param $gridId + */ + public function _getGridDynafields($proUid, $gridId) + { + $fields = array(); + $fieldsNames = array(); + $excludeFieldsList = array('title', 'subtitle', 'link', 'file', 'button', 'reset', 'submit', + 'listbox', 'checkgroup', 'grid', 'javascript'); + + $labelFieldsTypeList = array('dropdown', 'checkbox', 'radiogroup', 'yesno'); + + $G_FORM = new Form($proUid . '/' . $gridId, PATH_DYNAFORM, SYS_LANG, false); + + if ($G_FORM->type == 'grid') { + foreach ($G_FORM->fields as $fieldName => $fieldNode) { + if (!in_array($fieldNode->type, $excludeFieldsList) && !in_array($fieldName, $fieldsNames)) { + $fields[] = array('name' => $fieldName, 'type' => $fieldNode->type, 'label' => $fieldNode->label); + $fieldsNames[] = $fieldName; + + if (in_array($fieldNode->type, $labelFieldsTypeList) + && !in_array($fieldName . '_label', $fieldsNames)) { + $fields[] = array('name' => $fieldName . '_label', + 'type' => $fieldNode->type, + 'label' => $fieldNode->label . '_label'); + $fieldsNames[] = $fieldName; + } + } + } + } + + return $fields; + } + + /** + * Get all dynaform fields inside all grids from a process + * @param $proUid + */ + public function _getGridFields($proUid) + { + require_once 'classes/model/Dynaform.php'; + G::loadSystem('dynaformhandler'); + $aFields = array(); + $aFieldsNames = array(); + + $oCriteria = new Criteria('workflow'); + $oCriteria->addSelectColumn(DynaformPeer::DYN_FILENAME); + $oCriteria->add(DynaformPeer::PRO_UID, $proUid); + $oCriteria->add(DynaformPeer::DYN_TYPE, 'xmlform'); + $oDataset = DynaformPeer::doSelectRS($oCriteria); + $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); + $oDataset->next(); + + while ($aRow = $oDataset->getRow()) { + //$G_FORM = new Form($aRow['DYN_FILENAME'], PATH_DYNAFORM, SYS_LANG); + $dynaformHandler = new dynaformHandler(PATH_DYNAFORM . $aRow['DYN_FILENAME'] . '.xml'); + $nodeFieldsList = $dynaformHandler->getFields(); + + foreach ($nodeFieldsList as $node) { + $arrayNode = $dynaformHandler->getArray($node); + $fieldName = $arrayNode['__nodeName__']; + $fieldType = $arrayNode['type']; + + if ($fieldType == 'grid') { + + if (!in_array($fieldName, $aFieldsNames)) { + $aFields[] = array('name' => $fieldName, + 'xmlform' => str_replace($proUid . '/', '', $arrayNode['xmlgrid'])); + $aFieldsNames[] = $fieldName; + } + } + } + + $oDataset->next(); + } + return $aFields; } - return $aFields; - } } + \ No newline at end of file