dolibarr 19.0.3
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
30class 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->rights->projet->lire) {
75 throw new RestException(401);
76 }
77
78 $result = $this->project->fetch($id);
79 if (!$result) {
80 throw new RestException(404, 'Project not found');
81 }
82
83 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
84 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
85 }
86
87 $this->project->fetchObjectLinked();
88 return $this->_cleanObjectDatas($this->project);
89 }
90
91
92
108 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $category = 0, $sqlfilters = '', $properties = '')
109 {
110 if (!DolibarrApiAccess::$user->rights->projet->lire) {
111 throw new RestException(401);
112 }
113
114 $obj_ret = array();
115
116 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
117 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $thirdparty_ids;
118
119 // If the internal user must only see his customers, force searching by him
120 $search_sale = 0;
121 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
122 $search_sale = DolibarrApiAccess::$user->id;
123 }
124
125 $sql = "SELECT t.rowid";
126 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
127 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
128 }
129 $sql .= " FROM ".MAIN_DB_PREFIX."projet as t";
130 $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
131 if ($category > 0) {
132 $sql .= ", ".MAIN_DB_PREFIX."categorie_project as c";
133 }
134 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
135 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
136 }
137
138 $sql .= ' WHERE t.entity IN ('.getEntity('project').')';
139 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
140 $sql .= " AND t.fk_soc = sc.fk_soc";
141 }
142 if ($socids) {
143 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
144 }
145 if ($search_sale > 0) {
146 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
147 }
148 // Insert sale filter
149 if ($search_sale > 0) {
150 $sql .= " AND sc.fk_user = ".((int) $search_sale);
151 }
152 // Select projects of given category
153 if ($category > 0) {
154 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_project = t.rowid ";
155 }
156 // Add sql filters
157 if ($sqlfilters) {
158 $errormessage = '';
159 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
160 if ($errormessage) {
161 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
162 }
163 }
164
165 $sql .= $this->db->order($sortfield, $sortorder);
166 if ($limit) {
167 if ($page < 0) {
168 $page = 0;
169 }
170 $offset = $limit * $page;
171
172 $sql .= $this->db->plimit($limit + 1, $offset);
173 }
174
175 dol_syslog("API Rest request");
176 $result = $this->db->query($sql);
177
178 if ($result) {
179 $num = $this->db->num_rows($result);
180 $min = min($num, ($limit <= 0 ? $num : $limit));
181 $i = 0;
182 while ($i < $min) {
183 $obj = $this->db->fetch_object($result);
184 $project_static = new Project($this->db);
185 if ($project_static->fetch($obj->rowid)) {
186 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($project_static), $properties);
187 }
188 $i++;
189 }
190 } else {
191 throw new RestException(503, 'Error when retrieve project list : '.$this->db->lasterror());
192 }
193
194 return $obj_ret;
195 }
196
203 public function post($request_data = null)
204 {
205 if (!DolibarrApiAccess::$user->rights->projet->creer) {
206 throw new RestException(401, "Insuffisant rights");
207 }
208 // Check mandatory fields
209 $result = $this->_validate($request_data);
210
211 foreach ($request_data as $field => $value) {
212 if ($field === 'caller') {
213 // 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 whith the caller
214 $this->project->context['caller'] = $request_data['caller'];
215 continue;
216 }
217
218 $this->project->$field = $value;
219 }
220 /*if (isset($request_data["lines"])) {
221 $lines = array();
222 foreach ($request_data["lines"] as $line) {
223 array_push($lines, (object) $line);
224 }
225 $this->project->lines = $lines;
226 }*/
227 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
228 throw new RestException(500, "Error creating project", array_merge(array($this->project->error), $this->project->errors));
229 }
230
231 return $this->project->id;
232 }
233
244 public function getLines($id, $includetimespent = 0)
245 {
246 if (!DolibarrApiAccess::$user->rights->projet->lire) {
247 throw new RestException(401);
248 }
249
250 $result = $this->project->fetch($id);
251 if (!$result) {
252 throw new RestException(404, 'Project not found');
253 }
254
255 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
256 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
257 }
258 $this->project->getLinesArray(DolibarrApiAccess::$user);
259 $result = array();
260 foreach ($this->project->lines as $line) { // $line is a task
261 if ($includetimespent == 1) {
262 $timespent = $line->getSummaryOfTimeSpent(0);
263 }
264 if ($includetimespent == 2) {
265 $timespent = $line->fetchTimeSpentOnTask();
266 }
267 array_push($result, $this->_cleanObjectDatas($line));
268 }
269 return $result;
270 }
271
272
282 public function getRoles($id, $userid = 0)
283 {
284 global $db;
285
286 if (!DolibarrApiAccess::$user->rights->projet->lire) {
287 throw new RestException(401);
288 }
289
290 $result = $this->project->fetch($id);
291 if (!$result) {
292 throw new RestException(404, 'Project not found');
293 }
294
295 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
296 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
297 }
298
299 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
300 $taskstatic = new Task($this->db);
301 $userp = DolibarrApiAccess::$user;
302 if ($userid > 0) {
303 $userp = new User($this->db);
304 $userp->fetch($userid);
305 }
306 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, null, $id, 0);
307 $result = array();
308 foreach ($this->project->roles as $line) {
309 array_push($result, $this->_cleanObjectDatas($line));
310 }
311
312 return $result;
313 }
314
315
326 /*
327 public function postLine($id, $request_data = null)
328 {
329 if(! DolibarrApiAccess::$user->rights->projet->creer) {
330 throw new RestException(401);
331 }
332
333 $result = $this->project->fetch($id);
334 if( ! $result ) {
335 throw new RestException(404, 'Project not found');
336 }
337
338 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
339 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
340 }
341
342 $request_data = (object) $request_data;
343
344 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
345
346 $updateRes = $this->project->addline(
347 $request_data->desc,
348 $request_data->subprice,
349 $request_data->qty,
350 $request_data->tva_tx,
351 $request_data->localtax1_tx,
352 $request_data->localtax2_tx,
353 $request_data->fk_product,
354 $request_data->remise_percent,
355 $request_data->info_bits,
356 $request_data->fk_remise_except,
357 'HT',
358 0,
359 $request_data->date_start,
360 $request_data->date_end,
361 $request_data->product_type,
362 $request_data->rang,
363 $request_data->special_code,
364 $fk_parent_line,
365 $request_data->fk_fournprice,
366 $request_data->pa_ht,
367 $request_data->label,
368 $request_data->array_options,
369 $request_data->fk_unit,
370 $this->element,
371 $request_data->id
372 );
373
374 if ($updateRes > 0) {
375 return $updateRes;
376
377 }
378 return false;
379 }
380 */
381
393 /*
394 public function putLine($id, $lineid, $request_data = null)
395 {
396 if(! DolibarrApiAccess::$user->rights->projet->creer) {
397 throw new RestException(401);
398 }
399
400 $result = $this->project->fetch($id);
401 if( ! $result ) {
402 throw new RestException(404, 'Project not found');
403 }
404
405 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
406 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
407 }
408
409 $request_data = (object) $request_data;
410
411 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
412
413 $updateRes = $this->project->updateline(
414 $lineid,
415 $request_data->desc,
416 $request_data->subprice,
417 $request_data->qty,
418 $request_data->remise_percent,
419 $request_data->tva_tx,
420 $request_data->localtax1_tx,
421 $request_data->localtax2_tx,
422 'HT',
423 $request_data->info_bits,
424 $request_data->date_start,
425 $request_data->date_end,
426 $request_data->product_type,
427 $request_data->fk_parent_line,
428 0,
429 $request_data->fk_fournprice,
430 $request_data->pa_ht,
431 $request_data->label,
432 $request_data->special_code,
433 $request_data->array_options,
434 $request_data->fk_unit
435 );
436
437 if ($updateRes > 0) {
438 $result = $this->get($id);
439 unset($result->line);
440 return $this->_cleanObjectDatas($result);
441 }
442 return false;
443 }*/
444
445
446
455 public function put($id, $request_data = null)
456 {
457 if (!DolibarrApiAccess::$user->rights->projet->creer) {
458 throw new RestException(401);
459 }
460
461 $result = $this->project->fetch($id);
462 if ($result <= 0) {
463 throw new RestException(404, 'Project not found');
464 }
465
466 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
467 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
468 }
469 foreach ($request_data as $field => $value) {
470 if ($field == 'id') {
471 continue;
472 }
473 if ($field === 'caller') {
474 // 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 whith the caller
475 $this->project->context['caller'] = $request_data['caller'];
476 continue;
477 }
478
479 $this->project->$field = $value;
480 }
481
482 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
483 return $this->get($id);
484 } else {
485 throw new RestException(500, $this->project->error);
486 }
487 }
488
496 public function delete($id)
497 {
498 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
499 throw new RestException(401);
500 }
501 $result = $this->project->fetch($id);
502 if (!$result) {
503 throw new RestException(404, 'Project not found');
504 }
505
506 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
507 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
508 }
509
510 if (!$this->project->delete(DolibarrApiAccess::$user)) {
511 throw new RestException(500, 'Error when delete project : '.$this->project->error);
512 }
513
514 return array(
515 'success' => array(
516 'code' => 200,
517 'message' => 'Project deleted'
518 )
519 );
520 }
521
540 public function validate($id, $notrigger = 0)
541 {
542 if (!DolibarrApiAccess::$user->rights->projet->creer) {
543 throw new RestException(401);
544 }
545 $result = $this->project->fetch($id);
546 if (!$result) {
547 throw new RestException(404, 'Project not found');
548 }
549
550 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
551 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
552 }
553
554 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
555 if ($result == 0) {
556 throw new RestException(304, 'Error nothing done. May be object is already validated');
557 }
558 if ($result < 0) {
559 throw new RestException(500, 'Error when validating Project: '.$this->project->error);
560 }
561
562 return array(
563 'success' => array(
564 'code' => 200,
565 'message' => 'Project validated'
566 )
567 );
568 }
569
570
571 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
578 protected function _cleanObjectDatas($object)
579 {
580 // phpcs:enable
581 $object = parent::_cleanObjectDatas($object);
582
583 unset($object->datec);
584 unset($object->datem);
585 unset($object->barcode_type);
586 unset($object->barcode_type_code);
587 unset($object->barcode_type_label);
588 unset($object->barcode_type_coder);
589 unset($object->cond_reglement_id);
590 unset($object->cond_reglement);
591 unset($object->fk_delivery_address);
592 unset($object->shipping_method_id);
593 unset($object->fk_account);
594 unset($object->note);
595 unset($object->fk_incoterms);
596 unset($object->label_incoterms);
597 unset($object->location_incoterms);
598 unset($object->name);
599 unset($object->lastname);
600 unset($object->firstname);
601 unset($object->civility_id);
602 unset($object->mode_reglement_id);
603 unset($object->country);
604 unset($object->country_id);
605 unset($object->country_code);
606
607 unset($object->weekWorkLoad);
608 unset($object->weekWorkLoad);
609
610 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
611
612 unset($object->total_ht);
613 unset($object->total_tva);
614 unset($object->total_localtax1);
615 unset($object->total_localtax2);
616 unset($object->total_ttc);
617
618 unset($object->comments);
619
620 return $object;
621 }
622
630 private function _validate($data)
631 {
632 $object = array();
633 foreach (self::$FIELDS as $field) {
634 if (!isset($data[$field])) {
635 throw new RestException(400, "$field field missing");
636 }
637 $object[$field] = $data[$field];
638 }
639 return $object;
640 }
641
642
643 // TODO
644 // getSummaryOfTimeSpent
645}
Class for API REST v1.
Definition api.class.php:31
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Class to manage projects.
post($request_data=null)
Create 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.
_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.
getRoles($id, $userid=0)
Get roles a user is assigned to a project with.
Class to manage tasks.
Class to manage Dolibarr users.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.