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 $newobject->array_options[$key] = $project[$key];
314 }
315 }
316
317 $db->begin();
318
319 $result = $newobject->create($fuser);
320 if (!$error && $result > 0) {
321 // Add myself as project leader
322 $result = $newobject->add_contact($fuser->id, 'PROJECTLEADER', 'internal');
323 if ($result < 0) {
324 $error++;
325 }
326 } else {
327 $error++;
328 }
329
330 if (!$error) {
331 $db->commit();
332 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref);
333 } else {
334 $db->rollback();
335 $error++;
336 $errorcode = 'KO';
337 $errorlabel = $newobject->error;
338 }
339 } else {
340 $error++;
341 $errorcode = 'PERMISSION_DENIED';
342 $errorlabel = 'User does not have permission for this request';
343 }
344 }
345
346 if ($error) {
347 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
348 }
349
350 return $objectresp;
351}
352
361function getProject($authentication, $id = '', $ref = '')
362{
363 global $db, $conf;
364
365 dol_syslog("Function: getProject login=".$authentication['login']." id=".$id." ref=".$ref);
366
367 if ($authentication['entity']) {
368 $conf->entity = $authentication['entity'];
369 }
370
371 // Init and check authentication
372 $objectresp = array();
373 $errorcode = '';
374 $errorlabel = '';
375 $error = 0;
376 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
377 // Check parameters
378 if (!$error && (($id && $ref))) {
379 $error++;
380 $errorcode = 'BAD_PARAMETERS';
381 $errorlabel = "Parameter id and ref can't be both provided. You must choose one or other but not both.";
382 }
383
384 if (!$error) {
385 $fuser->loadRights();
386
387 if ($fuser->hasRight('projet', 'lire')) {
388 $project = new Project($db);
389 $result = $project->fetch((int) $id, $ref);
390 if ($result > 0) {
391 $project_result_fields = array(
392 'id' => $project->id,
393 'ref' => $project->ref,
394 'label' => $project->title,
395 'thirdparty_id' => $project->socid,
396 'public' => $project->public,
397 'status' => $project->statut,
398 'date_start' => $project->date_start ? dol_print_date($project->date_start, 'dayrfc') : '',
399 'date_end' => $project->date_end ? dol_print_date($project->date_end, 'dayrfc') : '',
400 'budget' => $project->budget_amount,
401 'description' => $project->description,
402 );
403
404 $elementtype = 'project';
405
406 //Retrieve all extrafields for project
407 $extrafields = new ExtraFields($db);
408 $extrafields->fetch_name_optionals_label($elementtype, true);
409
410 //Get extrafield values
411 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
412 $project->fetch_optionals();
413 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
414 $project_result_fields = array_merge($project_result_fields, array('options_'.$key => $project->array_options['options_'.$key]));
415 }
416 }
417
418 //Get linked elements
419 global $listofreferent;
420 $elements = array();
421 foreach ($listofreferent as $key => $tablename) {
422 $elements[$key] = array();
423 $element_array = $project->get_element_list($key, $tablename);
424 if (is_array($element_array) && count($element_array) > 0) {
425 foreach ($element_array as $element) {
426 $tmp = explode('_', $element);
427 $idofelement = count($tmp) > 0 ? $tmp[0] : "";
428 $idofelementuser = count($tmp) > 1 ? $tmp[1] : "";
429 $elements[$key][] = array('id' => $idofelement, 'user' => $idofelementuser);
430 }
431 }
432 }
433 $project_result_fields['elements'] = $elements;
434
435 //Result
436 $objectresp = array(
437 'result' => array('result_code' => 'OK', 'result_label' => ''),
438 'project' => $project_result_fields
439 );
440 } else {
441 $error++;
442 $errorcode = 'NOT_FOUND';
443 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref;
444 }
445 } else {
446 $error++;
447 $errorcode = 'PERMISSION_DENIED';
448 $errorlabel = 'User does not have permission for this request';
449 }
450 }
451
452 if ($error) {
453 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
454 }
455
456 return $objectresp;
457}
458
459// Return the results.
460$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 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:442
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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