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