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