dolibarr  21.0.0-alpha
api_projects.class.php
1 <?php
2 /* Copyright (C) 2015 Jean-Fran├žois Ferry <jfefe@aternatik.fr>
3  * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
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 
19  use Luracast\Restler\RestException;
20 
21  require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
22  require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
23 
30 class Projects extends DolibarrApi
31 {
35  public static $FIELDS = array(
36  'ref',
37  'title'
38  );
39 
43  public $project;
44 
48  public $task;
49 
50 
54  public function __construct()
55  {
56  global $db, $conf;
57  $this->db = $db;
58  $this->project = new Project($this->db);
59  $this->task = new Task($this->db);
60  }
61 
72  public function get($id)
73  {
74  if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
75  throw new RestException(403);
76  }
77 
78  $result = $this->project->fetch($id);
79  if (!$result) {
80  throw new RestException(404, 'Project with supplied id not found');
81  }
82 
83  if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
84  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
85  }
86 
87  $this->project->fetchObjectLinked();
88  return $this->_cleanObjectDatas($this->project);
89  }
90 
103  public function getByRef($ref)
104  {
105  if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
106  throw new RestException(403);
107  }
108 
109  $result = $this->project->fetch('', $ref);
110  if (!$result) {
111  throw new RestException(404, 'Project with supplied ref not found');
112  }
113 
114  if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
115  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
116  }
117 
118  $this->project->fetchObjectLinked();
119  return $this->_cleanObjectDatas($this->project);
120  }
121 
134  public function getByRefExt($ref_ext)
135  {
136  if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
137  throw new RestException(403);
138  }
139 
140  $result = $this->project->fetch('', '', $ref_ext);
141  if (!$result) {
142  throw new RestException(404, 'Project with supplied ref_ext not found');
143  }
144 
145  if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
146  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
147  }
148 
149  $this->project->fetchObjectLinked();
150  return $this->_cleanObjectDatas($this->project);
151  }
152 
165  public function getByMsgId($email_msgid)
166  {
167  if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
168  throw new RestException(403);
169  }
170 
171  $result = $this->project->fetch('', '', '', $email_msgid);
172  if (!$result) {
173  throw new RestException(404, 'Project with supplied email_msgid not found');
174  }
175 
176  if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
177  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
178  }
179 
180  $this->project->fetchObjectLinked();
181  return $this->_cleanObjectDatas($this->project);
182  }
183 
199  public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $category = 0, $sqlfilters = '', $properties = '')
200  {
201  if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
202  throw new RestException(403);
203  }
204 
205  $obj_ret = array();
206 
207  // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
208  $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
209 
210  // If the internal user must only see his customers, force searching by him
211  $search_sale = 0;
212  if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
213  $search_sale = DolibarrApiAccess::$user->id;
214  }
215 
216  $sql = "SELECT t.rowid";
217  $sql .= " FROM ".MAIN_DB_PREFIX."projet as t";
218  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet_extrafields AS ef ON ef.fk_object = t.rowid"; // So we will be able to filter on extrafields
219  if ($category > 0) {
220  $sql .= ", ".MAIN_DB_PREFIX."categorie_project as c";
221  }
222  $sql .= ' WHERE t.entity IN ('.getEntity('project').')';
223  if ($socids) {
224  $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
225  }
226  // Search on sale representative
227  if ($search_sale && $search_sale != '-1') {
228  if ($search_sale == -2) {
229  $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
230  } elseif ($search_sale > 0) {
231  $sql .= " AND EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc AND sc.fk_user = ".((int) $search_sale).")";
232  }
233  }
234  // Select projects of given category
235  if ($category > 0) {
236  $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_project = t.rowid ";
237  }
238  // Add sql filters
239  if ($sqlfilters) {
240  $errormessage = '';
241  $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
242  if ($errormessage) {
243  throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
244  }
245  }
246 
247  $sql .= $this->db->order($sortfield, $sortorder);
248  if ($limit) {
249  if ($page < 0) {
250  $page = 0;
251  }
252  $offset = $limit * $page;
253 
254  $sql .= $this->db->plimit($limit + 1, $offset);
255  }
256 
257  dol_syslog("API Rest request");
258  $result = $this->db->query($sql);
259 
260  if ($result) {
261  $num = $this->db->num_rows($result);
262  $min = min($num, ($limit <= 0 ? $num : $limit));
263  $i = 0;
264  while ($i < $min) {
265  $obj = $this->db->fetch_object($result);
266  $project_static = new Project($this->db);
267  if ($project_static->fetch($obj->rowid)) {
268  $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($project_static), $properties);
269  }
270  $i++;
271  }
272  } else {
273  throw new RestException(503, 'Error when retrieve project list : '.$this->db->lasterror());
274  }
275 
276  return $obj_ret;
277  }
278 
285  public function post($request_data = null)
286  {
287  if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
288  throw new RestException(403, "Insuffisant rights");
289  }
290  // Check mandatory fields
291  $result = $this->_validate($request_data);
292 
293  foreach ($request_data as $field => $value) {
294  if ($field === 'caller') {
295  // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
296  $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
297  continue;
298  }
299 
300  $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
301  }
302  /*if (isset($request_data["lines"])) {
303  $lines = array();
304  foreach ($request_data["lines"] as $line) {
305  array_push($lines, (object) $line);
306  }
307  $this->project->lines = $lines;
308  }*/
309  if ($this->project->create(DolibarrApiAccess::$user) < 0) {
310  throw new RestException(500, "Error creating project", array_merge(array($this->project->error), $this->project->errors));
311  }
312 
313  return $this->project->id;
314  }
315 
326  public function getLines($id, $includetimespent = 0)
327  {
328  if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
329  throw new RestException(403);
330  }
331 
332  $result = $this->project->fetch($id);
333  if (!$result) {
334  throw new RestException(404, 'Project not found');
335  }
336 
337  if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
338  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
339  }
340  $this->project->getLinesArray(DolibarrApiAccess::$user);
341  $result = array();
342  foreach ($this->project->lines as $line) { // $line is a task
343  if ($includetimespent == 1) {
344  $timespent = $line->getSummaryOfTimeSpent(0);
345  }
346  if ($includetimespent == 2) {
347  $timespent = $line->fetchTimeSpentOnTask();
348  }
349  array_push($result, $this->_cleanObjectDatas($line));
350  }
351  return $result;
352  }
353 
354 
364  public function getRoles($id, $userid = 0)
365  {
366  global $db;
367 
368  if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
369  throw new RestException(403);
370  }
371 
372  $result = $this->project->fetch($id);
373  if (!$result) {
374  throw new RestException(404, 'Project not found');
375  }
376 
377  if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
378  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
379  }
380 
381  require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
382  $taskstatic = new Task($this->db);
383  $userp = DolibarrApiAccess::$user;
384  if ($userid > 0) {
385  $userp = new User($this->db);
386  $userp->fetch($userid);
387  }
388  $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, null, $id, 0);
389  $result = array();
390  foreach ($this->project->roles as $line) {
391  array_push($result, $this->_cleanObjectDatas($line));
392  }
393 
394  return $result;
395  }
396 
397 
408  /*
409  public function postLine($id, $request_data = null)
410  {
411  if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
412  throw new RestException(403);
413  }
414 
415  $result = $this->project->fetch($id);
416  if( ! $result ) {
417  throw new RestException(404, 'Project not found');
418  }
419 
420  if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
421  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
422  }
423 
424  $request_data = (object) $request_data;
425 
426  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
427 
428  $updateRes = $this->project->addline(
429  $request_data->desc,
430  $request_data->subprice,
431  $request_data->qty,
432  $request_data->tva_tx,
433  $request_data->localtax1_tx,
434  $request_data->localtax2_tx,
435  $request_data->fk_product,
436  $request_data->remise_percent,
437  $request_data->info_bits,
438  $request_data->fk_remise_except,
439  'HT',
440  0,
441  $request_data->date_start,
442  $request_data->date_end,
443  $request_data->product_type,
444  $request_data->rang,
445  $request_data->special_code,
446  $fk_parent_line,
447  $request_data->fk_fournprice,
448  $request_data->pa_ht,
449  $request_data->label,
450  $request_data->array_options,
451  $request_data->fk_unit,
452  $this->element,
453  $request_data->id
454  );
455 
456  if ($updateRes > 0) {
457  return $updateRes;
458 
459  }
460  return false;
461  }
462  */
463 
475  /*
476  public function putLine($id, $lineid, $request_data = null)
477  {
478  if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
479  throw new RestException(403);
480  }
481 
482  $result = $this->project->fetch($id);
483  if( ! $result ) {
484  throw new RestException(404, 'Project not found');
485  }
486 
487  if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
488  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
489  }
490 
491  $request_data = (object) $request_data;
492 
493  $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
494 
495  $updateRes = $this->project->updateline(
496  $lineid,
497  $request_data->desc,
498  $request_data->subprice,
499  $request_data->qty,
500  $request_data->remise_percent,
501  $request_data->tva_tx,
502  $request_data->localtax1_tx,
503  $request_data->localtax2_tx,
504  'HT',
505  $request_data->info_bits,
506  $request_data->date_start,
507  $request_data->date_end,
508  $request_data->product_type,
509  $request_data->fk_parent_line,
510  0,
511  $request_data->fk_fournprice,
512  $request_data->pa_ht,
513  $request_data->label,
514  $request_data->special_code,
515  $request_data->array_options,
516  $request_data->fk_unit
517  );
518 
519  if ($updateRes > 0) {
520  $result = $this->get($id);
521  unset($result->line);
522  return $this->_cleanObjectDatas($result);
523  }
524  return false;
525  }*/
526 
527 
528 
536  public function put($id, $request_data = null)
537  {
538  if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
539  throw new RestException(403);
540  }
541 
542  $result = $this->project->fetch($id);
543  if ($result <= 0) {
544  throw new RestException(404, 'Project not found');
545  }
546 
547  if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
548  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
549  }
550  foreach ($request_data as $field => $value) {
551  if ($field == 'id') {
552  continue;
553  }
554  if ($field === 'caller') {
555  // Add a mention of caller so on trigger called after action, we can filter to avoid a loop if we try to sync back again with the caller
556  $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
557  continue;
558  }
559  if ($field == 'array_options' && is_array($value)) {
560  foreach ($value as $index => $val) {
561  $this->project->array_options[$index] = $this->_checkValForAPI($field, $val, $this->project);
562  }
563  continue;
564  }
565 
566  $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
567  }
568 
569  if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
570  return $this->get($id);
571  } else {
572  throw new RestException(500, $this->project->error);
573  }
574  }
575 
583  public function delete($id)
584  {
585  if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
586  throw new RestException(403);
587  }
588  $result = $this->project->fetch($id);
589  if (!$result) {
590  throw new RestException(404, 'Project not found');
591  }
592 
593  if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
594  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
595  }
596 
597  if (!$this->project->delete(DolibarrApiAccess::$user)) {
598  throw new RestException(500, 'Error when delete project : '.$this->project->error);
599  }
600 
601  return array(
602  'success' => array(
603  'code' => 200,
604  'message' => 'Project deleted'
605  )
606  );
607  }
608 
627  public function validate($id, $notrigger = 0)
628  {
629  if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
630  throw new RestException(403);
631  }
632  $result = $this->project->fetch($id);
633  if (!$result) {
634  throw new RestException(404, 'Project not found');
635  }
636 
637  if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
638  throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
639  }
640 
641  $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
642  if ($result == 0) {
643  throw new RestException(304, 'Error nothing done. May be object is already validated');
644  }
645  if ($result < 0) {
646  throw new RestException(500, 'Error when validating Project: '.$this->project->error);
647  }
648 
649  return array(
650  'success' => array(
651  'code' => 200,
652  'message' => 'Project validated'
653  )
654  );
655  }
656 
657 
658  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
665  protected function _cleanObjectDatas($object)
666  {
667  // phpcs:enable
668  $object = parent::_cleanObjectDatas($object);
669 
670  unset($object->datec);
671  unset($object->datem);
672  unset($object->barcode_type);
673  unset($object->barcode_type_code);
674  unset($object->barcode_type_label);
675  unset($object->barcode_type_coder);
676  unset($object->cond_reglement_id);
677  unset($object->cond_reglement);
678  unset($object->fk_delivery_address);
679  unset($object->shipping_method_id);
680  unset($object->fk_account);
681  unset($object->note);
682  unset($object->fk_incoterms);
683  unset($object->label_incoterms);
684  unset($object->location_incoterms);
685  unset($object->name);
686  unset($object->lastname);
687  unset($object->firstname);
688  unset($object->civility_id);
689  unset($object->mode_reglement_id);
690  unset($object->country);
691  unset($object->country_id);
692  unset($object->country_code);
693 
694  unset($object->weekWorkLoad);
695  unset($object->weekWorkLoad);
696 
697  //unset($object->lines); // for task we use timespent_lines, but for project we use lines
698 
699  unset($object->total_ht);
700  unset($object->total_tva);
701  unset($object->total_localtax1);
702  unset($object->total_localtax2);
703  unset($object->total_ttc);
704 
705  unset($object->comments);
706 
707  return $object;
708  }
709 
717  private function _validate($data)
718  {
719  $object = array();
720  foreach (self::$FIELDS as $field) {
721  if (!isset($data[$field])) {
722  throw new RestException(400, "$field field missing");
723  }
724  $object[$field] = $data[$field];
725  }
726  return $object;
727  }
728 
729 
730  // TODO
731  // getSummaryOfTimeSpent
732 }
if($user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition: card.php:58
Class for API REST v1.
Definition: api.class.php:30
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
Definition: api.class.php:136
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Definition: api.class.php:369
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
Definition: api.class.php:82
Class to manage projects.
post($request_data=null)
Create project object.
getByMsgId($email_msgid)
Get properties of a project object.
getLines($id, $includetimespent=0)
Get tasks of a project.
validate($id, $notrigger=0)
Validate a project.
_validate($data)
Validate fields before create or update object.
getByRef($ref)
Get properties of a project object.
_cleanObjectDatas($object)
Clean sensible object datas.
__construct()
Constructor.
put($id, $request_data=null)
Add a task to given project.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $category=0, $sqlfilters='', $properties='')
List projects.
getByRefExt($ref_ext)
Get properties of a project object.
getRoles($id, $userid=0)
Get roles a user is assigned to a project with.
Class to manage tasks.
Definition: task.class.php:41
Class to manage Dolibarr users.
Definition: user.class.php:50
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:751
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.