server_contact.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 (!getDolGlobalString('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 = '';
  248. $errorlabel = '';
  249. $error = 0;
  250. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  251. // Check parameters
  252. if (!$error && ($id && $ref_ext)) {
  253. $error++;
  254. $errorcode = 'BAD_PARAMETERS';
  255. $errorlabel = "Parameter id and ref_ext can't be both provided. You must choose one or other but not both.";
  256. }
  257. if (!$error) {
  258. $fuser->getrights();
  259. $contact = new Contact($db);
  260. $result = $contact->fetch($id, 0, $ref_ext);
  261. if ($result > 0) {
  262. // Only internal user who have contact read permission
  263. // Or for external user who have contact read permission, with restrict on socid
  264. if ($fuser->hasRight('societe', 'contact', 'lire') && !$fuser->socid
  265. || ($fuser->hasRight('societe', 'contact', 'lire') && ($fuser->socid == $contact->socid))
  266. ) {
  267. $contact_result_fields = array(
  268. 'id' => $contact->id,
  269. 'ref_ext' => $contact->ref_ext,
  270. 'lastname' => $contact->lastname,
  271. 'firstname' => $contact->firstname,
  272. 'address' => $contact->address,
  273. 'zip' => $contact->zip,
  274. 'town' => $contact->town,
  275. 'state_id' => $contact->state_id,
  276. 'state_code' => $contact->state_code,
  277. 'state' => $contact->state,
  278. 'country_id' => $contact->country_id,
  279. 'country_code' => $contact->country_code,
  280. 'country' => $contact->country,
  281. 'socid' => $contact->socid,
  282. 'status' => $contact->statut,
  283. 'phone_pro' => $contact->phone_pro,
  284. 'fax' => $contact->fax,
  285. 'phone_perso' => $contact->phone_perso,
  286. 'phone_mobile' => $contact->phone_mobile,
  287. 'code' => $contact->code,
  288. 'email' => $contact->email,
  289. 'birthday' => $contact->birthday,
  290. 'default_lang' => $contact->default_lang,
  291. 'note' => $contact->note,
  292. 'ref_facturation' => $contact->ref_facturation,
  293. 'ref_contrat' => $contact->ref_contrat,
  294. 'ref_commande' => $contact->ref_commande,
  295. 'ref_propal' => $contact->ref_propal,
  296. 'user_id' => $contact->user_id,
  297. 'user_login' => $contact->user_login,
  298. 'civility_id' => $contact->civility_id,
  299. 'poste' => $contact->poste
  300. );
  301. $elementtype = 'socpeople';
  302. //Retrieve all extrafield for thirdsparty
  303. // fetch optionals attributes and labels
  304. $extrafields = new ExtraFields($db);
  305. $extrafields->fetch_name_optionals_label($elementtype, true);
  306. //Get extrafield values
  307. $contact->fetch_optionals();
  308. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  309. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  310. $contact_result_fields = array_merge($contact_result_fields, array('options_'.$key => $contact->array_options['options_'.$key]));
  311. }
  312. }
  313. // Create
  314. $objectresp = array(
  315. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  316. 'contact'=>$contact_result_fields
  317. );
  318. } else {
  319. $error++;
  320. $errorcode = 'PERMISSION_DENIED';
  321. $errorlabel = 'User does not have permission for this request';
  322. }
  323. } else {
  324. $error++;
  325. $errorcode = 'NOT_FOUND';
  326. $errorlabel = 'Object not found for id='.$id.' nor ref_ext='.$ref_ext;
  327. }
  328. }
  329. if ($error) {
  330. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  331. }
  332. return $objectresp;
  333. }
  334. /**
  335. * Create Contact
  336. *
  337. * @param array $authentication Array of authentication information
  338. * @param Contact $contact $contact
  339. * @return array Array result
  340. */
  341. function createContact($authentication, $contact)
  342. {
  343. global $db, $conf, $langs;
  344. $now = dol_now();
  345. dol_syslog("Function: createContact login=".$authentication['login']);
  346. if ($authentication['entity']) {
  347. $conf->entity = $authentication['entity'];
  348. }
  349. // Init and check authentication
  350. $objectresp = array();
  351. $errorcode = '';
  352. $errorlabel = '';
  353. $error = 0;
  354. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  355. // Check parameters
  356. if (empty($contact['lastname'])) {
  357. $error++;
  358. $errorcode = 'KO';
  359. $errorlabel = "Name is mandatory.";
  360. }
  361. if (!$error) {
  362. $newobject = new Contact($db);
  363. $newobject->id = $contact['id'];
  364. $newobject->ref_ext = $contact['ref_ext'];
  365. $newobject->civility_id = $contact['civility_id'];
  366. $newobject->lastname = $contact['lastname'];
  367. $newobject->firstname = $contact['firstname'];
  368. $newobject->address = $contact['address'];
  369. $newobject->zip = $contact['zip'];
  370. $newobject->town = $contact['town'];
  371. $newobject->state_id = $contact['state_id'];
  372. $newobject->state_code = $contact['state_code'];
  373. $newobject->state = $contact['state'];
  374. $newobject->country_id = $contact['country_id'];
  375. $newobject->country_code = $contact['country_code'];
  376. $newobject->country = $contact['country'];
  377. $newobject->socid = $contact['socid'];
  378. $newobject->statut = $contact['status'];
  379. $newobject->phone_pro = $contact['phone_pro'];
  380. $newobject->fax = $contact['fax'];
  381. $newobject->phone_perso = $contact['phone_perso'];
  382. $newobject->phone_mobile = $contact['phone_mobile'];
  383. $newobject->code = $contact['code'];
  384. $newobject->email = $contact['email'];
  385. $newobject->birthday = $contact['birthday'];
  386. $newobject->default_lang = $contact['default_lang'];
  387. $newobject->note = $contact['note'];
  388. $newobject->ref_facturation = $contact['ref_facturation'];
  389. $newobject->ref_contrat = $contact['ref_contrat'];
  390. $newobject->ref_commande = $contact['ref_commande'];
  391. $newobject->ref_propal = $contact['ref_propal'];
  392. $newobject->user_id = $contact['user_id'];
  393. $newobject->user_login = $contact['user_login'];
  394. $newobject->poste = $contact['poste'];
  395. $elementtype = 'socpeople';
  396. //Retrieve all extrafield for thirdsparty
  397. // fetch optionals attributes and labels
  398. $extrafields = new ExtraFields($db);
  399. $extrafields->fetch_name_optionals_label($elementtype, true);
  400. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  401. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  402. $key = 'options_'.$key;
  403. $newobject->array_options[$key] = $contact[$key];
  404. }
  405. }
  406. //...
  407. $db->begin();
  408. $result = $newobject->create($fuser);
  409. if ($result <= 0) {
  410. $error++;
  411. }
  412. if (!$error) {
  413. $db->commit();
  414. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
  415. } else {
  416. $db->rollback();
  417. $error++;
  418. $errorcode = 'KO';
  419. $errorlabel = $newobject->error;
  420. }
  421. }
  422. if ($error) {
  423. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  424. }
  425. return $objectresp;
  426. }
  427. /**
  428. * Get list of contacts for third party
  429. *
  430. * @param array $authentication Array of authentication information
  431. * @param int $idthirdparty Id thirdparty
  432. * @return array Array result
  433. */
  434. function getContactsForThirdParty($authentication, $idthirdparty)
  435. {
  436. global $db, $conf, $langs;
  437. dol_syslog("Function: getContactsForThirdParty login=".$authentication['login']." idthirdparty=".$idthirdparty);
  438. if ($authentication['entity']) {
  439. $conf->entity = $authentication['entity'];
  440. }
  441. // Init and check authentication
  442. $objectresp = array();
  443. $errorcode = '';
  444. $errorlabel = '';
  445. $error = 0;
  446. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  447. // Check parameters
  448. if (!$error && empty($idthirdparty)) {
  449. $error++;
  450. $errorcode = 'BAD_PARAMETERS';
  451. $errorlabel = 'Parameter id is not provided';
  452. }
  453. if (!$error) {
  454. $linesinvoice = array();
  455. $sql = "SELECT c.rowid, c.fk_soc, c.civility as civility_id, c.lastname, c.firstname, c.statut as status,";
  456. $sql .= " c.address, c.zip, c.town,";
  457. $sql .= " c.fk_pays as country_id,";
  458. $sql .= " c.fk_departement as state_id,";
  459. $sql .= " c.birthday,";
  460. $sql .= " c.poste, c.phone, c.phone_perso, c.phone_mobile, c.fax, c.email,";
  461. $sql .= " co.label as country, co.code as country_code,";
  462. $sql .= " d.nom as state, d.code_departement as state_code,";
  463. $sql .= " u.rowid as user_id, u.login as user_login,";
  464. $sql .= " s.nom as socname, s.address as socaddress, s.zip as soccp, s.town as soccity, s.default_lang as socdefault_lang";
  465. $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as c";
  466. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON c.fk_pays = co.rowid";
  467. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_departements as d ON c.fk_departement = d.rowid";
  468. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON c.rowid = u.fk_socpeople";
  469. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON c.fk_soc = s.rowid";
  470. $sql .= " WHERE c.fk_soc = ".((int) $idthirdparty);
  471. $resql = $db->query($sql);
  472. if ($resql) {
  473. $num = $db->num_rows($resql);
  474. $i = 0;
  475. while ($i < $num) {
  476. // En attendant remplissage par boucle
  477. $obj = $db->fetch_object($resql);
  478. $contact = new Contact($db);
  479. $contact->fetch($obj->rowid);
  480. // Now define invoice
  481. $linescontact[] = array(
  482. 'id' => $contact->id,
  483. 'ref' => $contact->ref,
  484. 'civility_id' => $contact->civility_id ? $contact->civility_id : '',
  485. 'lastname' => $contact->lastname ? $contact->lastname : '',
  486. 'firstname' => $contact->firstname ? $contact->firstname : '',
  487. 'address' => $contact->address ? $contact->address : '',
  488. 'zip' => $contact->zip ? $contact->zip : '',
  489. 'town' => $contact->town ? $contact->town : '',
  490. 'state_id' => $contact->state_id ? $contact->state_id : '',
  491. 'state_code' => $contact->state_code ? $contact->state_code : '',
  492. 'state' => $contact->state ? $contact->state : '',
  493. 'country_id' => $contact->country_id ? $contact->country_id : '',
  494. 'country_code' => $contact->country_code ? $contact->country_code : '',
  495. 'country' => $contact->country ? $contact->country : '',
  496. 'socid' => $contact->socid ? $contact->socid : '',
  497. 'socname' => $contact->socname ? $contact->socname : '',
  498. 'poste' => $contact->poste ? $contact->poste : '',
  499. 'phone_pro' => $contact->phone_pro ? $contact->phone_pro : '',
  500. 'fax' => $contact->fax ? $contact->fax : '',
  501. 'phone_perso' => $contact->phone_perso ? $contact->phone_perso : '',
  502. 'phone_mobile' => $contact->phone_mobile ? $contact->phone_mobile : '',
  503. 'email' => $contact->email ? $contact->email : '',
  504. 'priv' => $contact->priv ? $contact->priv : '',
  505. 'mail' => $contact->mail ? $contact->mail : '',
  506. 'birthday' => $contact->birthday ? $contact->birthday : '',
  507. 'default_lang' => $contact->default_lang ? $contact->default_lang : '',
  508. 'note' => $contact->note ? $contact->note : '',
  509. 'ref_facturation' => $contact->ref_facturation ? $contact->ref_facturation : '',
  510. 'ref_contrat' => $contact->ref_contrat ? $contact->ref_contrat : '',
  511. 'ref_commande' => $contact->ref_commande ? $contact->ref_commande : '',
  512. 'ref_propal' => $contact->ref_propal ? $contact->ref_propal : '',
  513. 'user_id' => $contact->user_id ? $contact->user_id : '',
  514. 'user_login' => $contact->user_login ? $contact->user_login : '',
  515. 'status' => $contact->statut ? $contact->statut : ''
  516. );
  517. $i++;
  518. }
  519. $objectresp = array(
  520. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  521. 'contacts'=>$linescontact
  522. );
  523. } else {
  524. $error++;
  525. $errorcode = $db->lasterrno();
  526. $errorlabel = $db->lasterror();
  527. }
  528. }
  529. if ($error) {
  530. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  531. }
  532. return $objectresp;
  533. }
  534. /**
  535. * Update a contact
  536. *
  537. * @param array $authentication Array of authentication information
  538. * @param Contact $contact Contact
  539. * @return array Array result
  540. */
  541. function updateContact($authentication, $contact)
  542. {
  543. global $db, $conf;
  544. $now = dol_now();
  545. dol_syslog("Function: updateContact login=".$authentication['login']);
  546. if ($authentication['entity']) {
  547. $conf->entity = $authentication['entity'];
  548. }
  549. // Init and check authentication
  550. $objectresp = array();
  551. $errorcode = '';
  552. $errorlabel = '';
  553. $error = 0;
  554. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  555. // Check parameters
  556. if (empty($contact['id']) && empty($contact['ref_ext'])) {
  557. $error++;
  558. $errorcode = 'KO';
  559. $errorlabel = "Contact id or ref_ext is mandatory.";
  560. }
  561. // Check parameters
  562. if (!$error && ($contact['id'] && $contact['ref_ext'])) {
  563. $error++;
  564. $errorcode = 'BAD_PARAMETERS';
  565. $errorlabel = "Parameter id and ref_ext can't be all provided. You must choose one of them.";
  566. }
  567. if (!$error) {
  568. $objectfound = false;
  569. include_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  570. $object = new Contact($db);
  571. $result = $object->fetch($contact['id'], 0, $contact['ref_ext']);
  572. if (!empty($object->id)) {
  573. $objectfound = true;
  574. $object->ref_ext = $contact['ref_ext'];
  575. $object->firstname = $contact['firstname'];
  576. $object->lastname = $contact['lastname'];
  577. $object->address = $contact['address'];
  578. $object->zip = $contact['zip'];
  579. $object->town = $contact['town'];
  580. $object->country_id = $contact['country_id'];
  581. if ($contact['country_code']) {
  582. $object->country_id = getCountry($contact['country_code'], 3);
  583. }
  584. $object->province_id = $contact['province_id'];
  585. $object->phone_pro = $contact['phone_pro'];
  586. $object->phone_perso = $contact['phone_perso'];
  587. $object->phone_mobile = $contact['phone_mobile'];
  588. $object->fax = $contact['fax'];
  589. $object->email = $contact['email'];
  590. $object->civility_id = $contact['civility_id'];
  591. $object->poste = $contact['poste'];
  592. $object->statut = $contact['status'];
  593. $elementtype = 'socpeople';
  594. //Retrieve all extrafield for contact
  595. // fetch optionals attributes and labels
  596. $extrafields = new ExtraFields($db);
  597. $extrafields->fetch_name_optionals_label($elementtype, true);
  598. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  599. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  600. $key = 'options_'.$key;
  601. $object->array_options[$key] = $contact[$key];
  602. }
  603. }
  604. $db->begin();
  605. $result = $object->update($contact['id'], $fuser);
  606. if ($result <= 0) {
  607. $error++;
  608. }
  609. }
  610. if ((!$error) && ($objectfound)) {
  611. $db->commit();
  612. $objectresp = array(
  613. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  614. 'id'=>$object->id
  615. );
  616. } elseif ($objectfound) {
  617. $db->rollback();
  618. $error++;
  619. $errorcode = 'KO';
  620. $errorlabel = $object->error;
  621. } else {
  622. $error++;
  623. $errorcode = 'NOT_FOUND';
  624. $errorlabel = 'Contact id='.$contact['id'].' cannot be found';
  625. }
  626. }
  627. if ($error) {
  628. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  629. }
  630. return $objectresp;
  631. }
  632. // Return the results.
  633. $server->service(file_get_contents("php://input"));