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