diff --git a/htdocs/core/db/Database.interface.php b/htdocs/core/db/Database.interface.php
index 28623f780a5..65699585f4a 100644
--- a/htdocs/core/db/Database.interface.php
+++ b/htdocs/core/db/Database.interface.php
@@ -280,7 +280,7 @@ interface Database
/**
* Create a table into database
*
- * @param string $table Nom de la table
+ * @param string $table Name of table
* @param array $fields Tableau associatif [nom champ][tableau des descriptions]
* @param string $primary_key Nom du champ qui sera la clef primaire
* @param string $type Type de la table
@@ -291,6 +291,14 @@ interface Database
*/
function DDLCreateTable($table, $fields, $primary_key, $type, $unique_keys = null, $fulltext_keys = null, $keys = null);
+ /**
+ * Drop a table into database
+ *
+ * @param string $table Name of table
+ * @return int <0 if KO, >=0 if OK
+ */
+ function DDLDropTable($table);
+
/**
* Return list of available charset that can be used to store data in database
*
diff --git a/htdocs/core/db/mssql.class.php b/htdocs/core/db/mssql.class.php
index 97c72160d76..79a0e212dfc 100644
--- a/htdocs/core/db/mssql.class.php
+++ b/htdocs/core/db/mssql.class.php
@@ -863,6 +863,22 @@ class DoliDBMssql extends DoliDB
return 1;
}
+ /**
+ * Drop a table into database
+ *
+ * @param string $table Name of table
+ * @return int <0 if KO, >=0 if OK
+ */
+ function DDLDropTable($table)
+ {
+ $sql = "DROP TABLE ".$table;
+
+ if (! $this->query($sql))
+ return -1;
+ else
+ return 1;
+ }
+
/**
* Return a pointer of line with description of a table or field
*
diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php
index 4206e75a165..4bad3c224f7 100644
--- a/htdocs/core/db/mysqli.class.php
+++ b/htdocs/core/db/mysqli.class.php
@@ -629,7 +629,7 @@ class DoliDBMysqli extends DoliDB
/**
* Create a table into database
*
- * @param string $table Nom de la table
+ * @param string $table Name of table
* @param array $fields Tableau associatif [nom champ][tableau des descriptions]
* @param string $primary_key Nom du champ qui sera la clef primaire
* @param string $type Type de la table
@@ -702,13 +702,28 @@ class DoliDBMysqli extends DoliDB
$sql .= ",".implode(',',$sqlk);
$sql .=") engine=".$type;
- dol_syslog($sql,LOG_DEBUG);
- if(! $this -> query($sql))
+ if(! $this->query($sql))
return -1;
else
return 1;
}
+ /**
+ * Drop a table into database
+ *
+ * @param string $table Name of table
+ * @return int <0 if KO, >=0 if OK
+ */
+ function DDLDropTable($table)
+ {
+ $sql = "DROP TABLE ".$table;
+
+ if (! $this->query($sql))
+ return -1;
+ else
+ return 1;
+ }
+
/**
* Return a pointer of line with description of a table or field
*
diff --git a/htdocs/core/db/pgsql.class.php b/htdocs/core/db/pgsql.class.php
index f0c437e0b96..1ba8f183cbb 100644
--- a/htdocs/core/db/pgsql.class.php
+++ b/htdocs/core/db/pgsql.class.php
@@ -999,6 +999,22 @@ class DoliDBPgsql extends DoliDB
return 1;
}
+ /**
+ * Drop a table into database
+ *
+ * @param string $table Name of table
+ * @return int <0 if KO, >=0 if OK
+ */
+ function DDLDropTable($table)
+ {
+ $sql = "DROP TABLE ".$table;
+
+ if (! $this->query($sql))
+ return -1;
+ else
+ return 1;
+ }
+
/**
* Create a user to connect to database
*
diff --git a/htdocs/core/db/sqlite3.class.php b/htdocs/core/db/sqlite3.class.php
index 2c52092b9c9..78762f282b4 100644
--- a/htdocs/core/db/sqlite3.class.php
+++ b/htdocs/core/db/sqlite3.class.php
@@ -945,6 +945,22 @@ class DoliDBSqlite3 extends DoliDB
return 1;
}
+ /**
+ * Drop a table into database
+ *
+ * @param string $table Name of table
+ * @return int <0 if KO, >=0 if OK
+ */
+ function DDLDropTable($table)
+ {
+ $sql = "DROP TABLE ".$table;
+
+ if (! $this->query($sql))
+ return -1;
+ else
+ return 1;
+ }
+
/**
* Return a pointer of line with description of a table or field
*
diff --git a/htdocs/langs/en_US/modulebuilder.lang b/htdocs/langs/en_US/modulebuilder.lang
index 2cec8d87788..78d6092f006 100644
--- a/htdocs/langs/en_US/modulebuilder.lang
+++ b/htdocs/langs/en_US/modulebuilder.lang
@@ -88,4 +88,7 @@ ToolkitForDevelopers=Toolkit for Dolibarr developers
TryToUseTheModuleBuilder=If you have knowledge in SQL and PHP, you can try to use the native module builder wizard. Just enable the module and use the wizard by clicking the on the top right menu. Warning: This is a developer feature, bad use may breaks your application.
SeeTopRightMenu=See on the top right menu
AddLanguageFile=Add language file
-YouCanUseTranslationKey=You can use here a key that is the translation key found into language file (see tab "Languages")
\ No newline at end of file
+YouCanUseTranslationKey=You can use here a key that is the translation key found into language file (see tab "Languages")
+DropTableIfEmpty=(Delete table if empty)
+TableDoesNotExists=The table %s does not exists
+TableDropped=Table %s deleted
\ No newline at end of file
diff --git a/htdocs/modulebuilder/index.php b/htdocs/modulebuilder/index.php
index df84d29ff72..634adbb02fa 100644
--- a/htdocs/modulebuilder/index.php
+++ b/htdocs/modulebuilder/index.php
@@ -371,6 +371,51 @@ if ($dirins && $action == 'initobject' && $module && $objectname)
}
}
+if ($dirins && ($action == 'droptable' || $action == 'droptableextrafields') && !empty($module) && ! empty($tabobj))
+{
+ $objectname = $tabobj;
+
+ $arrayoftables=array();
+ if ($action == 'droptable') $arrayoftables[] = MAIN_DB_PREFIX.strtolower($module).'_'.strtolower($tabobj);
+ if ($action == 'droptableextrafields') $arrayoftables[] = MAIN_DB_PREFIX.strtolower($module).'_'.strtolower($tabobj).'_extrafields';
+
+ foreach($arrayoftables as $tabletodrop)
+ {
+ $nb = -1;
+ $sql="SELECT COUNT(*) as nb FROM ".$tabletodrop;
+ $resql = $db->query($sql);
+ if ($resql)
+ {
+ $obj = $db->fetch_object($resql);
+ if ($obj)
+ {
+ $nb = $obj->nb;
+ }
+ }
+ else
+ {
+ if ($db->lasterrno() == 'DB_ERROR_NOSUCHTABLE')
+ {
+ setEventMessages($langs->trans("TableDoesNotExists", $tabletodrop), null, 'warnings');
+ }
+ else
+ {
+ dol_print_error($db);
+ }
+ }
+ if ($nb == 0)
+ {
+ $resql=$db->DDLDropTable($tabletodrop);
+ //var_dump($resql);
+ setEventMessages($langs->trans("TableDropped", $tabletodrop), null, 'mesgs');
+ }
+ elseif ($nb > 0)
+ {
+ setEventMessages($langs->trans("TableNotEmptyDropCanceled", $tabletodrop), null, 'warnings');
+ }
+ }
+}
+
if ($dirins && $action == 'addproperty' && !empty($module) && ! empty($tabobj))
{
$objectname = $tabobj;
@@ -1515,6 +1560,7 @@ elseif (! empty($module))
print '
';
print ' '.$langs->trans("SqlFileExtraFields").' : '.($realpathtosqlextra?'':'').$pathtosqlextra.($realpathtosqlextra?'':'').'';
print ' '.img_picto($langs->trans("Edit"), 'edit').'';
+ print ' '.$langs->trans("DropTableIfEmpty").'';
//print ' '.$langs->trans("RunSql").'';
print '
';
print ' '.$langs->trans("SqlFileKey").' : '.($realpathtosqlkey?'':'').$pathtosqlkey.($realpathtosqlkey?'':'').'';
diff --git a/htdocs/modulebuilder/template/core/modules/modMyModule.class.php b/htdocs/modulebuilder/template/core/modules/modMyModule.class.php
index 63a132699b7..a9e29a1a0ab 100644
--- a/htdocs/modulebuilder/template/core/modules/modMyModule.class.php
+++ b/htdocs/modulebuilder/template/core/modules/modMyModule.class.php
@@ -230,7 +230,6 @@ class modMyModule extends DolibarrModules
// Add here entries to declare new menus
- // Example to declare a new Top Menu entry and its Left menu entry:
/* BEGIN MODULEBUILDER TOPMENU */
$this->menu[$r++]=array('fk_menu'=>'', // '' if this is a top menu. For left menu, use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
'type'=>'top', // This is a Top menu entry
@@ -247,7 +246,6 @@ class modMyModule extends DolibarrModules
/* END MODULEBUILDER TOPMENU */
- // Example to declare a Left Menu entry into an existing Top menu entry:
/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT
$this->menu[$r++]=array( 'fk_menu'=>'fk_mainmenu=mymodule', // '' if this is a top menu. For left menu, use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode
'type'=>'left', // This is a Left menu entry