dolibarr 21.0.0-beta
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 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$server->wsdl->schemaTargetNamespace = $ns;
99
100// Define WSDL Authentication object
101$server->wsdl->addComplexType(
102 'authentication',
103 'complexType',
104 'struct',
105 'all',
106 '',
107 array(
108 'dolibarrkey' => array('name' => 'dolibarrkey', 'type' => 'xsd:string'),
109 'sourceapplication' => array('name' => 'sourceapplication', 'type' => 'xsd:string'),
110 'login' => array('name' => 'login', 'type' => 'xsd:string'),
111 'password' => array('name' => 'password', 'type' => 'xsd:string'),
112 'entity' => array('name' => 'entity', 'type' => 'xsd:string'),
113 )
114);
115
116// Define WSDL Return object
117$server->wsdl->addComplexType(
118 'result',
119 'complexType',
120 'struct',
121 'all',
122 '',
123 array(
124 'result_code' => array('name' => 'result_code', 'type' => 'xsd:string'),
125 'result_label' => array('name' => 'result_label', 'type' => 'xsd:string'),
126 )
127);
128
129// Define other specific objects
130$server->wsdl->addComplexType(
131 'element',
132 'complexType',
133 'struct',
134 'all',
135 '',
136 array(
137 'id' => array('name' => 'id', 'type' => 'xsd:int'),
138 'user' => array('name' => 'user', 'type' => 'xsd:int'),
139 )
140);
141
142$server->wsdl->addComplexType(
143 'elementsArray',
144 'complexType',
145 'array',
146 'sequence',
147 '',
148 array(
149 'elements' => array(
150 'name' => 'elementsArray',
151 'type' => 'tns:element',
152 'minOccurs' => '0',
153 'maxOccurs' => 'unbounded'
154 )
155 )
156);
157
158$project_elements = array();
159foreach ($listofreferent as $key => $label) {
160 $project_elements[$key] = array('name' => $key, 'type' => 'tns:elementsArray');
161}
162$server->wsdl->addComplexType(
163 'elements',
164 'complexType',
165 'struct',
166 'all',
167 '',
168 $project_elements
169);
170
171// Define project
172$project_fields = array(
173 'id' => array('name' => 'id', 'type' => 'xsd:string'),
174 'ref' => array('name' => 'ref', 'type' => 'xsd:string'),
175 'label' => array('name' => 'label', 'type' => 'xsd:string'),
176 'thirdparty_id' => array('name' => 'thirdparty_id', 'type' => 'xsd:int'),
177 'public' => array('name' => 'public', 'type' => 'xsd:int'),
178 'status' => array('name' => 'status', 'type' => 'xsd:int'),
179 'date_start' => array('name' => 'date_start', 'type' => 'xsd:date'),
180 'date_end' => array('name' => 'date_end', 'type' => 'xsd:date'),
181 'budget' => array('name' => 'budget', 'type' => 'xsd:int'),
182 'description' => array('name' => 'description', 'type' => 'xsd:string'),
183 'elements' => array('name' => 'elements', 'type' => 'tns:elements')
184);
185
186$elementtype = 'project';
187
188//Retrieve all extrafield for thirdsparty
189// fetch optionals attributes and labels
190$extrafields = new ExtraFields($db);
191$extrafields->fetch_name_optionals_label($elementtype, true);
192$extrafield_array = null;
193if (is_array($extrafields->attributes) && $extrafields->attributes[$elementtype]['count'] > 0) {
194 $extrafield_array = array();
195}
196if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
197 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
198 //$value=$object->array_options["options_".$key];
199 $type = $extrafields->attributes[$elementtype]['type'][$key];
200 if ($type == 'date' || $type == 'datetime') {
201 $type = 'xsd:dateTime';
202 } else {
203 $type = 'xsd:string';
204 }
205 $extrafield_array['options_'.$key] = array('name' => 'options_'.$key, 'type' => $type);
206 }
207}
208if (is_array($extrafield_array)) {
209 $project_fields = array_merge($project_fields, $extrafield_array);
210}
211
212$server->wsdl->addComplexType(
213 'project',
214 'complexType',
215 'struct',
216 'all',
217 '',
218 $project_fields
219);
220
221// 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
222// Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
223// http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
224$styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
225$styleuse = 'encoded'; // encoded/literal/literal wrapped
226// Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
227
228// Register WSDL
229$server->register(
230 'createProject',
231 // Entry values
232 array('authentication' => 'tns:authentication', 'project' => 'tns:project'),
233 // Exit values
234 array('result' => 'tns:result', 'id' => 'xsd:string', 'ref' => 'xsd:string'),
235 $ns,
236 $ns.'#createProject',
237 $styledoc,
238 $styleuse,
239 'WS to create project'
240);
241
242// Register WSDL
243$server->register(
244 'getProject',
245 // Entry values
246 array('authentication' => 'tns:authentication', 'id' => 'xsd:string', 'ref' => 'xsd:string'),
247 // Exit values
248 array('result' => 'tns:result', 'project' => 'tns:project'),
249 $ns,
250 $ns.'#getProject',
251 $styledoc,
252 $styleuse,
253 'WS to get project'
254);
255
256// Full methods code
264function createProject($authentication, $project)
265{
266 global $db, $conf;
267
268 dol_syslog("Function: createProject login=".$authentication['login']);
269
270 if ($authentication['entity']) {
271 $conf->entity = $authentication['entity'];
272 }
273
274 // Init and check authentication
275 $objectresp = array();
276 $errorcode = '';
277 $errorlabel = '';
278 $error = 0;
279 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
280 // Check parameters
281 if (empty($project['ref'])) {
282 $error++;
283 $errorcode = 'KO';
284 $errorlabel = "Name is mandatory.";
285 }
286
287
288 if (!$error) {
289 $fuser->loadRights();
290
291 if ($fuser->hasRight('projet', 'creer')) {
292 $newobject = new Project($db);
293 $newobject->ref = $project['ref'];
294 $newobject->title = $project['label'];
295 $newobject->socid = $project['thirdparty_id'];
296 $newobject->public = $project['public'];
297 $newobject->statut = $project['status'];
298 $newobject->date_start = dol_stringtotime($project['date_start'], 'dayrfc');
299 $newobject->date_end = dol_stringtotime($project['date_end'], 'dayrfc');
300 $newobject->budget_amount = $project['budget'];
301 $newobject->description = $project['description'];
302
303 $elementtype = 'project';
304
305 // Retrieve all extrafields for project
306 // fetch optionals attributes and labels
307 $extrafields = new ExtraFields($db);
308 $extrafields->fetch_name_optionals_label($elementtype, true);
309 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
310 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
311 $key = 'options_'.$key;
312 $newobject->array_options[$key] = $project[$key];
313 }
314 }
315
316 $db->begin();
317
318 $result = $newobject->create($fuser);
319 if (!$error && $result > 0) {
320 // Add myself as project leader
321 $result = $newobject->add_contact($fuser->id, 'PROJECTLEADER', 'internal');
322 if ($result < 0) {
323 $error++;
324 }
325 } else {
326 $error++;
327 }
328
329 if (!$error) {
330 $db->commit();
331 $objectresp = array('result' => array('result_code' => 'OK', 'result_label' => ''), 'id' => $newobject->id, 'ref' => $newobject->ref);
332 } else {
333 $db->rollback();
334 $error++;
335 $errorcode = 'KO';
336 $errorlabel = $newobject->error;
337 }
338 } else {
339 $error++;
340 $errorcode = 'PERMISSION_DENIED';
341 $errorlabel = 'User does not have permission for this request';
342 }
343 }
344
345 if ($error) {
346 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
347 }
348
349 return $objectresp;
350}
351
360function getProject($authentication, $id = '', $ref = '')
361{
362 global $db, $conf;
363
364 dol_syslog("Function: getProject login=".$authentication['login']." id=".$id." ref=".$ref);
365
366 if ($authentication['entity']) {
367 $conf->entity = $authentication['entity'];
368 }
369
370 // Init and check authentication
371 $objectresp = array();
372 $errorcode = '';
373 $errorlabel = '';
374 $error = 0;
375 $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
376 // Check parameters
377 if (!$error && (($id && $ref))) {
378 $error++;
379 $errorcode = 'BAD_PARAMETERS';
380 $errorlabel = "Parameter id and ref can't be both provided. You must choose one or other but not both.";
381 }
382
383 if (!$error) {
384 $fuser->loadRights();
385
386 if ($fuser->hasRight('projet', 'lire')) {
387 $project = new Project($db);
388 $result = $project->fetch($id, $ref);
389 if ($result > 0) {
390 $project_result_fields = array(
391 'id' => $project->id,
392 'ref' => $project->ref,
393 'label' => $project->title,
394 'thirdparty_id' => $project->socid,
395 'public' => $project->public,
396 'status' => $project->statut,
397 'date_start' => $project->date_start ? dol_print_date($project->date_start, 'dayrfc') : '',
398 'date_end' => $project->date_end ? dol_print_date($project->date_end, 'dayrfc') : '',
399 'budget' => $project->budget_amount,
400 'description' => $project->description,
401 );
402
403 $elementtype = 'project';
404
405 //Retrieve all extrafields for project
406 $extrafields = new ExtraFields($db);
407 $extrafields->fetch_name_optionals_label($elementtype, true);
408
409 //Get extrafield values
410 if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
411 $project->fetch_optionals();
412 foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
413 $project_result_fields = array_merge($project_result_fields, array('options_'.$key => $project->array_options['options_'.$key]));
414 }
415 }
416
417 //Get linked elements
418 global $listofreferent;
419 $elements = array();
420 foreach ($listofreferent as $key => $tablename) {
421 $elements[$key] = array();
422 $element_array = $project->get_element_list($key, $tablename);
423 if (count($element_array) > 0 && is_array($element_array)) {
424 foreach ($element_array as $element) {
425 $tmp = explode('_', $element);
426 $idofelement = count($tmp) > 0 ? $tmp[0] : "";
427 $idofelementuser = count($tmp) > 1 ? $tmp[1] : "";
428 $elements[$key][] = array('id' => $idofelement, 'user' => $idofelementuser);
429 }
430 }
431 }
432 $project_result_fields['elements'] = $elements;
433
434 //Result
435 $objectresp = array(
436 'result' => array('result_code' => 'OK', 'result_label' => ''),
437 'project' => $project_result_fields
438 );
439 } else {
440 $error++;
441 $errorcode = 'NOT_FOUND';
442 $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref;
443 }
444 } else {
445 $error++;
446 $errorcode = 'PERMISSION_DENIED';
447 $errorlabel = 'User does not have permission for this request';
448 }
449 }
450
451 if ($error) {
452 $objectresp = array('result' => array('result_code' => $errorcode, 'result_label' => $errorlabel));
453 }
454
455 return $objectresp;
456}
457
458// Return the results.
459$server->service(file_get_contents("php://input"));
$id
Definition account.php:48
Class to manage standard extra fields.
Class to manage projects.
dol_stringtotime($string, $gm=1)
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:431
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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