From 966f13531696381b7294175e9d5e1b12c93aace0 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 26 Oct 2013 16:18:28 +0200
Subject: [PATCH] Fix: Migration must load new boxes

---
 htdocs/core/modules/DolibarrModules.class.php | 40 ++++++++++---------
 htdocs/core/modules/modCommande.class.php     |  4 +-
 htdocs/core/modules/modFacture.class.php      |  4 +-
 htdocs/core/modules/modProduct.class.php      | 23 ++++++-----
 htdocs/core/modules/modService.class.php      | 10 +++--
 htdocs/install/upgrade2.php                   | 32 ++++++++-------
 6 files changed, 62 insertions(+), 51 deletions(-)

diff --git a/htdocs/core/modules/DolibarrModules.class.php b/htdocs/core/modules/DolibarrModules.class.php
index 6e623d183d2..12f739331b8 100644
--- a/htdocs/core/modules/DolibarrModules.class.php
+++ b/htdocs/core/modules/DolibarrModules.class.php
@@ -52,12 +52,13 @@ abstract class DolibarrModules
 
     var $dbversion = "-";
 
+
     /**
-     *      Fonction d'activation. Insere en base les constantes et boites du module
+     *  Method to enable a module. Insert into database all constants, boxes of module
      *
-     *      @param      array	$array_sql  Array of SQL requests to execute when enabling module
-     *      @param      string	$options    Options when enabling module ('', 'noboxes')
-     *      @return     int              	1 if OK, 0 if KO
+     *  @param      array	$array_sql  	Array of SQL requests to execute when enabling module
+     *  @param      string	$options    	String with options when disabling module ('newboxdefonly|noboxes')
+     *  @return     int              		1 if OK, 0 if KO
      */
     function _init($array_sql, $options='')
     {
@@ -78,8 +79,8 @@ abstract class DolibarrModules
         // Insert constant defined by modules, into llx_const
         if (! $err) $err+=$this->insert_const();
 
-        // Insere les boites dans llx_boxes_def
-        if (! $err && $options != 'noboxes') $err+=$this->insert_boxes();
+        // Insert boxes def into llx_boxes_def and boxes setup into llx_boxes
+        if (! $err && ! preg_match('/noboxes/',$options)) $err+=$this->insert_boxes($options);
 
         // Insert permission definitions of module into llx_rights_def. If user is admin, grant this permission to user.
         if (! $err) $err+=$this->insert_permissions(1);
@@ -147,7 +148,7 @@ abstract class DolibarrModules
      *  Fonction de desactivation. Supprime de la base les constantes et boites du module
      *
      *  @param      array	$array_sql      Array of SQL requests to execute when disable module
-     *  @param      string	$options		Options when disabling module ('', 'noboxes')
+     *  @param      string	$options		String with options when disabling module ('newboxdefonly|noboxes')
      *  @return     int      		       	1 if OK, 0 if KO
      */
     function _remove($array_sql, $options='')
@@ -170,7 +171,7 @@ abstract class DolibarrModules
         if (! $err) $err+=$this->delete_const();
 
         // Remove list of module's available boxes (entry in llx_boxes)
-        if (! $err && $options != 'noboxes') $err+=$this->delete_boxes();
+        if (! $err && ! preg_match('/(newboxdefonly|noboxes)/',$options)) $err+=$this->delete_boxes();	// We don't have to delete if option ask to keep boxes safe or ask to add new box def only
 
         // Remove module's permissions from list of available permissions (entries in llx_rights_def)
         if (! $err) $err+=$this->delete_permissions();
@@ -419,7 +420,7 @@ abstract class DolibarrModules
 		if (empty($reldir)) return 1;
 
         include_once DOL_DOCUMENT_ROOT .'/core/lib/admin.lib.php';
-        
+
         $ok = 1;
         foreach($conf->file->dol_document_root as $dirroot)
         {
@@ -497,9 +498,10 @@ abstract class DolibarrModules
     /**
      *  Insert boxes into llx_boxes_def
      *
-     *  @return     int     Nb of errors (0 if OK)
+     *	@param		string	$option		String with options when disabling module ('newboxdefonly'=insert only boxes definition)
+     *  @return     int     			Nb of errors (0 if OK)
      */
-    function insert_boxes()
+    function insert_boxes($option='')
     {
 		require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
 
@@ -518,11 +520,13 @@ abstract class DolibarrModules
             	if (empty($file)) $file  = isset($this->boxes[$key][1])?$this->boxes[$key][1]:'';	// For backward compatibility
                 if (empty($note)) $note  = isset($this->boxes[$key][2])?$this->boxes[$key][2]:'';	// For backward compatibility
 
+                // Search if boxes def already present
                 $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."boxes_def";
                 $sql.= " WHERE file = '".$this->db->escape($file)."'";
                 $sql.= " AND entity = ".$conf->entity;
                 if ($note) $sql.=" AND note ='".$this->db->escape($note)."'";
 
+                dol_syslog(get_class($this)."::insert_boxes sql=".$sql);
                 $result=$this->db->query($sql);
                 if ($result)
                 {
@@ -533,9 +537,9 @@ abstract class DolibarrModules
 
                         if (! $err)
                         {
-                            $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file,entity,note)";
-                            $sql.= " VALUES ('".$this->db->escape($file)."',";
-                            $sql.= $conf->entity.",";
+                            $sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes_def (file, entity, note)";
+                            $sql.= " VALUES ('".$this->db->escape($file)."', ";
+                            $sql.= $conf->entity.", ";
                             $sql.= $note?"'".$this->db->escape($note)."'":"null";
                             $sql.= ")";
 
@@ -544,7 +548,7 @@ abstract class DolibarrModules
                             if (! $resql) $err++;
 
                         }
-                        if (! $err)
+                        if (! $err && ! preg_match('/newboxdefonly/',$options))
                         {
                             $lastid=$this->db->last_insert_id(MAIN_DB_PREFIX."boxes_def","rowid");
 
@@ -568,7 +572,7 @@ abstract class DolibarrModules
                             $this->db->commit();
                         }
                         else
-                        {
+						{
                             $this->error=$this->db->lasterror();
                             dol_syslog(get_class($this)."::insert_boxes ".$this->error, LOG_ERR);
                             $this->db->rollback();
@@ -609,7 +613,7 @@ abstract class DolibarrModules
                 //$note  = $this->boxes[$key][2];
 
                 if (empty($file)) $file  = isset($this->boxes[$key][1])?$this->boxes[$key][1]:'';	// For backward compatibility
-                
+
                 $sql = "DELETE FROM ".MAIN_DB_PREFIX."boxes";
                 $sql.= " USING ".MAIN_DB_PREFIX."boxes, ".MAIN_DB_PREFIX."boxes_def";
                 $sql.= " WHERE ".MAIN_DB_PREFIX."boxes.box_id = ".MAIN_DB_PREFIX."boxes_def.rowid";
@@ -898,7 +902,7 @@ abstract class DolibarrModules
                             break;
                         }
                         else dol_syslog(get_class($this)."::insert_permissions record already exists", LOG_INFO);
-                        
+
                     }
                     $this->db->free($resqlinsert);
 
diff --git a/htdocs/core/modules/modCommande.class.php b/htdocs/core/modules/modCommande.class.php
index 779cf75ea64..f9d552c286b 100644
--- a/htdocs/core/modules/modCommande.class.php
+++ b/htdocs/core/modules/modCommande.class.php
@@ -194,7 +194,7 @@ class modCommande extends DolibarrModules
 	 *		The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
 	 *		It also creates data directories
 	 *
-     *      @param      string	$options    Options when enabling module ('', 'noboxes')
+     *      @param      string	$options    Options when enabling module ('', 'newboxdefonly', 'noboxes')
 	 *      @return     int             	1 if OK, 0 if KO
 	 */
 	function init($options='')
@@ -236,7 +236,7 @@ class modCommande extends DolibarrModules
 	 *      Remove from database constants, boxes and permissions from Dolibarr database.
 	 *		Data directories are not deleted
 	 *
-     *      @param      string	$options    Options when enabling module ('', 'noboxes')
+     *      @param      string	$options    Options when enabling module ('', 'newboxdefonly', 'noboxes')
 	 *      @return     int             	1 if OK, 0 if KO
      */
     function remove($options='')
diff --git a/htdocs/core/modules/modFacture.class.php b/htdocs/core/modules/modFacture.class.php
index bb56366182d..08bd0e008ac 100644
--- a/htdocs/core/modules/modFacture.class.php
+++ b/htdocs/core/modules/modFacture.class.php
@@ -221,7 +221,7 @@ class modFacture extends DolibarrModules
 	 *		The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
 	 *		It also creates data directories
 	 *
-     *      @param      string	$options    Options when enabling module ('', 'noboxes')
+     *      @param      string	$options    Options when enabling module ('', 'newboxdefonly', 'noboxes')
 	 *      @return     int             	1 if OK, 0 if KO
 	 */
 	function init($options='')
@@ -262,7 +262,7 @@ class modFacture extends DolibarrModules
 	 *      Remove from database constants, boxes and permissions from Dolibarr database.
 	 *		Data directories are not deleted
 	 *
-     *      @param      string	$options    Options when enabling module ('', 'noboxes')
+     *      @param      string	$options    Options when enabling module ('', 'newboxdefonly', 'noboxes')
 	 *      @return     int             	1 if OK, 0 if KO
      */
     function remove($options='')
diff --git a/htdocs/core/modules/modProduct.class.php b/htdocs/core/modules/modProduct.class.php
index ecca09e136e..088c688a4fd 100644
--- a/htdocs/core/modules/modProduct.class.php
+++ b/htdocs/core/modules/modProduct.class.php
@@ -89,11 +89,12 @@ class modProduct extends DolibarrModules
 		$r++;
 
 		// Boxes
-		$this->boxes = array();
-		$this->boxes[0][1] = "box_produits.php";
-		$this->boxes[1][1] = "box_produits_alerte_stock.php";
-		$this->boxes[2][1] = "box_graph_product_distribution.php";
-		
+		$this->boxes = array(
+			0=>array('file'=>'box_produits.php','enabledbydefaulton'=>'Home'),
+			1=>array('file'=>'box_produits_alerte_stock.php','enabledbydefaulton'=>''),
+			2=>array('file'=>'box_graph_product_distribution.php','enabledbydefaulton'=>'Home')
+		);
+
 		// Permissions
 		$this->rights = array();
 		$this->rights_class = 'produit';
@@ -244,7 +245,7 @@ class modProduct extends DolibarrModules
 					'sp.remise_percent'=>'0'
 			);
 		}
-		
+
 		if (! empty($conf->global->PRODUIT_MULTIPRICES)) {
 		// Exports product multiprice
 		//--------
@@ -268,12 +269,12 @@ class modProduct extends DolibarrModules
 		$this->export_sql_end[$r]  =' FROM '.MAIN_DB_PREFIX.'product as p';
 		$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'product_price as pr ON p.rowid = pr.fk_product';
 		$this->export_sql_end[$r] .=' WHERE p.fk_product_type = 0 AND p.entity IN ('.getEntity("product", 1).')';
-		
-		
+
+
 		// Import product multiprice
 		//--------
 		$r=0;
-		
+
 		$r++;
 		$this->import_code[$r]=$this->rights_class.'_'.$r;
 		$this->import_label[$r]="ProductsMultiPrice";	// Translation key
@@ -304,7 +305,7 @@ class modProduct extends DolibarrModules
 	 *		The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
 	 *		It also creates data directories
 	 *
-     *      @param      string	$options    Options when enabling module ('', 'noboxes')
+     *      @param      string	$options    Options when enabling module ('', 'newboxdefonly', 'noboxes')
 	 *      @return     int             	1 if OK, 0 if KO
 	 */
 	function init($options='')
@@ -322,7 +323,7 @@ class modProduct extends DolibarrModules
 	 *      Remove from database constants, boxes and permissions from Dolibarr database.
 	 *		Data directories are not deleted
 	 *
-     *      @param      string	$options    Options when enabling module ('', 'noboxes')
+     *      @param      string	$options    Options when enabling module ('', 'newboxdefonly', 'noboxes')
 	 *      @return     int             	1 if OK, 0 if KO
      */
     function remove($options='')
diff --git a/htdocs/core/modules/modService.class.php b/htdocs/core/modules/modService.class.php
index 09e8116af41..eb472a25799 100644
--- a/htdocs/core/modules/modService.class.php
+++ b/htdocs/core/modules/modService.class.php
@@ -75,8 +75,10 @@ class modService extends DolibarrModules
 		$this->const = array();
 
 		// Boxes
-		$this->boxes = array();
-		$this->boxes[0][1] = "box_services_contracts.php";
+		$this->boxes = array(
+			0=>array('file'=>'box_services_contracts.php','enabledbydefaulton'=>'Home'),
+			1=>array('file'=>'box_graph_product_distribution.php','enabledbydefaulton'=>'Home')
+		);
 
 		// Permissions
 		$this->rights = array();
@@ -186,7 +188,7 @@ class modService extends DolibarrModules
 	 *		The init function add constants, boxes, permissions and menus (defined in constructor) into Dolibarr database.
 	 *		It also creates data directories
 	 *
-     *      @param      string	$options    Options when enabling module ('', 'noboxes')
+     *      @param      string	$options    Options when enabling module ('', 'newboxdefonly', 'noboxes')
 	 *      @return     int             	1 if OK, 0 if KO
 	 */
 	function init($options='')
@@ -204,7 +206,7 @@ class modService extends DolibarrModules
 	 *      Remove from database constants, boxes and permissions from Dolibarr database.
 	 *		Data directories are not deleted
 	 *
-     *      @param      string	$options    Options when enabling module ('', 'noboxes')
+     *      @param      string	$options    Options when enabling module ('', 'newboxdefonly', 'noboxes')
 	 *      @return     int             	1 if OK, 0 if KO
      */
     function remove($options='')
diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php
index 280d9c5b746..78c223a9807 100644
--- a/htdocs/install/upgrade2.php
+++ b/htdocs/install/upgrade2.php
@@ -331,8 +331,12 @@ if (! GETPOST("action") || preg_match('/upgrade/i',GETPOST('action')))
         	migrate_categorie_association($db,$langs,$conf);
         }
 
-        $afterversionarray=explode('.','3.3.9');
-        $beforeversionarray=explode('.','3.4.9');
+		// Script for VX (X<3.4) -> V3.4
+		// No specific scripts
+
+        // Tasks to do always and only into last targeted version
+        $afterversionarray=explode('.','3.4.9');	// target is after this
+        $beforeversionarray=explode('.','3.5.9');	// target is before this
         if (versioncompare($versiontoarray,$afterversionarray) >= 0 && versioncompare($versiontoarray,$beforeversionarray) <= 0)
         {
         	// Reload modules (this must be always and only into last targeted version)
@@ -3619,7 +3623,7 @@ function migrate_reload_modules($db,$langs,$conf)
         if ($res) {
             $mod=new modAgenda($db);
             $mod->remove('noboxes');
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_SOCIETE))
@@ -3629,7 +3633,7 @@ function migrate_reload_modules($db,$langs,$conf)
         if ($res) {
             $mod=new modSociete($db);
             $mod->remove('noboxes');
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_PRODUIT))    // Permission has changed into 2.7
@@ -3639,7 +3643,7 @@ function migrate_reload_modules($db,$langs,$conf)
         if ($res) {
             $mod=new modProduct($db);
             //$mod->remove('noboxes');
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_SERVICE))    // Permission has changed into 2.7
@@ -3649,7 +3653,7 @@ function migrate_reload_modules($db,$langs,$conf)
             $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modService.class.php';
             $mod=new modService($db);
             //$mod->remove('noboxes');
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_COMMANDE))   // Permission has changed into 2.9
@@ -3659,7 +3663,7 @@ function migrate_reload_modules($db,$langs,$conf)
             $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modCommande.class.php';
             $mod=new modCommande($db);
             //$mod->remove('noboxes');
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_FACTURE))    // Permission has changed into 2.9
@@ -3669,7 +3673,7 @@ function migrate_reload_modules($db,$langs,$conf)
             $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modFacture.class.php';
             $mod=new modFacture($db);
             //$mod->remove('noboxes');
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_FOURNISSEUR))    // Permission has changed into 2.9
@@ -3679,7 +3683,7 @@ function migrate_reload_modules($db,$langs,$conf)
         if ($res) {
             $mod=new modFournisseur($db);
             //$mod->remove('noboxes');
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
 
@@ -3690,7 +3694,7 @@ function migrate_reload_modules($db,$langs,$conf)
         if ($res) {
             $mod=new modUser($db);
             //$mod->remove('noboxes');  // We need to remove because id of module has changed
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_DEPLACEMENT))    // Permission has changed into 3.0
@@ -3700,7 +3704,7 @@ function migrate_reload_modules($db,$langs,$conf)
         if ($res) {
             $mod=new modDeplacement($db);
             //$mod->remove('noboxes');	// We need to remove because a permission id has been removed
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_DON))    // Permission has changed into 3.0
@@ -3710,7 +3714,7 @@ function migrate_reload_modules($db,$langs,$conf)
         if ($res) {
             $mod=new modDon($db);
             //$mod->remove('noboxes');	// We need to remove because a permission id has been removed
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_ECM))    // Permission has changed into 3.0 and 3.1
@@ -3720,7 +3724,7 @@ function migrate_reload_modules($db,$langs,$conf)
             $res=@include_once DOL_DOCUMENT_ROOT.'/core/modules/modECM.class.php';
             $mod=new modECM($db);
             $mod->remove('noboxes');	// We need to remove because a permission id has been removed
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
     if (! empty($conf->global->MAIN_MODULE_PAYBOX))    // Permission has changed into 3.0
@@ -3730,7 +3734,7 @@ function migrate_reload_modules($db,$langs,$conf)
         if ($res) {
             $mod=new modPaybox($db);
             $mod->remove('noboxes');  // We need to remove because id of module has changed
-            $mod->init('noboxes');
+            $mod->init('newboxdefonly');
         }
     }
 
-- 
GitLab