dolibarr 24.0.0-beta
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';
29
37class Tasks extends DolibarrApi
38{
42 public static $FIELDS = array(
43 'ref',
44 'label',
45 'fk_project'
46 );
47
51 public $task;
52
56 public function __construct()
57 {
58 global $db, $conf;
59 $this->db = $db;
60 $this->task = new Task($this->db);
61 }
62
76 public function get($id, $includetimespent = 0)
77 {
78 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
79 throw new RestException(403);
80 }
81
82 $result = $this->task->fetch($id);
83 if (!$result) {
84 throw new RestException(404, 'Task not found');
85 }
86
87 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
88 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
89 }
90
91 if ($includetimespent == 1) {
92 $timespent = $this->task->getSummaryOfTimeSpent(0);
93 }
94 if ($includetimespent == 2) {
95 $timespent = $this->task->fetchTimeSpentOnTask();
96 }
97
98 return $this->_cleanObjectDatas($this->task);
99 }
100
101
102
121 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '', $pagination_data = false)
122 {
123 global $db, $conf;
124
125 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
126 throw new RestException(403);
127 }
128
129 $obj_ret = array();
130
131 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
132 $socids = DolibarrApiAccess::$user->socid ?: 0;
133
134 // If the internal user must only see his customers, force searching by him
135 $search_sale = 0;
136 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
137 $search_sale = DolibarrApiAccess::$user->id;
138 }
139
140 $sql = "SELECT t.rowid";
141 $sql .= " FROM " . MAIN_DB_PREFIX . "projet_task AS t";
142 $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
143 $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "projet AS p ON p.rowid = t.fk_projet";
144 $sql .= ' WHERE t.entity IN (' . getEntity('project') . ')';
145 if ($socids) {
146 $sql .= " AND p.fk_soc IN (" . $this->db->sanitize((string) $socids) . ")";
147 }
148 // Search on sale representative
149 if ($search_sale && $search_sale != '-1') {
150 if ($search_sale == -2) {
151 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = p.fk_soc)";
152 } elseif ($search_sale > 0) {
153 $sql .= " AND EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = p.fk_soc AND sc.fk_user = " . ((int) $search_sale) . ")";
154 }
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 //this query will return total tasks with the filters given
166 $sqlTotals = str_replace('SELECT t.rowid', 'SELECT count(t.rowid) as total', $sql);
167
168 $sql .= $this->db->order($sortfield, $sortorder);
169 if ($limit) {
170 if ($page < 0) {
171 $page = 0;
172 }
173 $offset = $limit * $page;
174
175 $sql .= $this->db->plimit($limit + 1, $offset);
176 }
177
178 dol_syslog("API Rest request");
179 $result = $this->db->query($sql);
180
181 if ($result) {
182 $num = $this->db->num_rows($result);
183 $min = min($num, ($limit <= 0 ? $num : $limit));
184 $i = 0;
185 while ($i < $min) {
186 $obj = $this->db->fetch_object($result);
187 $task_static = new Task($this->db);
188 if ($task_static->fetch($obj->rowid)) {
189 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($task_static), $properties);
190 }
191 $i++;
192 }
193 } else {
194 throw new RestException(503, 'Error when retrieve task list : ' . $this->db->lasterror());
195 }
196
197 //if $pagination_data is true the response will contain element data with all values and element pagination with pagination data(total,page,limit)
198 if ($pagination_data) {
199 $totalsResult = $this->db->query($sqlTotals);
200 $total = $this->db->fetch_object($totalsResult)->total;
201
202 $tmp = $obj_ret;
203 $obj_ret = [];
204
205 $obj_ret['data'] = $tmp;
206 $obj_ret['pagination'] = [
207 'total' => (int) $total,
208 'page' => $page, //count starts from 0
209 'page_count' => ceil((int) $total / $limit),
210 'limit' => $limit
211 ];
212 }
213
214 return $obj_ret;
215 }
216
227 public function post($request_data = null)
228 {
229 global $conf;
230 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
231 throw new RestException(403, "Insufficiant rights");
232 }
233 // Check mandatory fields
234 $result = $this->_validate($request_data);
235
236 foreach ($request_data as $field => $value) {
237 if ($field === 'caller') {
238 // 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
239 $this->task->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
240 continue;
241 }
242
243 $this->task->$field = $this->_checkValForAPI($field, $value, $this->task);
244 }
245 /*if (isset($request_data["lines"])) {
246 $lines = array();
247 foreach ($request_data["lines"] as $line) {
248 array_push($lines, (object) $line);
249 }
250 $this->project->lines = $lines;
251 }*/
252
253 // Auto-generate the "ref" field if it is set to "auto"
254 if ($this->task->ref == -1 || $this->task->ref === 'auto') {
255 $reldir = '';
256 $defaultref = '';
257 $file = '';
258 $classname = '';
259 $filefound = 0;
260 $modele = getDolGlobalString('PROJECT_TASK_ADDON', 'mod_task_simple');
261
262 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
263 foreach ($dirmodels as $reldir) {
264 $file = dol_buildpath($reldir."core/modules/project/task/".$modele.'.php', 0);
265 if (file_exists($file)) {
266 $filefound = 1;
267 $classname = $modele;
268 break;
269 }
270 }
271 if ($filefound && !empty($classname)) {
272 $result = dol_include_once($reldir . "core/modules/project/task/" . $modele . '.php');
273 if ($result !== false && class_exists($classname)) {
274 $modTask = new $classname();
275 '@phan-var-force ModeleNumRefTask $modTask';
276 $defaultref = $modTask->getNextValue(null, $this->task);
277 } else {
278 dol_syslog("Failed to include module file or invalid classname: " . $reldir . "core/modules/project/task/" . $modele . '.php', LOG_ERR);
279 }
280 } else {
281 dol_syslog("Module file not found or classname is empty: " . $modele, LOG_ERR);
282 }
283
284 if (is_numeric($defaultref) && $defaultref <= 0) {
285 $defaultref = '';
286 }
287
288 if (empty($defaultref)) {
289 $defaultref = 'TK' . dol_print_date(dol_now(), 'dayrfc');
290 }
291
292 $this->task->ref = $defaultref;
293 }
294
295 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
296 throw new RestException(500, "Error creating task", array_merge(array($this->task->error), $this->task->errors));
297 }
298
299 return $this->task->id;
300 }
301
312 public function getTimespent($id)
313 {
314 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
315 throw new RestException(403);
316 }
317
318 $result = $this->task->fetch($id);
319 if (!$result) {
320 throw new RestException(404, 'Task not found');
321 }
322
323 if (!DolibarrApi::_checkAccessToResource('tasks', $this->task->id)) {
324 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
325 }
326
327 $this->task->fetchTimeSpentOnTask();
328
329 $result = array();
330 foreach ($this->task->lines as $line) {
331 array_push($result, $this->_cleanObjectDatas($line));
332 }
333
334 return $result;
335 }
336
350 public function getRoles($id, $userid = 0)
351 {
352 global $db;
353
354 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
355 throw new RestException(403);
356 }
357
358 $result = $this->task->fetch($id);
359 if (!$result) {
360 throw new RestException(404, 'Task not found');
361 }
362
363 if (!DolibarrApi::_checkAccessToResource('tasks', $this->task->id)) {
364 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
365 }
366
367 $usert = DolibarrApiAccess::$user;
368 if ($userid > 0) {
369 $usert = new User($this->db);
370 $usert->fetch($userid);
371 }
372 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(null, $usert, '0', $id);
373 $result = array();
374 foreach ($this->task->roles as $line) {
375 array_push($result, $this->_cleanObjectDatas($line));
376 }
377
378 return $result;
379 }
380
381
382 // /**
383 // * Add a task to given project
384 // *
385 // * @param int $id Id of project to update
386 // * @param array $request_data Projectline data
387 // * @phan-param ?array<string,string> $request_data
388 // * @phpstan-param ?array<string,string> $request_data
389 // *
390 // * @url POST {id}/tasks
391 // *
392 // * @return int
393 // */
394 /*
395 public function postLine($id, $request_data = null)
396 {
397 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
398 throw new RestException(403);
399 }
400
401 $result = $this->project->fetch($id);
402 if( ! $result ) {
403 throw new RestException(404, 'Project not found');
404 }
405
406 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
407 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
408 }
409
410 $request_data = (object) $request_data;
411
412 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
413
414 $updateRes = $this->project->addline(
415 $request_data->desc,
416 $request_data->subprice,
417 $request_data->qty,
418 $request_data->tva_tx,
419 $request_data->localtax1_tx,
420 $request_data->localtax2_tx,
421 $request_data->fk_product,
422 $request_data->remise_percent,
423 $request_data->info_bits,
424 $request_data->fk_remise_except,
425 'HT',
426 0,
427 $request_data->date_start,
428 $request_data->date_end,
429 $request_data->product_type,
430 $request_data->rang,
431 $request_data->special_code,
432 $fk_parent_line,
433 $request_data->fk_fournprice,
434 $request_data->pa_ht,
435 $request_data->label,
436 $request_data->array_options,
437 $request_data->fk_unit,
438 $this->element,
439 $request_data->id
440 );
441
442 if ($updateRes > 0) {
443 return $updateRes;
444
445 }
446 return false;
447 }
448 */
449
450 // /**
451 // * Update a task of a given project
452 // *
453 // * @param int $id Id of project to update
454 // * @param int $taskid Id of task to update
455 // * @param array $request_data Projectline data
456 // * @phan-param ?array<string,string> $request_data
457 // * @phpstan-param ?array<string,string> $request_data
458 // *
459 // * @url PUT {id}/tasks/{taskid}
460 // *
461 // * @return object
462 // */
463 /*
464 public function putLine($id, $lineid, $request_data = null)
465 {
466 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
467 throw new RestException(403);
468 }
469
470 $result = $this->project->fetch($id);
471 if( ! $result ) {
472 throw new RestException(404, 'Project not found');
473 }
474
475 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
476 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
477 }
478
479 $request_data = (object) $request_data;
480
481 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
482
483 $updateRes = $this->project->updateline(
484 $lineid,
485 $request_data->desc,
486 $request_data->subprice,
487 $request_data->qty,
488 $request_data->remise_percent,
489 $request_data->tva_tx,
490 $request_data->localtax1_tx,
491 $request_data->localtax2_tx,
492 'HT',
493 $request_data->info_bits,
494 $request_data->date_start,
495 $request_data->date_end,
496 $request_data->product_type,
497 $request_data->fk_parent_line,
498 0,
499 $request_data->fk_fournprice,
500 $request_data->pa_ht,
501 $request_data->label,
502 $request_data->special_code,
503 $request_data->array_options,
504 $request_data->fk_unit
505 );
506
507 if ($updateRes > 0) {
508 $result = $this->get($id);
509 unset($result->line);
510 return $this->_cleanObjectDatas($result);
511 }
512 return false;
513 }*/
514
515
527 public function put($id, $request_data = null)
528 {
529 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
530 throw new RestException(403);
531 }
532
533 $result = $this->task->fetch($id);
534 if (!$result) {
535 throw new RestException(404, 'Task not found');
536 }
537
538 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
539 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
540 }
541 foreach ($request_data as $field => $value) {
542 if ($field == 'id') {
543 continue;
544 }
545 if ($field === 'caller') {
546 // 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
547 $this->task->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
548 continue;
549 }
550 if ($field == 'array_options' && is_array($value)) {
551 foreach ($value as $index => $val) {
552 $this->task->array_options[$index] = $this->_checkValForAPI($field, $val, $this->task);
553 }
554 continue;
555 }
556
557 $this->task->$field = $this->_checkValForAPI($field, $value, $this->task);
558 }
559
560 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
561 return $this->get($id);
562 } else {
563 throw new RestException(500, $this->task->error);
564 }
565 }
566
579 public function delete($id)
580 {
581 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
582 throw new RestException(403);
583 }
584 $result = $this->task->fetch($id);
585 if (!$result) {
586 throw new RestException(404, 'Task not found');
587 }
588
589 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
590 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
591 }
592
593 if ($this->task->delete(DolibarrApiAccess::$user) <= 0) {
594 throw new RestException(500, 'Error when delete task : ' . $this->task->error);
595 }
596
597 return array(
598 'success' => array(
599 'code' => 200,
600 'message' => 'Task deleted'
601 )
602 );
603 }
604
619 public function getTimeSpentByID($id, $timespent_id)
620 {
621 dol_syslog("API Rest request::getTimeSpent", LOG_DEBUG);
622 if (! DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
623 throw new RestException(403);
624 }
625
626 $taskresult = $this->task->fetch($id);
627 if (!$taskresult ) {
628 throw new RestException(404, 'Task with id='.$id.' not found');
629 }
630 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
631 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
632 }
633
634 $timespent = new TimeSpent($this->db);
635 $timeresult = $timespent->fetch($timespent_id);
636 if (!$timeresult ) {
637 throw new RestException(404, 'Timespent with id='.$timespent_id.' not found');
638 }
639 if (!DolibarrApi::_checkAccessToResource('time', $timespent->id)) {
640 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
641 }
642
643 return $this->_cleanTimeSpentObjectDatas($timespent);
644 }
645
669 public function addTimeSpent($id, $date, $duration, $product_id = null, $user_id = 0, $note = '', $progress = -1)
670 {
671 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
672 throw new RestException(403);
673 }
674 $result = $this->task->fetch($id);
675 if ($result <= 0) {
676 throw new RestException(404, 'Task not found');
677 }
678
679 if (!DolibarrApi::_checkAccessToResource('project', (int) $this->task->fk_project)) {
680 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
681 }
682
683 $uid = $user_id;
684 if (empty($uid)) {
685 $uid = DolibarrApiAccess::$user->id;
686 }
687
688 $newdate = dol_stringtotime($date, 1);
689
690 $this->task->timespent_date = $newdate;
691 $this->task->timespent_datehour = $newdate;
692 $this->task->timespent_withhour = 1;
693 $this->task->timespent_duration = $duration;
694 $this->task->timespent_fk_product = $product_id;
695 $this->task->timespent_fk_user = $uid;
696 $this->task->timespent_note = $note;
697 if (!empty($progress) && $progress >= 0 && $progress <= 100) {
698 $this->task->progress = $progress;
699 }
700
701 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
702 if ($result == 0) {
703 throw new RestException(304, 'Error nothing done. May be object is already validated');
704 }
705 if ($result < 0) {
706 throw new RestException(500, 'Error when adding time: ' . $this->task->error);
707 }
708
709 return array(
710 'success' => array(
711 'code' => 200,
712 'message' => 'Time spent added'
713 )
714 );
715 }
716
739 public function putTimeSpent($id, $timespent_id, $date, $duration, $product_id = null, $user_id = 0, $note = '')
740 {
741 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
742 throw new RestException(403);
743 }
744 $this->timespentRecordChecks($id, $timespent_id);
745
746 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
747 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
748 }
749
750 $newdate = dol_stringtotime($date, 1);
751 $this->task->timespent_date = $newdate;
752 $this->task->timespent_datehour = $newdate;
753 $this->task->timespent_withhour = 1;
754 $this->task->timespent_duration = $duration;
755 $this->task->timespent_fk_product = $product_id;
756 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
757 $this->task->timespent_note = $note;
758
759 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
760 if ($result == 0) {
761 throw new RestException(304, 'Error nothing done.');
762 }
763 if ($result < 0) {
764 throw new RestException(500, 'Error when updating time spent: ' . $this->task->error);
765 }
766
767 return array(
768 'success' => array(
769 'code' => 200,
770 'message' => 'Time spent updated'
771 )
772 );
773 }
774
789 public function deleteTimeSpent($id, $timespent_id)
790 {
791 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
792 throw new RestException(403);
793 }
794 $this->timespentRecordChecks($id, $timespent_id);
795
796 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
797 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
798 }
799
800 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
801 throw new RestException(500, 'Error when deleting time spent: ' . $this->task->error);
802 }
803
804 return array(
805 'success' => array(
806 'code' => 200,
807 'message' => 'Time spent deleted'
808 )
809 );
810 }
811
821 private function timespentRecordChecks($id, $timespent_id)
822 {
823 dol_syslog("API Rest request::timespentRecordChecks", LOG_DEBUG);
824 if ($this->task->fetch($id) <= 0) {
825 throw new RestException(404, 'Task not found');
826 }
827 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
828 throw new RestException(404, 'Timespent not found');
829 } elseif ($this->task->id != $id) {
830 throw new RestException(404, 'Timespent not found in selected task');
831 }
832 }
833
834 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
845 protected function _cleanObjectDatas($object)
846 {
847 // phpcs:enable
848 $object = parent::_cleanObjectDatas($object);
849
850 unset($object->barcode_type);
851 unset($object->barcode_type_code);
852 unset($object->barcode_type_label);
853 unset($object->barcode_type_coder);
854 unset($object->cond_reglement_id);
855 unset($object->cond_reglement);
856 unset($object->fk_delivery_address);
857 unset($object->shipping_method_id);
858 unset($object->fk_account);
859 unset($object->note);
860 unset($object->fk_incoterms);
861 unset($object->label_incoterms);
862 unset($object->location_incoterms);
863 unset($object->name);
864 unset($object->lastname);
865 unset($object->firstname);
866 unset($object->civility_id);
867 unset($object->mode_reglement_id);
868 unset($object->country);
869 unset($object->country_id);
870 unset($object->country_code);
871
872 unset($object->weekWorkLoad);
873 unset($object->weekWorkLoad);
874
875 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
876
877 unset($object->total_ht);
878 unset($object->total_tva);
879 unset($object->total_localtax1);
880 unset($object->total_localtax2);
881 unset($object->total_ttc);
882
883 unset($object->comments);
884
885 return $object;
886 }
887
888 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
900 {
901 if (!$object->note_private) {
902 $object->note_private = $object->note;
903 // unsure if we should use note_private or note_public, but note_private should be more secure
904 }
905 $saving_fk_element = $object->fk_element;
906 // because calling parent::_cleanObjectDatas clears fk_element
907
908 // phpcs:enable
909 $object = parent::_cleanObjectDatas($object);
910
911 unset($object->barcode_type);
912 unset($object->barcode_type_code);
913 unset($object->barcode_type_label);
914 unset($object->barcode_type_coder);
915 unset($object->cond_reglement_id);
916 unset($object->cond_reglement);
917 unset($object->fk_delivery_address);
918 unset($object->shipping_method_id);
919 unset($object->fk_account);
920 unset($object->fk_incoterms);
921 unset($object->label_incoterms);
922 unset($object->location_incoterms);
923 unset($object->name);
924 unset($object->lastname);
925 unset($object->firstname);
926 unset($object->civility_id);
927 unset($object->mode_reglement_id);
928 unset($object->country);
929 unset($object->country_id);
930 unset($object->country_code);
931
932 unset($object->weekWorkLoad);
933 unset($object->weekWorkLoad);
934
935 unset($object->actiontypecode);
936 unset($object->array_languages);
937 unset($object->array_options);
938 unset($object->canvas);
939 unset($object->civility_code);
940 unset($object->cond_reglement_supplier_id);
941 unset($object->contact_id);
942 unset($object->contacts_ids);
943 unset($object->contacts_ids_internal);
944 unset($object->date_cloture);
945 unset($object->date_validation);
946 unset($object->demand_reason_id);
947 unset($object->deposit_percent);
948 unset($object->entity);
949 unset($object->extraparams);
950 unset($object->fk_multicurrency);
951 unset($object->fk_project);
952 unset($object->fk_user_creat);
953 unset($object->fk_user_modif);
954 unset($object->last_main_doc);
955 unset($object->lines);
956 unset($object->linkedObjectsIds);
957 unset($object->module);
958 unset($object->multicurrency_code);
959 unset($object->multicurrency_total_ht);
960 unset($object->multicurrency_total_localtax1);
961 unset($object->multicurrency_total_localtax2);
962 unset($object->multicurrency_total_ttc);
963 unset($object->multicurrency_total_tva);
964 unset($object->multicurrency_tx);
965 unset($object->note_public);
966 unset($object->origin_id);
967 unset($object->origin_type);
968 unset($object->product);
969 unset($object->ref);
970 unset($object->region_id);
971 unset($object->retained_warranty_fk_cond_reglement);
972 unset($object->rowid);
973 unset($object->shipping_method);
974 unset($object->specimen);
975 unset($object->state_id);
976 unset($object->status);
977 unset($object->statut);
978 unset($object->totalpaid);
979 unset($object->transport_mode_id);
980 unset($object->user);
981 unset($object->user_author);
982 unset($object->user_closing_id);
983 unset($object->user_creation);
984 unset($object->user_creation_id);
985 unset($object->user_modification);
986 unset($object->user_modification_id);
987 unset($object->user_valid);
988 unset($object->user_validation);
989 unset($object->user_validation_id);
990 unset($object->warehouse_id);
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 if (!$object->date_creation) {
1001 $object->date_creation = $object->datec;
1002 }
1003 if (!$object->date_modification) {
1004 $object->date_modification = $object->tms;
1005 }
1006 if (!$object->fk_element) {
1007 $object->fk_element = $saving_fk_element;
1008 // because calling parent::_cleanObjectDatas clears fk_element
1009 }
1010
1011 return $object;
1012 }
1013
1021 private function _validate($data)
1022 {
1023 if ($data === null) {
1024 $data = array();
1025 }
1026 $object = array();
1027 foreach (self::$FIELDS as $field) {
1028 if (!isset($data[$field])) {
1029 throw new RestException(400, "$field field missing");
1030 }
1031 $object[$field] = $data[$field];
1032 }
1033 return $object;
1034 }
1035
1051 public function getContacts($id, $type = '')
1052 {
1053 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
1054 throw new RestException(403);
1055 }
1056
1057 $result = $this->task->fetch($id);
1058 if (!$result) {
1059 throw new RestException(404, 'Task not found');
1060 }
1061
1062 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
1063 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1064 }
1065
1066 $contacts = $this->task->liste_contact(-1, 'external', 0, $type);
1067 $socpeoples = $this->task->liste_contact(-1, 'internal', 0, $type);
1068
1069 $contacts = array_merge($contacts, $socpeoples);
1070
1071 return $contacts; // Return array
1072 }
1073
1094 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
1095 {
1096 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1097 throw new RestException(403);
1098 }
1099
1100 $result = $this->task->fetch($id);
1101 if (!$result) {
1102 throw new RestException(404, 'Task not found');
1103 }
1104
1105 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
1106 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1107 }
1108
1109 $result = $this->task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1110 if ($result <= 0) {
1111 throw new RestException(500, 'Error : ' . $this->task->error);
1112 }
1113
1114 $result = $this->task->fetch($id);
1115 if (!$result) {
1116 throw new RestException(404, 'Task not found');
1117 }
1118
1119 return $this->_cleanObjectDatas($this->task);
1120 }
1121
1122
1139 public function deleteContact($id, $contactid, $type)
1140 {
1141 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1142 throw new RestException(403);
1143 }
1144
1145 $result = $this->task->fetch($id);
1146 if (!$result) {
1147 throw new RestException(404, 'Task not found');
1148 }
1149
1150 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
1151 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1152 }
1153
1154 foreach (array('internal', 'external') as $source) {
1155 $contacts = $this->task->liste_contact(-1, $source);
1156
1157 foreach ($contacts as $contact) {
1158 if ($contact['id'] == $contactid && $contact['code'] == $type) {
1159 $result = $this->task->delete_contact($contact['rowid']);
1160 if (!$result) {
1161 throw new RestException(500, 'Error when deleted the contact');
1162 }
1163 break 2;
1164 }
1165 }
1166 }
1167
1168 return $this->_cleanObjectDatas($this->task);
1169 }
1170}
$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.
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.
Class to manage tasks.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
_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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List tasks.
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:435
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.
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.