static function generateTablesAndFields($delete_old = false) { rex_xform_manager_table::reload(); $types = rex_xform::getTypeArray(); foreach (rex_xform_manager_table::getAll() as $table) { $c = rex_sql::factory(); $c->debugsql = self::$debug; $c->setQuery('CREATE TABLE IF NOT EXISTS `' . $table['table_name'] . '` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY )'); // remember fields, create and in case delete $c->setQuery('SHOW COLUMNS FROM `' . $table['table_name'] . '`'); $saved_columns = $c->getArray(); foreach ($table->getFields() as $field) { $type_name = $field['type_name']; $type_id = $field['type_id']; if ($type_id == 'value') { $type_label = $field['name']; $dbtype = $types[$type_id][$type_name]['dbtype']; if ($dbtype != 'none' && $dbtype != '') { if (isset($types[$type_id][$type_name]['hooks']['preCreate'])) { $result = call_user_func($types[$type_id][$type_name]['hooks']['preCreate'], $field); if (false === $result) { continue; } elseif (is_string($result)) { $dbtype = $result; } } $add_column = true; foreach ($saved_columns as $uu => $vv) { if ($vv['Field'] == $type_label) { $add_column = false; unset($saved_columns[$uu]); break; } } if ($add_column) { $null = isset($types[$type_id][$type_name]['null']) && $types[$type_id][$type_name]['null']; $null = $null ? '' : ' NOT NULL'; $c->setQuery('ALTER TABLE `' . $table['table_name'] . '` ADD `' . $type_label . '` ' . $dbtype . $null); } } } } if ($delete_old === true) { foreach ($saved_columns as $uu => $vv) { if ($vv['Field'] != 'id') { $c->setQuery('ALTER TABLE `' . $table['table_name'] . '` DROP `' . $vv['Field'] . '` '); } } } } }