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