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 public 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) {
84 $timespent = $this->task->getSummaryOfTimeSpent(0);
86 if ($includetimespent == 2) {
87 $timespent = $this->task->fetchTimeSpentOnTask();
107 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'')
111 if (!DolibarrApiAccess::$user->rights->projet->lire) {
112 throw new RestException(401);
118 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid :
'';
122 if (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) {
123 $search_sale = DolibarrApiAccess::$user->id;
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";
130 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet_task as t";
132 if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socids) || $search_sale > 0) {
133 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
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";
141 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
143 if ($search_sale > 0) {
144 $sql .=
" AND t.rowid = sc.fk_soc";
147 if ($search_sale > 0) {
148 $sql .=
" AND sc.fk_user = ".((int) $search_sale);
154 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
156 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
157 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
160 $sql .= $this->
db->order($sortfield, $sortorder);
165 $offset = $limit * $page;
167 $sql .= $this->
db->plimit($limit + 1, $offset);
171 $result = $this->
db->query($sql);
174 $num = $this->
db->num_rows($result);
175 $min = min($num, ($limit <= 0 ? $num : $limit));
178 $obj = $this->
db->fetch_object($result);
179 $task_static =
new Task($this->
db);
180 if ($task_static->fetch($obj->rowid)) {
186 throw new RestException(503,
'Error when retrieve task list : '.$this->
db->lasterror());
188 if (!count($obj_ret)) {
189 throw new RestException(404,
'No task found');
200 public function post($request_data =
null)
202 if (!DolibarrApiAccess::$user->rights->projet->creer) {
203 throw new RestException(401,
"Insuffisant rights");
206 $result = $this->
_validate($request_data);
208 foreach ($request_data as $field => $value) {
209 $this->task->$field = $value;
218 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
219 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
222 return $this->task->id;
281 if (!DolibarrApiAccess::$user->rights->projet->lire) {
282 throw new RestException(401);
285 $result = $this->task->fetch($id);
287 throw new RestException(404,
'Task not found');
291 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
294 $usert = DolibarrApiAccess::$user;
296 $usert =
new User($this->
db);
297 $usert->fetch($userid);
299 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(
null, $usert, 0, $id);
301 foreach ($this->task->roles as $line) {
446 public function put($id, $request_data =
null)
448 if (!DolibarrApiAccess::$user->rights->projet->creer) {
449 throw new RestException(401);
452 $result = $this->task->fetch($id);
454 throw new RestException(404,
'Task not found');
458 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
460 foreach ($request_data as $field => $value) {
461 if ($field ==
'id') {
464 $this->task->$field = $value;
467 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
468 return $this->
get($id);
470 throw new RestException(500, $this->task->error);
481 public function delete($id)
483 if (!DolibarrApiAccess::$user->rights->projet->supprimer) {
484 throw new RestException(401);
486 $result = $this->task->fetch($id);
488 throw new RestException(404,
'Task not found');
492 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 if (!$this->task->delete(DolibarrApiAccess::$user)) {
496 throw new RestException(500,
'Error when delete task : '.$this->task->error);
502 'message' =>
'Task deleted'
523 public function addTimeSpent($id, $date, $duration, $user_id = 0, $note =
'')
525 if (!DolibarrApiAccess::$user->rights->projet->creer) {
526 throw new RestException(401);
528 $result = $this->task->fetch($id);
530 throw new RestException(404,
'Task not found');
534 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
539 $uid = DolibarrApiAccess::$user->id;
543 $this->task->timespent_date = $newdate;
544 $this->task->timespent_datehour = $newdate;
545 $this->task->timespent_withhour = 1;
546 $this->task->timespent_duration = $duration;
547 $this->task->timespent_fk_user = $uid;
548 $this->task->timespent_note = $note;
550 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
552 throw new RestException(304,
'Error nothing done. May be object is already validated');
555 throw new RestException(500,
'Error when adding time: '.$this->task->error);
561 'message' =>
'Time spent added'
577 $object = parent::_cleanObjectDatas($object);
579 unset($object->barcode_type);
580 unset($object->barcode_type_code);
581 unset($object->barcode_type_label);
582 unset($object->barcode_type_coder);
583 unset($object->cond_reglement_id);
584 unset($object->cond_reglement);
585 unset($object->fk_delivery_address);
586 unset($object->shipping_method_id);
587 unset($object->fk_account);
588 unset($object->note);
589 unset($object->fk_incoterms);
590 unset($object->label_incoterms);
591 unset($object->location_incoterms);
592 unset($object->name);
593 unset($object->lastname);
594 unset($object->firstname);
595 unset($object->civility_id);
596 unset($object->mode_reglement_id);
597 unset($object->country);
598 unset($object->country_id);
599 unset($object->country_code);
601 unset($object->weekWorkLoad);
602 unset($object->weekWorkLoad);
606 unset($object->total_ht);
607 unset($object->total_tva);
608 unset($object->total_localtax1);
609 unset($object->total_localtax2);
610 unset($object->total_ttc);
612 unset($object->comments);
627 foreach (self::$FIELDS as $field) {
628 if (!isset($data[$field])) {
629 throw new RestException(400,
"$field field missing");
631 $object[$field] = $data[$field];
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
_cleanObjectDatas($object)
Clean sensible object datas.
_validate($data)
Validate fields before create or update object.
post($request_data=null)
Create task object.
put($id, $request_data=null)
Update task general fields (won't touch time spent of task)
getRoles($id, $userid=0)
Get roles a user is assigned to a task with.
__construct()
Constructor.
addTimeSpent($id, $date, $duration, $user_id=0, $note='')
Add time spent to 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...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.