Skip to content

Commit

Permalink
Verschiedene DB Feldtypen erlauben
Browse files Browse the repository at this point in the history
  • Loading branch information
dergel committed Sep 4, 2018
1 parent f137a02 commit 67ef975
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 34 deletions.
4 changes: 2 additions & 2 deletions lib/yform/value/integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public function getDefinitions()
'notice' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_defaults_notice')],
],
'description' => rex_i18n::msg('yform_values_integer_description'),
'dbtype' => 'int',
'null' => true,
'db_type' => ['int'],
'db_null' => true,
];
}

Expand Down
2 changes: 1 addition & 1 deletion lib/yform/value/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getDefinitions()
'notice' => ['type' => 'text', 'label' => rex_i18n::msg('yform_values_defaults_notice')],
],
'description' => rex_i18n::msg('yform_values_text_description'),
'dbtype' => 'text',
'db_type' => ['varchar(191)', 'text'],
'famous' => true,
];
}
Expand Down
1 change: 1 addition & 0 deletions plugins/manager/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
->ensureColumn(new rex_sql_column('prio', 'int(11)'))
->ensureColumn(new rex_sql_column('type_id', 'varchar(191)'))
->ensureColumn(new rex_sql_column('type_name', 'varchar(191)'))
->ensureColumn(new rex_sql_column('db_type', 'varchar(191)'))
->ensureColumn(new rex_sql_column('list_hidden', 'tinyint(1)'))
->ensureColumn(new rex_sql_column('search', 'tinyint(1)'))
->ensureColumn(new rex_sql_column('name', 'text'))
Expand Down
5 changes: 4 additions & 1 deletion plugins/manager/lang/de_de.lang
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,7 @@ yform_values_prio_scope = Beschränkung
yform_values_prio_default = Am Anfang

yform_is = ist
yform_will_set_to = wird
yform_will_set_to = wird

yform_field_add = Feld hinzufügen
yform_field_update = Feld aktualisieren
14 changes: 10 additions & 4 deletions plugins/manager/lib/yform/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,15 +1044,15 @@ public function getFieldPage()
}
break;

case 'no_db':
/* case 'no_db':
if (!isset($v['values'])) {
$v['values'] = [0,1];
}
if (!isset($v['default']) || $v['default'] != 1) {
$v['default'] = 0;
}
$yform->setValueField('checkbox', ['name' => $k_field, 'label' => rex_i18n::msg('yform_donotsaveindb'), 'values' => 'no_db', 'default' => $v['default']]);
break;
break;*/

