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
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->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 global $conf;
288 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
289 throw new RestException(403, "Insuffisant rights");
290 }
291 // Check mandatory fields
292 $result = $this->_validate($request_data);
293
294 foreach ($request_data as $field => $value) {
295 if ($field === 'caller') {
296 // 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
297 $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
298 continue;
299 }
300
301 $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
302 }
303 /*if (isset($request_data["lines"])) {
304 $lines = array();
305 foreach ($request_data["lines"] as $line) {
306 array_push($lines, (object) $line);
307 }
308 $this->project->lines = $lines;
309 }*/
310
311 // Auto-generate the "ref" field if it is set to "auto"
312 if ($this->project->ref == -1 || $this->project->ref === 'auto') {
313 $reldir = '';
314 $defaultref = '';
315 $file = '';
316 $classname = '';
317 $filefound = 0;
318 $modele = getDolGlobalString('PROJECT_ADDON', 'mod_project_simple');
319
320 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
321 foreach ($dirmodels as $reldir) {
322 $file = dol_buildpath($reldir."core/modules/project/".$modele.'.php', 0);
323 if (file_exists($file)) {
324 $filefound = 1;
325 $classname = $modele;
326 break;
327 }
328 }
329 if ($filefound && !empty($classname)) {
330 $result = dol_include_once($reldir . "core/modules/project/" . $modele . '.php');
331 if ($result !== false && class_exists($classname)) {
332 $modProject = new $classname();
333 $defaultref = $modProject->getNextValue(null, $this->project);
334 } else {
335 dol_syslog("Failed to include module file or invalid classname: " . $reldir . "core/modules/project/" . $modele . '.php', LOG_ERR);
336 }
337 } else {
338 dol_syslog("Module file not found or classname is empty: " . $modele, LOG_ERR);
339 }
340
341 if (is_numeric($defaultref) && $defaultref <= 0) {
342 $defaultref = '';
343 }
344
345 if (empty($defaultref)) {
346 $defaultref = 'PJ' . dol_print_date(dol_now(), 'dayrfc');
347 }
348
349 $this->project->ref = $defaultref;
350 }
351
352 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
353 throw new RestException(500, "Error creating project", array_merge(array($this->project->error), $this->project->errors));
354 }
355
356 return $this->project->id;
357 }
358
369 public function getLines($id, $includetimespent = 0)
370 {
371 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
372 throw new RestException(403);
373 }
374
375 $result = $this->project->fetch($id);
376 if (!$result) {
377 throw new RestException(404, 'Project not found');
378 }
379
380 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
381 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
382 }
383 $this->project->getLinesArray(DolibarrApiAccess::$user);
384 $result = array();
385 foreach ($this->project->lines as $line) { // $line is a task
386 if ($includetimespent == 1) {
387 $timespent = $line->getSummaryOfTimeSpent(0);
388 }
389 if ($includetimespent == 2) {
390 $timespent = $line->fetchTimeSpentOnTask();
391 }
392 array_push($result, $this->_cleanObjectDatas($line));
393 }
394 return $result;
395 }
396
397
407 public function getRoles($id, $userid = 0)
408 {
409 global $db;
410
411 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
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 require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
425 $taskstatic = new Task($this->db);
426 $userp = DolibarrApiAccess::$user;
427 if ($userid > 0) {
428 $userp = new User($this->db);
429 $userp->fetch($userid);
430 }
431 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, null, $id, 0);
432 $result = array();
433 foreach ($this->project->roles as $line) {
434 array_push($result, $this->_cleanObjectDatas($line));
435 }
436
437 return $result;
438 }
439
440
451 /*
452 public function postLine($id, $request_data = null)
453 {
454 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
455 throw new RestException(403);
456 }
457
458 $result = $this->project->fetch($id);
459 if( ! $result ) {
460 throw new RestException(404, 'Project not found');
461 }
462
463 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
464 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
465 }
466
467 $request_data = (object) $request_data;
468
469 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
470
471 $updateRes = $this->project->addline(
472 $request_data->desc,
473 $request_data->subprice,
474 $request_data->qty,
475 $request_data->tva_tx,
476 $request_data->localtax1_tx,
477 $request_data->localtax2_tx,
478 $request_data->fk_product,
479 $request_data->remise_percent,
480 $request_data->info_bits,
481 $request_data->fk_remise_except,
482 'HT',
483 0,
484 $request_data->date_start,
485 $request_data->date_end,
486 $request_data->product_type,
487 $request_data->rang,
488 $request_data->special_code,
489 $fk_parent_line,
490 $request_data->fk_fournprice,
491 $request_data->pa_ht,
492 $request_data->label,
493 $request_data->array_options,
494 $request_data->fk_unit,
495 $this->element,
496 $request_data->id
497 );
498
499 if ($updateRes > 0) {
500 return $updateRes;
501
502 }
503 return false;
504 }
505 */
506
518 /*
519 public function putLine($id, $lineid, $request_data = null)
520 {
521 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
522 throw new RestException(403);
523 }
524
525 $result = $this->project->fetch($id);
526 if( ! $result ) {
527 throw new RestException(404, 'Project not found');
528 }
529
530 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
531 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
532 }
533
534 $request_data = (object) $request_data;
535
536 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
537
538 $updateRes = $this->project->updateline(
539 $lineid,
540 $request_data->desc,
541 $request_data->subprice,
542 $request_data->qty,
543 $request_data->remise_percent,
544 $request_data->tva_tx,
545 $request_data->localtax1_tx,
546 $request_data->localtax2_tx,
547 'HT',
548 $request_data->info_bits,
549 $request_data->date_start,
550 $request_data->date_end,
551 $request_data->product_type,
552 $request_data->fk_parent_line,
553 0,
554 $request_data->fk_fournprice,
555 $request_data->pa_ht,
556 $request_data->label,
557 $request_data->special_code,
558 $request_data->array_options,
559 $request_data->fk_unit
560 );
561
562 if ($updateRes > 0) {
563 $result = $this->get($id);
564 unset($result->line);
565 return $this->_cleanObjectDatas($result);
566 }
567 return false;
568 }*/
569
570
571
579 public function put($id, $request_data = null)
580 {
581 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
582 throw new RestException(403);
583 }
584
585 $result = $this->project->fetch($id);
586 if ($result <= 0) {
587 throw new RestException(404, 'Project not found');
588 }
589
590 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
591 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
592 }
593 foreach ($request_data as $field => $value) {
594 if ($field == 'id') {
595 continue;
596 }
597 if ($field === 'caller') {
598 // 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
599 $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
600 continue;
601 }
602 if ($field == 'array_options' && is_array($value)) {
603 foreach ($value as $index => $val) {
604 $this->project->array_options[$index] = $this->_checkValForAPI($field, $val, $this->project);
605 }
606 continue;
607 }
608
609 $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
610 }
611
612 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
613 return $this->get($id);
614 } else {
615 throw new RestException(500, $this->project->error);
616 }
617 }
618
626 public function delete($id)
627 {
628 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
629 throw new RestException(403);
630 }
631 $result = $this->project->fetch($id);
632 if (!$result) {
633 throw new RestException(404, 'Project not found');
634 }
635
636 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
637 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
638 }
639
640 if (!$this->project->delete(DolibarrApiAccess::$user)) {
641 throw new RestException(500, 'Error when delete project : '.$this->project->error);
642 }
643
644 return array(
645 'success' => array(
646 'code' => 200,
647 'message' => 'Project deleted'
648 )
649 );
650 }
651
670 public function validate($id, $notrigger = 0)
671 {
672 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
673 throw new RestException(403);
674 }
675 $result = $this->project->fetch($id);
676 if (!$result) {
677 throw new RestException(404, 'Project not found');
678 }
679
680 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
681 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
682 }
683
684 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
685 if ($result == 0) {
686 throw new RestException(304, 'Error nothing done. May be object is already validated');
687 }
688 if ($result < 0) {
689 throw new RestException(500, 'Error when validating Project: '.$this->project->error);
690 }
691
692 return array(
693 'success' => array(
694 'code' => 200,
695 'message' => 'Project validated'
696 )
697 );
698 }
699
700
701 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
708 protected function _cleanObjectDatas($object)
709 {
710 // phpcs:enable
711 $object = parent::_cleanObjectDatas($object);
712
713 unset($object->datec);
714 unset($object->datem);
715 unset($object->barcode_type);
716 unset($object->barcode_type_code);
717 unset($object->barcode_type_label);
718 unset($object->barcode_type_coder);
719 unset($object->cond_reglement_id);
720 unset($object->cond_reglement);
721 unset($object->fk_delivery_address);
722 unset($object->shipping_method_id);
723 unset($object->fk_account);
724 unset($object->note);
725 unset($object->fk_incoterms);
726 unset($object->label_incoterms);
727 unset($object->location_incoterms);
728 unset($object->name);
729 unset($object->lastname);
730 unset($object->firstname);
731 unset($object->civility_id);
732 unset($object->mode_reglement_id);
733 unset($object->country);
734 unset($object->country_id);
735 unset($object->country_code);
736
737 unset($object->weekWorkLoad);
738 unset($object->weekWorkLoad);
739
740 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
741
742 unset($object->total_ht);
743 unset($object->total_tva);
744 unset($object->total_localtax1);
745 unset($object->total_localtax2);
746 unset($object->total_ttc);
747
748 unset($object->comments);
749
750 return $object;
751 }
752
760 private function _validate($data)
761 {
762 $object = array();
763 foreach (self::$FIELDS as $field) {
764 if (!isset($data[$field])) {
765 throw new RestException(400, "$field field missing");
766 }
767 $object[$field] = $data[$field];
768 }
769 return $object;
770 }
771
772
773 // TODO
774 // getSummaryOfTimeSpent
775}
$id
Definition account.php:39
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.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_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.
Class to manage Dolibarr users.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.