From cb0404a750c8b4d48865d9eef8bc6a3c67fb28af Mon Sep 17 00:00:00 2001
From: jfefe <jfefe@aternatik.fr>
Date: Wed, 20 Feb 2013 11:15:05 +0100
Subject: [PATCH] New webservice method to update contact

---
 htdocs/webservices/server_contact.php | 120 +++++++++++++++++++++++++-
 1 file changed, 119 insertions(+), 1 deletion(-)

diff --git a/htdocs/webservices/server_contact.php b/htdocs/webservices/server_contact.php
index dfb235354c3..7fad69f468f 100644
--- a/htdocs/webservices/server_contact.php
+++ b/htdocs/webservices/server_contact.php
@@ -211,7 +211,19 @@ $server->register(
 	'WS to get all contacts of a third party'
 );
 
-
+// Register WSDL
+$server->register(
+	'updateContact',
+	// Entry values
+	array('authentication'=>'tns:authentication','contact'=>'tns:contact'),
+	// Exit values
+	array('result'=>'tns:result','id'=>'xsd:string'),
+	$ns,
+	$ns.'#updateContact',
+	$styledoc,
+	$styleuse,
+	'WS to update a contact'
+);
 
 
 /**
@@ -581,6 +593,112 @@ function getContactsForThirdParty($authentication,$idthirdparty)
 	return $objectresp;
 }
 
+
+/**
+ * Update a contact
+ *
+ * @param	array		$authentication		Array of authentication information
+ * @param	Contact		$contact		    Contact
+ * @return	array							Array result
+ */
+function updateContact($authentication,$contact)
+{
+	global $db,$conf,$langs;
+
+	$now=dol_now();
+
+	dol_syslog("Function: updateContact login=".$authentication['login']);
+
+	if ($authentication['entity']) $conf->entity=$authentication['entity'];
+
+	// Init and check authentication
+	$objectresp=array();
+	$errorcode='';$errorlabel='';
+	$error=0;
+	$fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
+	// Check parameters
+	if (empty($contact['id']))	{
+		$error++; $errorcode='KO'; $errorlabel="Contact id is mandatory.";
+	}
+
+	if (! $error)
+	{
+		$objectfound=false;
+
+		include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
+
+		$object=new Contact($db);
+		$result=$object->fetch($contact['id']);
+
+		if (!empty($object->id)) {
+
+			$objectfound=true;
+				
+			
+			$object->firstname=$contact['firstname'];
+			$object->lastname=$contact['lastname'];
+			
+			$object->address=$contact['address'];
+			$object->zip=$contact['zip'];
+			$object->town=$contact['town'];
+
+			$object->country_id=$contact['country_id'];
+			if ($contact['country_code']) $object->country_id=getCountry($contact['country_code'],3);
+			$object->province_id=$contact['province_id'];
+		
+
+			$object->phone_perso=$contact['phone_perso'];
+			$object->phone_mobile=$contact['phone_mobile'];
+			$object->fax=$contact['fax'];
+			$object->email=$contact['email'];
+			
+
+			//Retreive all extrafield for contact
+			// fetch optionals attributes and labels
+			$extrafields=new ExtraFields($db);
+			$extralabels=$extrafields->fetch_name_optionals_label('contact',true);
+			foreach($extrafields->attribute_label as $key=>$label)
+			{
+				$key='options_'.$key;
+				$object->array_options[$key]=$contact[$key];
+			}
+
+			$db->begin();
+
+			$result=$object->update($contact['id'],$fuser);
+			if ($result <= 0) {
+				$error++;
+			}
+		}
+
+		if ((! $error) && ($objectfound))
+		{
+			$db->commit();
+			$objectresp=array(
+			'result'=>array('result_code'=>'OK', 'result_label'=>''),
+			'id'=>$object->id
+			);
+		}
+		elseif ($objectfound)
+		{
+			$db->rollback();
+			$error++;
+			$errorcode='KO';
+			$errorlabel=$object->error;
+		} else {
+			$error++;
+			$errorcode='NOT_FOUND';
+			$errorlabel='Contact id='.$contact['id'].' cannot be found';
+		}
+	}
+
+	if ($error)
+	{
+		$objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
+	}
+
+	return $objectresp;
+}
 // Return the results.
 $server->service($HTTP_RAW_POST_DATA);
 
-- 
GitLab