dolibarr  19.0.0-dev
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 
24 if (!defined('NOCSRFCHECK')) {
25  define('NOCSRFCHECK', '1'); // Do not check anti CSRF attack test
26 }
27 if (!defined('NOTOKENRENEWAL')) {
28  define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
29 }
30 if (!defined('NOREQUIREMENU')) {
31  define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
32 }
33 if (!defined('NOREQUIREHTML')) {
34  define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
35 }
36 if (!defined('NOREQUIREAJAX')) {
37  define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
38 }
39 if (!defined("NOLOGIN")) {
40  define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
41 }
42 if (!defined("NOSESSION")) {
43  define("NOSESSION", '1');
44 }
45 
46 require '../main.inc.php';
47 require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
48 require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
49 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
50 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
51 require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
52 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
53 
54 
55 dol_syslog("Call Dolibarr webservices interfaces");
56 
57 $langs->load("main");
58 
59 // Enable and test if module web services is enabled
60 if (empty($conf->global->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();
153 foreach ($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;
187 if (is_array($extrafields) && count($extrafields) > 0) {
188  $extrafield_array = array();
189 }
190 if (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 }
202 if (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
258 function 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 = ''; $errorlabel = '';
271  $error = 0;
272  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
273  // Check parameters
274  if (empty($project['ref'])) {
275  $error++; $errorcode = 'KO'; $errorlabel = "Name is mandatory.";
276  }
277 
278 
279  if (!$error) {
280  $fuser->getrights();
281 
282  if ($fuser->rights->projet->creer) {
283  $newobject = new Project($db);
284  $newobject->ref = $project['ref'];
285  $newobject->title = $project['label'];
286  $newobject->socid = $project['thirdparty_id'];
287  $newobject->public = $project['public'];
288  $newobject->statut = $project['status'];
289  $newobject->date_start = dol_stringtotime($project['date_start'], 'dayrfc');
290  $newobject->date_end = dol_stringtotime($project['date_end'], 'dayrfc');
291  $newobject->budget_amount = $project['budget'];
292  $newobject->description = $project['description'];
293 
294  $elementtype = 'project';
295 
296  // Retrieve all extrafields for project
297  // fetch optionals attributes and labels
298  $extrafields = new ExtraFields($db);
299  $extrafields->fetch_name_optionals_label($elementtype, true);
300  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
301  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
302  $key = 'options_'.$key;
303  $newobject->array_options[$key] = $project[$key];
304  }
305  }
306 
307  $db->begin();
308 
309  $result = $newobject->create($fuser);
310  if (!$error && $result > 0) {
311  // Add myself as project leader
312  $result = $newobject->add_contact($fuser->id, 'PROJECTLEADER', 'internal');
313  if ($result < 0) {
314  $error++;
315  }
316  } else {
317  $error++;
318  }
319 
320  if (!$error) {
321  $db->commit();
322  $objectresp = array('result'=>array('result_code'=>'OK', 'result_label'=>''), 'id'=>$newobject->id, 'ref'=>$newobject->ref);
323  } else {
324  $db->rollback();
325  $error++;
326  $errorcode = 'KO';
327  $errorlabel = $newobject->error;
328  }
329  } else {
330  $error++;
331  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
332  }
333  }
334 
335  if ($error) {
336  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
337  }
338 
339  return $objectresp;
340 }
341 
350 function getProject($authentication, $id = '', $ref = '')
351 {
352  global $db, $conf;
353 
354  dol_syslog("Function: getProject login=".$authentication['login']." id=".$id." ref=".$ref);
355 
356  if ($authentication['entity']) {
357  $conf->entity = $authentication['entity'];
358  }
359 
360  // Init and check authentication
361  $objectresp = array();
362  $errorcode = ''; $errorlabel = '';
363  $error = 0;
364  $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
365  // Check parameters
366  if (!$error && (($id && $ref))) {
367  $error++;
368  $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id and ref can't be both provided. You must choose one or other but not both.";
369  }
370 
371  if (!$error) {
372  $fuser->getrights();
373 
374  if ($fuser->rights->projet->lire) {
375  $project = new Project($db);
376  $result = $project->fetch($id, $ref);
377  if ($result > 0) {
378  $project_result_fields = array(
379  'id' => $project->id,
380  'ref' => $project->ref,
381  'label' => $project->title,
382  'thirdparty_id' => $project->socid,
383  'public' => $project->public,
384  'status' => $project->statut,
385  'date_start' => $project->date_start ? dol_print_date($project->date_start, 'dayrfc') : '',
386  'date_end' => $project->date_end ? dol_print_date($project->date_end, 'dayrfc') : '',
387  'budget' => $project->budget_amount,
388  'description' => $project->description,
389  );
390 
391  $elementtype = 'project';
392 
393  //Retrieve all extrafields for project
394  $extrafields = new ExtraFields($db);
395  $extrafields->fetch_name_optionals_label($elementtype, true);
396 
397  //Get extrafield values
398  if (isset($extrafields->attributes[$elementtype]['label']) && is_array($extrafields->attributes[$elementtype]['label']) && count($extrafields->attributes[$elementtype]['label'])) {
399  $project->fetch_optionals();
400  foreach ($extrafields->attributes[$elementtype]['label'] as $key => $label) {
401  $project_result_fields = array_merge($project_result_fields, array('options_'.$key => $project->array_options['options_'.$key]));
402  }
403  }
404 
405  //Get linked elements
406  global $listofreferent;
407  $elements = array();
408  foreach ($listofreferent as $key => $tablename) {
409  $elements[$key] = array();
410  $element_array = $project->get_element_list($key, $tablename);
411  if (count($element_array) > 0 && is_array($element_array)) {
412  foreach ($element_array as $element) {
413  $tmp = explode('_', $element);
414  $idofelement = count($tmp) > 0 ? $tmp[0] : "";
415  $idofelementuser = count($tmp) > 1 ? $tmp[1] : "";
416  $elements[$key][] = array('id' => $idofelement, 'user' => $idofelementuser);
417  }
418  }
419  }
420  $project_result_fields['elements'] = $elements;
421 
422  //Result
423  $objectresp = array(
424  'result'=>array('result_code'=>'OK', 'result_label'=>''),
425  'project'=>$project_result_fields
426  );
427  } else {
428  $error++;
429  $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id.' nor ref='.$ref;
430  }
431  } else {
432  $error++;
433  $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
434  }
435  }
436 
437  if ($error) {
438  $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
439  }
440 
441  return $objectresp;
442 }
443 
444 // Return the results.
445 $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:408
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
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