server_project.php 13 KB

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