dolibarr 25.0.0-alpha
api_tasks.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 Frédéric France <frederic.france@free.fr>
5 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2025 Jessica Kowal <jessicakowal69@gmail.com>
7 * Copyright (C) 2025 Charlene Benke <charlene@patas-monkey.com>
8 * Copyright (C) 2026 William Mead <william@m34d.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24use Luracast\Restler\RestException;
25
26require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/core/class/timespent.class.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
30
38class Tasks extends DolibarrApi
39{
43 public static $FIELDS = array(
44 'ref',
45 'label',
46 'fk_project'
47 );
48
52 public $task;
53
57 public function __construct()
58 {
59 global $db, $conf;
60 $this->db = $db;
61 $this->task = new Task($this->db);
62 }
63
77 public function get($id, $includetimespent = 0)
78 {
79 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
80 throw new RestException(403);
81 }
82
83 $result = $this->task->fetch($id);
84 if (!$result) {
85 throw new RestException(404, 'Task not found');
86 }
87
88 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
89 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
90 }
91
92 if ($includetimespent == 1) {
93 $timespent = $this->task->getSummaryOfTimeSpent(0);
94 }
95 if ($includetimespent == 2) {
96 $timespent = $this->task->fetchTimeSpentOnTask();
97 }
98
99 return $this->_cleanObjectDatas($this->task);
100 }
101
102
103
123 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false, $includetimespent = 0)
124 {
125 global $db, $conf;
126
127 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
128 throw new RestException(403);
129 }
130
131 $obj_ret = array();
132
133 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
134 $socids = DolibarrApiAccess::$user->socid ?: 0;
135
136 // If the internal user must only see his customers, force searching by him
137 $search_sale = 0;
138 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
139 $search_sale = DolibarrApiAccess::$user->id;
140 }
141
142 $sql = "SELECT t.rowid";
143 $sql .= " FROM " . MAIN_DB_PREFIX . "projet_task AS t";
144 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "projet_task_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
145 $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "projet AS p ON p.rowid = t.fk_projet";
146 $sql .= ' WHERE t.entity IN (' . getEntity('project') . ')';
147 if ($socids) {
148 $sql .= " AND p.fk_soc IN (" . $this->db->sanitize((string) $socids) . ")";
149 }
150 // Search on sale representative
151 if ($search_sale && $search_sale != '-1') {
152 if ($search_sale == -2) {
153 $sql .= " AND ".getSalesRepresentativeSqlFilter('p.fk_soc', 0, 1);
154 } elseif ($search_sale > 0) {
155 $sql .= " AND ".getSalesRepresentativeSqlFilter('p.fk_soc', (int) $search_sale);
156 }
157 }
158 // Add sql filters
159 if ($sqlfilters) {
160 $errormessage = '';
161 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
162 if ($errormessage) {
163 throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage);
164 }
165 }
166
167 //this query will return total tasks with the filters given
168 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
169
170 $sql .= $this->db->order($sortfield, $sortorder);
171 if ($limit) {
172 if ($page < 0) {
173 $page = 0;
174 }
175 $offset = $limit * $page;
176
177 $sql .= $this->db->plimit($limit + 1, $offset);
178 }
179
180 dol_syslog("API Rest request");
181 $result = $this->db->query($sql);
182
183 if ($result) {
184 $num = $this->db->num_rows($result);
185 $min = min($num, ($limit <= 0 ? $num : $limit));
186 $i = 0;
187 while ($i < $min) {
188 $obj = $this->db->fetch_object($result);
189 $task_static = new Task($this->db);
190 if ($task_static->fetch($obj->rowid)) {
191 if ($includetimespent == 1) {
192 $task_static->getSummaryOfTimeSpent(0);
193 }
194 if ($includetimespent == 2) {
195 $task_static->fetchTimeSpentOnTask();
196 }
197 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($task_static), $properties);
198 }
199 $i++;
200 }
201 } else {
202 throw new RestException(503, 'Error when retrieve task list : ' . $this->db->lasterror());
203 }
204
205 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
206 if ($pagination_data) {
207 $totalsResult = $this->db->query($sqlTotals);
208 $total = $this->db->fetch_object($totalsResult)->total;
209
210 $tmp = $obj_ret;
211 $obj_ret = [];
212
213 $obj_ret['data'] = $tmp;
214 $obj_ret['pagination'] = [
215 'total' => (int) $total,
216 'page' => $page, //count starts from 0
217 'page_count' => ceil((int) $total / $limit),
218 'limit' => $limit
219 ];
220 }
221
222 return $obj_ret;
223 }
224
235 public function post($request_data = null)
236 {
237 global $conf;
238 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
239 throw new RestException(403, "Insufficiant rights");
240 }
241 // Check mandatory fields
242 $result = $this->_validate($request_data);
243
244 foreach ($request_data as $field => $value) {
245 if ($field === 'caller') {
246 // 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
247 $this->task->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
248 continue;
249 }
250
251 $this->task->$field = $this->_checkValForAPI($field, $value, $this->task);
252 }
253 /*if (isset($request_data["lines"])) {
254 $lines = array();
255 foreach ($request_data["lines"] as $line) {
256 array_push($lines, (object) $line);
257 }
258 $this->project->lines = $lines;
259 }*/
260
261 // Auto-generate the "ref" field if it is set to "auto"
262 if ($this->task->ref == -1 || $this->task->ref === 'auto') {
263 $reldir = '';
264 $defaultref = '';
265 $file = '';
266 $classname = '';
267 $filefound = 0;
268 $modele = getDolGlobalString('PROJECT_TASK_ADDON', 'mod_task_simple');
269
270 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
271 foreach ($dirmodels as $reldir) {
272 $file = dol_buildpath($reldir."core/modules/project/task/".$modele.'.php', 0);
273 if (file_exists($file)) {
274 $filefound = 1;
275 $classname = $modele;
276 break;
277 }
278 }
279 if ($filefound && !empty($classname)) {
280 $result = dol_include_once($reldir . "core/modules/project/task/" . $modele . '.php');
281 if ($result !== false && class_exists($classname)) {
282 $modTask = new $classname();
283 '@phan-var-force ModeleNumRefTask $modTask';
284 $defaultref = $modTask->getNextValue(null, $this->task);
285 } else {
286 dol_syslog("Failed to include module file or invalid classname: " . $reldir . "core/modules/project/task/" . $modele . '.php', LOG_ERR);
287 }
288 } else {
289 dol_syslog("Module file not found or classname is empty: " . $modele, LOG_ERR);
290 }
291
292 if (is_numeric($defaultref) && $defaultref <= 0) {
293 $defaultref = '';
294 }
295
296 if (empty($defaultref)) {
297 $defaultref = 'TK' . dol_print_date(dol_now(), 'dayrfc');
298 }
299
300 $this->task->ref = $defaultref;
301 }
302
303 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
304 throw new RestException(500, "Error creating task", array_merge(array($this->task->error), $this->task->errors));
305 }
306
307 return $this->task->id;
308 }
309
320 public function getTimespent($id)
321 {
322 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
323 throw new RestException(403);
324 }
325
326 $result = $this->task->fetch($id);
327 if (!$result) {
328 throw new RestException(404, 'Task not found');
329 }
330
331 if (!DolibarrApi::_checkAccessToResource('tasks', $this->task->id)) {
332 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
333 }
334
335 $this->task->fetchTimeSpentOnTask();
336
337 $result = array();
338 foreach ($this->task->lines as $line) {
339 array_push($result, $this->_cleanObjectDatas($line));
340 }
341
342 return $result;
343 }
344
358 public function getRoles($id, $userid = 0)
359 {
360 global $db;
361
362 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
363 throw new RestException(403);
364 }
365
366 $result = $this->task->fetch($id);
367 if (!$result) {
368 throw new RestException(404, 'Task not found');
369 }
370
371 if (!DolibarrApi::_checkAccessToResource('tasks', $this->task->id)) {
372 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
373 }
374
375 $usert = DolibarrApiAccess::$user;
376 if ($userid > 0) {
377 $usert = new User($this->db);
378 $usert->fetch($userid);
379 }
380 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(null, $usert, '0', $id);
381 $result = array();
382 foreach ($this->task->roles as $line) {
383 array_push($result, $this->_cleanObjectDatas($line));
384 }
385
386 return $result;
387 }
388
389
390 // /**
391 // * Add a task to given project
392 // *
393 // * @param int $id Id of project to update
394 // * @param array $request_data Projectline data
395 // * @phan-param ?array<string,string> $request_data
396 // * @phpstan-param ?array<string,string> $request_data
397 // *
398 // * @url POST {id}/tasks
399 // *
400 // * @return int
401 // */
402 /*
403 public function postLine($id, $request_data = null)
404 {
405 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
406 throw new RestException(403);
407 }
408
409 $result = $this->project->fetch($id);
410 if( ! $result ) {
411 throw new RestException(404, 'Project not found');
412 }
413
414 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
415 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
416 }
417
418 $request_data = (object) $request_data;
419
420 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
421
422 $updateRes = $this->project->addline(
423 $request_data->desc,
424 $request_data->subprice,
425 $request_data->qty,
426 $request_data->tva_tx,
427 $request_data->localtax1_tx,
428 $request_data->localtax2_tx,
429 $request_data->fk_product,
430 $request_data->remise_percent,
431 $request_data->info_bits,
432 $request_data->fk_remise_except,
433 'HT',
434 0,
435 $request_data->date_start,
436 $request_data->date_end,
437 $request_data->product_type,
438 $request_data->rang,
439 $request_data->special_code,
440 $fk_parent_line,
441 $request_data->fk_fournprice,
442 $request_data->pa_ht,
443 $request_data->label,
444 $request_data->array_options,
445 $request_data->fk_unit,
446 $this->element,
447 $request_data->id
448 );
449
450 if ($updateRes > 0) {
451 return $updateRes;
452
453 }
454 return false;
455 }
456 */
457
458 // /**
459 // * Update a task of a given project
460 // *
461 // * @param int $id Id of project to update
462 // * @param int $taskid Id of task to update
463 // * @param array $request_data Projectline data
464 // * @phan-param ?array<string,string> $request_data
465 // * @phpstan-param ?array<string,string> $request_data
466 // *
467 // * @url PUT {id}/tasks/{taskid}
468 // *
469 // * @return object
470 // */
471 /*
472 public function putLine($id, $lineid, $request_data = null)
473 {
474 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
475 throw new RestException(403);
476 }
477
478 $result = $this->project->fetch($id);
479 if( ! $result ) {
480 throw new RestException(404, 'Project not found');
481 }
482
483 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
484 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
485 }
486
487 $request_data = (object) $request_data;
488
489 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
490
491 $updateRes = $this->project->updateline(
492 $lineid,
493 $request_data->desc,
494 $request_data->subprice,
495 $request_data->qty,
496 $request_data->remise_percent,
497 $request_data->tva_tx,
498 $request_data->localtax1_tx,
499 $request_data->localtax2_tx,
500 'HT',
501 $request_data->info_bits,
502 $request_data->date_start,
503 $request_data->date_end,
504 $request_data->product_type,
505 $request_data->fk_parent_line,
506 0,
507 $request_data->fk_fournprice,
508 $request_data->pa_ht,
509 $request_data->label,
510 $request_data->special_code,
511 $request_data->array_options,
512 $request_data->fk_unit
513 );
514
515 if ($updateRes > 0) {
516 $result = $this->get($id);
517 unset($result->line);
518 return $this->_cleanObjectDatas($result);
519 }
520 return false;
521 }*/
522
523
535 public function put($id, $request_data = null)
536 {
537 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
538 throw new RestException(403);
539 }
540
541 $result = $this->task->fetch($id);
542 if (!$result) {
543 throw new RestException(404, 'Task not found');
544 }
545
546 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
547 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
548 }
549 foreach ($request_data as $field => $value) {
550 if ($field == 'id') {
551 continue;
552 }
553 if ($field === 'caller') {
554 // 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
555 $this->task->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
556 continue;
557 }
558 if ($field == 'array_options' && is_array($value)) {
559 foreach ($value as $index => $val) {
560 $this->task->array_options[$index] = $this->_checkValForAPI($field, $val, $this->task);
561 }
562 continue;
563 }
564
565 $this->task->$field = $this->_checkValForAPI($field, $value, $this->task);
566 }
567
568 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
569 return $this->get($id);
570 } else {
571 throw new RestException(500, $this->task->error);
572 }
573 }
574
587 public function delete($id)
588 {
589 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
590 throw new RestException(403);
591 }
592 $result = $this->task->fetch($id);
593 if (!$result) {
594 throw new RestException(404, 'Task not found');
595 }
596
597 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
598 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
599 }
600
601 if ($this->task->delete(DolibarrApiAccess::$user) <= 0) {
602 throw new RestException(500, 'Error when delete task : ' . $this->task->error);
603 }
604
605 return array(
606 'success' => array(
607 'code' => 200,
608 'message' => 'Task deleted'
609 )
610 );
611 }
612
627 public function getTimeSpentByID($id, $timespent_id)
628 {
629 dol_syslog("API Rest request::getTimeSpent", LOG_DEBUG);
630 if (! DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
631 throw new RestException(403);
632 }
633
634 $taskresult = $this->task->fetch($id);
635 if (!$taskresult ) {
636 throw new RestException(404, 'Task with id='.$id.' not found');
637 }
638 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
639 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
640 }
641
642 $timespent = new TimeSpent($this->db);
643 $timeresult = $timespent->fetch($timespent_id);
644 if (!$timeresult ) {
645 throw new RestException(404, 'Timespent with id='.$timespent_id.' not found');
646 }
647 if (!DolibarrApi::_checkAccessToResource('time', $timespent->id)) {
648 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
649 }
650
651 return $this->_cleanTimeSpentObjectDatas($timespent);
652 }
653
677 public function addTimeSpent($id, $date, $duration, $product_id = null, $user_id = 0, $note = '', $progress = -1)
678 {
679 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
680 throw new RestException(403);
681 }
682 $result = $this->task->fetch($id);
683 if ($result <= 0) {
684 throw new RestException(404, 'Task not found');
685 }
686
687 if (!DolibarrApi::_checkAccessToResource('project', (int) $this->task->fk_project)) {
688 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
689 }
690
691 $uid = $user_id;
692 if (empty($uid)) {
693 $uid = DolibarrApiAccess::$user->id;
694 }
695
696 $newdate = dol_stringtotime($date, 1);
697
698 $this->task->timespent_date = $newdate;
699 $this->task->timespent_datehour = $newdate;
700 $this->task->timespent_withhour = 1;
701 $this->task->timespent_duration = $duration;
702 $this->task->timespent_fk_product = $product_id;
703 $this->task->timespent_fk_user = $uid;
704 $this->task->timespent_note = $note;
705 if (!empty($progress) && $progress >= 0 && $progress <= 100) {
706 $this->task->progress = $progress;
707 }
708
709 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
710 if ($result == 0) {
711 throw new RestException(304, 'Error nothing done. May be object is already validated');
712 }
713 if ($result < 0) {
714 throw new RestException(500, 'Error when adding time: ' . $this->task->error);
715 }
716
717 return array(
718 'success' => array(
719 'code' => 200,
720 'message' => 'Time spent added'
721 )
722 );
723 }
724
747 public function putTimeSpent($id, $timespent_id, $date, $duration, $product_id = null, $user_id = 0, $note = '')
748 {
749 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
750 throw new RestException(403);
751 }
752 $this->timespentRecordChecks($id, $timespent_id);
753
754 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
755 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
756 }
757
758 $newdate = dol_stringtotime($date, 1);
759 $this->task->timespent_date = $newdate;
760 $this->task->timespent_datehour = $newdate;
761 $this->task->timespent_withhour = 1;
762 $this->task->timespent_duration = $duration;
763 $this->task->timespent_fk_product = $product_id;
764 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
765 $this->task->timespent_note = $note;
766
767 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
768 if ($result == 0) {
769 throw new RestException(304, 'Error nothing done.');
770 }
771 if ($result < 0) {
772 throw new RestException(500, 'Error when updating time spent: ' . $this->task->error);
773 }
774
775 return array(
776 'success' => array(
777 'code' => 200,
778 'message' => 'Time spent updated'
779 )
780 );
781 }
782
797 public function deleteTimeSpent($id, $timespent_id)
798 {
799 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
800 throw new RestException(403);
801 }
802 $this->timespentRecordChecks($id, $timespent_id);
803
804 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
805 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
806 }
807
808 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
809 throw new RestException(500, 'Error when deleting time spent: ' . $this->task->error);
810 }
811
812 return array(
813 'success' => array(
814 'code' => 200,
815 'message' => 'Time spent deleted'
816 )
817 );
818 }
819
829 private function timespentRecordChecks($id, $timespent_id)
830 {
831 dol_syslog("API Rest request::timespentRecordChecks", LOG_DEBUG);
832 if ($this->task->fetch($id) <= 0) {
833 throw new RestException(404, 'Task not found');
834 }
835 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
836 throw new RestException(404, 'Timespent not found');
837 } elseif ($this->task->id != $id) {
838 throw new RestException(404, 'Timespent not found in selected task');
839 }
840 }
841
842 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
853 protected function _cleanObjectDatas($object)
854 {
855 // phpcs:enable
856 $object = parent::_cleanObjectDatas($object);
857
858 unset($object->barcode_type);
859 unset($object->barcode_type_code);
860 unset($object->barcode_type_label);
861 unset($object->barcode_type_coder);
862 unset($object->cond_reglement_id);
863 unset($object->cond_reglement);
864 unset($object->fk_delivery_address);
865 unset($object->shipping_method_id);
866 unset($object->fk_account);
867 unset($object->note);
868 unset($object->fk_incoterms);
869 unset($object->label_incoterms);
870 unset($object->location_incoterms);
871 unset($object->name);
872 unset($object->lastname);
873 unset($object->firstname);
874 unset($object->civility_id);
875 unset($object->mode_reglement_id);
876 unset($object->country);
877 unset($object->country_id);
878 unset($object->country_code);
879
880 unset($object->weekWorkLoad);
881 unset($object->weekWorkLoad);
882
883 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
884
885 unset($object->total_ht);
886 unset($object->total_tva);
887 unset($object->total_localtax1);
888 unset($object->total_localtax2);
889 unset($object->total_ttc);
890
891 unset($object->comments);
892
893 return $object;
894 }
895
896 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
908 {
909 if (!$object->note_private) {
910 $object->note_private = $object->note;
911 // unsure if we should use note_private or note_public, but note_private should be more secure
912 }
913 $saving_fk_element = $object->fk_element;
914 // because calling parent::_cleanObjectDatas clears fk_element
915
916 // phpcs:enable
917 $object = parent::_cleanObjectDatas($object);
918
919 unset($object->barcode_type);
920 unset($object->barcode_type_code);
921 unset($object->barcode_type_label);
922 unset($object->barcode_type_coder);
923 unset($object->cond_reglement_id);
924 unset($object->cond_reglement);
925 unset($object->fk_delivery_address);
926 unset($object->shipping_method_id);
927 unset($object->fk_account);
928 unset($object->fk_incoterms);
929 unset($object->label_incoterms);
930 unset($object->location_incoterms);
931 unset($object->name);
932 unset($object->lastname);
933 unset($object->firstname);
934 unset($object->civility_id);
935 unset($object->mode_reglement_id);
936 unset($object->country);
937 unset($object->country_id);
938 unset($object->country_code);
939
940 unset($object->weekWorkLoad);
941 unset($object->weekWorkLoad);
942
943 unset($object->actiontypecode);
944 unset($object->array_languages);
945 unset($object->array_options);
946 unset($object->canvas);
947 unset($object->civility_code);
948 unset($object->cond_reglement_supplier_id);
949 unset($object->contact_id);
950 unset($object->contacts_ids);
951 unset($object->contacts_ids_internal);
952 unset($object->date_cloture);
953 unset($object->date_validation);
954 unset($object->demand_reason_id);
955 unset($object->deposit_percent);
956 unset($object->entity);
957 unset($object->extraparams);
958 unset($object->fk_multicurrency);
959 unset($object->fk_project);
960 unset($object->fk_user_creat);
961 unset($object->fk_user_modif);
962 unset($object->last_main_doc);
963 unset($object->lines);
964 unset($object->linkedObjectsIds);
965 unset($object->module);
966 unset($object->multicurrency_code);
967 unset($object->multicurrency_total_ht);
968 unset($object->multicurrency_total_localtax1);
969 unset($object->multicurrency_total_localtax2);
970 unset($object->multicurrency_total_ttc);
971 unset($object->multicurrency_total_tva);
972 unset($object->multicurrency_tx);
973 unset($object->note_public);
974 unset($object->origin_id);
975 unset($object->origin_type);
976 unset($object->product);
977 unset($object->ref);
978 unset($object->region_id);
979 unset($object->retained_warranty_fk_cond_reglement);
980 unset($object->rowid);
981 unset($object->shipping_method);
982 unset($object->specimen);
983 unset($object->state_id);
984 unset($object->status);
985 unset($object->statut);
986 unset($object->totalpaid);
987 unset($object->transport_mode_id);
988 unset($object->user);
989 unset($object->user_author);
990 unset($object->user_closing_id);
991 unset($object->user_creation);
992 unset($object->user_creation_id);
993 unset($object->user_modification);
994 unset($object->user_modification_id);
995 unset($object->user_valid);
996 unset($object->user_validation);
997 unset($object->user_validation_id);
998 unset($object->warehouse_id);
999
1000 unset($object->total_ht);
1001 unset($object->total_tva);
1002 unset($object->total_localtax1);
1003 unset($object->total_localtax2);
1004 unset($object->total_ttc);
1005
1006 unset($object->comments);
1007
1008 if (!$object->date_creation) {
1009 $object->date_creation = $object->datec;
1010 }
1011 if (!$object->date_modification) {
1012 $object->date_modification = $object->tms;
1013 }
1014 if (!$object->fk_element) {
1015 $object->fk_element = $saving_fk_element;
1016 // because calling parent::_cleanObjectDatas clears fk_element
1017 }
1018
1019 return $object;
1020 }
1021
1029 private function _validate($data)
1030 {
1031 if ($data === null) {
1032 $data = array();
1033 }
1034 $object = array();
1035 foreach (self::$FIELDS as $field) {
1036 if (!isset($data[$field])) {
1037 throw new RestException(400, "$field field missing");
1038 }
1039 $object[$field] = $data[$field];
1040 }
1041 return $object;
1042 }
1043
1059 public function getContacts($id, $type = '')
1060 {
1061 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
1062 throw new RestException(403);
1063 }
1064
1065 $result = $this->task->fetch($id);
1066 if (!$result) {
1067 throw new RestException(404, 'Task not found');
1068 }
1069
1070 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
1071 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1072 }
1073
1074 $contacts = $this->task->liste_contact(-1, 'external', 0, $type);
1075 $socpeoples = $this->task->liste_contact(-1, 'internal', 0, $type);
1076
1077 $contacts = array_merge($contacts, $socpeoples);
1078
1079 return $contacts; // Return array
1080 }
1081
1102 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
1103 {
1104 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1105 throw new RestException(403);
1106 }
1107
1108 $result = $this->task->fetch($id);
1109 if (!$result) {
1110 throw new RestException(404, 'Task not found');
1111 }
1112
1113 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
1114 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1115 }
1116
1117 $result = $this->task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1118 if ($result <= 0) {
1119 throw new RestException(500, 'Error : ' . $this->task->error);
1120 }
1121
1122 $result = $this->task->fetch($id);
1123 if (!$result) {
1124 throw new RestException(404, 'Task not found');
1125 }
1126
1127 return $this->_cleanObjectDatas($this->task);
1128 }
1129
1130
1147 public function deleteContact($id, $contactid, $type)
1148 {
1149 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1150 throw new RestException(403);
1151 }
1152
1153 $result = $this->task->fetch($id);
1154 if (!$result) {
1155 throw new RestException(404, 'Task not found');
1156 }
1157
1158 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
1159 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1160 }
1161
1162 foreach (array('internal', 'external') as $source) {
1163 $contacts = $this->task->liste_contact(-1, $source);
1164
1165 foreach ($contacts as $contact) {
1166 if ($contact['id'] == $contactid && $contact['code'] == $type) {
1167 $result = $this->task->delete_contact($contact['rowid']);
1168 if (!$result) {
1169 throw new RestException(500, 'Error when deleted the contact');
1170 }
1171 break 2;
1172 }
1173 }
1174 }
1175
1176 return $this->_cleanObjectDatas($this->task);
1177 }
1178}
$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:35
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage tasks.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false, $includetimespent=0)
List tasks.
_validate($data)
Validate fields before create or update object.
deleteContact($id, $contactid, $type)
Delete a contact type of given task.
post($request_data=null)
Create task object.
addTimeSpent($id, $date, $duration, $product_id=null, $user_id=0, $note='', $progress=-1)
Add time spent to a task of a project.
put($id, $request_data=null)
Add a task to given project.
getTimeSpentByID($id, $timespent_id)
Get time spent of a task.
_cleanTimeSpentObjectDatas($object)
Clean sensitive object data @phpstan-template T of Object.
getRoles($id, $userid=0)
Get roles a user is assigned to a task with.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to a task.
getContacts($id, $type='')
Get contacts of given task.
getTimespent($id)
Get time spent of a task.
__construct()
Constructor.
putTimeSpent($id, $timespent_id, $date, $duration, $product_id=null, $user_id=0, $note='')
Update time spent for a task of a project.
timespentRecordChecks($id, $timespent_id)
Validate task & timespent IDs for timespent API methods.
deleteTimeSpent($id, $timespent_id)
Delete time spent for a task of a project.
Class for TimeSpent.
Class to manage Dolibarr users.
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:436
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
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.