dolibarr  19.0.0-dev
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'); // Do not check anti CSRF attack test
26 }
27 if (!defined('NOTOKENRENEWAL')) {
28  define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
29 }
30 if (!defined('NOREQUIREMENU')) {
31  define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
32 }
33 if (!defined('NOREQUIREHTML')) {
34  define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
35 }
36 if (!defined('NOREQUIREAJAX')) {
37  define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
38 }
39 if (!defined("NOLOGIN")) {
40  define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
41 }
42 if (!defined("NOSESSION")) {
43  define("NOSESSION", '1');
44 }
45 
46 require '../main.inc.php';
47 require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
48 require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
49 require_once DOL_DOCUMENT_ROOT."/categories/class/categorie.class.php";
50 
51 
52 dol_syslog("Call Dolibarr webservices interfaces");
53 
54 // Enable and test if module web services is enabled
55 if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
56  $langs->load("admin");
57  dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
58  print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
59  print $langs->trans("ToActivateModule");
60  exit;
61 }
62 
63 // Create the soap Object
64 $server = new nusoap_server();
65 $server->soap_defencoding = 'UTF-8';
66 $server->decode_utf8 = false;
67 $ns = 'http://www.dolibarr.org/ns/';
68 $server->configureWSDL('WebServicesDolibarrCategorie', $ns);
69 $server->wsdl->schemaTargetNamespace = $ns;
70 
71 
72 // Define WSDL content
73 $server->wsdl->addComplexType(
74  'authentication',
75  'complexType',
76  'struct',
77  'all',
78  '',
79  array(
80  'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
81  'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
82  'login' => array('name'=>'login', 'type'=>'xsd:string'),
83  'password' => array('name'=>'password', 'type'=>'xsd:string'),
84  'entity' => array('name'=>'entity', 'type'=>'xsd:string'),
85  )
86 );
87 
88 /*
89  * Une catégorie
90  */
91 $server->wsdl->addComplexType(
92  'categorie',
93  'complexType',
94  'struct',
95  'all',
96  '',
97  array(
98  'id' => array('name'=>'id', 'type'=>'xsd:string'),
99  'id_mere' => array('name'=>'id_mere', 'type'=>'xsd:string'),
100  'label' => array('name'=>'label', 'type'=>'xsd:string'),
101  'description' => array('name'=>'description', 'type'=>'xsd:string'),
102  'socid' => array('name'=>'socid', 'type'=>'xsd:string'),
103  'type' => array('name'=>'type', 'type'=>'xsd:string'),
104  'visible' => array('name'=>'visible', 'type'=>'xsd:string'),
105  'dir'=> array('name'=>'dir', 'type'=>'xsd:string'),
106  'photos' => array('name'=>'photos', 'type'=>'tns:PhotosArray'),
107  'filles' => array('name'=>'filles', 'type'=>'tns:FillesArray')
108  )
109 );
110 
111 /*
112  * Les catégories filles, sous tableau dez la catégorie
113  */
114 $server->wsdl->addComplexType(
115  'FillesArray',
116  'complexType',
117  'array',
118  '',
119  'SOAP-ENC:Array',
120  array(),
121  array(
122  array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:categorie[]')
123  ),
124  'tns:categorie'
125 );
126 
127  /*
128  * Image of product
129  */
130 $server->wsdl->addComplexType(
131  'PhotosArray',
132  'complexType',
133  'array',
134  'sequence',
135  '',
136  array(
137  'image' => array(
138  'name' => 'image',
139  'type' => 'tns:image',
140  'minOccurs' => '0',
141  'maxOccurs' => 'unbounded'
142  )
143  )
144 );
145 
146  /*
147  * An image
148  */
149 $server->wsdl->addComplexType(
150  'image',
151  'complexType',
152  'struct',
153  'all',
154  '',
155  array(
156  'photo' => array('name'=>'photo', 'type'=>'xsd:string'),
157  'photo_vignette' => array('name'=>'photo_vignette', 'type'=>'xsd:string'),
158  'imgWidth' => array('name'=>'imgWidth', 'type'=>'xsd:string'),
159  'imgHeight' => array('name'=>'imgHeight', 'type'=>'xsd:string')
160  )
161 );
162 
163 /*
164  * Retour
165  */
166 $server->wsdl->addComplexType(
167  'result',
168  'complexType',
169  'struct',
170  'all',
171  '',
172  array(
173  'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
174  'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
175  )
176 );
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 
185 
186 // Register WSDL
187 $server->register(
188  'getCategory',
189  // Entry values
190  array('authentication'=>'tns:authentication', 'id'=>'xsd:string'),
191  // Exit values
192  array('result'=>'tns:result', 'categorie'=>'tns:categorie'),
193  $ns,
194  $ns.'#getCategory',
195  $styledoc,
196  $styleuse,
197  'WS to get category'
198 );
199 
200 
208 function getCategory($authentication, $id)
209 {
210  global $db, $conf, $langs;
211 
212  $nbmax = 10;
213 
214  dol_syslog("Function: getCategory login=".$authentication['login']." id=".$id);
215 
216  if ($authentication['entity']) {
217  $conf->entity = $authentication['entity'];
218  }
219 
220  $objectresp = array();
221  $errorcode = ''; $errorlabel = '';
222  $error = 0;
223  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
224 
225  if (!$error && !$id) {
226  $error++;
227  $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id must be provided.";
228  }
229 
230  if (!$error) {
231  $fuser->getrights();
232 
233  $nbmax = 10;
234  if ($fuser->rights->categorie->lire) {
235  $categorie = new Categorie($db);
236  $result = $categorie->fetch($id);
237  if ($result > 0) {
238  $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
239  $pdir = get_exdir($categorie->id, 2, 0, 0, $categorie, 'category').$categorie->id."/photos/";
240  $dir = $dir.'/'.$pdir;
241 
242  $cat = array(
243  'id' => $categorie->id,
244  'id_mere' => $categorie->id_mere,
245  'label' => $categorie->label,
246  'description' => $categorie->description,
247  'socid' => $categorie->socid,
248  //'visible'=>$categorie->visible,
249  'type' => $categorie->type,
250  'dir' => $pdir,
251  'photos' => $categorie->liste_photos($dir, $nbmax)
252  );
253 
254  $cats = $categorie->get_filles();
255  if (count($cats) > 0) {
256  foreach ($cats as $fille) {
257  $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
258  $pdir = get_exdir($fille->id, 2, 0, 0, $categorie, 'category').$fille->id."/photos/";
259  $dir = $dir.'/'.$pdir;
260  $cat['filles'][] = array(
261  'id'=>$fille->id,
262  'id_mere' => $categorie->id_mere,
263  'label'=>$fille->label,
264  'description'=>$fille->description,
265  'socid'=>$fille->socid,
266  //'visible'=>$fille->visible,
267  'type'=>$fille->type,
268  'dir' => $pdir,
269  'photos' => $fille->liste_photos($dir, $nbmax)
270  );
271  }
272  }
273 
274  // Create
275  $objectresp = array(
276  'result'=>array('result_code'=>'OK', 'result_label'=>''),
277  'categorie'=> $cat
278  );
279  } else {
280  $error++;
281  $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id;
282  }
283  } else {
284  $error++;
285  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
286  }
287  }
288 
289  if ($error) {
290  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
291  }
292 
293  return $objectresp;
294 }
295 
296 // Return the results.
297 $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