server_project.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2016 Ion Agorria <ion@agorria.com>
  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_project.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.'/core/lib/date.lib.php';
  47. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  48. require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
  49. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  50. dol_syslog("Call Dolibarr webservices interfaces");
  51. $langs->load("main");
  52. // Enable and test if module web services is enabled
  53. if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
  54. $langs->load("admin");
  55. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  56. print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
  57. print $langs->trans("ToActivateModule");
  58. exit;
  59. }
  60. // Create associated types array, with each table
  61. $listofreferent = array(
  62. 'propal' => 'propal',
  63. 'order' => 'commande',
  64. 'invoice' => 'facture',
  65. 'invoice_predefined' => 'facture_rec',
  66. 'proposal_supplier' => 'commande_fournisseur',
  67. 'order_supplier' => 'commande_fournisseur',
  68. 'invoice_supplier' => 'facture_fourn',
  69. 'contract' => 'contrat',
  70. 'intervention' => 'fichinter',
  71. 'trip' => 'deplacement',
  72. 'expensereport' => 'expensereport_det',
  73. 'donation' => 'don',
  74. 'agenda' => 'actioncomm',
  75. 'project_task' => 'projet_task',
  76. );
  77. // Create the soap Object
  78. $server = new nusoap_server();
  79. $server->soap_defencoding = 'UTF-8';
  80. $server->decode_utf8 = false;
  81. $ns = 'http://www.dolibarr.org/ns/';
  82. $server->configureWSDL('WebServicesDolibarrOther', $ns);
  83. $server->wsdl->schemaTargetNamespace = $ns;
  84. // Define WSDL Authentication object
  85. $server->wsdl->addComplexType(
  86. 'authentication',
  87. 'complexType',
  88. 'struct',
  89. 'all',
  90. '',
  91. array(
  92. 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
  93. 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
  94. 'login' => array('name'=>'login', 'type'=>'xsd:string'),
  95. 'password' => array('name'=>'password', 'type'=>'xsd:string'),
  96. 'entity' => array('name'=>'entity', 'type'=>'xsd:string'),
  97. )
  98. );
  99. // Define WSDL Return object
  100. $server->wsdl->addComplexType(
  101. 'result',
  102. 'complexType',
  103. 'struct',
  104. 'all',
  105. '',
  106. array(
  107. 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
  108. 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
  109. )
  110. );
  111. // Define other specific objects
  112. $server->wsdl->addComplexType(
  113. 'element',
  114. 'complexType',
  115. 'struct',
  116. 'all',
  117. '',
  118. array(
  119. 'id' => array('name'=>'id', 'type'=>'xsd:int'),
  120. 'user' => array('name'=>'user', 'type'=>'xsd:int'),
  121. )
  122. );
  123. $server->wsdl->addComplexType(
  124. 'elementsArray',
  125. 'complexType',
  126. 'array',
  127. 'sequence',
  128. '',
  129. array(
  130. 'elements' => array(
  131. 'name' => 'elementsArray',
  132. 'type' => 'tns:element',
  133. 'minOccurs' => '0',
  134. 'maxOccurs' => 'unbounded'
  135. )
  136. )
  137. );
  138. $project_elements = array();
  139. foreach ($listofreferent as $key => $label) {
  140. $project_elements[$key] = array('name'=>$key, 'type'=>'tns:elementsArray');
  141. }
  142. $server->wsdl->addComplexType(
  143. 'elements',
  144. 'complexType',
  145. 'struct',
  146. 'all',
  147. '',
  148. $project_elements
  149. );
  150. // Define project
  151. $project_fields = array(
  152. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  153. 'ref' => array('name'=>'ref', 'type'=>'xsd:string'),
  154. 'label' => array('name'=>'label', 'type'=>'xsd:string'),
  155. 'thirdparty_id' => array('name'=>'thirdparty_id', 'type'=>'xsd:int'),
  156. 'public' => array('name'=>'public', 'type'=>'xsd:int'),
  157. 'status' => array('name'=>'status', 'type'=>'xsd:int'),
  158. 'date_start' => array('name'=>'date_start', 'type'=>'xsd:date'),
  159. 'date_end' => array('name'=>'date_end', 'type'=>'xsd:date'),
  160. 'budget' => array('name'=>'budget', 'type'=>'xsd:int'),
  161. 'description' => array('name'=>'description', 'type'=>'xsd:string'),
  162. 'elements' => array('name'=>'elements', 'type'=>'tns:elements')
  163. );
  164. $elementtype = 'project';
  165. //Retrieve all extrafield for thirdsparty
  166. // fetch optionals attributes and labels
  167. $extrafields = new ExtraFields($db);
  168. $extrafields->fetch_name_optionals_label($elementtype, true);
  169. $extrafield_array = null;
  170. if (is_array($extrafields) && count($extrafields) > 0) {
  171. $extrafield_array = array();
  172. }
  173. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  174. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  175. //$value=$object->array_options["options_".$key];
  176. $type = $extrafields->attributes[$elementtype]['type'][$key];
  177. if ($type == 'date' || $type == 'datetime') {
  178. $type = 'xsd:dateTime';
  179. } else {
  180. $type = 'xsd:string';
  181. }
  182. $extrafield_array['options_'.$key] = array('name'=>'options_'.$key, 'type'=>$type);
  183. }
  184. }
  185. if (is_array($extrafield_array)) {
  186. $project_fields = array_merge($project_fields, $extrafield_array);
  187. }
  188. $server->wsdl->addComplexType(
  189. 'project',
  190. 'complexType',
  191. 'struct',
  192. 'all',
  193. '',
  194. $project_fields
  195. );
  196. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  197. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  198. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  199. $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  200. $styleuse = 'encoded'; // encoded/literal/literal wrapped
  201. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  202. // Register WSDL
  203. $server->register(
  204. 'createProject',
  205. // Entry values
  206. array('authentication'=>'tns:authentication', 'project'=>'tns:project'),
  207. // Exit values
  208. array('result'=>'tns:result', 'id'=>'xsd:string', 'ref'=>'xsd:string'),
  209. $ns,
  210. $ns.'#createProject',
  211. $styledoc,
  212. $styleuse,
  213. 'WS to create project'
  214. );
  215. // Register WSDL
  216. $server->register(
  217. 'getProject',
  218. // Entry values
  219. array('authentication'=>'tns:authentication', 'id'=>'xsd:string', 'ref'=>'xsd:string'),
  220. // Exit values
  221. array('result'=>'tns:result', 'project'=>'tns:project'),
  222. $ns,
  223. $ns.'#getProject',
  224. $styledoc,
  225. $styleuse,
  226. 'WS to get project'
  227. );
  228. // Full methods code
  229. /**
  230. * Create project
  231. *
  232. * @param array $authentication Array of authentication information
  233. * @param array $project Project info
  234. * @return array array of new order
  235. */
  236. function createProject($authentication, $project)
  237. {
  238. global $db, $conf;
  239. dol_syslog("Function: createProject login=".$authentication['login']);
  240. if ($authentication['entity']) {
  241. $conf->entity = $authentication['entity'];
  242. }
  243. // Init and check authentication
  244. $objectresp = array();
  245. $errorcode = ''; $errorlabel = '';
  246. $error = 0;
  247. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  248. // Check parameters
  249. if (empty($project['ref'])) {
  250. $error++; $errorcode = 'KO'; $errorlabel = "Name is mandatory.";
  251. }
  252. if (!$error) {
  253. $fuser->getrights();
  254. if ($fuser->hasRight('projet', 'creer')) {
  255. $newobject = new Project($db);
  256. $newobject->ref = $project['ref'];
  257. $newobject->title = $project['label'];
  258. $newobject->socid = $project['thirdparty_id'];
  259. $newobject->public = $project['public'];
  260. $newobject->statut = $project['status'];
  261. $newobject->date_start = dol_stringtotime($project['date_start'], 'dayrfc');
  262. $newobject->date_end = dol_stringtotime($project['date_end'], 'dayrfc');
  263. $newobject->budget_amount = $project['budget'];
  264. $newobject->description = $project['description'];
  265. $elementtype = 'project';
  266. // Retrieve all extrafields for project
  267. // fetch optionals attributes and labels
  268. $extrafields = new ExtraFields($db);
  269. $extrafields->fetch_name_optionals_label($elementtype, true);
  270. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  271. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  272. $key = 'options_'.$key;
  273. $newobject->array_options[$key] = $project[$key];
  274. }
  275. }
  276. $db->begin();
  277. $result = $newobject->create($fuser);
  278. if (!$error && $result > 0) {
  279. // Add myself as project leader
  280. $result = $newobject->add_contact($fuser->id, 'PROJECTLEADER', 'internal');
  281. if ($result < 0) {
  282. $error++;
  283. }
  284. } else {
  285. $error++;
  286. }
  287. if (!$error) {
  288. $db->commit();
  289. $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
  290. } else {
  291. $db->rollback();
  292. $error++;
  293. $errorcode = 'KO';
  294. $errorlabel = $newobject->error;
  295. }
  296. } else {
  297. $error++;
  298. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  299. }
  300. }
  301. if ($error) {
  302. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  303. }
  304. return $objectresp;
  305. }
  306. /**
  307. * Get a project
  308. *
  309. * @param array $authentication Array of authentication information
  310. * @param string $id internal id
  311. * @param string $ref internal reference
  312. * @return array Array result
  313. */
  314. function getProject($authentication, $id = '', $ref = '')
  315. {
  316. global $db, $conf;
  317. dol_syslog("Function: getProject login=".$authentication['login']." id=".$id." ref=".$ref);
  318. if ($authentication['entity']) {
  319. $conf->entity = $authentication['entity'];
  320. }
  321. // Init and check authentication
  322. $objectresp = array();
  323. $errorcode = ''; $errorlabel = '';
  324. $error = 0;
  325. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  326. // Check parameters
  327. if (!$error && (($id && $ref))) {
  328. $error++;
  329. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id and ref can't be both provided. You must choose one or other but not both.";
  330. }
  331. if (!$error) {
  332. $fuser->getrights();
  333. if ($fuser->hasRight('projet', 'lire')) {
  334. $project = new Project($db);
  335. $result = $project->fetch($id, $ref);
  336. if ($result > 0) {
  337. $project_result_fields = array(
  338. 'id' => $project->id,
  339. 'ref' => $project->ref,
  340. 'label' => $project->title,
  341. 'thirdparty_id' => $project->socid,
  342. 'public' => $project->public,
  343. 'status' => $project->statut,
  344. 'date_start' => $project->date_start ? dol_print_date($project->date_start, 'dayrfc') : '',
  345. 'date_end' => $project->date_end ? dol_print_date($project->date_end, 'dayrfc') : '',
  346. 'budget' => $project->budget_amount,
  347. 'description' => $project->description,
  348. );
  349. $elementtype = 'project';
  350. //Retrieve all extrafields for project
  351. $extrafields = new ExtraFields($db);
  352. $extrafields->fetch_name_optionals_label($elementtype, true);
  353. //Get extrafield values
  354. if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
  355. $project->fetch_optionals();
  356. foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
  357. $project_result_fields = array_merge($project_result_fields, array('options_'.$key => $project->array_options['options_'.$key]));
  358. }
  359. }
  360. //Get linked elements
  361. global $listofreferent;
  362. $elements = array();
  363. foreach ($listofreferent as $key => $tablename) {
  364. $elements[$key] = array();
  365. $element_array = $project->get_element_list($key, $tablename);
  366. if (count($element_array) > 0 && is_array($element_array)) {
  367. foreach ($element_array as $element) {
  368. $tmp = explode('_', $element);
  369. $idofelement = count($tmp) > 0 ? $tmp[0] : "";
  370. $idofelementuser = count($tmp) > 1 ? $tmp[1] : "";
  371. $elements[$key][] = array('id' => $idofelement, 'user' => $idofelementuser);
  372. }
  373. }
  374. }
  375. $project_result_fields['elements'] = $elements;
  376. //Result
  377. $objectresp = array(
  378. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  379. 'project'=>$project_result_fields
  380. );
  381. } else {
  382. $error++;
  383. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref;
  384. }
  385. } else {
  386. $error++;
  387. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  388. }
  389. }
  390. if ($error) {
  391. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  392. }
  393. return $objectresp;
  394. }
  395. // Return the results.
  396. $server->service(file_get_contents("php://input"));