server_project.php 14 KB

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