dolibarr 23.0.3
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 *
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/task.class.php';
26require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
27require_once DOL_DOCUMENT_ROOT.'/core/class/timespent.class.php';
28
35class Tasks extends DolibarrApi
36{
40 public static $FIELDS = array(
41 'ref',
42 'label',
43 'fk_project'
44 );
45
49 public $task;
50
54 public function __construct()
55 {
56 global $db, $conf;
57 $this->db = $db;
58 $this->task = new Task($this->db);
59 }
60
72 public function get($id, $includetimespent = 0)
73 {
74 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
75 throw new RestException(403);
76 }
77
78 $result = $this->task->fetch($id);
79 if (!$result) {
80 throw new RestException(404, 'Task not found');
81 }
82
83 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
84 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
85 }
86
87 if ($includetimespent == 1) {
88 $timespent = $this->task->getSummaryOfTimeSpent(0);
89 }
90 if ($includetimespent == 2) {
91 $timespent = $this->task->fetchTimeSpentOnTask();
92 }
93
94 return $this->_cleanObjectDatas($this->task);
95 }
96
97
98
114 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
115 {
116 global $db, $conf;
117
118 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
119 throw new RestException(403);
120 }
121
122 $obj_ret = array();
123
124 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
125 $socids = DolibarrApiAccess::$user->socid ?: 0;
126
127 // If the internal user must only see his customers, force searching by him
128 $search_sale = 0;
129 if (!DolibarrApiAccess::$user->hasRight('societe', 'client', 'voir') && !$socids) {
130 $search_sale = DolibarrApiAccess::$user->id;
131 }
132
133 $sql = "SELECT t.rowid";
134 $sql .= " FROM " . MAIN_DB_PREFIX . "projet_task AS t";
135 $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
136 $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "projet AS p ON p.rowid = t.fk_projet";
137 $sql .= ' WHERE t.entity IN (' . getEntity('project') . ')';
138 if ($socids) {
139 $sql .= " AND t.fk_soc IN (" . $this->db->sanitize((string) $socids) . ")";
140 }
141 // Search on sale representative
142 if ($search_sale && $search_sale != '-1') {
143 if ($search_sale == -2) {
144 $sql .= " AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX . "societe_commerciaux as sc WHERE sc.fk_soc = p.fk_soc)";
145 } elseif ($search_sale > 0) {
146 $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) . ")";
147 }
148 }
149 // Add sql filters
150 if ($sqlfilters) {
151 $errormessage = '';
152 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
153 if ($errormessage) {
154 throw new RestException(400, 'Error when validating parameter sqlfilters -> ' . $errormessage);
155 }
156 }
157
158 $sql .= $this->db->order($sortfield, $sortorder);
159 if ($limit) {
160 if ($page < 0) {
161 $page = 0;
162 }
163 $offset = $limit * $page;
164
165 $sql .= $this->db->plimit($limit + 1, $offset);
166 }
167
168 dol_syslog("API Rest request");
169 $result = $this->db->query($sql);
170
171 if ($result) {
172 $num = $this->db->num_rows($result);
173 $min = min($num, ($limit <= 0 ? $num : $limit));
174 $i = 0;
175 while ($i < $min) {
176 $obj = $this->db->fetch_object($result);
177 $task_static = new Task($this->db);
178 if ($task_static->fetch($obj->rowid)) {
179 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($task_static), $properties);
180 }
181 $i++;
182 }
183 } else {
184 throw new RestException(503, 'Error when retrieve task list : ' . $this->db->lasterror());
185 }
186
187 return $obj_ret;
188 }
189
198 public function post($request_data = null)
199 {
200 global $conf;
201 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
202 throw new RestException(403, "Insufficiant rights");
203 }
204 // Check mandatory fields
205 $result = $this->_validate($request_data);
206
207 foreach ($request_data as $field => $value) {
208 if ($field === 'caller') {
209 // 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
210 $this->task->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
211 continue;
212 }
213
214 $this->task->$field = $this->_checkValForAPI($field, $value, $this->task);
215 }
216 /*if (isset($request_data["lines"])) {
217 $lines = array();
218 foreach ($request_data["lines"] as $line) {
219 array_push($lines, (object) $line);
220 }
221 $this->project->lines = $lines;
222 }*/
223
224 // Auto-generate the "ref" field if it is set to "auto"
225 if ($this->task->ref == -1 || $this->task->ref === 'auto') {
226 $reldir = '';
227 $defaultref = '';
228 $file = '';
229 $classname = '';
230 $filefound = 0;
231 $modele = getDolGlobalString('PROJECT_TASK_ADDON', 'mod_task_simple');
232
233 $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
234 foreach ($dirmodels as $reldir) {
235 $file = dol_buildpath($reldir."core/modules/project/task/".$modele.'.php', 0);
236 if (file_exists($file)) {
237 $filefound = 1;
238 $classname = $modele;
239 break;
240 }
241 }
242 if ($filefound && !empty($classname)) {
243 $result = dol_include_once($reldir . "core/modules/project/task/" . $modele . '.php');
244 if ($result !== false && class_exists($classname)) {
245 $modTask = new $classname();
246 '@phan-var-force ModeleNumRefTask $modTask';
247 $defaultref = $modTask->getNextValue(null, $this->task);
248 } else {
249 dol_syslog("Failed to include module file or invalid classname: " . $reldir . "core/modules/project/task/" . $modele . '.php', LOG_ERR);
250 }
251 } else {
252 dol_syslog("Module file not found or classname is empty: " . $modele, LOG_ERR);
253 }
254
255 if (is_numeric($defaultref) && $defaultref <= 0) {
256 $defaultref = '';
257 }
258
259 if (empty($defaultref)) {
260 $defaultref = 'TK' . dol_print_date(dol_now(), 'dayrfc');
261 }
262
263 $this->task->ref = $defaultref;
264 }
265
266 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
267 throw new RestException(500, "Error creating task", array_merge(array($this->task->error), $this->task->errors));
268 }
269
270 return $this->task->id;
271 }
272
281 public function getTimespent($id)
282 {
283 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
284 throw new RestException(403);
285 }
286
287 $result = $this->task->fetch($id);
288 if (!$result) {
289 throw new RestException(404, 'Task not found');
290 }
291
292 if (!DolibarrApi::_checkAccessToResource('tasks', $this->task->id)) {
293 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
294 }
295
296 $this->task->fetchTimeSpentOnTask();
297
298 $result = array();
299 foreach ($this->task->lines as $line) {
300 array_push($result, $this->_cleanObjectDatas($line));
301 }
302
303 return $result;
304 }
305
317 public function getRoles($id, $userid = 0)
318 {
319 global $db;
320
321 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
322 throw new RestException(403);
323 }
324
325 $result = $this->task->fetch($id);
326 if (!$result) {
327 throw new RestException(404, 'Task not found');
328 }
329
330 if (!DolibarrApi::_checkAccessToResource('tasks', $this->task->id)) {
331 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
332 }
333
334 $usert = DolibarrApiAccess::$user;
335 if ($userid > 0) {
336 $usert = new User($this->db);
337 $usert->fetch($userid);
338 }
339 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(null, $usert, '0', $id);
340 $result = array();
341 foreach ($this->task->roles as $line) {
342 array_push($result, $this->_cleanObjectDatas($line));
343 }
344
345 return $result;
346 }
347
348
349 // /**
350 // * Add a task to given project
351 // *
352 // * @param int $id Id of project to update
353 // * @param array $request_data Projectline data
354 // * @phan-param ?array<string,string> $request_data
355 // * @phpstan-param ?array<string,string> $request_data
356 // *
357 // * @url POST {id}/tasks
358 // *
359 // * @return int
360 // */
361 /*
362 public function postLine($id, $request_data = null)
363 {
364 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
365 throw new RestException(403);
366 }
367
368 $result = $this->project->fetch($id);
369 if( ! $result ) {
370 throw new RestException(404, 'Project not found');
371 }
372
373 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
374 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
375 }
376
377 $request_data = (object) $request_data;
378
379 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
380
381 $updateRes = $this->project->addline(
382 $request_data->desc,
383 $request_data->subprice,
384 $request_data->qty,
385 $request_data->tva_tx,
386 $request_data->localtax1_tx,
387 $request_data->localtax2_tx,
388 $request_data->fk_product,
389 $request_data->remise_percent,
390 $request_data->info_bits,
391 $request_data->fk_remise_except,
392 'HT',
393 0,
394 $request_data->date_start,
395 $request_data->date_end,
396 $request_data->product_type,
397 $request_data->rang,
398 $request_data->special_code,
399 $fk_parent_line,
400 $request_data->fk_fournprice,
401 $request_data->pa_ht,
402 $request_data->label,
403 $request_data->array_options,
404 $request_data->fk_unit,
405 $this->element,
406 $request_data->id
407 );
408
409 if ($updateRes > 0) {
410 return $updateRes;
411
412 }
413 return false;
414 }
415 */
416
417 // /**
418 // * Update a task of a given project
419 // *
420 // * @param int $id Id of project to update
421 // * @param int $taskid Id of task to update
422 // * @param array $request_data Projectline data
423 // * @phan-param ?array<string,string> $request_data
424 // * @phpstan-param ?array<string,string> $request_data
425 // *
426 // * @url PUT {id}/tasks/{taskid}
427 // *
428 // * @return object
429 // */
430 /*
431 public function putLine($id, $lineid, $request_data = null)
432 {
433 if(! DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
434 throw new RestException(403);
435 }
436
437 $result = $this->project->fetch($id);
438 if( ! $result ) {
439 throw new RestException(404, 'Project not found');
440 }
441
442 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
443 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
444 }
445
446 $request_data = (object) $request_data;
447
448 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
449
450 $updateRes = $this->project->updateline(
451 $lineid,
452 $request_data->desc,
453 $request_data->subprice,
454 $request_data->qty,
455 $request_data->remise_percent,
456 $request_data->tva_tx,
457 $request_data->localtax1_tx,
458 $request_data->localtax2_tx,
459 'HT',
460 $request_data->info_bits,
461 $request_data->date_start,
462 $request_data->date_end,
463 $request_data->product_type,
464 $request_data->fk_parent_line,
465 0,
466 $request_data->fk_fournprice,
467 $request_data->pa_ht,
468 $request_data->label,
469 $request_data->special_code,
470 $request_data->array_options,
471 $request_data->fk_unit
472 );
473
474 if ($updateRes > 0) {
475 $result = $this->get($id);
476 unset($result->line);
477 return $this->_cleanObjectDatas($result);
478 }
479 return false;
480 }*/
481
482
492 public function put($id, $request_data = null)
493 {
494 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
495 throw new RestException(403);
496 }
497
498 $result = $this->task->fetch($id);
499 if (!$result) {
500 throw new RestException(404, 'Task not found');
501 }
502
503 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
504 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
505 }
506 foreach ($request_data as $field => $value) {
507 if ($field == 'id') {
508 continue;
509 }
510 if ($field === 'caller') {
511 // 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
512 $this->task->context['caller'] = sanitizeVal($request_data['caller'], 'aZ09');
513 continue;
514 }
515 if ($field == 'array_options' && is_array($value)) {
516 foreach ($value as $index => $val) {
517 $this->task->array_options[$index] = $this->_checkValForAPI($field, $val, $this->task);
518 }
519 continue;
520 }
521
522 $this->task->$field = $this->_checkValForAPI($field, $value, $this->task);
523 }
524
525 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
526 return $this->get($id);
527 } else {
528 throw new RestException(500, $this->task->error);
529 }
530 }
531
542 public function delete($id)
543 {
544 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
545 throw new RestException(403);
546 }
547 $result = $this->task->fetch($id);
548 if (!$result) {
549 throw new RestException(404, 'Task not found');
550 }
551
552 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
553 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
554 }
555
556 if ($this->task->delete(DolibarrApiAccess::$user) <= 0) {
557 throw new RestException(500, 'Error when delete task : ' . $this->task->error);
558 }
559
560 return array(
561 'success' => array(
562 'code' => 200,
563 'message' => 'Task deleted'
564 )
565 );
566 }
567
580 public function getTimeSpentByID($id, $timespent_id)
581 {
582 dol_syslog("API Rest request::getTimeSpent", LOG_DEBUG);
583 if (! DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
584 throw new RestException(403);
585 }
586
587 $taskresult = $this->task->fetch($id);
588 if (!$taskresult ) {
589 throw new RestException(404, 'Task with id='.$id.' not found');
590 }
591 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
592 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
593 }
594
595 $timespent = new TimeSpent($this->db);
596 $timeresult = $timespent->fetch($timespent_id);
597 if (!$timeresult ) {
598 throw new RestException(404, 'Timespent with id='.$timespent_id.' not found');
599 }
600 if (!DolibarrApi::_checkAccessToResource('time', $timespent->id)) {
601 throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
602 }
603
604 return $this->_cleanTimeSpentObjectDatas($timespent);
605 }
606
628 public function addTimeSpent($id, $date, $duration, $product_id = null, $user_id = 0, $note = '', $progress = -1)
629 {
630 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
631 throw new RestException(403);
632 }
633 $result = $this->task->fetch($id);
634 if ($result <= 0) {
635 throw new RestException(404, 'Task not found');
636 }
637
638 if (!DolibarrApi::_checkAccessToResource('project', $this->task->fk_project)) {
639 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
640 }
641
642 $uid = $user_id;
643 if (empty($uid)) {
644 $uid = DolibarrApiAccess::$user->id;
645 }
646
647 $newdate = dol_stringtotime($date, 1);
648
649 $this->task->timespent_date = $newdate;
650 $this->task->timespent_datehour = $newdate;
651 $this->task->timespent_withhour = 1;
652 $this->task->timespent_duration = $duration;
653 $this->task->timespent_fk_product = $product_id;
654 $this->task->timespent_fk_user = $uid;
655 $this->task->timespent_note = $note;
656 if (!empty($progress) && $progress >= 0 && $progress <= 100) {
657 $this->task->progress = $progress;
658 }
659
660 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
661 if ($result == 0) {
662 throw new RestException(304, 'Error nothing done. May be object is already validated');
663 }
664 if ($result < 0) {
665 throw new RestException(500, 'Error when adding time: ' . $this->task->error);
666 }
667
668 return array(
669 'success' => array(
670 'code' => 200,
671 'message' => 'Time spent added'
672 )
673 );
674 }
675
696 public function putTimeSpent($id, $timespent_id, $date, $duration, $product_id = null, $user_id = 0, $note = '')
697 {
698 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
699 throw new RestException(403);
700 }
701 $this->timespentRecordChecks($id, $timespent_id);
702
703 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
704 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
705 }
706
707 $newdate = dol_stringtotime($date, 1);
708 $this->task->timespent_date = $newdate;
709 $this->task->timespent_datehour = $newdate;
710 $this->task->timespent_withhour = 1;
711 $this->task->timespent_duration = $duration;
712 $this->task->timespent_fk_product = $product_id;
713 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
714 $this->task->timespent_note = $note;
715
716 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
717 if ($result == 0) {
718 throw new RestException(304, 'Error nothing done.');
719 }
720 if ($result < 0) {
721 throw new RestException(500, 'Error when updating time spent: ' . $this->task->error);
722 }
723
724 return array(
725 'success' => array(
726 'code' => 200,
727 'message' => 'Time spent updated'
728 )
729 );
730 }
731
744 public function deleteTimeSpent($id, $timespent_id)
745 {
746 if (!DolibarrApiAccess::$user->hasRight('projet', 'supprimer')) {
747 throw new RestException(403);
748 }
749 $this->timespentRecordChecks($id, $timespent_id);
750
751 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
752 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
753 }
754
755 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
756 throw new RestException(500, 'Error when deleting time spent: ' . $this->task->error);
757 }
758
759 return array(
760 'success' => array(
761 'code' => 200,
762 'message' => 'Time spent deleted'
763 )
764 );
765 }
766
776 private function timespentRecordChecks($id, $timespent_id)
777 {
778 dol_syslog("API Rest request::timespentRecordChecks", LOG_DEBUG);
779 if ($this->task->fetch($id) <= 0) {
780 throw new RestException(404, 'Task not found');
781 }
782 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
783 throw new RestException(404, 'Timespent not found');
784 } elseif ($this->task->id != $id) {
785 throw new RestException(404, 'Timespent not found in selected task');
786 }
787 }
788
789 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
800 protected function _cleanObjectDatas($object)
801 {
802 // phpcs:enable
803 $object = parent::_cleanObjectDatas($object);
804
805 unset($object->barcode_type);
806 unset($object->barcode_type_code);
807 unset($object->barcode_type_label);
808 unset($object->barcode_type_coder);
809 unset($object->cond_reglement_id);
810 unset($object->cond_reglement);
811 unset($object->fk_delivery_address);
812 unset($object->shipping_method_id);
813 unset($object->fk_account);
814 unset($object->note);
815 unset($object->fk_incoterms);
816 unset($object->label_incoterms);
817 unset($object->location_incoterms);
818 unset($object->name);
819 unset($object->lastname);
820 unset($object->firstname);
821 unset($object->civility_id);
822 unset($object->mode_reglement_id);
823 unset($object->country);
824 unset($object->country_id);
825 unset($object->country_code);
826
827 unset($object->weekWorkLoad);
828 unset($object->weekWorkLoad);
829
830 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
831
832 unset($object->total_ht);
833 unset($object->total_tva);
834 unset($object->total_localtax1);
835 unset($object->total_localtax2);
836 unset($object->total_ttc);
837
838 unset($object->comments);
839
840 return $object;
841 }
842
843 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
855 {
856 if (!$object->note_private) {
857 $object->note_private = $object->note;
858 // unsure if we should use note_private or note_public, but note_private should be more secure
859 }
860 $saving_fk_element = $object->fk_element;
861 // because calling parent::_cleanObjectDatas clears fk_element
862
863 // phpcs:enable
864 $object = parent::_cleanObjectDatas($object);
865
866 unset($object->barcode_type);
867 unset($object->barcode_type_code);
868 unset($object->barcode_type_label);
869 unset($object->barcode_type_coder);
870 unset($object->cond_reglement_id);
871 unset($object->cond_reglement);
872 unset($object->fk_delivery_address);
873 unset($object->shipping_method_id);
874 unset($object->fk_account);
875 unset($object->fk_incoterms);
876 unset($object->label_incoterms);
877 unset($object->location_incoterms);
878 unset($object->name);
879 unset($object->lastname);
880 unset($object->firstname);
881 unset($object->civility_id);
882 unset($object->mode_reglement_id);
883 unset($object->country);
884 unset($object->country_id);
885 unset($object->country_code);
886
887 unset($object->weekWorkLoad);
888 unset($object->weekWorkLoad);
889
890 unset($object->actiontypecode);
891 unset($object->array_languages);
892 unset($object->array_options);
893 unset($object->canvas);
894 unset($object->civility_code);
895 unset($object->cond_reglement_supplier_id);
896 unset($object->contact_id);
897 unset($object->contacts_ids);
898 unset($object->contacts_ids_internal);
899 unset($object->date_cloture);
900 unset($object->date_validation);
901 unset($object->demand_reason_id);
902 unset($object->deposit_percent);
903 unset($object->entity);
904 unset($object->extraparams);
905 unset($object->fk_multicurrency);
906 unset($object->fk_project);
907 unset($object->fk_user_creat);
908 unset($object->fk_user_modif);
909 unset($object->last_main_doc);
910 unset($object->lines);
911 unset($object->linkedObjectsIds);
912 unset($object->module);
913 unset($object->multicurrency_code);
914 unset($object->multicurrency_total_ht);
915 unset($object->multicurrency_total_localtax1);
916 unset($object->multicurrency_total_localtax2);
917 unset($object->multicurrency_total_ttc);
918 unset($object->multicurrency_total_tva);
919 unset($object->multicurrency_tx);
920 unset($object->note_public);
921 unset($object->origin_id);
922 unset($object->origin_type);
923 unset($object->product);
924 unset($object->ref);
925 unset($object->region_id);
926 unset($object->retained_warranty_fk_cond_reglement);
927 unset($object->rowid);
928 unset($object->shipping_method);
929 unset($object->specimen);
930 unset($object->state_id);
931 unset($object->status);
932 unset($object->statut);
933 unset($object->totalpaid);
934 unset($object->transport_mode_id);
935 unset($object->user);
936 unset($object->user_author);
937 unset($object->user_closing_id);
938 unset($object->user_creation);
939 unset($object->user_creation_id);
940 unset($object->user_modification);
941 unset($object->user_modification_id);
942 unset($object->user_valid);
943 unset($object->user_validation);
944 unset($object->user_validation_id);
945 unset($object->warehouse_id);
946
947 unset($object->total_ht);
948 unset($object->total_tva);
949 unset($object->total_localtax1);
950 unset($object->total_localtax2);
951 unset($object->total_ttc);
952
953 unset($object->comments);
954
955 if (!$object->date_creation) {
956 $object->date_creation = $object->datec;
957 }
958 if (!$object->date_modification) {
959 $object->date_modification = $object->tms;
960 }
961 if (!$object->fk_element) {
962 $object->fk_element = $saving_fk_element;
963 // because calling parent::_cleanObjectDatas clears fk_element
964 }
965
966 return $object;
967 }
968
976 private function _validate($data)
977 {
978 if ($data === null) {
979 $data = array();
980 }
981 $object = array();
982 foreach (self::$FIELDS as $field) {
983 if (!isset($data[$field])) {
984 throw new RestException(400, "$field field missing");
985 }
986 $object[$field] = $data[$field];
987 }
988 return $object;
989 }
990
1004 public function getContacts($id, $type = '')
1005 {
1006 if (!DolibarrApiAccess::$user->hasRight('projet', 'lire')) {
1007 throw new RestException(403);
1008 }
1009
1010 $result = $this->task->fetch($id);
1011 if (!$result) {
1012 throw new RestException(404, 'Task not found');
1013 }
1014
1015 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
1016 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1017 }
1018
1019 $contacts = $this->task->liste_contact(-1, 'external', 0, $type);
1020 $socpeoples = $this->task->liste_contact(-1, 'internal', 0, $type);
1021
1022 $contacts = array_merge($contacts, $socpeoples);
1023
1024 return $contacts; // Return array
1025 }
1026
1045 public function addContact($id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
1046 {
1047 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1048 throw new RestException(403);
1049 }
1050
1051 $result = $this->task->fetch($id);
1052 if (!$result) {
1053 throw new RestException(404, 'Task not found');
1054 }
1055
1056 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
1057 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1058 }
1059
1060 $result = $this->task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1061 if ($result <= 0) {
1062 throw new RestException(500, 'Error : ' . $this->task->error);
1063 }
1064
1065 $result = $this->task->fetch($id);
1066 if (!$result) {
1067 throw new RestException(404, 'Task not found');
1068 }
1069
1070 return $this->_cleanObjectDatas($this->task);
1071 }
1072
1073
1088 public function deleteContact($id, $contactid, $type)
1089 {
1090 if (!DolibarrApiAccess::$user->hasRight('projet', 'creer')) {
1091 throw new RestException(403);
1092 }
1093
1094 $result = $this->task->fetch($id);
1095 if (!$result) {
1096 throw new RestException(404, 'Task not found');
1097 }
1098
1099 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
1100 throw new RestException(403, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1101 }
1102
1103 foreach (array('internal', 'external') as $source) {
1104 $contacts = $this->task->liste_contact(-1, $source);
1105
1106 foreach ($contacts as $contact) {
1107 if ($contact['id'] == $contactid && $contact['code'] == $type) {
1108 $result = $this->task->delete_contact($contact['rowid']);
1109 if (!$result) {
1110 throw new RestException(500, 'Error when deleted the contact');
1111 }
1112 break 2;
1113 }
1114 }
1115 }
1116
1117 return $this->_cleanObjectDatas($this->task);
1118 }
1119}
$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 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.
timespentRecordChecks($id, $timespent_id)
Validate task & timespent IDs for timespent API methods.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='')
List tasks.
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:434
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.