19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
22 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
36 static $FIELDS = array(
54 $this->task =
new Task($this->
db);
68 public function get($id, $includetimespent = 0)
70 if (!DolibarrApiAccess::$user->rights->projet->lire) {
71 throw new RestException(401);
74 $result = $this->task->fetch($id);
76 throw new RestException(404,
'Task not found');
80 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
83 if ($includetimespent == 1)
85 $timespent = $this->task->getSummaryOfTimeSpent(0);
87 if ($includetimespent == 1)
110 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
117 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
121 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) $search_sale = DolibarrApiAccess::$user->id;
123 $sql =
"SELECT t.rowid";
124 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", sc.fk_soc, sc.fk_user";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet_task as t";
127 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
129 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
130 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) $sql .=
" AND t.fk_soc = sc.fk_soc";
131 if ($socids) $sql .=
" AND t.fk_soc IN (".$socids.
")";
132 if ($search_sale > 0) $sql .=
" AND t.rowid = sc.fk_soc";
134 if ($search_sale > 0)
136 $sql .=
" AND sc.fk_user = ".$search_sale;
143 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
145 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
146 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
149 $sql .= $this->
db->order($sortfield, $sortorder);
155 $offset = $limit * $page;
157 $sql .= $this->
db->plimit($limit + 1, $offset);
161 $result = $this->
db->query($sql);
165 $num = $this->
db->num_rows($result);
166 $min = min($num, ($limit <= 0 ? $num : $limit));
170 $obj = $this->
db->fetch_object($result);
171 $task_static =
new Task($this->
db);
172 if ($task_static->fetch($obj->rowid)) {
178 throw new RestException(503,
'Error when retrieve task list : '.$this->
db->lasterror());
180 if (!count($obj_ret)) {
181 throw new RestException(404,
'No task found');
192 public function post($request_data = null)
194 if (!DolibarrApiAccess::$user->rights->projet->creer) {
195 throw new RestException(401,
"Insuffisant rights");
198 $result = $this->
_validate($request_data);
200 foreach ($request_data as $field => $value) {
201 $this->task->$field = $value;
210 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
211 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
214 return $this->task->id;
273 if (!DolibarrApiAccess::$user->rights->projet->lire) {
274 throw new RestException(401);
277 $result = $this->task->fetch($id);
279 throw new RestException(404,
'Task not found');
283 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
286 $usert = DolibarrApiAccess::$user;
289 $usert =
new User($this->
db);
290 $usert->fetch($userid);
292 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(0, $usert, 0, $id);
294 foreach ($this->task->roles as $line) {
431 public function put($id, $request_data = null)
433 if (!DolibarrApiAccess::$user->rights->projet->creer) {
434 throw new RestException(401);
437 $result = $this->task->fetch($id);
439 throw new RestException(404,
'Task not found');
443 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
445 foreach ($request_data as $field => $value) {
446 if ($field ==
'id')
continue;
447 $this->task->$field = $value;
450 if ($this->task->update(DolibarrApiAccess::$user) > 0)
452 return $this->
get($id);
454 throw new RestException(500, $this->task->error);
465 public function delete($id)
467 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
468 throw new RestException(401);
470 $result = $this->task->fetch($id);
472 throw new RestException(404,
'Task not found');
476 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
479 if (!$this->task->delete(DolibarrApiAccess::$user)) {
480 throw new RestException(500,
'Error when delete task : '.$this->task->error);
486 'message' =>
'Task deleted'
507 public function addTimeSpent($id, $date, $duration, $user_id = 0, $note =
'')
509 if (!DolibarrApiAccess::$user->rights->projet->creer) {
510 throw new RestException(401);
512 $result = $this->task->fetch($id);
514 throw new RestException(404,
'Task not found');
518 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
522 if (empty($uid)) $uid = DolibarrApiAccess::$user->id;
525 $this->task->timespent_date = $newdate;
526 $this->task->timespent_datehour = $newdate;
527 $this->task->timespent_withhour = 1;
528 $this->task->timespent_duration = $duration;
529 $this->task->timespent_fk_user = $user_id;
530 $this->task->timespent_note = $note;
532 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
534 throw new RestException(304,
'Error nothing done. May be object is already validated');
537 throw new RestException(500,
'Error when adding time: '.$this->task->error);
543 'message' =>
'Time spent added'
559 $object = parent::_cleanObjectDatas($object);
561 unset($object->barcode_type);
562 unset($object->barcode_type_code);
563 unset($object->barcode_type_label);
564 unset($object->barcode_type_coder);
565 unset($object->cond_reglement_id);
566 unset($object->cond_reglement);
567 unset($object->fk_delivery_address);
568 unset($object->shipping_method_id);
569 unset($object->fk_account);
570 unset($object->note);
571 unset($object->fk_incoterms);
572 unset($object->label_incoterms);
573 unset($object->location_incoterms);
574 unset($object->name);
575 unset($object->lastname);
576 unset($object->firstname);
577 unset($object->civility_id);
578 unset($object->mode_reglement_id);
579 unset($object->country);
580 unset($object->country_id);
581 unset($object->country_code);
583 unset($object->weekWorkLoad);
584 unset($object->weekWorkLoad);
588 unset($object->total_ht);
589 unset($object->total_tva);
590 unset($object->total_localtax1);
591 unset($object->total_localtax2);
592 unset($object->total_ttc);
594 unset($object->comments);
609 foreach (self::$FIELDS as $field) {
610 if (!isset($data[$field]))
611 throw new RestException(400,
"$field field missing");
612 $object[$field] = $data[$field];
addTimeSpent($id, $date, $duration, $user_id=0, $note= '')
Add time spent to a task of a project.
getRoles($id, $userid=0)
Get time spent of a task.
_validate($data)
Validate fields before create or update object.
Class to manage Dolibarr users.
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $sqlfilters= '')
List tasks.
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...
__construct()
Constructor.
post($request_data=null)
Create task object.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
_cleanObjectDatas($object)
Clean sensible object datas.
put($id, $request_data=null)
Add a task to given project.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.