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