19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
22require_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->hasRight(
'projet',
'lire')) {
71 throw new RestException(403);
74 $result = $this->task->fetch(
$id);
76 throw new RestException(404,
'Task not found');
80 throw new RestException(403,
'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();
108 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
112 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
113 throw new RestException(403);
119 $socids = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : 0;
123 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
124 $search_sale = DolibarrApiAccess::$user->id;
127 $sql =
"SELECT t.rowid";
128 $sql .=
" FROM ".MAIN_DB_PREFIX.
"projet_task AS t";
129 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"projet_task_extrafields AS ef ON (ef.fk_object = t.rowid)";
130 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"projet AS p ON p.rowid = t.fk_projet";
131 $sql .=
' WHERE t.entity IN ('.getEntity(
'project').
')';
133 $sql .=
" AND t.fk_soc IN (".$this->db->sanitize($socids).
")";
136 if ($search_sale && $search_sale !=
'-1') {
137 if ($search_sale == -2) {
138 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM ".MAIN_DB_PREFIX.
"societe_commerciaux as sc WHERE sc.fk_soc = p.fk_soc)";
139 } elseif ($search_sale > 0) {
140 $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).
")";
148 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
152 $sql .= $this->db->order($sortfield, $sortorder);
157 $offset = $limit * $page;
159 $sql .= $this->db->plimit($limit + 1, $offset);
163 $result = $this->db->query($sql);
166 $num = $this->db->num_rows($result);
167 $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());
190 public function post($request_data =
null)
192 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
193 throw new RestException(403,
"Insuffisant rights");
196 $result = $this->
_validate($request_data);
198 foreach ($request_data as $field => $value) {
199 if ($field ===
'caller') {
201 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
205 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
214 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
215 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
218 return $this->task->id;
277 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
278 throw new RestException(403);
281 $result = $this->task->fetch(
$id);
283 throw new RestException(404,
'Task not found');
287 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
290 $usert = DolibarrApiAccess::$user;
292 $usert =
new User($this->db);
293 $usert->fetch($userid);
295 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(
null, $usert, 0,
$id);
297 foreach ($this->task->roles as $line) {
442 public function put(
$id, $request_data =
null)
444 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
445 throw new RestException(403);
448 $result = $this->task->fetch(
$id);
450 throw new RestException(404,
'Task not found');
454 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
456 foreach ($request_data as $field => $value) {
457 if ($field ==
'id') {
460 if ($field ===
'caller') {
462 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
465 if ($field ==
'array_options' && is_array($value)) {
466 foreach ($value as $index => $val) {
467 $this->task->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->task);;
472 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
475 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
476 return $this->
get(
$id);
478 throw new RestException(500, $this->task->error);
489 public function delete(
$id)
491 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
492 throw new RestException(403);
494 $result = $this->task->fetch(
$id);
496 throw new RestException(404,
'Task not found');
500 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
503 if (!$this->task->delete(DolibarrApiAccess::$user)) {
504 throw new RestException(500,
'Error when delete task : '.$this->task->error);
510 'message' =>
'Task deleted'
534 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
535 throw new RestException(403);
537 $result = $this->task->fetch(
$id);
539 throw new RestException(404,
'Task not found');
543 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
548 $uid = DolibarrApiAccess::$user->id;
552 $this->task->timespent_date = $newdate;
553 $this->task->timespent_datehour = $newdate;
554 $this->task->timespent_withhour = 1;
555 $this->task->timespent_duration = $duration;
556 $this->task->timespent_fk_user = $uid;
557 $this->task->timespent_note = $note;
559 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
561 throw new RestException(304,
'Error nothing done. May be object is already validated');
564 throw new RestException(500,
'Error when adding time: '.$this->task->error);
570 'message' =>
'Time spent added'
591 public function putTimeSpent(
$id, $timespent_id, $date, $duration, $user_id = 0, $note =
'')
593 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
594 throw new RestException(403);
599 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
603 $this->task->timespent_date = $newdate;
604 $this->task->timespent_datehour = $newdate;
605 $this->task->timespent_withhour = 1;
606 $this->task->timespent_duration = $duration;
607 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
608 $this->task->timespent_note = $note;
610 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
612 throw new RestException(304,
'Error nothing done.');
615 throw new RestException(500,
'Error when updating time spent: '.$this->task->error);
621 'message' =>
'Time spent updated'
638 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
639 throw new RestException(403);
644 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
647 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
648 throw new RestException(500,
'Error when deleting time spent: '.$this->task->error);
654 'message' =>
'Time spent deleted'
670 if ($this->task->fetch(
$id) <= 0) {
671 throw new RestException(404,
'Task not found');
673 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
674 throw new RestException(404,
'Timespent not found');
675 } elseif ($this->task->id !=
$id) {
676 throw new RestException(404,
'Timespent not found in selected task');
693 unset(
$object->barcode_type_code);
694 unset(
$object->barcode_type_label);
695 unset(
$object->barcode_type_coder);
696 unset(
$object->cond_reglement_id);
697 unset(
$object->cond_reglement);
698 unset(
$object->fk_delivery_address);
699 unset(
$object->shipping_method_id);
703 unset(
$object->label_incoterms);
704 unset(
$object->location_incoterms);
709 unset(
$object->mode_reglement_id);
721 unset(
$object->total_localtax1);
722 unset(
$object->total_localtax2);
740 foreach (self::$FIELDS as $field) {
741 if (!isset($data[$field])) {
742 throw new RestException(400,
"$field field missing");
744 $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.