server_contact.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 JF FERRY <jfefe@aternatik.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/webservices/server_contact.php
  20. * \brief File that is entry point to call Dolibarr WebServices
  21. */
  22. if (! defined("NOCSRFCHECK")) define("NOCSRFCHECK",'1');
  23. require "../master.inc.php";
  24. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  25. require_once DOL_DOCUMENT_ROOT."/core/lib/ws.lib.php";
  26. require_once DOL_DOCUMENT_ROOT."/contact/class/contact.class.php";
  27. require_once DOL_DOCUMENT_ROOT."/core/class/extrafields.class.php";
  28. dol_syslog("Call Contact webservices interfaces");
  29. // Enable and test if module web services is enabled
  30. if (empty($conf->global->MAIN_MODULE_WEBSERVICES))
  31. {
  32. $langs->load("admin");
  33. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  34. print $langs->trans("WarningModuleNotActive",'WebServices').'.<br><br>';
  35. print $langs->trans("ToActivateModule");
  36. exit;
  37. }
  38. // Create the soap Object
  39. $server = new nusoap_server();
  40. $server->soap_defencoding='UTF-8';
  41. $server->decode_utf8=false;
  42. $ns='http://www.dolibarr.org/ns/';
  43. $server->configureWSDL('WebServicesDolibarrContact',$ns);
  44. $server->wsdl->schemaTargetNamespace=$ns;
  45. // Define WSDL Authentication object
  46. $server->wsdl->addComplexType(
  47. 'authentication',
  48. 'complexType',
  49. 'struct',
  50. 'all',
  51. '',
  52. array(
  53. 'dolibarrkey' => array('name'=>'dolibarrkey','type'=>'xsd:string'),
  54. 'sourceapplication' => array('name'=>'sourceapplication','type'=>'xsd:string'),
  55. 'login' => array('name'=>'login','type'=>'xsd:string'),
  56. 'password' => array('name'=>'password','type'=>'xsd:string'),
  57. 'entity' => array('name'=>'entity','type'=>'xsd:string'),
  58. )
  59. );
  60. // Define WSDL Return object
  61. $server->wsdl->addComplexType(
  62. 'result',
  63. 'complexType',
  64. 'struct',
  65. 'all',
  66. '',
  67. array(
  68. 'result_code' => array('name'=>'result_code','type'=>'xsd:string'),
  69. 'result_label' => array('name'=>'result_label','type'=>'xsd:string'),
  70. )
  71. );
  72. $contact_fields = array(
  73. 'id' => array('name'=>'id','type'=>'xsd:string'),
  74. 'ref_ext' => array('name'=>'ref_ext','type'=>'xsd:string'),
  75. 'lastname' => array('name'=>'lastname','type'=>'xsd:string'),
  76. 'firstname' => array('name'=>'firstname','type'=>'xsd:string'),
  77. 'address' => array('name'=>'address','type'=>'xsd:string'),
  78. 'zip' => array('name'=>'zip','type'=>'xsd:string'),
  79. 'town' => array('name'=>'town','type'=>'xsd:string'),
  80. 'state_id' => array('name'=>'state_id','type'=>'xsd:string'),
  81. 'state_code' => array('name'=>'state_code','type'=>'xsd:string'),
  82. 'state' => array('name'=>'state','type'=>'xsd:string'),
  83. 'country_id' => array('name'=>'country_id','type'=>'xsd:string'),
  84. 'country_code' => array('name'=>'country_code','type'=>'xsd:string'),
  85. 'country' => array('name'=>'country','type'=>'xsd:string'),
  86. 'socid' => array('name'=>'socid','type'=>'xsd:string'),
  87. 'status' => array('name'=>'status','type'=>'xsd:string'),
  88. 'phone_pro' => array('name'=>'phone_pro','type'=>'xsd:string'),
  89. 'fax' => array('name'=>'fax','type'=>'xsd:string'),
  90. 'phone_perso' => array('name'=>'phone_perso','type'=>'xsd:string'),
  91. 'phone_mobile' => array('name'=>'phone_mobile','type'=>'xsd:string'),
  92. 'code' => array('name'=>'code','type'=>'xsd:string'),
  93. 'email' => array('name'=>'email','type'=>'xsd:string'),
  94. 'birthday' => array('name'=>'birthday','type'=>'xsd:string'),
  95. 'default_lang' => array('name'=>'default_lang','type'=>'xsd:string'),
  96. 'note' => array('name'=>'note','type'=>'xsd:string'),
  97. 'no_email' => array('name'=>'no_email','type'=>'xsd:string'),
  98. 'ref_facturation' => array('name'=>'ref_facturation','type'=>'xsd:string'),
  99. 'ref_contrat' => array('name'=>'ref_contrat','type'=>'xsd:string'),
  100. 'ref_commande' => array('name'=>'ref_commande','type'=>'xsd:string'),
  101. 'ref_propal' => array('name'=>'ref_propal','type'=>'xsd:string'),
  102. 'user_id' => array('name'=>'user_id','type'=>'xsd:string'),
  103. 'user_login' => array('name'=>'user_login','type'=>'xsd:string'),
  104. 'civility_id' => array('name'=>'civility_id','type'=>'xsd:string'),
  105. 'poste' => array('name'=>'poste','type'=>'xsd:string')
  106. //...
  107. );
  108. //Retreive all extrafield for contact
  109. // fetch optionals attributes and labels
  110. $extrafields=new ExtraFields($db);
  111. $extralabels=$extrafields->fetch_name_optionals_label('socpeople',true);
  112. $extrafield_array=null;
  113. if (is_array($extrafields) && count($extrafields)>0) {
  114. $extrafield_array = array();
  115. }
  116. foreach($extrafields->attribute_label as $key=>$label)
  117. {
  118. $type =$extrafields->attribute_type[$key];
  119. if ($type=='date' || $type=='datetime') {$type='xsd:dateTime';}
  120. else {$type='xsd:string';}
  121. $extrafield_array['options_'.$key]=array('name'=>'options_'.$key,'type'=>$type);
  122. }
  123. if (is_array($extrafield_array)) $contact_fields=array_merge($contact_fields,$extrafield_array);
  124. // Define other specific objects
  125. $server->wsdl->addComplexType(
  126. 'contact',
  127. 'complexType',
  128. 'struct',
  129. 'all',
  130. '',
  131. $contact_fields
  132. );
  133. $server->wsdl->addComplexType(
  134. 'ContactsArray2',
  135. 'complexType',
  136. 'array',
  137. 'sequence',
  138. '',
  139. array(
  140. 'contact' => array(
  141. 'name' => 'contact',
  142. 'type' => 'tns:contact',
  143. 'minOccurs' => '0',
  144. 'maxOccurs' => 'unbounded'
  145. )
  146. )
  147. );
  148. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  149. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  150. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  151. $styledoc='rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  152. $styleuse='encoded'; // encoded/literal/literal wrapped
  153. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  154. // Register WSDL
  155. $server->register(
  156. 'getContact',
  157. // Entry values
  158. array('authentication'=>'tns:authentication','id'=>'xsd:string','ref_ext'=>'xsd:string'),
  159. // Exit values
  160. array('result'=>'tns:result','contact'=>'tns:contact'),
  161. $ns,
  162. $ns.'#getContact',
  163. $styledoc,
  164. $styleuse,
  165. 'WS to get a contact'
  166. );
  167. // Register WSDL
  168. $server->register(
  169. 'createContact',
  170. // Entry values
  171. array('authentication'=>'tns:authentication','contact'=>'tns:contact'),
  172. // Exit values
  173. array('result'=>'tns:result','id'=>'xsd:string'),
  174. $ns,
  175. $ns.'#createContact',
  176. $styledoc,
  177. $styleuse,
  178. 'WS to create a contact'
  179. );
  180. $server->register(
  181. 'getContactsForThirdParty',
  182. // Entry values
  183. array('authentication'=>'tns:authentication','idthirdparty'=>'xsd:string'),
  184. // Exit values
  185. array('result'=>'tns:result','contacts'=>'tns:ContactsArray2'),
  186. $ns,
  187. $ns.'#getContactsForThirdParty',
  188. $styledoc,
  189. $styleuse,
  190. 'WS to get all contacts of a third party'
  191. );
  192. // Register WSDL
  193. $server->register(
  194. 'updateContact',
  195. // Entry values
  196. array('authentication'=>'tns:authentication','contact'=>'tns:contact'),
  197. // Exit values
  198. array('result'=>'tns:result','id'=>'xsd:string'),
  199. $ns,
  200. $ns.'#updateContact',
  201. $styledoc,
  202. $styleuse,
  203. 'WS to update a contact'
  204. );
  205. /**
  206. * Get Contact
  207. *
  208. * @param array $authentication Array of authentication information
  209. * @param int $id Id of object
  210. * @param string $ref_ext Ref external of object
  211. * @return mixed
  212. */
  213. function getContact($authentication,$id,$ref_ext)
  214. {
  215. global $db,$conf,$langs;
  216. dol_syslog("Function: getContact login=".$authentication['login']." id=".$id." ref_ext=".$ref_ext);
  217. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  218. // Init and check authentication
  219. $objectresp=array();
  220. $errorcode='';$errorlabel='';
  221. $error=0;
  222. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  223. // Check parameters
  224. if (! $error && ($id && $ref_ext))
  225. {
  226. $error++;
  227. $errorcode='BAD_PARAMETERS'; $errorlabel="Parameter id and ref_ext can't be both provided. You must choose one or other but not both.";
  228. }
  229. if (! $error)
  230. {
  231. $fuser->getrights();
  232. $contact=new Contact($db);
  233. $result=$contact->fetch($id,0,$ref_ext);
  234. if ($result > 0)
  235. {
  236. // Only internal user who have contact read permission
  237. // Or for external user who have contact read permission, with restrict on societe_id
  238. if (
  239. $fuser->rights->societe->contact->lire && !$fuser->societe_id
  240. || ( $fuser->rights->societe->contact->lire && ($fuser->societe_id == $contact->socid))
  241. ){
  242. $contact_result_fields =array(
  243. 'id' => $contact->id,
  244. 'ref_ext' => $contact->ref_ext,
  245. 'lastname' => $contact->lastname,
  246. 'firstname' => $contact->firstname,
  247. 'address' => $contact->address,
  248. 'zip' => $contact->zip,
  249. 'town' => $contact->town,
  250. 'state_id' => $contact->state_id,
  251. 'state_code' => $contact->state_code,
  252. 'state' => $contact->state,
  253. 'country_id' => $contact->country_id,
  254. 'country_code' => $contact->country_code,
  255. 'country' => $contact->country,
  256. 'socid' => $contact->socid,
  257. 'status' => $contact->statut,
  258. 'phone_pro' => $contact->phone_pro,
  259. 'fax' => $contact->fax,
  260. 'phone_perso' => $contact->phone_perso,
  261. 'phone_mobile' => $contact->phone_mobile,
  262. 'code' => $contact->code,
  263. 'email' => $contact->email,
  264. 'birthday' => $contact->birthday,
  265. 'default_lang' => $contact->default_lang,
  266. 'note' => $contact->note,
  267. 'no_email' => $contact->no_email,
  268. 'ref_facturation' => $contact->ref_facturation,
  269. 'ref_contrat' => $contact->ref_contrat,
  270. 'ref_commande' => $contact->ref_commande,
  271. 'ref_propal' => $contact->ref_propal,
  272. 'user_id' => $contact->user_id,
  273. 'user_login' => $contact->user_login,
  274. 'civility_id' => $contact->civility_id,
  275. 'poste' => $contact->poste
  276. );
  277. //Retreive all extrafield for thirdsparty
  278. // fetch optionals attributes and labels
  279. $extrafields=new ExtraFields($db);
  280. $extralabels=$extrafields->fetch_name_optionals_label('socpeople',true);
  281. //Get extrafield values
  282. $contact->fetch_optionals();
  283. foreach($extrafields->attribute_label as $key=>$label)
  284. {
  285. $contact_result_fields=array_merge($contact_result_fields,array('options_'.$key => $contact->array_options['options_'.$key]));
  286. }
  287. // Create
  288. $objectresp = array(
  289. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  290. 'contact'=>$contact_result_fields
  291. );
  292. }
  293. else
  294. {
  295. $error++;
  296. $errorcode='PERMISSION_DENIED'; $errorlabel='User does not have permission for this request';
  297. }
  298. }
  299. else
  300. {
  301. $error++;
  302. $errorcode='NOT_FOUND'; $errorlabel='Object not found for id='.$id.' nor ref_ext='.$ref_ext;
  303. }
  304. }
  305. if ($error)
  306. {
  307. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  308. }
  309. return $objectresp;
  310. }
  311. /**
  312. * Create Contact
  313. *
  314. * @param array $authentication Array of authentication information
  315. * @param Contact $contact $contact
  316. * @return array Array result
  317. */
  318. function createContact($authentication,$contact)
  319. {
  320. global $db,$conf,$langs;
  321. $now=dol_now();
  322. dol_syslog("Function: createContact login=".$authentication['login']);
  323. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  324. // Init and check authentication
  325. $objectresp=array();
  326. $errorcode='';$errorlabel='';
  327. $error=0;
  328. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  329. // Check parameters
  330. if (empty($contact['lastname']))
  331. {
  332. $error++; $errorcode='KO'; $errorlabel="Name is mandatory.";
  333. }
  334. if (! $error)
  335. {
  336. $newobject=new Contact($db);
  337. $newobject->id=$contact['id'];
  338. $newobject->ref_ext=$contact['ref_ext'];
  339. $newobject->civility_id=$contact['civility_id'];
  340. $newobject->lastname=$contact['lastname'];
  341. $newobject->firstname=$contact['firstname'];
  342. $newobject->address=$contact['address'];
  343. $newobject->zip=$contact['zip'];
  344. $newobject->town=$contact['town'];
  345. $newobject->state_id=$contact['state_id'];
  346. $newobject->state_code=$contact['state_code'];
  347. $newobject->state=$contact['state'];
  348. $newobject->country_id=$contact['country_id'];
  349. $newobject->country_code=$contact['country_code'];
  350. $newobject->country=$contact['country'];
  351. $newobject->socid=$contact['socid'];
  352. $newobject->statut=$contact['status'];
  353. $newobject->phone_pro=$contact['phone_pro'];
  354. $newobject->fax=$contact['fax'];
  355. $newobject->phone_perso=$contact['phone_perso'];
  356. $newobject->phone_mobile=$contact['phone_mobile'];
  357. $newobject->code=$contact['code'];
  358. $newobject->email=$contact['email'];
  359. $newobject->birthday=$contact['birthday'];
  360. $newobject->default_lang=$contact['default_lang'];
  361. $newobject->note=$contact['note'];
  362. $newobject->no_email=$contact['no_email'];
  363. $newobject->ref_facturation=$contact['ref_facturation'];
  364. $newobject->ref_contrat=$contact['ref_contrat'];
  365. $newobject->ref_commande=$contact['ref_commande'];
  366. $newobject->ref_propal=$contact['ref_propal'];
  367. $newobject->user_id=$contact['user_id'];
  368. $newobject->user_login=$contact['user_login'];
  369. $newobject->poste=$contact['poste'];
  370. //Retreive all extrafield for thirdsparty
  371. // fetch optionals attributes and labels
  372. $extrafields=new ExtraFields($db);
  373. $extralabels=$extrafields->fetch_name_optionals_label('socpeople',true);
  374. foreach($extrafields->attribute_label as $key=>$label)
  375. {
  376. $key='options_'.$key;
  377. $newobject->array_options[$key]=$contact[$key];
  378. }
  379. //...
  380. $db->begin();
  381. $result=$newobject->create($fuser);
  382. if ($result <= 0)
  383. {
  384. $error++;
  385. }
  386. if (! $error)
  387. {
  388. $db->commit();
  389. $objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>''),'id'=>$newobject->id,'ref'=>$newobject->ref);
  390. }
  391. else
  392. {
  393. $db->rollback();
  394. $error++;
  395. $errorcode='KO';
  396. $errorlabel=$newobject->error;
  397. }
  398. }
  399. if ($error)
  400. {
  401. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  402. }
  403. return $objectresp;
  404. }
  405. /**
  406. * Get list of contacts for third party
  407. *
  408. * @param array $authentication Array of authentication information
  409. * @param int $idthirdparty Id thirdparty
  410. * @return array Array result
  411. */
  412. function getContactsForThirdParty($authentication,$idthirdparty)
  413. {
  414. global $db,$conf,$langs;
  415. dol_syslog("Function: getContactsForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
  416. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  417. // Init and check authentication
  418. $objectresp=array();
  419. $errorcode='';$errorlabel='';
  420. $error=0;
  421. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  422. // Check parameters
  423. if (! $error && empty($idthirdparty))
  424. {
  425. $error++;
  426. $errorcode='BAD_PARAMETERS'; $errorlabel='Parameter id is not provided';
  427. }
  428. if (! $error)
  429. {
  430. $linesinvoice=array();
  431. $sql = "SELECT c.rowid, c.fk_soc, c.civility as civility_id, c.lastname, c.firstname, c.statut as status,";
  432. $sql.= " c.address, c.zip, c.town,";
  433. $sql.= " c.fk_pays as country_id,";
  434. $sql.= " c.fk_departement,";
  435. $sql.= " c.birthday,";
  436. $sql.= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email, c.jabberid,";
  437. //$sql.= " c.priv, c.note, c.default_lang, c.no_email, c.canvas,";
  438. $sql.= " co.label as country, co.code as country_code,";
  439. $sql.= " d.nom as state, d.code_departement as state_code,";
  440. $sql.= " u.rowid as user_id, u.login as user_login,";
  441. $sql.= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
  442. $sql.= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  443. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
  444. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
  445. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
  446. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
  447. $sql.= " WHERE c.fk_soc=$idthirdparty";
  448. $resql=$db->query($sql);
  449. if ($resql)
  450. {
  451. $num=$db->num_rows($resql);
  452. $i=0;
  453. while ($i < $num)
  454. {
  455. // En attendant remplissage par boucle
  456. $obj=$db->fetch_object($resql);
  457. $contact=new Contact($db);
  458. $contact->fetch($obj->rowid);
  459. // Now define invoice
  460. $linescontact[]=array(
  461. 'id' => $contact->id,
  462. 'ref' => $contact->ref,
  463. 'civility_id' => $contact->civility_id?$contact->civility_id:'',
  464. 'lastname' => $contact->lastname?$contact->lastname:'',
  465. 'firstname' => $contact->firstname?$contact->firstname:'',
  466. 'address' => $contact->address?$contact->address:'',
  467. 'zip' => $contact->zip?$contact->zip:'',
  468. 'town' => $contact->town?$contact->town:'',
  469. 'state_id' => $contact->state_id?$contact->state_id:'',
  470. 'state_code' => $contact->state_code?$contact->state_code:'',
  471. 'state' => $contact->state?$contact->state:'',
  472. 'country_id' => $contact->country_id?$contact->country_id:'',
  473. 'country_code' => $contact->country_code?$contact->country_code:'',
  474. 'country' => $contact->country?$contact->country:'',
  475. 'socid' => $contact->socid?$contact->socid:'',
  476. 'socname' => $contact->socname?$contact->socname:'',
  477. 'poste' => $contact->poste?$contact->poste:'',
  478. 'phone_pro' => $contact->phone_pro?$contact->phone_pro:'',
  479. 'fax' => $contact->fax?$contact->fax:'',
  480. 'phone_perso' => $contact->phone_perso?$contact->phone_perso:'',
  481. 'phone_mobile' => $contact->phone_mobile?$contact->phone_mobile:'',
  482. 'email' => $contact->email?$contact->email:'',
  483. 'jabberid' => $contact->jabberid?$contact->jabberid:'',
  484. 'priv' => $contact->priv?$contact->priv:'',
  485. 'mail' => $contact->mail?$contact->mail:'',
  486. 'birthday' => $contact->birthday?$contact->birthday:'',
  487. 'default_lang' => $contact->default_lang?$contact->default_lang:'',
  488. 'note' => $contact->note?$contact->note:'',
  489. 'no_email' => $contact->no_email?$contact->no_email:'',
  490. 'ref_facturation' => $contact->ref_facturation?$contact->ref_facturation:'',
  491. 'ref_contrat' => $contact->ref_contrat?$contact->ref_contrat:'',
  492. 'ref_commande' => $contact->ref_commande?$contact->ref_commande:'',
  493. 'ref_propal' => $contact->ref_propal?$contact->ref_propal:'',
  494. 'user_id' => $contact->user_id?$contact->user_id:'',
  495. 'user_login' => $contact->user_login?$contact->user_login:'',
  496. 'status' => $contact->statut?$contact->statut:''
  497. );
  498. $i++;
  499. }
  500. $objectresp=array(
  501. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  502. 'contacts'=>$linescontact
  503. );
  504. }
  505. else
  506. {
  507. $error++;
  508. $errorcode=$db->lasterrno(); $errorlabel=$db->lasterror();
  509. }
  510. }
  511. if ($error)
  512. {
  513. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  514. }
  515. return $objectresp;
  516. }
  517. /**
  518. * Update a contact
  519. *
  520. * @param array $authentication Array of authentication information
  521. * @param Contact $contact Contact
  522. * @return array Array result
  523. */
  524. function updateContact($authentication,$contact)
  525. {
  526. global $db,$conf,$langs;
  527. $now=dol_now();
  528. dol_syslog("Function: updateContact login=".$authentication['login']);
  529. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  530. // Init and check authentication
  531. $objectresp=array();
  532. $errorcode='';$errorlabel='';
  533. $error=0;
  534. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  535. // Check parameters
  536. if (empty($contact['id']) && empty($contact['ref_ext'])) {
  537. $error++; $errorcode='KO'; $errorlabel="Contact id or ref_ext is mandatory.";
  538. }
  539. // Check parameters
  540. if (! $error && ($id && $ref_ext))
  541. {
  542. $error++;
  543. $errorcode='BAD_PARAMETERS'; $errorlabel="Parameter id and ref_ext can't be all provided. You must choose one of them.";
  544. }
  545. if (! $error)
  546. {
  547. $objectfound=false;
  548. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  549. $object=new Contact($db);
  550. $result=$object->fetch($contact['id'],0,$contact['ref_ext']);
  551. if (!empty($object->id)) {
  552. $objectfound=true;
  553. $object->firstname=$contact['firstname'];
  554. $object->lastname=$contact['lastname'];
  555. $object->address=$contact['address'];
  556. $object->zip=$contact['zip'];
  557. $object->town=$contact['town'];
  558. $object->country_id=$contact['country_id'];
  559. if ($contact['country_code']) $object->country_id=getCountry($contact['country_code'],3);
  560. $object->province_id=$contact['province_id'];
  561. $object->phone_pro=$contact['phone_pro'];
  562. $object->phone_perso=$contact['phone_perso'];
  563. $object->phone_mobile=$contact['phone_mobile'];
  564. $object->fax=$contact['fax'];
  565. $object->email=$contact['email'];
  566. $object->civility_id=$contact['civility_id'];
  567. $object->poste=$contact['poste'];
  568. $object->statut=$contact['status'];
  569. //Retreive all extrafield for contact
  570. // fetch optionals attributes and labels
  571. $extrafields=new ExtraFields($db);
  572. $extralabels=$extrafields->fetch_name_optionals_label('socpeople',true);
  573. foreach($extrafields->attribute_label as $key=>$label)
  574. {
  575. $key='options_'.$key;
  576. $object->array_options[$key]=$contact[$key];
  577. }
  578. $db->begin();
  579. $result=$object->update($contact['id'],$fuser);
  580. if ($result <= 0) {
  581. $error++;
  582. }
  583. }
  584. if ((! $error) && ($objectfound))
  585. {
  586. $db->commit();
  587. $objectresp=array(
  588. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  589. 'id'=>$object->id
  590. );
  591. }
  592. elseif ($objectfound)
  593. {
  594. $db->rollback();
  595. $error++;
  596. $errorcode='KO';
  597. $errorlabel=$object->error;
  598. } else {
  599. $error++;
  600. $errorcode='NOT_FOUND';
  601. $errorlabel='Contact id='.$contact['id'].' cannot be found';
  602. }
  603. }
  604. if ($error)
  605. {
  606. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  607. }
  608. return $objectresp;
  609. }
  610. // Return the results.
  611. $server->service(file_get_contents("php://input"));