From 3d7752ca761b766f92b5c24e5c82246bb28dfe0a Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@users.sourceforge.net>
Date: Wed, 25 May 2011 22:49:11 +0000
Subject: [PATCH] Qual: Simplify canvas code

---
 htdocs/core/class/canvas.class.php     | 45 +++++++++++---------------
 htdocs/societe/class/societe.class.php | 11 ++++---
 htdocs/societe/soc.php                 |  2 ++
 3 files changed, 26 insertions(+), 32 deletions(-)

diff --git a/htdocs/core/class/canvas.class.php b/htdocs/core/class/canvas.class.php
index 1721a6a28a9..182d58d08e3 100644
--- a/htdocs/core/class/canvas.class.php
+++ b/htdocs/core/class/canvas.class.php
@@ -72,42 +72,33 @@ class Canvas
 
 
 	/**
-	 * 	Initialize properties like ->control, ->control->object, ->template_dir
-	 * 	@param		module		Name of target module (thirdparty, ...)
-	 * 	@param		card	 	Type of card (ex: card, info, ...)
-	 * 	@param		canvas		Name of canvas (ex: mycanvas@mymodule)
+	 * 	Initialize properties: ->targetmodule, ->card, ->canvas
+	 *  and MVC properties:    ->control (Controller), ->control->object (Model), ->template_dir (View)
+	 * 	@param		module		Name of target module (thirdparty, contact, ...)
+	 * 	@param		card	 	Type of card (ex: card, info, contactcard, ...)
+	 * 	@param		canvas		Name of canvas (ex: mycanvas, default, or mycanvas@myexternalmodule)
 	 */
