dolibarr 23.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 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
6 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2025 Jessica Kowal <jessicakowal69@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23use Luracast\Restler\RestException;
24
25require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
26require_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php';
27
34class Projects extends DolibarrApi
35{
39 public static $FIELDS = array(
40 'ref',
41 'title'
42 );
43
47 public $project;
48
52 public $task;
53
54
58 public function __construct()
59 {
60 global $db, $conf;
61 $this->db = $db;
62 $this->project = new Project($this->db);
63 $this->task = new Task($this->db);
64 }
65
76 public function get($id)
77 {
78 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
79 throw new RestException(403);
80 }
81
82 $result = $this->project->fetch($id);
83 if (!$result) {
84 throw new RestException(404, 'Project with supplied id not found');
85 }
86
87 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
88 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
89 }
90
91 $this->project->fetchObjectLinked();
92 return $this->_cleanObjectDatas($this->project);
93 }
94
107 public function getByRef($ref)
108 {
109 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
110 throw new RestException(403);
111 }
112
113 $result = $this->project->fetch(0, $ref);
114 if (!$result) {
115 throw new RestException(404, 'Project with supplied ref not found');
116 }
117
118 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
119 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
120 }
121
122 $this->project->fetchObjectLinked();
123 return $this->_cleanObjectDatas($this->project);
124 }
125
138 public function getByRefExt($ref_ext)
139 {
140 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
141 throw new RestException(403);
142 }
143
144 $result = $this->project->fetch(0, '', $ref_ext);
145 if (!$result) {
146 throw new RestException(404, 'Project with supplied ref_ext not found');
147 }
148
149 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
150 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
151 }
152
153 $this->project->fetchObjectLinked();
154 return $this->_cleanObjectDatas($this->project);
155 }
156
169 public function getByMsgId($email_msgid)
170 {
171 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
172 throw new RestException(403);
173 }
174
175 $result = $this->project->fetch(0, '', '', $email_msgid);
176 if (!$result) {
177 throw new RestException(404, 'Project with supplied email_msgid not found');
178 }
179
180 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
181 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
182 }
183
184 $this->project->fetchObjectLinked();
185 return $this->_cleanObjectDatas($this->project);
186 }
187
206 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
207 {
208 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
209 throw new RestException(403);
210 }
211
212 $obj_ret = array();
213
214 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
215 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
216
217 // If the internal user must only see his customers, force searching by him
218 $search_sale = 0;
219 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
220 $search_sale = DolibarrApiAccess::$user->id;
221 }
222
223 $sql = "SELECT t.rowid";
224 $sql .= " FROM " . MAIN_DB_PREFIX . "projet as t";
225 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe AS s ON (s.rowid = t.fk_soc)";
226 $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
227 if ($category > 0) {
228 $sql .= ", " . MAIN_DB_PREFIX . "categorie_project as c";
229 }
230 $sql .= ' WHERE t.entity IN (' . getEntity('project') . ')';
231 if ($socids) {
232 $sql .= " AND t.fk_soc IN (" . $this->db->sanitize($socids) . ")";
233 }
234 // Search on sale representative
235 if ($search_sale && $search_sale != '-1') {
236 if ($search_sale == -2) {
237 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
238 } elseif ($search_sale > 0) {
239 $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) . ")";
240 }
241 }
242 // Select projects of given category
243 if ($category > 0) {
244 $sql .= " AND c.fk_categorie = " . ((int) $category) . " AND c.fk_project = t.rowid ";
245 }
246 // Add sql filters
247 if ($sqlfilters) {
248 $errormessage = '';
249 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
250 if ($errormessage) {
251 throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage);
252 }
253 }
254
255 //this query will return total orders with the filters given
256 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
257
258 $sql .= $this->db->order($sortfield, $sortorder);
259 if ($limit) {
260 if ($page < 0) {
261 $page = 0;
262 }
263 $offset = $limit * $page;
264
265 $sql .= $this->db->plimit($limit + 1, $offset);
266 }
267
268 dol_syslog("API Rest request");
269 $result = $this->db->query($sql);
270
271 if ($result) {
272 $num = $this->db->num_rows($result);
273 $min = min($num, ($limit <= 0 ? $num : $limit));
274 $i = 0;
275 while ($i < $min) {
276 $obj = $this->db->fetch_object($result);
277 $project_static = new Project($this->db);
278 if ($project_static->fetch($obj->rowid)) {
279 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($project_static), $properties);
280 }
281 $i++;
282 }
283 } else {
284 throw new RestException(503, 'Error when retrieve project list : ' . $this->db->lasterror());
285 }
286
287 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
288 if ($pagination_data) {
289 $totalsResult = $this->db->query($sqlTotals);
290 $total = $this->db->fetch_object($totalsResult)->total;
291
292 $tmp = $obj_ret;
293 $obj_ret = [];
294
295 $obj_ret['data'] = $tmp;
296 $obj_ret['pagination'] = [
297 'total' => (int) $total,
298 'page' => $page, //count starts from 0
299 'page_count' => ceil((int) $total / $limit),
300 'limit' => $limit
301 ];
302 }
303
304 return $obj_ret;
305 }
306
315 public function post($request_data = null)
316 {
317 global $conf;
318 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
319 throw new RestException(403, "Insufficiant rights");
320 }
321 // Check mandatory fields
322 $result = $this->_validate($request_data);
323
324 foreach ($request_data as $field => $value) {
325 if ($field === 'caller') {
326 // 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
327 $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
328 continue;
329 }
330
331 $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
332 }
333 /*if (isset($request_data["lines"])) {
334 $lines = array();
335 foreach ($request_data["lines"] as $line) {
336 array_push($lines, (object) $line);
337 }
338 $this->project->lines = $lines;
339 }*/
340
341 // Auto-generate the "ref" field if it is set to "auto"
342 if ($this->project->ref == -1 || $this->project->ref === 'auto') {
343 $reldir = '';
344 $defaultref = '';
345 $file = '';
346 $classname = '';
347 $filefound = 0;
348 $modele = getDolGlobalString('PROJECT_ADDON', 'mod_project_simple');
349
350 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
351 foreach ($dirmodels as $reldir) {
352 $file = dol_buildpath($reldir . "core/modules/project/" . $modele . '.php', 0);
353 if (file_exists($file)) {
354 $filefound = 1;
355 $classname = $modele;
356 break;
357 }
358 }
359 if ($filefound && !empty($classname)) {
360 $result = dol_include_once($reldir . "core/modules/project/" . $modele . '.php');
361 if ($result !== false && class_exists($classname)) {
362 $modProject = new $classname();
363 '@phan-var-force ModeleNumRefProjects $modProject';
364 $defaultref = $modProject->getNextValue(null, $this->project);
365 } else {
366 dol_syslog("Failed to include module file or invalid classname: " . $reldir . "core/modules/project/" . $modele . '.php', LOG_ERR);
367 }
368 } else {
369 dol_syslog("Module file not found or classname is empty: " . $modele, LOG_ERR);
370 }
371
372 if (is_numeric($defaultref) && $defaultref <= 0) {
373 $defaultref = '';
374 }
375
376 if (empty($defaultref)) {
377 $defaultref = 'PJ' . dol_print_date(dol_now(), 'dayrfc');
378 }
379
380 $this->project->ref = $defaultref;
381 }
382
383 if ($this->project->create(DolibarrApiAccess::$user) < 0) {
384 throw new RestException(500, "Error creating project", array_merge(array($this->project->error), $this->project->errors));
385 }
386
387 return $this->project->id;
388 }
389
408 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
409 {
410 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
411 throw new RestException(403);
412 }
413
414 $result = $this->project->fetch($id);
415 if (!$result) {
416 throw new RestException(404, 'project not found');
417 }
418
419 if (!DolibarrApi::_checkAccessToResource('ficheinter', $this->project->id)) {
420 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
421 }
422
423 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
424 if ($result < 0) {
425 throw new RestException(500, 'Error : ' . $this->project->error);
426 }
427
428 return $this->_cleanObjectDatas($this->project);
429 }
430
445 public function deleteContact($id, $contactid, $type)
446 {
447 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
448 throw new RestException(403);
449 }
450
451 $result = $this->project->fetch($id);
452
453 if (!$result) {
454 throw new RestException(404, 'Project not found');
455 }
456
457 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
458 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
459 }
460 foreach (array('internal', 'external') as $source) {
461 $contacts = $this->project->liste_contact(-1, $source);
462
463 foreach ($contacts as $contact) {
464 if ($contact['id'] == $contactid && $contact['code'] == $type) {
465 $result = $this->project->delete_contact($contact['rowid']);
466 if (!$result) {
467 throw new RestException(500, 'Error when deleted the contact');
468 }
469 }
470 }
471 }
472 return $this->_cleanObjectDatas($this->project);
473 }
486 public function getLines($id, $includetimespent = 0)
487 {
488 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
489 throw new RestException(403);
490 }
491
492 $result = $this->project->fetch($id);
493 if (!$result) {
494 throw new RestException(404, 'Project not found');
495 }
496
497 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
498 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
499 }
500 $this->project->getLinesArray(DolibarrApiAccess::$user);
501 $result = array();
502 foreach ($this->project->lines as $line) { // $line is a task
503 if ($includetimespent == 1) {
504 $timespent = $line->getSummaryOfTimeSpent(0);
505 }
506 if ($includetimespent == 2) {
507 $timespent = $line->fetchTimeSpentOnTask();
508 }
509 array_push($result, $this->_cleanObjectDatas($line));
510 }
511 return $result;
512 }
513
514
526 public function getRoles($id, $userid = 0)
527 {
528 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
529 throw new RestException(403);
530 }
531
532 $result = $this->project->fetch($id);
533 if (!$result) {
534 throw new RestException(404, 'Project not found');
535 }
536
537 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
538 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
539 }
540
541 require_once DOL_DOCUMENT_ROOT . '/projet/class/task.class.php';
542 $taskstatic = new Task($this->db);
543 $userp = DolibarrApiAccess::$user;
544 if ($userid > 0) {
545 $userp = new User($this->db);
546 $userp->fetch($userid);
547 }
548 $this->project->roles = $taskstatic->getUserRolesForProjectsOrTasks($userp, null, (string) $id, 0);
549 $result = array();
550 foreach ($this->project->roles as $line) {
551 array_push($result, $this->_cleanObjectDatas($line));
552 }
553
554 return $result;
555 }
556
557
570 /*
571 public function postLine($id, $request_data = null)
572 {
573 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
574 throw new RestException(403);
575 }
576
577 $result = $this->project->fetch($id);
578 if( ! $result ) {
579 throw new RestException(404, 'Project not found');
580 }
581
582 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
583 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
584 }
585
586 $request_data = (object) $request_data;
587
588 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
589
590 $updateRes = $this->project->addline(
591 $request_data->desc,
592 $request_data->subprice,
593 $request_data->qty,
594 $request_data->tva_tx,
595 $request_data->localtax1_tx,
596 $request_data->localtax2_tx,
597 $request_data->fk_product,
598 $request_data->remise_percent,
599 $request_data->info_bits,
600 $request_data->fk_remise_except,
601 'HT',
602 0,
603 $request_data->date_start,
604 $request_data->date_end,
605 $request_data->product_type,
606 $request_data->rang,
607 $request_data->special_code,
608 $fk_parent_line,
609 $request_data->fk_fournprice,
610 $request_data->pa_ht,
611 $request_data->label,
612 $request_data->array_options,
613 $request_data->fk_unit,
614 $this->element,
615 $request_data->id
616 );
617
618 if ($updateRes > 0) {
619 return $updateRes;
620
621 }
622 return false;
623 }
624 */
625
639 /*
640 public function putLine($id, $lineid, $request_data = null)
641 {
642 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
643 throw new RestException(403);
644 }
645
646 $result = $this->project->fetch($id);
647 if( ! $result ) {
648 throw new RestException(404, 'Project not found');
649 }
650
651 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
652 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
653 }
654
655 $request_data = (object) $request_data;
656
657 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
658
659 $updateRes = $this->project->updateline(
660 $lineid,
661 $request_data->desc,
662 $request_data->subprice,
663 $request_data->qty,
664 $request_data->remise_percent,
665 $request_data->tva_tx,
666 $request_data->localtax1_tx,
667 $request_data->localtax2_tx,
668 'HT',
669 $request_data->info_bits,
670 $request_data->date_start,
671 $request_data->date_end,
672 $request_data->product_type,
673 $request_data->fk_parent_line,
674 0,
675 $request_data->fk_fournprice,
676 $request_data->pa_ht,
677 $request_data->label,
678 $request_data->special_code,
679 $request_data->array_options,
680 $request_data->fk_unit
681 );
682
683 if ($updateRes > 0) {
684 $result = $this->get($id);
685 unset($result->line);
686 return $this->_cleanObjectDatas($result);
687 }
688 return false;
689 }*/
690
691
692
704 public function put($id, $request_data = null)
705 {
706 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
707 throw new RestException(403);
708 }
709
710 $result = $this->project->fetch($id);
711 if ($result <= 0) {
712 throw new RestException(404, 'Project not found');
713 }
714
715 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
716 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
717 }
718 foreach ($request_data as $field => $value) {
719 if ($field == 'id') {
720 continue;
721 }
722 if ($field === 'caller') {
723 // 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
724 $this->project->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
725 continue;
726 }
727 if ($field == 'array_options' && is_array($value)) {
728 foreach ($value as $index => $val) {
729 $this->project->array_options[$index] = $this->_checkValForAPI($field, $val, $this->project);
730 }
731 continue;
732 }
733
734 $this->project->$field = $this->_checkValForAPI($field, $value, $this->project);
735 }
736
737 if ($this->project->update(DolibarrApiAccess::$user) >= 0) {
738 return $this->get($id);
739 } else {
740 throw new RestException(500, $this->project->error);
741 }
742 }
743
753 public function delete($id)
754 {
755 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
756 throw new RestException(403);
757 }
758 $result = $this->project->fetch($id);
759 if (!$result) {
760 throw new RestException(404, 'Project not found');
761 }
762
763 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
764 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
765 }
766
767 if (!$this->project->delete(DolibarrApiAccess::$user)) {
768 throw new RestException(500, 'Error when delete project : ' . $this->project->error);
769 }
770
771 return array(
772 'success' => array(
773 'code' => 200,
774 'message' => 'Project deleted'
775 )
776 );
777 }
778
801 public function validate($id, $notrigger = 0)
802 {
803 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
804 throw new RestException(403);
805 }
806 $result = $this->project->fetch($id);
807 if (!$result) {
808 throw new RestException(404, 'Project not found');
809 }
810
811 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
812 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
813 }
814
815 $result = $this->project->setValid(DolibarrApiAccess::$user, $notrigger);
816 if ($result == 0) {
817 throw new RestException(304, 'Error nothing done. May be object is already validated');
818 }
819 if ($result < 0) {
820 throw new RestException(500, 'Error when validating Project: ' . $this->project->error);
821 }
822
823 return array(
824 'success' => array(
825 'code' => 200,
826 'message' => 'Project validated'
827 )
828 );
829 }
830
848 public function listTimespent($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $thirdparty_ids = '', $category = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
849 {
850 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
851 throw new RestException(403);
852 }
853
854 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
855 $socids = DolibarrApiAccess::$user->socid ?: $thirdparty_ids;
856
857 // If the internal user must only see his customers, force searching by him
858 $search_sale = 0;
859 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
860 $search_sale = DolibarrApiAccess::$user->id;
861 }
862
863 $sql = "SELECT et.rowid, et.element_duration, et.element_datehour, et.fk_user, et.note as time_note, et.thm,";
864 $sql .= " u.login as user_login, u.firstname as user_firstname, u.lastname as user_lastname,";
865 $sql .= " p.rowid as project_id, p.ref as project_ref, p.title as project_title,";
866 $sql .= " t.rowid as task_id, t.ref as task_ref, t.label as task_label,";
867 $sql .= " s.rowid as soc_id, s.nom as soc_name";
868 $sql .= " FROM ".MAIN_DB_PREFIX."projet as p";
869 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."projet_task as t ON (t.fk_projet = p.rowid)";
870 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."element_time as et ON (et.fk_element = t.rowid AND et.elementtype = 'task')";
871 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe AS s ON (s.rowid = p.fk_soc)";
872 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."user AS u ON (u.rowid = et.fk_user)";
873 $sql .= ' WHERE t.entity IN ('.getEntity('project').')';
874 if ($socids) {
875 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
876 }
877
878 // Search on sale representative
879 if ($search_sale && $search_sale != '-1') {
880 if ($search_sale == -2) {
881 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = t.fk_soc)";
882 } elseif ($search_sale > 0) {
883 $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).")";
884 }
885 }
886 // Select projects of given category
887 if ($category > 0) {
888 $sql .= " AND c.fk_categorie = ".((int) $category)." AND c.fk_project = t.rowid ";
889 }
890
891 // Add sql filters
892 if ($sqlfilters) {
893 $errormessage = '';
894 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
895 if ($errormessage) {
896 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
897 }
898 }
899
900 //this query will return total orders with the filters given
901 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
902
903 $sql .= $this->db->order($sortfield, $sortorder);
904 if ($limit) {
905 if ($page < 0) {
906 $page = 0;
907 }
908 $offset = $limit * $page;
909
910 $sql .= $this->db->plimit($limit + 1, $offset);
911 }
912
913 dol_syslog("API Rest request");
914 $result = $this->db->query($sql);
915 $obj_ret = array();
916 if ($result) {
917 $num = $this->db->num_rows($result);
918 $min = min($num, ($limit <= 0 ? $num : $limit));
919 $i = 0;
920 while ($i < $min) {
921 $obj = $this->db->fetch_object($result);
922 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($obj), $properties);
923 $i++;
924 }
925 } else {
926 throw new RestException(503, 'Error when retrieve timestamp list : '.$this->db->lasterror());
927 }
928
929
930 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
931 if ($pagination_data) {
932 $totalsResult = $this->db->query($sqlTotals);
933 $total = $this->db->fetch_object($totalsResult)->total;
934
935 $tmp = $obj_ret;
936 $obj_ret['data'] = $tmp;
937 $obj_ret['pagination'] = [
938 'total' => (int) $total,
939 'page' => $page, //count starts from 0
940 'page_count' => ceil((int) $total / $limit),
941 'limit' => $limit
942 ];
943 }
944
945 return $obj_ret;
946 }
947
948 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
958 protected function _cleanObjectDatas($object)
959 {
960 // phpcs:enable
961 $object = parent::_cleanObjectDatas($object);
962
963 unset($object->datec);
964 unset($object->datem);
965 unset($object->barcode_type);
966 unset($object->barcode_type_code);
967 unset($object->barcode_type_label);
968 unset($object->barcode_type_coder);
969 unset($object->cond_reglement_id);
970 unset($object->cond_reglement);
971 unset($object->fk_delivery_address);
972 unset($object->shipping_method_id);
973 unset($object->fk_account);
974 unset($object->note);
975 unset($object->fk_incoterms);
976 unset($object->label_incoterms);
977 unset($object->location_incoterms);
978 unset($object->name);
979 unset($object->lastname);
980 unset($object->firstname);
981 unset($object->civility_id);
982 unset($object->mode_reglement_id);
983 unset($object->country);
984 unset($object->country_id);
985 unset($object->country_code);
986
987 unset($object->weekWorkLoad);
988 unset($object->weekWorkLoad);
989
990 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
991
992 unset($object->total_ht);
993 unset($object->total_tva);
994 unset($object->total_localtax1);
995 unset($object->total_localtax2);
996 unset($object->total_ttc);
997
998 unset($object->comments);
999
1000 return $object;
1001 }
1002
1010 private function _validate($data)
1011 {
1012 $object = array();
1013 foreach (self::$FIELDS as $field) {
1014 if (!isset($data[$field])) {
1015 throw new RestException(400, "$field field missing");
1016 }
1017 $object[$field] = $data[$field];
1018 }
1019 return $object;
1020 }
1021
1022
1023
1024
1038 public function getContacts($id, $type = '')
1039 {
1040 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
1041 throw new RestException(403);
1042 }
1043
1044 $result = $this->project->fetch($id);
1045 if (!$result) {
1046 throw new RestException(404, 'Project not found');
1047 }
1048
1049 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
1050 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1051 }
1052
1053 $contacts = $this->project->liste_contact(-1, 'external', 0, $type);
1054 $socpeoples = $this->project->liste_contact(-1, 'internal', 0, $type);
1055
1056 $contacts = array_merge($contacts, $socpeoples);
1057
1058 return $contacts;
1059 }
1060
1080 public function addToContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0, $affect_to_tasks = null)
1081 {
1082 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1083 throw new RestException(403);
1084 }
1085
1086 $result = $this->project->fetch($id);
1087 if (!$result) {
1088 throw new RestException(404, 'Project not found');
1089 }
1090
1091 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
1092 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1093 }
1094
1095 // Ajouter le contact au projet
1096 $result = $this->project->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1097 if ($result <= 0) {
1098 throw new RestException(500, 'Error : ' . $this->project->error . 'result :' . $result);
1099 }
1100
1101 // Si demandé, ajouter le contact aux tâches
1102 if ($affect_to_tasks !== null) {
1103 $this->project->getLinesArray(DolibarrApiAccess::$user);
1104
1105 foreach ($this->project->lines as $task) {
1106 // Si $affect_to_tasks est vide, on affecte à toutes les tâches
1107 // Sinon, on vérifie si la tâche est dans la liste
1108 if (empty($affect_to_tasks) || in_array($task->id, $affect_to_tasks)) {
1109 $task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1110 }
1111 }
1112 }
1113
1114 $result = $this->project->fetch($id);
1115 if (!$result) {
1116 throw new RestException(404, 'Project not found');
1117 }
1118
1119 return $this->_cleanObjectDatas($this->project);
1120 }
1135 public function deleteToContact($id, $contactid, $type)
1136 {
1137 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1138 throw new RestException(403);
1139 }
1140
1141 $result = $this->project->fetch($id);
1142 if (!$result) {
1143 throw new RestException(404, 'Project not found');
1144 }
1145
1146 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
1147 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1148 }
1149
1150 foreach (array('internal', 'external') as $source) {
1151 $contacts = $this->project->liste_contact(-1, $source);
1152
1153 foreach ($contacts as $contact) {
1154 if ($contact['id'] == $contactid && $contact['code'] == $type) {
1155 $result = $this->project->delete_contact($contact['rowid']);
1156 if (!$result) {
1157 throw new RestException(500, 'Error when deleted the contact');
1158 }
1159 break 2;
1160 }
1161 }
1162 }
1163
1164 return $this->_cleanObjectDatas($this->project);
1165 }
1166
1177 public function getTimespent($id)
1178 {
1179 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
1180 throw new RestException(403);
1181 }
1182
1183 $result = $this->project->fetch($id);
1184 if (!$result) {
1185 throw new RestException(404, 'Project not found');
1186 }
1187
1188 if (!DolibarrApi::_checkAccessToResource('project', $this->project->id)) {
1189 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1190 }
1191
1192
1193 $this->project->getLinesArray(DolibarrApiAccess::$user);
1194 $allTimespent = array();
1195 foreach ($this->project->lines as $task) {
1196 $allTimespent[] = $task->getSummaryOfTimeSpent();
1197 //var_dump($taskTimespent);
1198 //foreach ($taskTimespent as $time) {
1199 // $this->_cleanObjectDatas($time);
1200
1201 //}
1202 }
1203
1204 return $allTimespent;
1205 }
1206 // TODO
1207 // getSummaryOfTimeSpent
1208}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class for API REST v1.
Definition api.class.php:33
_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:98
Class to manage projects.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to an project.
post($request_data=null)
Create project object.
addToContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0, $affect_to_tasks=null)
Adds a contact to a project.
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 @phpstan-template T.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $category=0, $sqlfilters='', $properties='', $pagination_data=false)
List projects.
getTimespent($id)
Get timespent of a project (from all its tasks)
__construct()
Constructor.
deleteToContact($id, $contactid, $type)
Delete a contact type of given project.
put($id, $request_data=null)
Add a task to given project.
listTimespent($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $thirdparty_ids='', $category=0, $sqlfilters='', $properties='', $pagination_data=false)
Get all timespent.
deleteContact($id, $contactid, $type)
Delete a contact type of given project.
getByRefExt($ref_ext)
Get properties of a project object.
getRoles($id, $userid=0)
Get roles a user is assigned to a project with.
getContacts($id, $type='')
Get contacts of given project.
Class to manage tasks.
Class to manage Dolibarr users.
dol_now($mode='gmt')
Return date for now.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.