case 'boolean':
if (!isset($v['values'])) {
Expand Down Expand Up @@ -1132,7 +1132,7 @@ public function getFieldPage()
$yform->setObjectparams('main_table', rex_yform_manager_field::table());

if ($func == 'edit') {
$yform->setObjectparams('submit_btn_label', rex_i18n::msg('yform_save'));
$yform->setObjectparams('submit_btn_label', rex_i18n::msg('yform_field_update'));
$yform->setHiddenField('field_id', $field_id);
$yform->setActionField('manage_db', [rex_yform_manager_field::table(), "id=$field_id"]);
$yform->setObjectparams('main_id', $field_id);
Expand All @@ -1150,18 +1150,24 @@ public function getFieldPage()
$yform->setObjectparams('sql_object', $sql);
$yform->setObjectparams('getdata', true);
} elseif ($func == 'add') {
$yform->setObjectparams('submit_btn_label', rex_i18n::msg('yform_add'));
$yform->setObjectparams('submit_btn_label', rex_i18n::msg('yform_field_add'));
$yform->setActionField('manage_db', [rex_yform_manager_field::table()]);
}

if ($type_id == 'value') {

$db_choices = $field->getDBTypes();
$default = $field->getDBDefaultType();
$yform->setValueField('choice', ['name' => 'db_type', 'label' => rex_i18n::msg('yform_db_type'), 'choices' => $db_choices, 'default' => $default]);

if (!$field->isHiddenInListDisabled()) {
$yform->setValueField('checkbox', ['name' => 'list_hidden', 'label' => rex_i18n::msg('yform_hideinlist'), 'default' => '1']);
}

if (!$field->isSearchableDisabled()) {
$yform->setValueField('checkbox', ['name' => 'search', 'label' => rex_i18n::msg('yform_useassearchfieldalidatenamenotempty'), 'default' => '1']);
}

} elseif ($type_id == 'validate') {
$yform->setValueField('hidden', ['list_hidden', 1]);
$yform->setValueField('hidden', ['search', 0]);
Expand Down
45 changes: 42 additions & 3 deletions plugins/manager/lib/yform/manager/field.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,49 @@ public function getElement($k)

public function getDBType()
{
if (!isset($this->definitions['dbtype'])) {
return 'none';
if (!isset($this->value['db_type'])) {
return $this->getDBDefaultType();
}
return $this->definitions['dbtype'];
return $this->value['db_type'];
}

public function getDBTypes()
{
$db_types = [];
if (isset($this->definitions['dbtype'])) {
$this->definitions['db_type'] = [$this->definitions['dbtype']];
}

if (!isset($this->definitions['db_type'])) {
$this->definitions['db_type'] = [];
} else if (!is_array($this->definitions['db_type'])) {
$this->definitions['db_type'] = [$this->definitions['db_type']];
}
foreach($this->definitions['db_type'] as $db_type){
$db_types[$db_type] = $db_type;
}
$db_types['none'] = '[none]';
return $db_types;
}

public function getDBDefaultType()
{
$db_types = $this->getDBTypes();
reset($db_types);
return key($db_types);
}

public function getDBNull()
{
if (isset($this->definitions['db_null']) && $this->definitions['db_null']) {
return true;
}
return false;
}

public function getHooks()
{
return (isset($this->definitions['hooks'])) ? $this->definitions['hooks'] : [];
}

public function isSearchableDisabled()
Expand Down
43 changes: 20 additions & 23 deletions plugins/manager/lib/yform/manager/table_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,48 +543,45 @@ public static function generateTablesAndFields($delete_old = false)
{

rex_yform_manager_table::deleteCache();
$types = rex_yform::getTypeArray();
foreach (rex_yform_manager_table::getAll() as $table) {

$c = rex_sql::factory();
$c->setDebug(self::$debug);
$c->setQuery('CREATE TABLE IF NOT EXISTS `' . $table['table_name'] . '` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
$c->setQuery('ALTER TABLE `' . $table['table_name'] . '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');
$c->setQuery('CREATE TABLE IF NOT EXISTS `' . $table->getTableName() . '` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
$c->setQuery('ALTER TABLE `' . $table->getTableName() . '` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;');

// remember fields, create and in case delete
$c->setQuery('SHOW COLUMNS FROM `' . $table['table_name'] . '`');
$saved_columns = $c->getArray();
$savedColumns = $table->getColumns();

$EnsureTable = rex_sql_table::get($table['table_name']);
$EnsureTable = rex_sql_table::get($table->getTableName());

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 ($field->getType() == 'value') {
$db_type = $field->getDBType();

if ($db_type != 'none' && $db_type != '') {

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);
$hooks = $field->getHooks();
if (isset($hooks['preCreate'])) {
$result = call_user_func($hooks['preCreate'], $field);
if (false === $result) {
continue;
}
if (is_string($result)) {
$dbtype = $result;
$db_type = $result;
}
}

foreach ($saved_columns as $uu => $vv) {
if ($vv['Field'] == $type_label) {
unset($saved_columns[$uu]);
foreach ($savedColumns as $savedColumn) {
if ($savedColumn['name'] == $field->getName()) {
unset($savedColumns[$savedColumn['name']]);
break;
}
}

$null = isset($types[$type_id][$type_name]['null']) && $types[$type_id][$type_name]['null'];
$EnsureTable
->ensureColumn(new rex_sql_column($type_label, $dbtype, $null))
->ensureColumn(new rex_sql_column($field->getName(), $db_type, $field->getDBNull()))
->ensure();

}
Expand All @@ -593,9 +590,9 @@ public static function generateTablesAndFields($delete_old = false)
}

if ($delete_old === true) {
foreach ($saved_columns as $uu => $vv) {
if ($vv['Field'] != 'id') {
$c->setQuery('ALTER TABLE `' . $table['table_name'] . '` DROP `' . $vv['Field'] . '` ');
foreach ($savedColumns as $savedColumn) {
if ($savedColumn['name'] != 'id') {
$c->setQuery('ALTER TABLE `' . $table->getTableName() . '` DROP `' . $savedColumn['name'] . '` ');
}
}
}
Expand Down

0 comments on commit 67ef975

Please sign in to comment.