From 376759c13d6e7bc4be26fe2c8c4311159f466c65 Mon Sep 17 00:00:00 2001
From: Regis Houssin <regis@dolibarr.fr>
Date: Tue, 10 Jan 2012 06:57:44 +0800
Subject: [PATCH] Fix: refactoring elements sharing management between entities

---
 htdocs/core/ajax/constantonoff.php |  6 +++---
 htdocs/core/class/conf.class.php   | 17 ++++-------------
 htdocs/core/lib/admin.lib.php      |  2 +-
 htdocs/core/lib/ajax.lib.php       | 15 ++++++++++-----
 4 files changed, 18 insertions(+), 22 deletions(-)

diff --git a/htdocs/core/ajax/constantonoff.php b/htdocs/core/ajax/constantonoff.php
index c6aeab1ac61..1231b4ff9eb 100644
--- a/htdocs/core/ajax/constantonoff.php
+++ b/htdocs/core/ajax/constantonoff.php
@@ -1,5 +1,5 @@
 <?php
-/* Copyright (C) 2011 Regis Houssin  <regis@dolibarr.fr>
+/* Copyright (C) 2011-2012 Regis Houssin  <regis@dolibarr.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -51,11 +51,11 @@ if((isset($_GET['action']) && ! empty($_GET['action'])) && (isset($_GET['name'])
 	{
 		if ($_GET['action'] == 'set')
 		{
-			dolibarr_set_const($db, $_GET['name'], 1, 'chaine', 0, '', $conf->entity);
+			dolibarr_set_const($db, GETPOST('name','alpha'), 1, 'chaine', 0, '', GETPOST('entity','int'));
 		}
 		else if ($_GET['action'] == 'del')
 		{
-			dolibarr_del_const($db, $_GET['name'], $conf->entity);
+			dolibarr_del_const($db, GETPOST('name','alpha'), GETPOST('entity','int'));
 		}
 	}
 }
diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php
index bd254742731..c7083dcba99 100644
--- a/htdocs/core/class/conf.class.php
+++ b/htdocs/core/class/conf.class.php
@@ -191,28 +191,19 @@ class Conf
 							$this->modules[]=$module;
 						}
 					}
-					// Sharings between entities
-					else if ($value && preg_match('/^MULTICOMPANY_([A-Z_]+)_SHARING$/',$key,$reg))
-					{
-						$module=strtolower($reg[1]);
-						$multicompany_sharing[$module]=$value;
-					}
 				}
 				$i++;
 			}
 
-			// Sharings between entities
-			if (! empty($this->multicompany->enabled) && ! empty($multicompany_sharing))
+			// Load shared elements between entities
+			if (! empty($this->multicompany->enabled))
 			{
 				$ret = @dol_include_once('/multicompany/class/actions_multicompany.class.php');
 				if ($ret)
 				{
 					$mc = new ActionsMulticompany($db);
-
-					foreach($multicompany_sharing as $key => $value)
-					{
-						$this->entities[$key]=$mc->check_entity($value);
-					}
+					$this->multicompany->entity = $mc->loadEntity();
+					$this->entities = $mc->loadSharedElements();
 				}
 			}
 		}
diff --git a/htdocs/core/lib/admin.lib.php b/htdocs/core/lib/admin.lib.php
index 41efdc2ef3c..c735baee1de 100644
--- a/htdocs/core/lib/admin.lib.php
+++ b/htdocs/core/lib/admin.lib.php
@@ -463,7 +463,7 @@ function dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $not
 
     $sql = "DELETE FROM ".MAIN_DB_PREFIX."const";
     $sql.= " WHERE name = ".$db->encrypt($name,1);
-    if ($entity > 0) $sql.= " AND entity = ".$entity;
+    if ($entity >= 0) $sql.= " AND entity = ".$entity;
 
     dol_syslog("admin.lib::dolibarr_set_const sql=".$sql, LOG_DEBUG);
     $resql=$db->query($sql);
diff --git a/htdocs/core/lib/ajax.lib.php b/htdocs/core/lib/ajax.lib.php
index 42df2a82211..e01ae8ef04e 100644
--- a/htdocs/core/lib/ajax.lib.php
+++ b/htdocs/core/lib/ajax.lib.php
@@ -218,13 +218,16 @@ function ajax_combobox($htmlname)
 /**
  * 	On/off button for constant
  *
- * 	@param		code	Name of constant
- * 	@param		input	Input element
+ * 	@param	string	$code		Name of constant
+ * 	@param	array	$input		Input element
+ * 	@param	int		$entity		Entity to set
  * 	TODO add different method for other input (show/hide, disable, ..)
  */
-function ajax_constantonoff($code,$input=array())
+function ajax_constantonoff($code,$input=array(),$entity=false)
 {
 	global $conf, $langs;
+	
+	$entity = ((! empty($entity) && is_numeric($entity) && $entity > 0) || $entity == 0 ? $entity : $conf->entity);
 
 	$out= '<script type="text/javascript">
 		$(function() {
@@ -234,7 +237,8 @@ function ajax_constantonoff($code,$input=array())
 			$( "#set_'.$code.'" ).click(function() {
 				$.get( "'.DOL_URL_ROOT.'/core/ajax/constantonoff.php", {
 					action: \'set\',
-					name: \''.$code.'\'
+					name: \''.$code.'\',
+					entity: \''.$entity.'\'
 				},
 				function() {
 					$("#set_'.$code.'" ).hide();
@@ -256,7 +260,8 @@ function ajax_constantonoff($code,$input=array())
 			$( "#del_'.$code.'" ).click(function() {
 				$.get( "'.DOL_URL_ROOT.'/core/ajax/constantonoff.php", {
 					action: \'del\',
-					name: \''.$code.'\'
+					name: \''.$code.'\',
+					entity: \''.$entity.'\'
 				},
 				function() {
 					$("#del_'.$code.'" ).hide();
-- 
GitLab