dolibarr 19.0.4
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
108 public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '', $properties = '')
109 {
110 global $db, $conf;
111
112 if (!DolibarrApiAccess::$user->rights->projet->lire) {
113 throw new RestException(401);
114 }
115
116 $obj_ret = array();
117
118 // case of external user, $thirdparty_ids param is ignored and replaced by user's socid
119 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : '';
120
121 // If the internal user must only see his customers, force searching by him
122 $search_sale = 0;
123 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
124 $search_sale = DolibarrApiAccess::$user->id;
125 }
126
127 $sql = "SELECT t.rowid";
128 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
129 $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)
130 }
131 $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
132
133 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
134 $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
135 }
136
137 $sql .= ' WHERE t.entity IN ('.getEntity('project').')';
138 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
139 $sql .= " AND t.fk_soc = sc.fk_soc";
140 }
141 if ($socids) {
142 $sql .= " AND t.fk_soc IN (".$this->db->sanitize($socids).")";
143 }
144 if ($search_sale > 0) {
145 $sql .= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
146 }
147 // Insert sale filter
148 if ($search_sale > 0) {
149 $sql .= " AND sc.fk_user = ".((int) $search_sale);
150 }
151 // Add sql filters
152 if ($sqlfilters) {
153 $errormessage = '';
154 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
155 if ($errormessage) {
156 throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
157 }
158 }
159
160 $sql .= $this->db->order($sortfield, $sortorder);
161 if ($limit) {
162 if ($page < 0) {
163 $page = 0;
164 }
165 $offset = $limit * $page;
166
167 $sql .= $this->db->plimit($limit + 1, $offset);
168 }
169
170 dol_syslog("API Rest request");
171 $result = $this->db->query($sql);
172
173 if ($result) {
174 $num = $this->db->num_rows($result);
175 $min = min($num, ($limit <= 0 ? $num : $limit));
176 $i = 0;
177 while ($i < $min) {
178 $obj = $this->db->fetch_object($result);
179 $task_static = new Task($this->db);
180 if ($task_static->fetch($obj->rowid)) {
181 $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($task_static), $properties);
182 }
183 $i++;
184 }
185 } else {
186 throw new RestException(503, 'Error when retrieve task list : '.$this->db->lasterror());
187 }
188
189 return $obj_ret;
190 }
191
198 public function post($request_data = null)
199 {
200 if (!DolibarrApiAccess::$user->rights->projet->creer) {
201 throw new RestException(401, "Insuffisant rights");
202 }
203 // Check mandatory fields
204 $result = $this->_validate($request_data);
205
206 foreach ($request_data as $field => $value) {
207 if ($field === 'caller') {
208 // 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 whith the caller
209 $this->task->context['caller'] = $request_data['caller'];
210 continue;
211 }
212
213 $this->task->$field = $value;
214 }
215 /*if (isset($request_data["lines"])) {
216 $lines = array();
217 foreach ($request_data["lines"] as $line) {
218 array_push($lines, (object) $line);
219 }
220 $this->project->lines = $lines;
221 }*/
222 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
223 throw new RestException(500, "Error creating task", array_merge(array($this->task->error), $this->task->errors));
224 }
225
226 return $this->task->id;
227 }
228
229 // /**
230 // * Get time spent of a task
231 // *
232 // * @param int $id Id of task
233 // * @return int
234 // *
235 // * @url GET {id}/tasks
236 // */
237 /*
238 public function getLines($id, $includetimespent=0)
239 {
240 if(! DolibarrApiAccess::$user->rights->projet->lire) {
241 throw new RestException(401);
242 }
243
244 $result = $this->project->fetch($id);
245 if( ! $result ) {
246 throw new RestException(404, 'Project not found');
247 }
248
249 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
250 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
251 }
252 $this->project->getLinesArray(DolibarrApiAccess::$user);
253 $result = array();
254 foreach ($this->project->lines as $line) // $line is a task
255 {
256 if ($includetimespent == 1)
257 {
258 $timespent = $line->getSummaryOfTimeSpent(0);
259 }
260 if ($includetimespent == 1)
261 {
262 // TODO
263 // Add class for timespent records and loop and fill $line->lines with records of timespent
264 }
265 array_push($result,$this->_cleanObjectDatas($line));
266 }
267 return $result;
268 }
269 */
270
281 public function getRoles($id, $userid = 0)
282 {
283 global $db;
284
285 if (!DolibarrApiAccess::$user->rights->projet->lire) {
286 throw new RestException(401);
287 }
288
289 $result = $this->task->fetch($id);
290 if (!$result) {
291 throw new RestException(404, 'Task not found');
292 }
293
294 if (!DolibarrApi::_checkAccessToResource('tasks', $this->task->id)) {
295 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
296 }
297
298 $usert = DolibarrApiAccess::$user;
299 if ($userid > 0) {
300 $usert = new User($this->db);
301 $usert->fetch($userid);
302 }
303 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(null, $usert, 0, $id);
304 $result = array();
305 foreach ($this->task->roles as $line) {
306 array_push($result, $this->_cleanObjectDatas($line));
307 }
308
309 return $result;
310 }
311
312
313 // /**
314 // * Add a task to given project
315 // *
316 // * @param int $id Id of project to update
317 // * @param array $request_data Projectline data
318 // *
319 // * @url POST {id}/tasks
320 // *
321 // * @return int
322 // */
323 /*
324 public function postLine($id, $request_data = null)
325 {
326 if(! DolibarrApiAccess::$user->rights->projet->creer) {
327 throw new RestException(401);
328 }
329
330 $result = $this->project->fetch($id);
331 if( ! $result ) {
332 throw new RestException(404, 'Project not found');
333 }
334
335 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
336 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
337 }
338
339 $request_data = (object) $request_data;
340
341 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
342
343 $updateRes = $this->project->addline(
344 $request_data->desc,
345 $request_data->subprice,
346 $request_data->qty,
347 $request_data->tva_tx,
348 $request_data->localtax1_tx,
349 $request_data->localtax2_tx,
350 $request_data->fk_product,
351 $request_data->remise_percent,
352 $request_data->info_bits,
353 $request_data->fk_remise_except,
354 'HT',
355 0,
356 $request_data->date_start,
357 $request_data->date_end,
358 $request_data->product_type,
359 $request_data->rang,
360 $request_data->special_code,
361 $fk_parent_line,
362 $request_data->fk_fournprice,
363 $request_data->pa_ht,
364 $request_data->label,
365 $request_data->array_options,
366 $request_data->fk_unit,
367 $this->element,
368 $request_data->id
369 );
370
371 if ($updateRes > 0) {
372 return $updateRes;
373
374 }
375 return false;
376 }
377 */
378
379 // /**
380 // * Update a task of a given project
381 // *
382 // * @param int $id Id of project to update
383 // * @param int $taskid Id of task to update
384 // * @param array $request_data Projectline data
385 // *
386 // * @url PUT {id}/tasks/{taskid}
387 // *
388 // * @return object
389 // */
390 /*
391 public function putLine($id, $lineid, $request_data = null)
392 {
393 if(! DolibarrApiAccess::$user->rights->projet->creer) {
394 throw new RestException(401);
395 }
396
397 $result = $this->project->fetch($id);
398 if( ! $result ) {
399 throw new RestException(404, 'Project not found');
400 }
401
402 if( ! DolibarrApi::_checkAccessToResource('project',$this->project->id)) {
403 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
404 }
405
406 $request_data = (object) $request_data;
407
408 $request_data->desc = sanitizeVal($request_data->desc, 'restricthtml');
409
410 $updateRes = $this->project->updateline(
411 $lineid,
412 $request_data->desc,
413 $request_data->subprice,
414 $request_data->qty,
415 $request_data->remise_percent,
416 $request_data->tva_tx,
417 $request_data->localtax1_tx,
418 $request_data->localtax2_tx,
419 'HT',
420 $request_data->info_bits,
421 $request_data->date_start,
422 $request_data->date_end,
423 $request_data->product_type,
424 $request_data->fk_parent_line,
425 0,
426 $request_data->fk_fournprice,
427 $request_data->pa_ht,
428 $request_data->label,
429 $request_data->special_code,
430 $request_data->array_options,
431 $request_data->fk_unit
432 );
433
434 if ($updateRes > 0) {
435 $result = $this->get($id);
436 unset($result->line);
437 return $this->_cleanObjectDatas($result);
438 }
439 return false;
440 }*/
441
442
451 public function put($id, $request_data = null)
452 {
453 if (!DolibarrApiAccess::$user->rights->projet->creer) {
454 throw new RestException(401);
455 }
456
457 $result = $this->task->fetch($id);
458 if (!$result) {
459 throw new RestException(404, 'Task not found');
460 }
461
462 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
463 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
464 }
465 foreach ($request_data as $field => $value) {
466 if ($field == 'id') {
467 continue;
468 }
469 if ($field === 'caller') {
470 // 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 whith the caller
471 $this->task->context['caller'] = $request_data['caller'];
472 continue;
473 }
474
475 $this->task->$field = $value;
476 }
477
478 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
479 return $this->get($id);
480 } else {
481 throw new RestException(500, $this->task->error);
482 }
483 }
484
492 public function delete($id)
493 {
494 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
495 throw new RestException(401);
496 }
497 $result = $this->task->fetch($id);
498 if (!$result) {
499 throw new RestException(404, 'Task not found');
500 }
501
502 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
503 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
504 }
505
506 if (!$this->task->delete(DolibarrApiAccess::$user)) {
507 throw new RestException(500, 'Error when delete task : '.$this->task->error);
508 }
509
510 return array(
511 'success' => array(
512 'code' => 200,
513 'message' => 'Task deleted'
514 )
515 );
516 }
517
518
535 public function addTimeSpent($id, $date, $duration, $user_id = 0, $note = '')
536 {
537 if (!DolibarrApiAccess::$user->rights->projet->creer) {
538 throw new RestException(401);
539 }
540 $result = $this->task->fetch($id);
541 if ($result <= 0) {
542 throw new RestException(404, 'Task not found');
543 }
544
545 if (!DolibarrApi::_checkAccessToResource('project', $this->task->fk_project)) {
546 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
547 }
548
549 $uid = $user_id;
550 if (empty($uid)) {
551 $uid = DolibarrApiAccess::$user->id;
552 }
553
554 $newdate = dol_stringtotime($date, 1);
555 $this->task->timespent_date = $newdate;
556 $this->task->timespent_datehour = $newdate;
557 $this->task->timespent_withhour = 1;
558 $this->task->timespent_duration = $duration;
559 $this->task->timespent_fk_user = $uid;
560 $this->task->timespent_note = $note;
561
562 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
563 if ($result == 0) {
564 throw new RestException(304, 'Error nothing done. May be object is already validated');
565 }
566 if ($result < 0) {
567 throw new RestException(500, 'Error when adding time: '.$this->task->error);
568 }
569
570 return array(
571 'success' => array(
572 'code' => 200,
573 'message' => 'Time spent added'
574 )
575 );
576 }
577
594 public function putTimeSpent($id, $timespent_id, $date, $duration, $user_id = 0, $note = '')
595 {
596 if (!DolibarrApiAccess::$user->rights->projet->creer) {
597 throw new RestException(401);
598 }
599 $this->timespentRecordChecks($id, $timespent_id);
600
601 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
602 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
603 }
604
605 $newdate = dol_stringtotime($date, 1);
606 $this->task->timespent_date = $newdate;
607 $this->task->timespent_datehour = $newdate;
608 $this->task->timespent_withhour = 1;
609 $this->task->timespent_duration = $duration;
610 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
611 $this->task->timespent_note = $note;
612
613 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
614 if ($result == 0) {
615 throw new RestException(304, 'Error nothing done.');
616 }
617 if ($result < 0) {
618 throw new RestException(500, 'Error when updating time spent: '.$this->task->error);
619 }
620
621 return array(
622 'success' => array(
623 'code' => 200,
624 'message' => 'Time spent updated'
625 )
626 );
627 }
628
639 public function deleteTimeSpent($id, $timespent_id)
640 {
641 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
642 throw new RestException(401);
643 }
644 $this->timespentRecordChecks($id, $timespent_id);
645
646 if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) {
647 throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
648 }
649
650 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
651 throw new RestException(500, 'Error when deleting time spent: '.$this->task->error);
652 }
653
654 return array(
655 'success' => array(
656 'code' => 200,
657 'message' => 'Time spent deleted'
658 )
659 );
660 }
661
671 protected function timespentRecordChecks($id, $timespent_id)
672 {
673 if ($this->task->fetch($id) <= 0) {
674 throw new RestException(404, 'Task not found');
675 }
676 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
677 throw new RestException(404, 'Timespent not found');
678 } elseif ($this->task->id != $id) {
679 throw new RestException(404, 'Timespent not found in selected task');
680 }
681 }
682
683 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
690 protected function _cleanObjectDatas($object)
691 {
692 // phpcs:enable
693 $object = parent::_cleanObjectDatas($object);
694
695 unset($object->barcode_type);
696 unset($object->barcode_type_code);
697 unset($object->barcode_type_label);
698 unset($object->barcode_type_coder);
699 unset($object->cond_reglement_id);
700 unset($object->cond_reglement);
701 unset($object->fk_delivery_address);
702 unset($object->shipping_method_id);
703 unset($object->fk_account);
704 unset($object->note);
705 unset($object->fk_incoterms);
706 unset($object->label_incoterms);
707 unset($object->location_incoterms);
708 unset($object->name);
709 unset($object->lastname);
710 unset($object->firstname);
711 unset($object->civility_id);
712 unset($object->mode_reglement_id);
713 unset($object->country);
714 unset($object->country_id);
715 unset($object->country_code);
716
717 unset($object->weekWorkLoad);
718 unset($object->weekWorkLoad);
719
720 //unset($object->lines); // for task we use timespent_lines, but for project we use lines
721
722 unset($object->total_ht);
723 unset($object->total_tva);
724 unset($object->total_localtax1);
725 unset($object->total_localtax2);
726 unset($object->total_ttc);
727
728 unset($object->comments);
729
730 return $object;
731 }
732
740 private function _validate($data)
741 {
742 $object = array();
743 foreach (self::$FIELDS as $field) {
744 if (!isset($data[$field])) {
745 throw new RestException(400, "$field field missing");
746 }
747 $object[$field] = $data[$field];
748 }
749 return $object;
750 }
751
752
753 // \todo
754 // getSummaryOfTimeSpent
755}
Class for API REST v1.
Definition api.class.php:31
_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.
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.
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 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:426
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.