From 85f0d6c3ea885feed77e856fead69258bf611277 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sun, 5 Oct 2014 04:51:34 +0200
Subject: [PATCH] Fix: Edition of event

---
 htdocs/comm/action/card.php                   | 97 ++++++++++---------
 htdocs/comm/action/class/actioncomm.class.php | 17 ++--
 2 files changed, 59 insertions(+), 55 deletions(-)

diff --git a/htdocs/comm/action/card.php b/htdocs/comm/action/card.php
index 74762b40c63..5f663d076ec 100644
--- a/htdocs/comm/action/card.php
+++ b/htdocs/comm/action/card.php
@@ -170,7 +170,7 @@ if ($action == 'add')
 	}
 
 	// Initialisation objet cactioncomm
-	if (! GETPOST('actioncode') > 0)
+	if (! GETPOST('actioncode') > 0)	// actioncode is id
 	{
 		$error++; $donotclearsession=1;
 		$action = 'create';
@@ -178,62 +178,63 @@ if ($action == 'add')
 	}
 	else
 	{
-		$result=$cactioncomm->fetch(GETPOST('actioncode'));
+		$object->type_code = GETPOST('actioncode');
 	}
 
-	// Initialisation objet actioncomm
-	$object->type_id = $cactioncomm->id;
-	$object->type_code = $cactioncomm->code;
-	$object->priority = GETPOST("priority")?GETPOST("priority"):0;
-	$object->fulldayevent = (! empty($fulldayevent)?1:0);
-	$object->location = GETPOST("location");
-	$object->label = trim(GETPOST('label'));
-	$object->fk_element = GETPOST("fk_element");
-	$object->elementtype = GETPOST("elementtype");
-	if (! GETPOST('label'))
+	if (! $error)
 	{
-		if (GETPOST('actioncode') == 'AC_RDV' && $contact->getFullName($langs))
-		{
-			$object->label = $langs->transnoentitiesnoconv("TaskRDVWith",$contact->getFullName($langs));
-		}
-		else
+		// Initialisation objet actioncomm
+		$object->priority = GETPOST("priority")?GETPOST("priority"):0;
+		$object->fulldayevent = (! empty($fulldayevent)?1:0);
+		$object->location = GETPOST("location");
+		$object->label = trim(GETPOST('label'));
+		$object->fk_element = GETPOST("fk_element");
+		$object->elementtype = GETPOST("elementtype");
+		if (! GETPOST('label'))
 		{
-			if ($langs->trans("Action".$object->type_code) != "Action".$object->type_code)
+			if (GETPOST('actioncode') == 'AC_RDV' && $contact->getFullName($langs))
 			{
-				$object->label = $langs->transnoentitiesnoconv("Action".$object->type_code)."\n";
+				$object->label = $langs->transnoentitiesnoconv("TaskRDVWith",$contact->getFullName($langs));
+			}
+			else
+			{
+				if ($langs->trans("Action".$object->type_code) != "Action".$object->type_code)
+				{
+					$object->label = $langs->transnoentitiesnoconv("Action".$object->type_code)."\n";
+				}
+				else $object->label = $cactioncomm->libelle;
 			}
-			else $object->label = $cactioncomm->libelle;
 		}
-	}
-	$object->fk_project = isset($_POST["projectid"])?$_POST["projectid"]:0;
-	$object->datep = $datep;
-	$object->datef = $datef;
-	$object->percentage = $percentage;
-	$object->duree=((float) (GETPOST('dureehour') * 60) + (float) GETPOST('dureemin')) * 60;
+		$object->fk_project = isset($_POST["projectid"])?$_POST["projectid"]:0;
+		$object->datep = $datep;
+		$object->datef = $datef;
+		$object->percentage = $percentage;
+		$object->duree=((float) (GETPOST('dureehour') * 60) + (float) GETPOST('dureemin')) * 60;
 
