dolibarr 18.0.6
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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use Luracast\Restler\RestException;
20
21require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
22require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
23
24
31class Tasks extends DolibarrApi
32{
36 public static $FIELDS = array(
37 'ref',
38 'label',
39 'fk_project'
40 );
41
45 public $task;
46
50 public function __construct()
51 {
52 global $db, $conf;
53 $this->db = $db;
54 $this->task = new Task($this->db);
55 }
56
68 public function get($id, $includetimespent = 0)
69 {
70 if (!DolibarrApiAccess::$user->rights->projet->lire) {
71 throw new RestException(401);
72 }
73
74 $result = $this->task->fetch($id);
75 if (!$result) {
76 throw new RestException(404, 'Task not found');
77 }
78
79 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
80 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
81 }
82
83 if ($includetimespent == 1) {
84 $timespent = $this->task->getSummaryOfTimeSpent(0);
85 }
86 if ($includetimespent == 2) {
87 $timespent = $this->task->fetchTimeSpentOnTask();
88 }
89
90 return $this->_cleanObjectDatas($this->task);
91 }
92
93
94
107 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
108 {
109 global $db, $conf;
110
111 if (!DolibarrApiAccess::$user->rights->projet->lire) {
112 throw new RestException(401);
113 }
114
115 $obj_ret = array();
116
117 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
118 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
119
120 // If the internal user must only see his customers, force searching by him
121 $search_sale = 0;
122 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
123 $search_sale = DolibarrApiAccess::$user->id;
124 }
125
126 $sql = "SELECT t.rowid";
127 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
128 $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
129 }
130 $sql .= " FROM ".MAIN_DB_PREFIX."projet_task AS t 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
131
132 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
133 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
134 }
135
136 $sql .= ' WHERE t.entity IN ('.getEntity('project').')';
137 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
138 $sql .= " AND t.fk_soc = sc.fk_soc";
139 }
140 if ($socids) {
141 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
142 }
143 if ($search_sale > 0) {
144 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
145 }
146 // Insert sale filter
147 if ($search_sale > 0) {
148 $sql .= " AND sc.fk_user = ".((int) $search_sale);
149 }
150 // Add sql filters
151 if ($sqlfilters) {
152 $errormessage = '';
153 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
154 if ($errormessage) {
155 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
156 }
157 }
158
159 $sql .= $this->db->order($sortfield, $sortorder);
160 if ($limit) {
161 if ($page < 0) {
162 $page = 0;
163 }
164 $offset = $limit * $page;
165
166 $sql .= $this->db->plimit($limit + 1, $offset);
167 }
168
169 dol_syslog("API Rest request");
170 $result = $this->db->query($sql);
171
172 if ($result) {
173 $num = $this->db->num_rows($result);
174 $min = min($num, ($limit <= 0 ? $num : $limit));
175 $i = 0;
176 while ($i < $min) {
177 $obj = $this->db->fetch_object($result);
178 $task_static = new Task($this->db);
179 if ($task_static->fetch($obj->rowid)) {
180 $obj_ret[] = $this->_cleanObjectDatas($task_static);
181 }
182 $i++;
183 }
184 } else {
185 throw new RestException(503, 'Error when retrieve task list : '.$this->db->lasterror());
186 }
187 if (!count($obj_ret)) {
188 throw new RestException(404, 'No task found');
189 }
190 return $obj_ret;
191 }
192
199 public function post($request_data = null)
200 {
201 if (!DolibarrApiAccess::$user->rights->projet->creer) {
202 throw new RestException(401, "Insuffisant rights");
203 }
204 // Check mandatory fields
205 $result = $this->_validate($request_data);
206
207 foreach ($request_data as $field => $value) {
208 $this->task->$field = $value;
209 }
210 /*if (isset($request_data["lines"])) {
211 $lines = array();
212 foreach ($request_data["lines"] as $line) {
213 array_push($lines, (object) $line);
214 }
215 $this->project->lines = $lines;
216 }*/
217 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
218 throw new RestException(500, "Error creating task", array_merge(array($this->task->error), $this->task->errors));
219 }
220
221 return $this->task->id;
222 }
223
224 // /**
225 // * Get time spent of a task
226 // *
227 // * @param int $id Id of task
228 // * @return int
229 // *
230 // * @url GET {id}/tasks
231 // */
232 /*
233 public function getLines($id, $includetimespent=0)
234 {
235 if(! DolibarrApiAccess::$user->rights->projet->lire) {
236 throw new RestException(401);
237 }
238
239 $result = $this->project->fetch($id);
240 if( ! $result ) {
241 throw new RestException(404, 'Project not found');
242 }
243
244 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
245 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
246 }
247 $this->project->getLinesArray(DolibarrApiAccess::$user);
248 $result = array();
249 foreach ($this->project->lines as $line) // $line is a task
250 {
251 if ($includetimespent == 1)
252 {
253 $timespent = $line->getSummaryOfTimeSpent(0);
254 }
255 if ($includetimespent == 1)
256 {
257 // TODO
258 // Add class for timespent records and loop and fill $line->lines with records of timespent
259 }
260 array_push($result,$this->_cleanObjectDatas($line));
261 }
262 return $result;
263 }
264 */
265
276 public function getRoles($id, $userid = 0)
277 {
278 global $db;
279
280 if (!DolibarrApiAccess::$user->rights->projet->lire) {
281 throw new RestException(401);
282 }
283
284 $result = $this->task->fetch($id);
285 if (!$result) {
286 throw new RestException(404, 'Task not found');
287 }
288
289 if (!DolibarrApi::_checkAccessToResource('tasks', $this->task->id)) {
290 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
291 }
292
293 $usert = DolibarrApiAccess::$user;
294 if ($userid > 0) {
295 $usert = new User($this->db);
296 $usert->fetch($userid);
297 }
298 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(null, $usert, 0, $id);
299 $result = array();
300 foreach ($this->task->roles as $line) {
301 array_push($result, $this->_cleanObjectDatas($line));
302 }
303
304 return $result;
305 }
306
307
308 // /**
309 // * Add a task to given project
310 // *
311 // * @param int $id Id of project to update
312 // * @param array $request_data Projectline data
313 // *
314 // * @url POST {id}/tasks
315 // *
316 // * @return int
317 // */
318 /*
319 public function postLine($id, $request_data = null)
320 {
321 if(! DolibarrApiAccess::$user->rights->projet->creer) {
322 throw new RestException(401);
323 }
324
325 $result = $this->project->fetch($id);
326 if( ! $result ) {
327 throw new RestException(404, 'Project not found');
328 }
329
330 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
331 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
332 }
333
334 $request_data = (object) $request_data;
335
336 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
337
338 $updateRes = $this->project->addline(
339 $request_data->desc,
340 $request_data->subprice,
341 $request_data->qty,
342 $request_data->tva_tx,
343 $request_data->localtax1_tx,
344 $request_data->localtax2_tx,
345 $request_data->fk_product,
346 $request_data->remise_percent,
347 $request_data->info_bits,
348 $request_data->fk_remise_except,
349 'HT',
350 0,
351 $request_data->date_start,
352 $request_data->date_end,
353 $request_data->product_type,
354 $request_data->rang,
355 $request_data->special_code,
356 $fk_parent_line,
357 $request_data->fk_fournprice,
358 $request_data->pa_ht,
359 $request_data->label,
360 $request_data->array_options,
361 $request_data->fk_unit,
362 $this->element,
363 $request_data->id
364 );
365
366 if ($updateRes > 0) {
367 return $updateRes;
368
369 }
370 return false;
371 }
372 */
373
374 // /**
375 // * Update a task of a given project
376 // *
377 // * @param int $id Id of project to update
378 // * @param int $taskid Id of task to update
379 // * @param array $request_data Projectline data
380 // *
381 // * @url PUT {id}/tasks/{taskid}
382 // *
383 // * @return object
384 // */
385 /*
386 public function putLine($id, $lineid, $request_data = null)
387 {
388 if(! DolibarrApiAccess::$user->rights->projet->creer) {
389 throw new RestException(401);
390 }
391
392 $result = $this->project->fetch($id);
393 if( ! $result ) {
394 throw new RestException(404, 'Project not found');
395 }
396
397 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
398 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
399 }
400
401 $request_data = (object) $request_data;
402
403 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
404
405 $updateRes = $this->project->updateline(
406 $lineid,
407 $request_data->desc,
408 $request_data->subprice,
409 $request_data->qty,
410 $request_data->remise_percent,
411 $request_data->tva_tx,
412 $request_data->localtax1_tx,
413 $request_data->localtax2_tx,
414 'HT',
415 $request_data->info_bits,
416 $request_data->date_start,
417 $request_data->date_end,
418 $request_data->product_type,
419 $request_data->fk_parent_line,
420 0,
421 $request_data->fk_fournprice,
422 $request_data->pa_ht,
423 $request_data->label,
424 $request_data->special_code,
425 $request_data->array_options,
426 $request_data->fk_unit
427 );
428
429 if ($updateRes > 0) {
430 $result = $this->get($id);
431 unset($result->line);
432 return $this->_cleanObjectDatas($result);
433 }
434 return false;
435 }*/
436
437
446 public function put($id, $request_data = null)
447 {
448 if (!DolibarrApiAccess::$user->rights->projet->creer) {
449 throw new RestException(401);
450 }
451
452 $result = $this->task->fetch($id);
453 if (!$result) {
454 throw new RestException(404, 'Task not found');
455 }
456
457 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
458 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
459 }
460 foreach ($request_data as $field => $value) {
461 if ($field == 'id') {
462 continue;
463 }
464 if ($field == 'array_options' && is_array($value)) {
465 foreach ($value as $index => $val) {
466 $this->task->array_options[$index] = $this->_checkValForAPI($field, $val, $this->task);
467 }
468 continue;
469 }
470 $this->task->$field = $value;
471 }
472
473 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
474 return $this->get($id);
475 } else {
476 throw new RestException(500, $this->task->error);
477 }
478 }
479
487 public function delete($id)
488 {
489 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
490 throw new RestException(401);
491 }
492 $result = $this->task->fetch($id);
493 if (!$result) {
494 throw new RestException(404, 'Task not found');
495 }
496
497 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
498 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
499 }
500
501 if (!$this->task->delete(DolibarrApiAccess::$user)) {
502 throw new RestException(500, 'Error when delete task : '.$this->task->error);
503 }
504
505 return array(
506 'success' => array(
507 'code' => 200,
508 'message' => 'Task deleted'
509 )
510 );
511 }
512
513
530 public function addTimeSpent($id, $date, $duration, $user_id = 0, $note = '')
531 {
532 if (!DolibarrApiAccess::$user->rights->projet->creer) {
533 throw new RestException(401);
534 }
535 $result = $this->task->fetch($id);
536 if ($result <= 0) {
537 throw new RestException(404, 'Task not found');
538 }
539
540 if (!DolibarrApi::_checkAccessToResource('project', $this->task->fk_project)) {
541 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
542 }
543
544 $uid = $user_id;
545 if (empty($uid)) {
546 $uid = DolibarrApiAccess::$user->id;
547 }
548
549 $newdate = dol_stringtotime($date, 1);
550 $this->task->timespent_date = $newdate;
551 $this->task->timespent_datehour = $newdate;
552 $this->task->timespent_withhour = 1;
553 $this->task->timespent_duration = $duration;
554 $this->task->timespent_fk_user = $uid;
555 $this->task->timespent_note = $note;
556
557 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
558 if ($result == 0) {
559 throw new RestException(304, 'Error nothing done. May be object is already validated');
560 }
561 if ($result < 0) {
562 throw new RestException(500, 'Error when adding time: '.$this->task->error);
563 }
564
565 return array(
566 'success' => array(
567 'code' => 200,
568 'message' => 'Time spent added'
569 )
570 );
571 }
572
589 public function putTimeSpent($id, $timespent_id, $date, $duration, $user_id = 0, $note = '')
590 {
591 if (!DolibarrApiAccess::$user->rights->projet->creer) {
592 throw new RestException(401);
593 }
594 $this->timespentRecordChecks($id, $timespent_id);
595
596 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
597 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
598 }
599
600 $newdate = dol_stringtotime($date, 1);
601 $this->task->timespent_date = $newdate;
602 $this->task->timespent_datehour = $newdate;
603 $this->task->timespent_withhour = 1;
604 $this->task->timespent_duration = $duration;
605 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
606 $this->task->timespent_note = $note;
607
608 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
609 if ($result == 0) {
610 throw new RestException(304, 'Error nothing done.');
611 }
612 if ($result < 0) {
613 throw new RestException(500, 'Error when updating time spent: '.$this->task->error);
614 }
615
616 return array(
617 'success' => array(
618 'code' => 200,
619 'message' => 'Time spent updated'
620 )
621 );
622 }
623
634 public function deleteTimeSpent($id, $timespent_id)
635 {
636 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
637 throw new RestException(401);
638 }
639 $this->timespentRecordChecks($id, $timespent_id);
640
641 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
642 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
643 }
644
645 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
646 throw new RestException(500, 'Error when deleting time spent: '.$this->task->error);
647 }
648
649 return array(
650 'success' => array(
651 'code' => 200,
652 'message' => 'Time spent deleted'
653 )
654 );
655 }
656
666 protected function timespentRecordChecks($id, $timespent_id)
667 {
668 if ($this->task->fetch($id) <= 0) {
669 throw new RestException(404, 'Task not found');
670 }
671 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
672 throw new RestException(404, 'Timespent not found');
673 } elseif ($this->task->id != $id) {
674 throw new RestException(404, 'Timespent not found in selected task');
675 }
676 }
677
678 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
685 protected function _cleanObjectDatas($object)
686 {
687 // phpcs:enable
688 $object = parent::_cleanObjectDatas($object);
689
690 unset($object->barcode_type);
691 unset($object->barcode_type_code);
692 unset($object->barcode_type_label);
693 unset($object->barcode_type_coder);
694 unset($object->cond_reglement_id);
695 unset($object->cond_reglement);
696 unset($object->fk_delivery_address);
697 unset($object->shipping_method_id);
698 unset($object->fk_account);
699 unset($object->note);
700 unset($object->fk_incoterms);
701 unset($object->label_incoterms);
702 unset($object->location_incoterms);
703 unset($object->name);
704 unset($object->lastname);
705 unset($object->firstname);
706 unset($object->civility_id);
707 unset($object->mode_reglement_id);
708 unset($object->country);
709 unset($object->country_id);
710 unset($object->country_code);
711
712 unset($object->weekWorkLoad);
713 unset($object->weekWorkLoad);
714
715 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
716
717 unset($object->total_ht);
718 unset($object->total_tva);
719 unset($object->total_localtax1);
720 unset($object->total_localtax2);
721 unset($object->total_ttc);
722
723 unset($object->comments);
724
725 return $object;
726 }
727
735 private function _validate($data)
736 {
737 $object = array();
738 foreach (self::$FIELDS as $field) {
739 if (!isset($data[$field])) {
740 throw new RestException(400, "$field field missing");
741 }
742 $object[$field] = $data[$field];
743 }
744 return $object;
745 }
746
747
748 // \todo
749 // getSummaryOfTimeSpent
750}
Class for API REST v1.
Definition api.class.php:31
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:86
Class to manage tasks.
_cleanObjectDatas($object)
Clean sensible object datas.
putTimeSpent($id, $timespent_id, $date, $duration, $user_id=0, $note='')
Update time spent for a task of a project.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create task object.
put($id, $request_data=null)
Add a task to given project.
getRoles($id, $userid=0)
Get time spent of a task.
__construct()
Constructor.
addTimeSpent($id, $date, $duration, $user_id=0, $note='')
Add time spent to 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.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='')
List tasks.
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:410
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.