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