-	$listofuserid=array();
-	if (! empty($_SESSION['assignedtouser'])) $listofuserid=dol_json_decode($_SESSION['assignedtouser']);
-	$i=0;
-	foreach($listofuserid as $key => $value)
-	{
-		if ($i == 0)	// First entry
+		$listofuserid=array();
+		if (! empty($_SESSION['assignedtouser'])) $listofuserid=dol_json_decode($_SESSION['assignedtouser']);
+		$i=0;
+		foreach($listofuserid as $key => $value)
 		{
-			$usertodo=new User($db);
-			if ($value['id'] > 0)
+			if ($i == 0)	// First entry
 			{
-				$usertodo->fetch($value['id']);
-				$object->userownerid = $usertodo->id;
+				$usertodo=new User($db);
+				if ($value['id'] > 0)
+				{
+					$usertodo->fetch($value['id']);
+					$object->userownerid = $usertodo->id;
+				}
+				$object->usertodo = $usertodo;
+				$object->transparency = (GETPOST("transparency")=='on'?1:0);
 			}
-			$object->usertodo = $usertodo;
-			$object->transparency = (GETPOST("transparency")=='on'?1:0);
-		}
 
-		$object->userassigned[$value['id']]=array('id'=>$value['id'], 'transparency'=>(GETPOST("transparency")=='on'?1:0));
+			$object->userassigned[$value['id']]=array('id'=>$value['id'], 'transparency'=>(GETPOST("transparency")=='on'?1:0));
 
-		$i++;
+			$i++;
+		}
 	}
 
-	if (! empty($conf->global->AGENDA_ENABLE_DONEBY))
+	if (! $error && ! empty($conf->global->AGENDA_ENABLE_DONEBY))
 	{
 		$userdone=new User($db);
 		if ($_POST["doneby"] > 0)
@@ -250,10 +251,10 @@ if ($action == 'add')
 
 	if (GETPOST('socid','int') > 0)
 	{
-		$societe = new Societe($db);
-		$societe->fetch(GETPOST('socid','int'));
-		$object->societe = $societe;	// deprecated
-		$object->thirdparty = $societe;
+		$object->socid=GETPOST('socid','int');
+		$object->fetch_thirdparty();
+
+		$object->societe = $object->thirdparty;	// For backward compatibility
 	}
 
 	// Special for module webcal and phenix
@@ -607,6 +608,7 @@ if ($action == 'create')
 	print '<input type="hidden" name="action" value="add">';
 	print '<input type="hidden" name="donotclearsession" value="1">';
 	if ($backtopage) print '<input type="hidden" name="backtopage" value="'.($backtopage != '1' ? $backtopage : $_SERVER["HTTP_REFERER"]).'">';
+	if (empty($conf->global->AGENDA_USE_EVENT_TYPE)) print '<input type="hidden" name="actioncode" value="'.dol_getIdFromCode($db, 'AC_OTH', 'c_actioncomm').'">';
 
 	if (GETPOST("actioncode") == 'AC_RDV') print_fiche_titre($langs->trans("AddActionRendezVous"));
 	else print_fiche_titre($langs->trans("AddAnAction"));
@@ -620,7 +622,6 @@ if ($action == 'create')
 		$formactions->select_type_actions(GETPOST("actioncode")?GETPOST("actioncode"):$object->type_code, "actioncode","systemauto");
 		print '</td></tr>';
 	}
-	else print '<input type="hidden" name="actioncode" value="AC_OTH">';
 
 	// Title
 	print '<tr><td'.(empty($conf->global->AGENDA_USE_EVENT_TYPE)?' class="fieldrequired"':'').'>'.$langs->trans("Title").'</td><td><input type="text" id="label" name="label" size="60" value="'.GETPOST('label').'"></td></tr>';
@@ -864,6 +865,7 @@ if ($id > 0)
 		print '<input type="hidden" name="id" value="'.$id.'">';
 		print '<input type="hidden" name="ref_ext" value="'.$object->ref_ext.'">';
 		if ($backtopage) print '<input type="hidden" name="backtopage" value="'.($backtopage != '1'? $backtopage : $_SERVER["HTTP_REFERER"]).'">';