-	function getCanvas($module,$card,$canvas)
+	function getCanvas($module, $card, $canvas)
 	{
 		global $conf, $langs;
 
 		$error='';
-		$this->card = $card;
 
-		// Define this->canvas, this->targetmodule, this->aliasmodule, targetmodule, childmodule
+		// Set properties with value specific to dolibarr core: this->targetmodule, this->card, this->canvas
+        $this->targetmodule = $module;
+        $this->card = $card;
         $this->canvas = $canvas;
-		$this->targetmodule = $this->aliasmodule = $module;
+        $dirmodule = $module;
+        // Correct values if canvas is into an external module
 		if (preg_match('/^([^@]+)@([^@]+)$/i',$canvas,$regs))
 		{
             $this->canvas = $regs[1];
-		    $this->aliasmodule = $regs[2];
+		    $dirmodule = $regs[2];
 		}
-        $targetmodule = $this->targetmodule;
-        $childmodule = $this->aliasmodule;
 		// For compatibility
-        if ($targetmodule == 'thirdparty') { $targetmodule = 'societe'; }
-        if ($childmodule == 'thirdparty')  { $childmodule = 'societe'; $this->aliasmodule = 'societe'; }
-        if ($targetmodule == 'contact')    { $targetmodule = 'societe'; }
-        if ($childmodule == 'contact')     { $childmodule = 'societe'; }
+        if ($dirmodule == 'thirdparty') { $dirmodule = 'societe'; }
 
-		/*print 'canvas='.$this->canvas.'<br>';
-		print 'childmodule='.$childmodule.' targetmodule='.$targetmodule.'<br>';
-		print 'this->aliasmodule='.$this->aliasmodule.' this->targetmodule='.$this->targetmodule.'<br>';
-		print 'childmodule='.$conf->$childmodule->enabled.' targetmodule='.$conf->$targetmodule->enabled.'<br>';*/
-
-		if (! $conf->$childmodule->enabled || ! $conf->$targetmodule->enabled) accessforbidden();
-
-		$controlclassfile = dol_buildpath('/'.$this->aliasmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
+		$controlclassfile = dol_buildpath('/'.$dirmodule.'/canvas/'.$this->canvas.'/actions_'.$this->card.'_'.$this->canvas.'.class.php');
 		if (file_exists($controlclassfile))
 		{
             // Include actions class (controller)
@@ -119,7 +110,7 @@ class Canvas
 		}
 
 		// TODO Dao should be declared and used by controller or templates when required only
-        $modelclassfile = dol_buildpath('/'.$this->aliasmodule.'/canvas/'.$this->canvas.'/dao_'.$this->targetmodule.'_'.$this->canvas.'.class.php');
+        $modelclassfile = dol_buildpath('/'.$dirmodule.'/canvas/'.$this->canvas.'/dao_'.$this->targetmodule.'_'.$this->canvas.'.class.php');
         if (file_exists($modelclassfile))
         {
             // Include dataservice class (model)
@@ -132,16 +123,16 @@ class Canvas
 
 		// Include specific library
 		// TODO Specific libraries must be included by files that need them only, so by actions and/or dao files.
-		$libfile = dol_buildpath('/'.$this->aliasmodule.'/lib/'.$this->aliasmodule.'.lib.php');
+		$libfile = dol_buildpath('/'.$dirmodule.'/lib/'.$dirmodule.'.lib.php');
 		if (file_exists($libfile)) require_once($libfile);
 
 		// Template dir
-		$this->template_dir = dol_buildpath('/'.$this->aliasmodule.'/canvas/'.$this->canvas.'/tpl/');
+		$this->template_dir = dol_buildpath('/'.$dirmodule.'/canvas/'.$this->canvas.'/tpl/');
         if (! is_dir($this->template_dir))
         {
             $this->template_dir='';
         }
-        //print '/'.$this->aliasmodule.'/canvas/'.$this->canvas.'/tpl/';
+        //print '/'.$dirmodule.'/canvas/'.$this->canvas.'/tpl/';
         //print 'template_dir='.$this->template_dir.'<br>';
 
 		return 1;
diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php
index d23b7c936ec..82fd0e358e5 100644
--- a/htdocs/societe/class/societe.class.php
+++ b/htdocs/societe/class/societe.class.php
@@ -173,9 +173,12 @@ class Societe extends CommonObject
         global $langs,$conf;
 
         // Clean parameters
+        if (empty($this->status)) $this->status=0;
         $this->name=$this->name?trim($this->name):trim($this->nom);
         if (! empty($conf->global->MAIN_FIRST_TO_UPPER)) $this->name=ucwords($this->name);
-        $this->nom=$this->name;     // For backward compatibility
+        $this->nom=$this->name; // For backward compatibility
+        if (empty($this->client))      $this->client=0;
+        if (empty($this->fournisseur)) $this->fournisseur=0;
 
         dol_syslog("Societe::create ".$this->name);
 
@@ -186,8 +189,8 @@ class Societe extends CommonObject
             $this->error = $langs->trans("ErrorBadEMail",$this->email);
             return -1;
         }
-        if (empty($this->client)) $this->client=0;
-        if (empty($this->fournisseur)) $this->fournisseur=0;
+
+        $now=dol_now();
 
         $this->db->begin();
 
@@ -195,8 +198,6 @@ class Societe extends CommonObject
         if ($this->code_client == -1)      $this->get_codeclient($this->prefix_comm,0);
         if ($this->code_fournisseur == -1) $this->get_codefournisseur($this->prefix_comm,1);
 
-        $now=dol_now();
-
         // Check more parameters
         $result = $this->verify();
 
diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php
index 5e043106b72..bc3765a61ef 100644
--- a/htdocs/societe/soc.php
+++ b/htdocs/societe/soc.php
@@ -56,6 +56,8 @@ $soc = new Societe($db);
 // Get object canvas (By default, this is not defined, so standard usage of dolibarr)
 if (!empty($socid)) $soc->getCanvas($socid);
 $canvas = (!empty($soc->canvas)?$soc->canvas:GETPOST("canvas"));
+
+
 if (! empty($canvas))
 {
 	require_once(DOL_DOCUMENT_ROOT."/core/class/canvas.class.php");
-- 
GitLab