server_contact.php 23 KB

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