20use Luracast\Restler\RestException;
22require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
23require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
37 public static $FIELDS = array(
55 $this->task =
new Task($this->db);
69 public function get(
$id, $includetimespent = 0)
71 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
72 throw new RestException(403);
75 $result = $this->task->fetch(
$id);
77 throw new RestException(404,
'Task not found');
81 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
84 if ($includetimespent == 1) {
85 $timespent = $this->task->getSummaryOfTimeSpent(0);
87 if ($includetimespent == 2) {
88 $timespent = $this->task->fetchTimeSpentOnTask();
109 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
113 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
114 throw new RestException(403);
120 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
124 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
125 $search_sale = DolibarrApiAccess::$user->id;
128 $sql =
"SELECT t.rowid";
129 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet_task AS t";
130 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"projet_task_extrafields AS ef ON (ef.fk_object = t.rowid)";
131 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"projet AS p ON p.rowid = t.fk_projet";
132 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
134 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
137 if ($search_sale && $search_sale !=
'-1') {
138 if ($search_sale == -2) {
139 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = p.fk_soc)";
140 } elseif ($search_sale > 0) {
141 $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).
")";
149 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
153 $sql .= $this->db->order($sortfield, $sortorder);
158 $offset = $limit * $page;
160 $sql .= $this->db->plimit($limit + 1, $offset);
164 $result = $this->db->query($sql);
167 $num = $this->db->num_rows($result);
168 $min = min($num, ($limit <= 0 ? $num : $limit));
171 $obj = $this->db->fetch_object($result);
172 $task_static =
new Task($this->db);
173 if ($task_static->fetch($obj->rowid)) {
179 throw new RestException(503,
'Error when retrieve task list : '.$this->db->lasterror());
191 public function post($request_data =
null)
193 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
194 throw new RestException(403,
"Insuffisant rights");
197 $result = $this->
_validate($request_data);
199 foreach ($request_data as $field => $value) {
200 if ($field ===
'caller') {
202 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
206 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
215 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
216 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
219 return $this->task->id;
278 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
279 throw new RestException(403);
282 $result = $this->task->fetch(
$id);
284 throw new RestException(404,
'Task not found');
288 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
291 $usert = DolibarrApiAccess::$user;
293 $usert =
new User($this->db);
294 $usert->fetch($userid);
296 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(
null, $usert, 0,
$id);
298 foreach ($this->task->roles as $line) {
443 public function put(
$id, $request_data =
null)
445 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
446 throw new RestException(403);
449 $result = $this->task->fetch(
$id);
451 throw new RestException(404,
'Task not found');
455 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
457 foreach ($request_data as $field => $value) {
458 if ($field ==
'id') {
461 if ($field ===
'caller') {
463 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
466 if ($field ==
'array_options' && is_array($value)) {
467 foreach ($value as $index => $val) {
468 $this->task->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->task);
473 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
476 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
477 return $this->
get(
$id);
479 throw new RestException(500, $this->task->error);
490 public function delete(
$id)
492 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
493 throw new RestException(403);
495 $result = $this->task->fetch(
$id);
497 throw new RestException(404,
'Task not found');
501 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
504 if (!$this->task->delete(DolibarrApiAccess::$user)) {
505 throw new RestException(500,
'Error when delete task : '.$this->task->error);
511 'message' =>
'Task deleted'
535 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
536 throw new RestException(403);
538 $result = $this->task->fetch(
$id);
540 throw new RestException(404,
'Task not found');
544 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
549 $uid = DolibarrApiAccess::$user->id;
553 $this->task->timespent_date = $newdate;
554 $this->task->timespent_datehour = $newdate;
555 $this->task->timespent_withhour = 1;
556 $this->task->timespent_duration = $duration;
557 $this->task->timespent_fk_user = $uid;
558 $this->task->timespent_note = $note;
560 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
562 throw new RestException(304,
'Error nothing done. May be object is already validated');
565 throw new RestException(500,
'Error when adding time: '.$this->task->error);
571 'message' =>
'Time spent added'
592 public function putTimeSpent(
$id, $timespent_id, $date, $duration, $user_id = 0, $note =
'')
594 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
595 throw new RestException(403);
600 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
604 $this->task->timespent_date = $newdate;
605 $this->task->timespent_datehour = $newdate;
606 $this->task->timespent_withhour = 1;
607 $this->task->timespent_duration = $duration;
608 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
609 $this->task->timespent_note = $note;
611 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
613 throw new RestException(304,
'Error nothing done.');
616 throw new RestException(500,
'Error when updating time spent: '.$this->task->error);
622 'message' =>
'Time spent updated'
639 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
640 throw new RestException(403);
645 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
648 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
649 throw new RestException(500,
'Error when deleting time spent: '.$this->task->error);
655 'message' =>
'Time spent deleted'
671 if ($this->task->fetch(
$id) <= 0) {
672 throw new RestException(404,
'Task not found');
674 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
675 throw new RestException(404,
'Timespent not found');
676 } elseif ($this->task->id !=
$id) {
677 throw new RestException(404,
'Timespent not found in selected task');
694 unset(
$object->barcode_type_code);
695 unset(
$object->barcode_type_label);
696 unset(
$object->barcode_type_coder);
697 unset(
$object->cond_reglement_id);
698 unset(
$object->cond_reglement);
699 unset(
$object->fk_delivery_address);
700 unset(
$object->shipping_method_id);
704 unset(
$object->label_incoterms);
705 unset(
$object->location_incoterms);
710 unset(
$object->mode_reglement_id);
722 unset(
$object->total_localtax1);
723 unset(
$object->total_localtax2);
741 foreach (self::$FIELDS as $field) {
742 if (!isset($data[$field])) {
743 throw new RestException(400,
"$field field missing");
745 $object[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_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.
_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...
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...