dolibarr 23.0.3
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-2025 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// @phan-suppress-next-line PhanUndeclaredProperty
75$server->wsdl->schemaTargetNamespace = $ns;
76
77
78// Define WSDL content
79$server->wsdl->addComplexType(
80 'authentication',
81 'complexType',
82 'struct',
83 'all',
84 '',
85 array(
86 'dolibarrkey' => array('name' => 'dolibarrkey', 'type' => 'xsd:string'),
87 'sourceapplication' => array('name' => 'sourceapplication', 'type' => 'xsd:string'),
88 'login' => array('name' => 'login', 'type' => 'xsd:string'),
89 'password' => array('name' => 'password', 'type' => 'xsd:string'),
90 'entity' => array('name' => 'entity', 'type' => 'xsd:string'),
91 )
92);
93
94/*
95 * Une catégorie
96 */
97$server->wsdl->addComplexType(
98 'categorie',
99 'complexType',
100 'struct',
101 'all',
102 '',
103 array(
104 'id' => array('name' => 'id', 'type' => 'xsd:string'),
105 'id_mere' => array('name' => 'id_mere', 'type' => 'xsd:string'),
106 'label' => array('name' => 'label', 'type' => 'xsd:string'),
107 'description' => array('name' => 'description', 'type' => 'xsd:string'),
108 'socid' => array('name' => 'socid', 'type' => 'xsd:string'),
109 'type' => array('name' => 'type', 'type' => 'xsd:string'),
110 'visible' => array('name' => 'visible', 'type' => 'xsd:string'),
111 'dir' => array('name' => 'dir', 'type' => 'xsd:string'),
112 'photos' => array('name' => 'photos', 'type' => 'tns:PhotosArray'),
113 'filles' => array('name' => 'filles', 'type' => 'tns:FillesArray')
114 )
115);
116
117/*
118 * The child categories, sub-tables of the category
119 */
120$server->wsdl->addComplexType(
121 'FillesArray',
122 'complexType',
123 'array',
124 '',
125 'SOAP-ENC:Array',
126 array(),
127 array(
128 array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:categorie[]')
129 ),
130 'tns:categorie'
131);
132
133/*
134 * Image of product
135*/
136$server->wsdl->addComplexType(
137 'PhotosArray',
138 'complexType',
139 'array',
140 'sequence',
141 '',
142 array(
143 'image' => array(
144 'name' => 'image',
145 'type' => 'tns:image',
146 'minOccurs' => '0',
147 'maxOccurs' => 'unbounded'
148 )
149 )
150);
151
152/*
153 * An image
154*/
155$server->wsdl->addComplexType(
156 'image',
157 'complexType',
158 'struct',
159 'all',
160 '',
161 array(
162 'photo' => array('name' => 'photo', 'type' => 'xsd:string'),
163 'photo_vignette' => array('name' => 'photo_vignette', 'type' => 'xsd:string'),
164 'imgWidth' => array('name' => 'imgWidth', 'type' => 'xsd:string'),
165 'imgHeight' => array('name' => 'imgHeight', 'type' => 'xsd:string')
166 )
167);
168
169/*
170 * Retour
171 */
172$server->wsdl->addComplexType(
173 'result',
174 'complexType',
175 'struct',
176 'all',
177 '',
178 array(
179 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
180 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
181 )
182);
183
184// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
185// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
186// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
187$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
188$styleuse = 'encoded'; // encoded/literal/literal wrapped
189// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
190
191
192// Register WSDL
193$server->register(
194 'getCategory',
195 // Entry values
196 array('authentication' => 'tns:authentication', 'id' => 'xsd:string'),
197 // Exit values
198 array('result' => 'tns:result', 'categorie' => 'tns:categorie'),
199 $ns,
200 $ns.'#getCategory',
201 $styledoc,
202 $styleuse,
203 'WS to get category'
204);
205
206
214function getCategory($authentication, $id)
215{
216 global $db, $conf, $langs;
217
218 $nbmax = 10;
219
220 dol_syslog("Function: getCategory login=".$authentication['login']." id=".$id);
221
222 if ($authentication['entity']) {
223 $conf->entity = $authentication['entity'];
224 }
225
226 $objectresp = array();
227 $errorcode = '';
228 $errorlabel = '';
229 $error = 0;
230 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
231
232 if (!$error && !$id) {
233 $error++;
234 $errorcode = 'BAD_PARAMETERS';
235 $errorlabel = "Parameter id must be provided.";
236 }
237
238 if (!$error) {
239 $fuser->loadRights();
240
241 $nbmax = 10;
242 if ($fuser->hasRight('categorie', 'lire')) {
243 $categorie = new Categorie($db);
244 $result = $categorie->fetch($id);
245 if ($result > 0) {
246 $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
247 $pdir = get_exdir($categorie->id, 2, 0, 0, $categorie, 'category').$categorie->id."/photos/";
248 $dir = $dir.'/'.$pdir;
249
250 $cat = array(
251 'id' => $categorie->id,
252 'id_mere' => $categorie->fk_parent,
253 'label' => $categorie->label,
254 'description' => $categorie->description,
255 'socid' => $categorie->socid,
256 //'visible'=>$categorie->visible,
257 'type' => $categorie->type,
258 'dir' => $pdir,
259 'photos' => $categorie->liste_photos($dir, $nbmax)
260 );
261
262 $cats = $categorie->get_filles();
263 if (count($cats) > 0) {
264 foreach ($cats as $child_cat) {
265 $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
266 $pdir = get_exdir($child_cat->id, 2, 0, 0, $categorie, 'category').$child_cat->id."/photos/";
267 $dir = $dir.'/'.$pdir;
268 $cat['filles'][] = array(
269 'id' => $child_cat->id,
270 'id_mere' => $categorie->fk_parent,
271 'label' => $child_cat->label,
272 'description' => $child_cat->description,
273 'socid' => $child_cat->socid,
274 //'visible'=>$child_cat->visible,
275 'type' => $child_cat->type,
276 'dir' => $pdir,
277 'photos' => $child_cat->liste_photos($dir, $nbmax)
278 );
279 }
280 }
281
282 // Create
283 $objectresp = array(
284 'result' => array('result_code' => 'OK', 'result_label' => ''),
285 'categorie' => $cat
286 );
287 } else {
288 $error++;
289 $errorcode = 'NOT_FOUND';
290 $errorlabel = 'Object not found for id='.$id;
291 }
292 } else {
293 $error++;
294 $errorcode = 'PERMISSION_DENIED';
295 $errorlabel = 'User does not have permission for this request';
296 }
297 }
298
299 if ($error) {
300 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
301 }
302
303 return $objectresp;
304}
305
306// Return the results.
307$server->service(file_get_contents("php://input"));
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
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.
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