diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 0040f1e3184..e25809559d2 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -528,7 +528,7 @@ class ExtraFields $typedb=$type; $lengthdb=$length; } - $field_desc = array('type'=>$typedb, 'value'=>$lengthdb, 'null'=>($required?'NOT NULL':'NULL')); + $field_desc = array('type'=>$typedb, 'value'=>$lengthdb, 'null'=>($required?'NOT NULL':'NULL'), 'default'=>$default); if ($type != 'separate') // No table update when separate type { diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php index 4358e8695fe..14256b37d29 100644 --- a/htdocs/core/db/mysqli.class.php +++ b/htdocs/core/db/mysqli.class.php @@ -779,7 +779,28 @@ class DoliDBMysqli extends DoliDB if ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int' || $field_desc['type'] == 'varchar') { $sql.="(".$field_desc['value'].")"; } - if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL') $sql.=" NOT NULL"; + if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL') + { + // We will try to change format of column to NOT NULL. To be sure the ALTER works, we try to update fields that are NULL + if ($field_desc['type'] == 'varchar' || $field_desc['type'] == 'text') + { + $sqlbis="UPDATE ".$table." SET ".$field_name." = '".$this->escape($field_desc['default'] ? $field_desc['default'] : '')."' WHERE ".$field_name." IS NULL"; + $this->query($sqlbis); + } + elseif ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') + { + $sqlbis="UPDATE ".$table." SET ".$field_name." = ".((int) $this->escape($field_desc['default'] ? $field_desc['default'] : 0))." WHERE ".$field_name." IS NULL"; + $this->query($sqlbis); + } + + $sql.=" NOT NULL"; + } + + if ($field_desc['default']) + { + if ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') $sql.=" DEFAULT ".$this->escape($field_desc['default']); + elseif ($field_desc['type'] == 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields + } dol_syslog(get_class($this)."::DDLUpdateField ".$sql,LOG_DEBUG); if (! $this->query($sql)) diff --git a/htdocs/core/db/pgsql.class.php b/htdocs/core/db/pgsql.class.php index d02ff4a1341..0448789fc50 100644 --- a/htdocs/core/db/pgsql.class.php +++ b/htdocs/core/db/pgsql.class.php @@ -1088,8 +1088,26 @@ class DoliDBPgsql extends DoliDB $sql.="(".$field_desc['value'].")"; } - // TODO May not work with pgsql. May need to run a second request. If it works, just remove the comment - if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL') $sql.=" NOT NULL"; + if ($field_desc['null'] == 'not null' || $field_desc['null'] == 'NOT NULL') + { + // We will try to change format of column to NOT NULL. To be sure the ALTER works, we try to update fields that are NULL + if ($field_desc['type'] == 'varchar' || $field_desc['type'] == 'text') + { + $sqlbis="UPDATE ".$table." SET ".$field_name." = '".$this->escape($field_desc['default'] ? $field_desc['default'] : '')."' WHERE ".$field_name." IS NULL"; + $this->query($sqlbis); + } + elseif ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') + { + $sqlbis="UPDATE ".$table." SET ".$field_name." = ".((int) $this->escape($field_desc['default'] ? $field_desc['default'] : 0))." WHERE ".$field_name." IS NULL"; + $this->query($sqlbis); + } + } + + if ($field_desc['default']) + { + if ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int') $sql.=" DEFAULT ".$this->escape($field_desc['default']); + elseif ($field_desc['type'] == 'text') $sql.=" DEFAULT '".$this->escape($field_desc['default'])."'"; // Default not supported on text fields + } dol_syslog($sql,LOG_DEBUG); if (! $this->query($sql)) diff --git a/htdocs/core/tpl/admin_extrafields_add.tpl.php b/htdocs/core/tpl/admin_extrafields_add.tpl.php index a7f98bdba72..6c5f563e106 100644 --- a/htdocs/core/tpl/admin_extrafields_add.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_add.tpl.php @@ -42,12 +42,12 @@ var alwayseditable = jQuery("#alwayseditable"); var list = jQuery("#list"); '); + init_typeoffields(''); jQuery("#type").change(function() { init_typeoffields($(this).val()); }); @@ -130,15 +130,15 @@ - + - + - + - - + +
trans("Label"); ?>
trans("Label"); ?>
trans("AttributeCode"); ?> (trans("AlphaNumOnlyLowerCharsAndNoSpace"); ?>)
trans("AttributeCode"); ?> (trans("AlphaNumOnlyLowerCharsAndNoSpace"); ?>)
trans("Type"); ?> -selectarray('type',$type2label,GETPOST('type')); ?> +selectarray('type',$type2label,GETPOST('type','alpha')); ?>
trans("Size"); ?>
trans("Size"); ?>
@@ -147,7 +147,7 @@ - + - + - + - + - +global->MAIN_CAN_HIDE_EXTRAFIELDS)) { ?> - +multicompany->enabled) { ?> - + global->MAIN_FEATURES_LEVEL >= 2) { ?> +
- + textwithpicto('', $langs->trans("ExtrafieldParamHelpselect"),1,0)?> textwithpicto('', $langs->trans("ExtrafieldParamHelpsellist"),1,0)?> @@ -162,27 +162,27 @@
trans("LanguageFile"); ?>
textwithpicto($langs->trans("ComputedFormula"), $langs->trans("ComputedFormulaDesc"), 1, 'help', '', 0, 2, 'tooltipcompute'); ?>">
textwithpicto($langs->trans("ComputedFormula"), $langs->trans("ComputedFormulaDesc"), 1, 'help', '', 0, 2, 'tooltipcompute'); ?>
trans("DefaultValue").' ('.$langs->trans("Database").')'; ?>">
trans("DefaultValue").' ('.$langs->trans("Database").')'; ?>
trans("Unique"); ?>>
trans("Unique"); ?>>
trans("Required"); ?>>
trans("Required"); ?>>
trans("AlwaysEditable"); ?>>
trans("AlwaysEditable"); ?>>
trans("Hidden"); ?>>
trans("Hidden"); ?>>
trans("AllEntities"); ?>>
trans("AllEntities"); ?>>
trans("ByDefaultInList"); ?> trans("FeatureNotYetSupported")); ?> ->
>
diff --git a/htdocs/core/tpl/admin_extrafields_edit.tpl.php b/htdocs/core/tpl/admin_extrafields_edit.tpl.php index beb1e34055e..85cfe510df8 100644 --- a/htdocs/core/tpl/admin_extrafields_edit.tpl.php +++ b/htdocs/core/tpl/admin_extrafields_edit.tpl.php @@ -41,12 +41,12 @@ var alwayseditable = jQuery("#alwayseditable"); var list = jQuery("#list"); $val) { $selected=''; - if ($key == (GETPOST('type')?GETPOST('type'):$type)) $selected=' selected="selected"'; + if ($key == (GETPOST('type','alpha')?GETPOST('type','alpha'):$type)) $selected=' selected="selected"'; if (in_array($key, $typewecanchangeinto[$type])) print ''; else print ''; } @@ -222,8 +222,8 @@ else
trans("LanguageFile"); ?>
textwithpicto($langs->trans("ComputedFormula"), $langs->trans("ComputedFormulaDesc"), 1, 'help', '', 0, 2, 'tooltipcompute'); ?>
trans("DefaultValue").' ('.$langs->trans("Database").')'; ?>
trans("Unique"); ?>>