/** * 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; }
$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); } $oAdditionalTables->createTable(strtoupper($pmTableName), 'wf', $aFields); $oAdditionalTables->createPropelClasses(strtoupper($pmTableName), $pmTableName, $pmTableFields, $sAddTabUid); require_once "classes/model/Application.php"; require_once "classes/model/AdditionalTables.php"; require_once "classes/model/Fields.php"; $Criteria = new Criteria('workflow'); $Criteria->addSelectColumn(ApplicationPeer::APP_UID); $Criteria->addSelectColumn(ApplicationPeer::APP_DATA); // $Criteria->add (AppCacheViewPeer::DEL_THREAD_STATUS, 'OPEN'); $oDataset = ApplicationPeer::doSelectRS($Criteria); $oDataset->setFetchmode(ResultSet::FETCHMODE_ASSOC); $oDataset->next(); $aProcesses = array();
//$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(); foreach ($addTabBeforeData['FIELDS'] as $field) { //if ($field['FLD_NAME'] == 'APP_UID' || $field['FLD_NAME'] == 'ROW') continue; $oldFields[$field['FLD_UID']] = $field; } } $aFields = array(); $fieldsList = array(); $editFieldsList = array(); 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, 'FLD_NULL' => isset($column->field_null) ? $column->field_null : 1, 'FLD_AUTO_INCREMENT' => 0, 'FLD_KEY' => isset($column->field_key) ? $column->field_key : 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); $fieldUid = $oFields->create($field); $fieldsList[] = $field; if ($data['REP_TAB_UID'] == '') { //new $aFields[] = array('sType' => $column->field_type, 'iSize' => $column->field_size, 'sFieldName' => $column->field_name, 'bNull' => isset($column->field_null) ? $column->field_null : 1, 'bAI' => 0, 'bPrimaryKey' => isset($column->field_key) ? $column->field_key : 0); } else { //editing $field['FLD_UID'] = $fieldUid; $aFields[$fieldUid] = $field; } } if ($data['REP_TAB_UID'] == '') { //create a new report table $oAdditionalTables->createTable($data['REP_TAB_NAME'], $data['REP_TAB_CONNECTION'], $aFields); } else { //editing
/** * @test */ public function it_sinks_a_ship_in_fields_with_a_ship() { $fields = []; $data = ['startPoint' => ['x' => 1, 'y' => 1], 'endPoint' => ['x' => 1, 'y' => 2], 'hits' => 1]; $ship = Ship::fromArray($data); $field = Field::generate(1, 1); $field->place($ship); $fields[] = $field; $field = Field::generate(1, 2); $field->place($ship); $fields[] = $field; $fieldsClass = Fields::create($fields); $fieldsClass->shoot(Coords::create(1, 1)); $this->assertEquals(true, $fieldsClass->didShipSankAt(Coords::create(1, 1))); $this->assertEquals(true, $fieldsClass->didAllShipsSink()); }