+		if (empty($conf->global->AGENDA_USE_EVENT_TYPE)) print '<input type="hidden" name="actioncode" value="'.$object->type_code.'">';
 
 		dol_fiche_head($head, 'card', $langs->trans("Action"),0,'action');
 
@@ -879,7 +881,6 @@ if ($id > 0)
 			$formactions->select_type_actions(GETPOST("actioncode")?GETPOST("actioncode"):$object->type_code, "actioncode","systemauto");
 			print '</td></tr>';
 		}
-		else print '<input type="hidden" name="actioncode" value="'.$object->type_code.'">';
 
 		// Title
 		print '<tr><td'.(empty($conf->global->AGENDA_USE_EVENT_TYPE)?' class="fieldrequired"':'').'>'.$langs->trans("Title").'</td><td colspan="3"><input type="text" name="label" size="50" value="'.$object->label.'"></td></tr>';
diff --git a/htdocs/comm/action/class/actioncomm.class.php b/htdocs/comm/action/class/actioncomm.class.php
index 692ce2058d3..1d99a8eec34 100644
--- a/htdocs/comm/action/class/actioncomm.class.php
+++ b/htdocs/comm/action/class/actioncomm.class.php
@@ -108,7 +108,8 @@ class ActionComm extends CommonObject
     }
 
     /**
-     *    Add an action/event into database
+     *    Add an action/event into database.
+     *    $this->type_id OR $this->type_code must be set.
      *
      *    @param	User	$user      		Object user making action
      *    @param    int		$notrigger		1 = disable triggers, 0 = enable triggers
@@ -144,24 +145,26 @@ class ActionComm extends CommonObject
         $userownerid=isset($this->usertodo->id)?$this->usertodo->id:$this->userownerid;		// For backward compatibility
         $userdoneid=isset($this->userdone->id)?$this->userdone->id:$this->userdoneid;		// For backward compatibility
 
-        if (! $this->type_id && $this->type_code)
+        if (! $this->type_id || ! $this->type_code)
         {
+        	$key=empty($this->type_id)?$this->type_code:$this->type_id;
+
             // Get id from code
             $cactioncomm=new CActionComm($this->db);
-            $result=$cactioncomm->fetch($this->type_code);
+            $result=$cactioncomm->fetch($key);
 
             if ($result > 0)
             {
                 $this->type_id=$cactioncomm->id;
-                $this->code=$cactioncomm->code;
+                $this->type_code=$cactioncomm->code;
             }
             else if ($result == 0)
             {
-                $this->error='Failed to get record with code '.$this->type_code.' from dictionary "type of events"';
+                $this->error='Failed to get record with id '.$this->type_id.' code '.$this->type_code.' from dictionary "type of events"';
                 return -1;
             }
             else
-            {
+			{
                 $this->error=$cactioncomm->error;
                 return -1;
             }
@@ -201,7 +204,7 @@ class ActionComm extends CommonObject
         $sql.= (strval($this->datef)!=''?"'".$this->db->idate($this->datef)."'":"null").",";
         $sql.= (isset($this->durationp) && $this->durationp >= 0 && $this->durationp != ''?"'".$this->durationp."'":"null").",";	// deprecated
         $sql.= (isset($this->type_id)?$this->type_id:"null").",";
-        $sql.= (isset($this->code)?" '".$this->code."'":"null").",";
+        $sql.= (isset($this->type_code)?" '".$this->type_code."'":"null").",";
         $sql.= (isset($this->socid) && $this->socid > 0?" '".$this->socid."'":"null").",";
         $sql.= (isset($this->fk_project) && $this->fk_project > 0?" '".$this->fk_project."'":"null").",";
         $sql.= " '".$this->db->escape($this->note)."',";
-- 
GitLab