server_category.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/webservices/server_category.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."/categories/class/categorie.class.php";
  27. dol_syslog("Call Dolibarr webservices interfaces");
  28. // Enable and test if module web services is enabled
  29. if (empty($conf->global->MAIN_MODULE_WEBSERVICES))
  30. {
  31. $langs->load("admin");
  32. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  33. print $langs->trans("WarningModuleNotActive",'WebServices').'.<br><br>';
  34. print $langs->trans("ToActivateModule");
  35. exit;
  36. }
  37. // Create the soap Object
  38. $server = new nusoap_server();
  39. $server->soap_defencoding='UTF-8';
  40. $server->decode_utf8=false;
  41. $ns='http://www.dolibarr.org/ns/';
  42. $server->configureWSDL('WebServicesDolibarrCategorie',$ns);
  43. $server->wsdl->schemaTargetNamespace=$ns;
  44. // Define WSDL content
  45. $server->wsdl->addComplexType(
  46. 'authentication',
  47. 'complexType',
  48. 'struct',
  49. 'all',
  50. '',
  51. array(
  52. 'dolibarrkey' => array('name'=>'dolibarrkey','type'=>'xsd:string'),
  53. 'sourceapplication' => array('name'=>'sourceapplication','type'=>'xsd:string'),
  54. 'login' => array('name'=>'login','type'=>'xsd:string'),
  55. 'password' => array('name'=>'password','type'=>'xsd:string'),
  56. 'entity' => array('name'=>'entity','type'=>'xsd:string'),
  57. )
  58. );
  59. /*
  60. * Une catégorie
  61. */
  62. $server->wsdl->addComplexType(
  63. 'categorie',
  64. 'complexType',
  65. 'struct',
  66. 'all',
  67. '',
  68. array(
  69. 'id' => array('name'=>'id','type'=>'xsd:string'),
  70. 'id_mere' => array('name'=>'id_mere','type'=>'xsd:string'),
  71. 'label' => array('name'=>'label','type'=>'xsd:string'),
  72. 'description' => array('name'=>'description','type'=>'xsd:string'),
  73. 'socid' => array('name'=>'socid','type'=>'xsd:string'),
  74. 'type' => array('name'=>'type','type'=>'xsd:string'),
  75. 'visible' => array('name'=>'visible','type'=>'xsd:string'),
  76. 'dir'=> array('name'=>'dir','type'=>'xsd:string'),
  77. 'photos' => array('name'=>'photos','type'=>'tns:PhotosArray'),
  78. 'filles' => array('name'=>'filles','type'=>'tns:FillesArray')
  79. )
  80. );
  81. /*
  82. * Les catégories filles, sous tableau dez la catégorie
  83. */
  84. $server->wsdl->addComplexType(
  85. 'FillesArray',
  86. 'complexType',
  87. 'array',
  88. '',
  89. 'SOAP-ENC:Array',
  90. array(),
  91. array(
  92. array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:categorie[]')
  93. ),
  94. 'tns:categorie'
  95. );
  96. /*
  97. * Image of product
  98. */
  99. $server->wsdl->addComplexType(
  100. 'PhotosArray',
  101. 'complexType',
  102. 'array',
  103. 'sequence',
  104. '',
  105. array(
  106. 'image' => array(
  107. 'name' => 'image',
  108. 'type' => 'tns:image',
  109. 'minOccurs' => '0',
  110. 'maxOccurs' => 'unbounded'
  111. )
  112. )
  113. );
  114. /*
  115. * An image
  116. */
  117. $server->wsdl->addComplexType(
  118. 'image',
  119. 'complexType',
  120. 'struct',
  121. 'all',
  122. '',
  123. array(
  124. 'photo' => array('name'=>'photo','type'=>'xsd:string'),
  125. 'photo_vignette' => array('name'=>'photo_vignette','type'=>'xsd:string'),
  126. 'imgWidth' => array('name'=>'imgWidth','type'=>'xsd:string'),
  127. 'imgHeight' => array('name'=>'imgHeight','type'=>'xsd:string')
  128. )
  129. );
  130. /*
  131. * Retour
  132. */
  133. $server->wsdl->addComplexType(
  134. 'result',
  135. 'complexType',
  136. 'struct',
  137. 'all',
  138. '',
  139. array(
  140. 'result_code' => array('name'=>'result_code','type'=>'xsd:string'),
  141. 'result_label' => array('name'=>'result_label','type'=>'xsd:string'),
  142. )
  143. );
  144. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  145. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  146. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  147. $styledoc='rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  148. $styleuse='encoded'; // encoded/literal/literal wrapped
  149. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  150. // Register WSDL
  151. $server->register(
  152. 'getCategory',
  153. // Entry values
  154. array('authentication'=>'tns:authentication','id'=>'xsd:string'),
  155. // Exit values
  156. array('result'=>'tns:result','categorie'=>'tns:categorie'),
  157. $ns,
  158. $ns.'#getCategory',
  159. $styledoc,
  160. $styleuse,
  161. 'WS to get category'
  162. );
  163. /**
  164. * Get category infos and children
  165. *
  166. * @param array $authentication Array of authentication information
  167. * @param int $id Id of object
  168. * @return mixed
  169. */
  170. function getCategory($authentication,$id)
  171. {
  172. global $db,$conf,$langs;
  173. dol_syslog("Function: getCategory login=".$authentication['login']." id=".$id);
  174. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  175. $objectresp=array();
  176. $errorcode='';$errorlabel='';
  177. $error=0;
  178. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  179. if (! $error && !$id)
  180. {
  181. $error++;
  182. $errorcode='BAD_PARAMETERS'; $errorlabel="Parameter id must be provided.";
  183. }
  184. if (! $error)
  185. {
  186. $fuser->getrights();
  187. if ($fuser->rights->categorie->lire)
  188. {
  189. $categorie=new Categorie($db);
  190. $result=$categorie->fetch($id);
  191. if ($result > 0)
  192. {
  193. $dir = (!empty($conf->categorie->dir_output)?$conf->categorie->dir_output:$conf->service->dir_output);
  194. $pdir = get_exdir($categorie->id,2,0,0,$categorie,'category') . $categorie->id ."/photos/";
  195. $dir = $dir . '/'. $pdir;
  196. $cat = array(
  197. 'id' => $categorie->id,
  198. 'id_mere' => $categorie->id_mere,
  199. 'label' => $categorie->label,
  200. 'description' => $categorie->description,
  201. 'socid' => $categorie->socid,
  202. //'visible'=>$categorie->visible,
  203. 'type' => $categorie->type,
  204. 'dir' => $pdir,
  205. 'photos' => $categorie->liste_photos($dir,$nbmax=10)
  206. );
  207. $cats = $categorie->get_filles();
  208. if (count($cats) > 0)
  209. {
  210. foreach($cats as $fille)
  211. {
  212. $dir = (!empty($conf->categorie->dir_output)?$conf->categorie->dir_output:$conf->service->dir_output);
  213. $pdir = get_exdir($fille->id,2,0,0,$categorie,'category') . $fille->id ."/photos/";
  214. $dir = $dir . '/'. $pdir;
  215. $cat['filles'][] = array(
  216. 'id'=>$fille->id,
  217. 'id_mere' => $categorie->id_mere,
  218. 'label'=>$fille->label,
  219. 'description'=>$fille->description,
  220. 'socid'=>$fille->socid,
  221. //'visible'=>$fille->visible,
  222. 'type'=>$fille->type,
  223. 'dir' => $pdir,
  224. 'photos' => $fille->liste_photos($dir,$nbmax=10)
  225. );
  226. }
  227. }
  228. // Create
  229. $objectresp = array(
  230. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  231. 'categorie'=> $cat
  232. );
  233. }
  234. else
  235. {
  236. $error++;
  237. $errorcode='NOT_FOUND'; $errorlabel='Object not found for id='.$id;
  238. }
  239. }
  240. else
  241. {
  242. $error++;
  243. $errorcode='PERMISSION_DENIED'; $errorlabel='User does not have permission for this request';
  244. }
  245. }
  246. if ($error)
  247. {
  248. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  249. }
  250. return $objectresp;
  251. }
  252. // Return the results.
  253. $server->service(file_get_contents("php://input"));