Exemplo n.º 1
0
         $gridIndex->field_size = '11';
         $gridIndex->field_dyn = '';
         $gridIndex->field_null = 0;
         $gridIndex->field_filter = false;
         array_push($defaultColumns, $gridIndex);
     }
     $columns = array_merge($defaultColumns, $columns);
     /** validations **/
     if (is_array($aNameTable)) {
         throw new Exception('The table "' . $data['REP_TAB_NAME'] . '" already exits.');
     }
     if (in_array(strtoupper($data['REP_TAB_NAME']), $aReservedWords)) {
         throw new Exception('Could not create the table with the name "' . $data['REP_TAB_NAME'] . '" because it is a reserved word.');
     }
     //create record
     $addTabUid = $oAdditionalTables->create($repTabData);
 } else {
     //editing report table
     $addTabUid = $data['REP_TAB_UID'];
     //loading old data before update
     $addTabBeforeData = $oAdditionalTables->load($addTabUid, true);
     //updating record
     $oAdditionalTables->update($repTabData);
     //removing old data fields references
     $oCriteria = new Criteria('workflow');
     $oCriteria->add(FieldsPeer::ADD_TAB_UID, $data['REP_TAB_UID']);
     //$oCriteria->add(FieldsPeer::FLD_NAME, 'APP_UID', Criteria::NOT_EQUAL);
     //$oCriteria->add(FieldsPeer::FLD_NAME, 'ROW', Criteria::NOT_EQUAL);
     FieldsPeer::doDelete($oCriteria);
     //getting old fieldnames
     $oldFields = array();
Exemplo n.º 2
0
 /**
  * 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 Table
         $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');
         // Reserved Words Field
         $reservedWordsPhp = array('case', 'catch', 'cfunction', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'do', 'else', 'elseif', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'interface', 'instanceof', 'private', 'namespace', 'new', 'old_function', 'or', 'throw', 'protected', 'public', 'static', 'switch', 'xor', 'try', 'use', 'var', 'while');
         $reservedWordsSql = G::reservedWordsSql();
         // 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) || in_array(strtoupper($data["REP_TAB_NAME"]), $reservedWordsSql)) {
                 throw new Exception(G::LoadTranslation("ID_PMTABLE_INVALID_NAME", array($data["REP_TAB_NAME"])));
             }
         }
         //backward compatility
         foreach ($columns as $i => $column) {
             if (in_array(strtoupper($columns[$i]->field_name), $reservedWordsSql) || in_array(strtolower($columns[$i]->field_name), $reservedWordsPhp)) {
                 throw new Exception(G::LoadTranslation("ID_PMTABLE_INVALID_FIELD_NAME", array($columns[$i]->field_name)));
             }
             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
                 // propel DATETIME equivalent is TIMESTAMP
                 case 'DATETIME':
                     $columns[$i]->field_type = 'TIMESTAMP';
                     break;
             }
             // VALIDATIONS
             if ($columns[$i]->field_autoincrement) {
                 $typeCol = $columns[$i]->field_type;
                 if (!($typeCol === 'INTEGER' || $typeCol === 'TINYINT' || $typeCol === 'SMALLINT' || $typeCol === 'BIGINT')) {
                     $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);
         if (isset($data["keepData"]) && $data["keepData"] == 1) {
             //PM Table
             $pmTable->setKeepData(true);
         }
         $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'], $addTabUid);
             } 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;
 }
$_POST = $filter->xssFilterHard($_POST);
require_once "classes/model/AdditionalTables.php";
require_once "classes/model/Fields.php";
// passing the parameters
$pmTableName = isset($_POST['tableName']) ? $_POST['tableName'] : 'contenders';
$pmTableFields = isset($_POST['tableFields']) ? G::json_decode($_POST['tableFields']) : array();
// default parameters
//$pmTableName   = 'Sender';
$pmTableFields = array(array('FLD_NAME' => 'APP_UID'), array('FLD_NAME' => 'CON_NAME'), array('FLD_NAME' => 'CON_ADDR'), array('FLD_NAME' => '_cedula'));
// setting the data to assemble the table
$aData = array();
$aData['ADD_TAB_NAME'] = $pmTableName;
// creating the objects to create the table and records
$oFields = new Fields();
$oAdditionalTables = new AdditionalTables();
$sAddTabUid = $oAdditionalTables->create($aData, $pmTableFields);
foreach ($pmTableFields as $iRow => $aRow) {
    $pmTableFields[$iRow]['FLD_NAME'] = strtoupper($aRow['FLD_NAME']);
    $pmTableFields[$iRow]['FLD_DESCRIPTION'] = isset($aRow['FLD_DESCRIPTION']) ? $aRow['FLD_DESCRIPTION'] : $aRow['FLD_NAME'];
    $pmTableFields[$iRow]['FLD_TYPE'] = isset($aRow['FLD_TYPE']) ? $aRow['FLD_TYPE'] : 'VARCHAR';
    $pmTableFields[$iRow]['FLD_SIZE'] = isset($aRow['FLD_SIZE']) ? $aRow['FLD_SIZE'] : '32';
    $pmTableFields[$iRow]['FLD_NULL'] = isset($aRow['FLD_NULL']) ? $aRow['FLD_NULL'] : 'off';
    $pmTableFields[$iRow]['FLD_AUTO_INCREMENT'] = isset($aRow['FLD_AUTO_INCREMENT']) ? $aRow['FLD_AUTO_INCREMENT'] : 'off';
    $pmTableFields[$iRow]['FLD_KEY'] = isset($aRow['FLD_KEY']) ? $aRow['FLD_KEY'] : 'off';
    $pmTableFields[$iRow]['FLD_FOREIGN_KEY'] = isset($aRow['FLD_FOREIGN_KEY']) ? $aRow['FLD_FOREIGN_KEY'] : 'off';
    $pmTableFields[$iRow]['FLD_FOREIGN_KEY_TABLE'] = isset($aRow['FLD_FOREIGN_KEY_TABLE']) ? $aRow['FLD_FOREIGN_KEY_TABLE'] : '';
}
foreach ($pmTableFields as $iRow => $aRow) {
    $oFields->create(array('FLD_INDEX' => $iRow + 1, 'ADD_TAB_UID' => $sAddTabUid, 'FLD_NAME' => $aRow['FLD_NAME'], 'FLD_DESCRIPTION' => isset($aRow['FLD_DESCRIPTION']) ? $aRow['FLD_DESCRIPTION'] : '', 'FLD_TYPE' => isset($aRow['FLD_TYPE']) ? $aRow['FLD_TYPE'] : 'VARCHAR', 'FLD_SIZE' => isset($aRow['FLD_SIZE']) ? $aRow['FLD_SIZE'] : '32', 'FLD_NULL' => $aRow['FLD_NULL'] == 'on' ? 1 : 0, 'FLD_AUTO_INCREMENT' => $aRow['FLD_AUTO_INCREMENT'] == 'on' ? 1 : 0, 'FLD_KEY' => $aRow['FLD_KEY'] == 'on' ? 1 : 0, 'FLD_FOREIGN_KEY' => $aRow['FLD_FOREIGN_KEY'] == 'on' ? 1 : 0, 'FLD_FOREIGN_KEY_TABLE' => isset($aRow['FLD_FOREIGN_KEY_TABLE']) ? $aRow['FLD_FOREIGN_KEY_TABLE'] : ''));
    $aFields[] = array('sType' => isset($aRow['FLD_TYPE']) ? $aRow['FLD_TYPE'] : 'VARCHAR', 'iSize' => isset($aRow['FLD_SIZE']) ? $aRow['FLD_SIZE'] : '32', 'sFieldName' => $aRow['FLD_NAME'], 'bNull' => $aRow['FLD_NULL'] == 'on' ? 1 : 0, 'bAI' => $aRow['FLD_AUTO_INCREMENT'] == 'on' ? 1 : 0, 'bPrimaryKey' => $aRow['FLD_KEY'] == 'on' ? 1 : 0);
}