dolibarr 25.0.0-alpha
server_project.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2016 Ion Agorria <ion@agorria.com>
4 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
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.'/core/lib/date.lib.php';
52require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
53require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
54require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
55
61dol_syslog("Call Dolibarr webservices interfaces");
62
63$langs->load("main");
64
65// Enable and test if module web services is enabled
66if (!getDolGlobalString('MAIN_MODULE_WEBSERVICES')) {
67 $langs->load("admin");
68 dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
69 print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
70 print $langs->trans("ToActivateModule");
71 exit;
72}
73
74// Create associated types array, with each table
75$listofreferent = array(
76 'propal' => 'propal',
77 'order' => 'commande',
78 'invoice' => 'facture',
79 'invoice_predefined' => 'facture_rec',
80 'proposal_supplier' => 'commande_fournisseur',
81 'order_supplier' => 'commande_fournisseur',
82 'invoice_supplier' => 'facture_fourn',
83 'contract' => 'contrat',
84 'intervention' => 'fichinter',
85 'trip' => 'deplacement',
86 'expensereport' => 'expensereport_det',
87 'donation' => 'don',
88 'agenda' => 'actioncomm',
89 'project_task' => 'projet_task',
90);
91
92// Create the soap Object
93$server = new nusoap_server();
94$server->soap_defencoding = 'UTF-8';
95$server->decode_utf8 = false;
96$ns = 'http://www.dolibarr.org/ns/';
97$server->configureWSDL('WebServicesDolibarrOther', $ns);
98// @phan-suppress-next-line PhanUndeclaredProperty
99$server->wsdl->schemaTargetNamespace = $ns;
100
101// Define WSDL Authentication object
102$server->wsdl->addComplexType(
103 'authentication',
104 'complexType',
105 'struct',
106 'all',
107 '',
108 array(
109 'dolibarrkey' => array('name' => 'dolibarrkey', 'type' => 'xsd:string'),
110 'sourceapplication' => array('name' => 'sourceapplication', 'type' => 'xsd:string'),
111 'login' => array('name' => 'login', 'type' => 'xsd:string'),
112 'password' => array('name' => 'password', 'type' => 'xsd:string'),
113 'entity' => array('name' => 'entity', 'type' => 'xsd:string'),
114 )
115);
116
117// Define WSDL Return object
118$server->wsdl->addComplexType(
119 'result',
120 'complexType',
121 'struct',
122 'all',
123 '',
124 array(
125 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
126 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
127 )
128);
129
130// Define other specific objects
131$server->wsdl->addComplexType(
132 'element',
133 'complexType',
134 'struct',
135 'all',
136 '',
137 array(
138 'id' => array('name' => 'id', 'type' => 'xsd:int'),
139 'user' => array('name' => 'user', 'type' => 'xsd:int'),
140 )
141);
142
143$server->wsdl->addComplexType(
144 'elementsArray',
145 'complexType',
146 'array',
147 'sequence',
148 '',
149 array(
150 'elements' => array(
151 'name' => 'elementsArray',
152 'type' => 'tns:element',
153 'minOccurs' => '0',
154 'maxOccurs' => 'unbounded'
155 )
156 )
157);
158
159$project_elements = array();
160foreach ($listofreferent as $key => $label) {
161 $project_elements[$key] = array('name' => $key, 'type' => 'tns:elementsArray');
162}
163$server->wsdl->addComplexType(
164 'elements',
165 'complexType',
166 'struct',
167 'all',
168 '',
169 $project_elements
170);
171
172// Define project
173$project_fields = array(
174 'id' => array('name' => 'id', 'type' => 'xsd:string'),
175 'ref' => array('name' => 'ref', 'type' => 'xsd:string'),
176 'label' => array('name' => 'label', 'type' => 'xsd:string'),
177 'thirdparty_id' => array('name' => 'thirdparty_id', 'type' => 'xsd:int'),
178 'public' => array('name' => 'public', 'type' => 'xsd:int'),
179 'status' => array('name' => 'status', 'type' => 'xsd:int'),
180 'date_start' => array('name' => 'date_start', 'type' => 'xsd:date'),
181 'date_end' => array('name' => 'date_end', 'type' => 'xsd:date'),
182 'budget' => array('name' => 'budget', 'type' => 'xsd:int'),
183 'description' => array('name' => 'description', 'type' => 'xsd:string'),
184 'elements' => array('name' => 'elements', 'type' => 'tns:elements')
185);
186
187$elementtype = 'project';
188
189//Retrieve all extrafield for thirdsparty
190// fetch optionals attributes and labels
191$extrafields = new ExtraFields($db);
192$extrafields->fetch_name_optionals_label($elementtype, true);
193$extrafield_array = null;
194if (is_array($extrafields->attributes) && $extrafields->attributes[$elementtype]['count'] > 0) {
195 $extrafield_array = array();
196}
197if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
198 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
199 //$value=$object->array_options["options_".$key];
200 $type = $extrafields->attributes[$elementtype]['type'][$key];
201 if ($type == 'date' || $type == 'datetime') {
202 $type = 'xsd:dateTime';
203 } else {
204 $type = 'xsd:string';
205 }
206 $extrafield_array['options_'.$key] = array('name' => 'options_'.$key, 'type' => $type);
207 }
208}
209if (is_array($extrafield_array)) {
210 $project_fields = array_merge($project_fields, $extrafield_array);
211}
212
213$server->wsdl->addComplexType(
214 'project',
215 'complexType',
216 'struct',
217 'all',
218 '',
219 $project_fields
220);
221
222// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
223// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
224// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
225$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
226$styleuse = 'encoded'; // encoded/literal/literal wrapped
227// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
228
229// Register WSDL
230$server->register(
231 'createProject',
232 // Entry values
233 array('authentication' => 'tns:authentication', 'project' => 'tns:project'),
234 // Exit values
235 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string'),
236 $ns,
237 $ns.'#createProject',
238 $styledoc,
239 $styleuse,
240 'WS to create project'
241);
242
243// Register WSDL
244$server->register(
245 'getProject',
246 // Entry values
247 array('authentication' => 'tns:authentication', 'id' => 'xsd:string', 'ref' => 'xsd:string'),
248 // Exit values
249 array('result' => 'tns:result', 'project' => 'tns:project'),
250 $ns,
251 $ns.'#getProject',
252 $styledoc,
253 $styleuse,
254 'WS to get project'
255);
256
257// Full methods code
265function createProject($authentication, $project)
266{
267 global $db, $conf;
268
269 dol_syslog("Function: createProject login=".$authentication['login']);
270
271 if ($authentication['entity']) {
272 $conf->entity = $authentication['entity'];
273 }
274
275 // Init and check authentication
276 $objectresp = array();
277 $errorcode = '';
278 $errorlabel = '';
279 $error = 0;
280 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
281 // Check parameters
282 if (empty($project['ref'])) {
283 $error++;
284 $errorcode = 'KO';
285 $errorlabel = "Name is mandatory.";
286 }
287
288
289 if (!$error) {
290 $fuser->loadRights();
291
292 if ($fuser->hasRight('projet', 'creer')) {
293 $newobject = new Project($db);
294 $newobject->ref = $project['ref'];
295 $newobject->title = $project['label'];
296 $newobject->socid = $project['thirdparty_id'];
297 $newobject->public = $project['public'];
298 $newobject->statut = $project['status'];
299 $newobject->date_start = dol_stringtotime($project['date_start'], 'dayrfc');
300 $newobject->date_end = dol_stringtotime($project['date_end'], 'dayrfc');
301 $newobject->budget_amount = $project['budget'];
302 $newobject->description = $project['description'];
303
304 $elementtype = 'project';
305
306 // Retrieve all extrafields for project
307 // fetch optionals attributes and labels
308 $extrafields = new ExtraFields($db);
309 $extrafields->fetch_name_optionals_label($elementtype, true);
310 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
311 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
312 $key = 'options_'.$key;
313 if (isset($project[$key])) {
314 $newobject->array_options[$key] = $project[$key];
315 }
316 }
317 }
318
319 $db->begin();
320
321 $result = $newobject->create($fuser);
322 if (!$error && $result > 0) {
323 // Add myself as project leader
324 $result = $newobject->add_contact($fuser->id, 'PROJECTLEADER', 'internal');
325 if ($result < 0) {
326 $error++;
327 }
328 } else {
329 $error++;
330 }
331
332 if (!$error) {
333 $db->commit();
334 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref);
335 } else {
336 $db->rollback();
337 $error++;
338 $errorcode = 'KO';
339 $errorlabel = $newobject->error;
340 }
341 } else {
342 $error++;
343 $errorcode = 'PERMISSION_DENIED';
344 $errorlabel = 'User does not have permission for this request';
345 }
346 }
347
348 if ($error) {
349 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
350 }
351
352 return $objectresp;
353}
354
363function getProject($authentication, $id = '', $ref = '')
364{
365 global $db, $conf;
366
367 dol_syslog("Function: getProject login=".$authentication['login']." id=".$id." ref=".$ref);
368
369 if ($authentication['entity']) {
370 $conf->entity = $authentication['entity'];
371 }
372
373 // Init and check authentication
374 $objectresp = array();
375 $errorcode = '';
376 $errorlabel = '';
377 $error = 0;
378 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
379 // Check parameters
380 if (!$error && (($id && $ref))) {
381 $error++;
382 $errorcode = 'BAD_PARAMETERS';
383 $errorlabel = "Parameter id and ref can't be both provided. You must choose one or other but not both.";
384 }
385
386 if (!$error) {
387 $fuser->loadRights();
388
389 if ($fuser->hasRight('projet', 'lire')) {
390 $project = new Project($db);
391 $result = $project->fetch((int) $id, $ref);
392 if ($result > 0) {
393 $project_result_fields = array(
394 'id' => $project->id,
395 'ref' => $project->ref,
396 'label' => $project->title,
397 'thirdparty_id' => $project->socid,
398 'public' => $project->public,
399 'status' => $project->statut,
400 'date_start' => $project->date_start ? dol_print_date($project->date_start, 'dayrfc') : '',
401 'date_end' => $project->date_end ? dol_print_date($project->date_end, 'dayrfc') : '',
402 'budget' => $project->budget_amount,
403 'description' => $project->description,
404 );
405
406 $elementtype = 'project';
407
408 //Retrieve all extrafields for project
409 $extrafields = new ExtraFields($db);
410 $extrafields->fetch_name_optionals_label($elementtype, true);
411
412 //Get extrafield values
413 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
414 $project->fetch_optionals();
415 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
416 $project_result_fields = array_merge($project_result_fields, array('options_'.$key => $project->array_options['options_'.$key]));
417 }
418 }
419
420 //Get linked elements
421 global $listofreferent;
422 $elements = array();
423 foreach ($listofreferent as $key => $tablename) {
424 $elements[$key] = array();
425 $element_array = $project->get_element_list($key, $tablename);
426 if (is_array($element_array) && count($element_array) > 0) {
427 foreach ($element_array as $element) {
428 $tmp = explode('_', $element);
429 $idofelement = count($tmp) > 0 ? $tmp[0] : "";
430 $idofelementuser = count($tmp) > 1 ? $tmp[1] : "";
431 $elements[$key][] = array('id' => $idofelement, 'user' => $idofelementuser);
432 }
433 }
434 }
435 $project_result_fields['elements'] = $elements;
436
437 //Result
438 $objectresp = array(
439 'result' => array('result_code' => 'OK', 'result_label' => ''),
440 'project' => $project_result_fields
441 );
442 } else {
443 $error++;
444 $errorcode = 'NOT_FOUND';
445 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref;
446 }
447 } else {
448 $error++;
449 $errorcode = 'PERMISSION_DENIED';
450 $errorlabel = 'User does not have permission for this request';
451 }
452 }
453
454 if ($error) {
455 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
456 }
457
458 return $objectresp;
459}
460
461// Return the results.
462$server->service(file_get_contents("php://input"));
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage standard extra fields.
Class to manage projects.
dol_stringtotime($string, $gm=1, $processnotimeasnoon=0)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition date.lib.php:452
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
createProject($authentication, $project)
Create project.
getProject($authentication, $id='', $ref='')
Get a project.
check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
Check authentication array and set error, errorcode, errorlabel.
Definition ws.lib.php:37