server_contact.php 24 KB

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