dolibarr 21.0.0-beta
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 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
26if (!defined('NOCSRFCHECK')) {
27 define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
28}
29if (!defined('NOTOKENRENEWAL')) {
30 define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
31}
32if (!defined('NOREQUIREMENU')) {
33 define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
34}
35if (!defined('NOREQUIREHTML')) {
36 define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
37}
38if (!defined('NOREQUIREAJAX')) {
39 define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
40}
41if (!defined("NOLOGIN")) {
42 define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
43}
44if (!defined("NOSESSION")) {
45 define("NOSESSION", '1');
46}
47
48require '../main.inc.php';
49require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
50require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
51require_once DOL_DOCUMENT_ROOT."/categories/class/categorie.class.php";
57dol_syslog("Call Dolibarr webservices interfaces");
58
59// Enable and test if module web services is enabled
60if (!getDolGlobalString('MAIN_MODULE_WEBSERVICES')) {
61 $langs->load("admin");
62 dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
63 print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
64 print $langs->trans("ToActivateModule");
65 exit;
66}
67
68// Create the soap Object
69$server = new nusoap_server();
70$server->soap_defencoding = 'UTF-8';
71$server->decode_utf8 = false;
72$ns = 'http://www.dolibarr.org/ns/';
73$server->configureWSDL('WebServicesDolibarrCategorie', $ns);
74$server->wsdl->schemaTargetNamespace = $ns;
75
76
77// Define WSDL content
78$server->wsdl->addComplexType(
79 'authentication',
80 'complexType',
81 'struct',
82 'all',
83 '',
84 array(
85 'dolibarrkey' => array('name' => 'dolibarrkey', 'type' => 'xsd:string'),
86 'sourceapplication' => array('name' => 'sourceapplication', 'type' => 'xsd:string'),
87 'login' => array('name' => 'login', 'type' => 'xsd:string'),
88 'password' => array('name' => 'password', 'type' => 'xsd:string'),
89 'entity' => array('name' => 'entity', 'type' => 'xsd:string'),
90 )
91);
92
93/*
94 * Une catégorie
95 */
96$server->wsdl->addComplexType(
97 'categorie',
98 'complexType',
99 'struct',
100 'all',
101 '',
102 array(
103 'id' => array('name' => 'id', 'type' => 'xsd:string'),
104 'id_mere' => array('name' => 'id_mere', 'type' => 'xsd:string'),
105 'label' => array('name' => 'label', 'type' => 'xsd:string'),
106 'description' => array('name' => 'description', 'type' => 'xsd:string'),
107 'socid' => array('name' => 'socid', 'type' => 'xsd:string'),
108 'type' => array('name' => 'type', 'type' => 'xsd:string'),
109 'visible' => array('name' => 'visible', 'type' => 'xsd:string'),
110 'dir' => array('name' => 'dir', 'type' => 'xsd:string'),
111 'photos' => array('name' => 'photos', 'type' => 'tns:PhotosArray'),
112 'filles' => array('name' => 'filles', 'type' => 'tns:FillesArray')
113 )
114);
115
116/*
117 * The child categories, sub-tables of the category
118 */
119$server->wsdl->addComplexType(
120 'FillesArray',
121 'complexType',
122 'array',
123 '',
124 'SOAP-ENC:Array',
125 array(),
126 array(
127 array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:categorie[]')
128 ),
129 'tns:categorie'
130);
131
132/*
133 * Image of product
134*/
135$server->wsdl->addComplexType(
136 'PhotosArray',
137 'complexType',
138 'array',
139 'sequence',
140 '',
141 array(
142 'image' => array(
143 'name' => 'image',
144 'type' => 'tns:image',
145 'minOccurs' => '0',
146 'maxOccurs' => 'unbounded'
147 )
148 )
149);
150
151/*
152 * An image
153*/
154$server->wsdl->addComplexType(
155 'image',
156 'complexType',
157 'struct',
158 'all',
159 '',
160 array(
161 'photo' => array('name' => 'photo', 'type' => 'xsd:string'),
162 'photo_vignette' => array('name' => 'photo_vignette', 'type' => 'xsd:string'),
163 'imgWidth' => array('name' => 'imgWidth', 'type' => 'xsd:string'),
164 'imgHeight' => array('name' => 'imgHeight', 'type' => 'xsd:string')
165 )
166);
167
168/*
169 * Retour
170 */
171$server->wsdl->addComplexType(
172 'result',
173 'complexType',
174 'struct',
175 'all',
176 '',
177 array(
178 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
179 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
180 )
181);
182
183// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
184// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
185// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
186$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
187$styleuse = 'encoded'; // encoded/literal/literal wrapped
188// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
189
190
191// Register WSDL
192$server->register(
193 'getCategory',
194 // Entry values
195 array('authentication' => 'tns:authentication', 'id' => 'xsd:string'),
196 // Exit values
197 array('result' => 'tns:result', 'categorie' => 'tns:categorie'),
198 $ns,
199 $ns.'#getCategory',
200 $styledoc,
201 $styleuse,
202 'WS to get category'
203);
204
205
213function getCategory($authentication, $id)
214{
215 global $db, $conf, $langs;
216
217 $nbmax = 10;
218
219 dol_syslog("Function: getCategory login=".$authentication['login']." id=".$id);
220
221 if ($authentication['entity']) {
222 $conf->entity = $authentication['entity'];
223 }
224
225 $objectresp = array();
226 $errorcode = '';
227 $errorlabel = '';
228 $error = 0;
229 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
230
231 if (!$error && !$id) {
232 $error++;
233 $errorcode = 'BAD_PARAMETERS';
234 $errorlabel = "Parameter id must be provided.";
235 }
236
237 if (!$error) {
238 $fuser->loadRights();
239
240 $nbmax = 10;
241 if ($fuser->hasRight('categorie', 'lire')) {
242 $categorie = new Categorie($db);
243 $result = $categorie->fetch($id);
244 if ($result > 0) {
245 $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
246 $pdir = get_exdir($categorie->id, 2, 0, 0, $categorie, 'category').$categorie->id."/photos/";
247 $dir = $dir.'/'.$pdir;
248
249 $cat = array(
250 'id' => $categorie->id,
251 'id_mere' => $categorie->id_mere,
252 'label' => $categorie->label,
253 'description' => $categorie->description,
254 'socid' => $categorie->socid,
255 //'visible'=>$categorie->visible,
256 'type' => $categorie->type,
257 'dir' => $pdir,
258 'photos' => $categorie->liste_photos($dir, $nbmax)
259 );
260
261 $cats = $categorie->get_filles();
262 if (count($cats) > 0) {
263 foreach ($cats as $child_cat) {
264 $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
265 $pdir = get_exdir($child_cat->id, 2, 0, 0, $categorie, 'category').$child_cat->id."/photos/";
266 $dir = $dir.'/'.$pdir;
267 $cat['filles'][] = array(
268 'id' => $child_cat->id,
269 'id_mere' => $categorie->id_mere,
270 'label' => $child_cat->label,
271 'description' => $child_cat->description,
272 'socid' => $child_cat->socid,
273 //'visible'=>$child_cat->visible,
274 'type' => $child_cat->type,
275 'dir' => $pdir,
276 'photos' => $child_cat->liste_photos($dir, $nbmax)
277 );
278 }
279 }
280
281 // Create
282 $objectresp = array(
283 'result' => array('result_code' => 'OK', 'result_label' => ''),
284 'categorie' => $cat
285 );
286 } else {
287 $error++;
288 $errorcode = 'NOT_FOUND';
289 $errorlabel = 'Object not found for id='.$id;
290 }
291 } else {
292 $error++;
293 $errorcode = 'PERMISSION_DENIED';
294 $errorlabel = 'User does not have permission for this request';
295 }
296 }
297
298 if ($error) {
299 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
300 }
301
302 return $objectresp;
303}
304
305// Return the results.
306$server->service(file_get_contents("php://input"));
$id
Definition account.php:48
Class to manage categories.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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:37