24use Luracast\Restler\RestException;
26require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
28require_once DOL_DOCUMENT_ROOT.
'/core/class/timespent.class.php';
42 public static $FIELDS = array(
60 $this->task =
new Task($this->db);
76 public function get(
$id, $includetimespent = 0)
78 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
79 throw new RestException(403);
82 $result = $this->task->fetch(
$id);
84 throw new RestException(404,
'Task not found');
88 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
91 if ($includetimespent == 1) {
92 $timespent = $this->task->getSummaryOfTimeSpent(0);
94 if ($includetimespent == 2) {
95 $timespent = $this->task->fetchTimeSpentOnTask();
121 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false)
125 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
126 throw new RestException(403);
132 $socids = DolibarrApiAccess::$user->socid ?: 0;
136 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
137 $search_sale = DolibarrApiAccess::$user->id;
140 $sql =
"SELECT t.rowid";
141 $sql .=
" FROM " . MAIN_DB_PREFIX .
"projet_task AS t";
142 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"projet_task_extrafields AS ef ON (ef.fk_object = t.rowid)";
143 $sql .=
" INNER JOIN " . MAIN_DB_PREFIX .
"projet AS p ON p.rowid = t.fk_projet";
144 $sql .=
' WHERE t.entity IN (' .
getEntity(
'project') .
')';
146 $sql .=
" AND p.fk_soc IN (" . $this->db->sanitize((
string) $socids) .
")";
149 if ($search_sale && $search_sale !=
'-1') {
150 if ($search_sale == -2) {
151 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX .
"societe_commerciaux as sc WHERE sc.fk_soc = p.fk_soc)";
152 } elseif ($search_sale > 0) {
153 $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) .
")";
159 $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
161 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
166 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
168 $sql .= $this->db->order($sortfield, $sortorder);
173 $offset = $limit * $page;
175 $sql .= $this->db->plimit($limit + 1, $offset);
179 $result = $this->db->query($sql);
182 $num = $this->db->num_rows($result);
183 $min = min($num, ($limit <= 0 ? $num : $limit));
186 $obj = $this->db->fetch_object($result);
187 $task_static =
new Task($this->db);
188 if ($task_static->fetch($obj->rowid)) {
194 throw new RestException(503,
'Error when retrieve task list : ' . $this->db->lasterror());
198 if ($pagination_data) {
199 $totalsResult = $this->db->query($sqlTotals);
200 $total = $this->db->fetch_object($totalsResult)->total;
205 $obj_ret[
'data'] = $tmp;
206 $obj_ret[
'pagination'] = [
207 'total' => (int) $total,
209 'page_count' => ceil((
int) $total / $limit),
227 public function post($request_data =
null)
230 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
231 throw new RestException(403,
"Insufficiant rights");
234 $result = $this->
_validate($request_data);
236 foreach ($request_data as $field => $value) {
237 if ($field ===
'caller') {
239 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
243 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
254 if ($this->task->ref == -1 || $this->task->ref ===
'auto') {
262 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
263 foreach ($dirmodels as $reldir) {
264 $file =
dol_buildpath($reldir.
"core/modules/project/task/".$modele.
'.php', 0);
265 if (file_exists($file)) {
267 $classname = $modele;
271 if ($filefound && !empty($classname)) {
272 $result =
dol_include_once($reldir .
"core/modules/project/task/" . $modele .
'.php');
273 if ($result !==
false && class_exists($classname)) {
274 $modTask =
new $classname();
275 '@phan-var-force ModeleNumRefTask $modTask';
276 $defaultref = $modTask->getNextValue(
null, $this->task);
278 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/task/" . $modele .
'.php', LOG_ERR);
281 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
284 if (is_numeric($defaultref) && $defaultref <= 0) {
288 if (empty($defaultref)) {
292 $this->task->ref = $defaultref;
295 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
296 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
299 return $this->task->id;
314 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
315 throw new RestException(403);
318 $result = $this->task->fetch(
$id);
320 throw new RestException(404,
'Task not found');
324 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
327 $this->task->fetchTimeSpentOnTask();
330 foreach ($this->task->lines as $line) {
354 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
355 throw new RestException(403);
358 $result = $this->task->fetch(
$id);
360 throw new RestException(404,
'Task not found');
364 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
367 $usert = DolibarrApiAccess::$user;
369 $usert =
new User($this->db);
370 $usert->fetch($userid);
372 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(
null, $usert,
'0',
$id);
374 foreach ($this->task->roles as $line) {
527 public function put(
$id, $request_data =
null)
529 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
530 throw new RestException(403);
533 $result = $this->task->fetch(
$id);
535 throw new RestException(404,
'Task not found');
539 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
541 foreach ($request_data as $field => $value) {
542 if ($field ==
'id') {
545 if ($field ===
'caller') {
547 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
550 if ($field ==
'array_options' && is_array($value)) {
551 foreach ($value as $index => $val) {
552 $this->task->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->task);
557 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
560 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
561 return $this->
get(
$id);
563 throw new RestException(500, $this->task->error);
579 public function delete(
$id)
581 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
582 throw new RestException(403);
584 $result = $this->task->fetch(
$id);
586 throw new RestException(404,
'Task not found');
590 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
593 if ($this->task->delete(DolibarrApiAccess::$user) <= 0) {
594 throw new RestException(500,
'Error when delete task : ' . $this->task->error);
600 'message' =>
'Task deleted'
621 dol_syslog(
"API Rest request::getTimeSpent", LOG_DEBUG);
622 if (! DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
623 throw new RestException(403);
626 $taskresult = $this->task->fetch(
$id);
628 throw new RestException(404,
'Task with id='.
$id.
' not found');
631 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
635 $timeresult = $timespent->fetch($timespent_id);
637 throw new RestException(404,
'Timespent with id='.$timespent_id.
' not found');
640 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
669 public function addTimeSpent(
$id, $date, $duration, $product_id =
null, $user_id = 0, $note =
'', $progress = -1)
671 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
672 throw new RestException(403);
674 $result = $this->task->fetch(
$id);
676 throw new RestException(404,
'Task not found');
680 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
685 $uid = DolibarrApiAccess::$user->id;
690 $this->task->timespent_date = $newdate;
691 $this->task->timespent_datehour = $newdate;
692 $this->task->timespent_withhour = 1;
693 $this->task->timespent_duration = $duration;
694 $this->task->timespent_fk_product = $product_id;
695 $this->task->timespent_fk_user = $uid;
696 $this->task->timespent_note = $note;
697 if (!empty($progress) && $progress >= 0 && $progress <= 100) {
698 $this->task->progress = $progress;
701 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
703 throw new RestException(304,
'Error nothing done. May be object is already validated');
706 throw new RestException(500,
'Error when adding time: ' . $this->task->error);
712 'message' =>
'Time spent added'
739 public function putTimeSpent(
$id, $timespent_id, $date, $duration, $product_id =
null, $user_id = 0, $note =
'')
741 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
742 throw new RestException(403);
747 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
751 $this->task->timespent_date = $newdate;
752 $this->task->timespent_datehour = $newdate;
753 $this->task->timespent_withhour = 1;
754 $this->task->timespent_duration = $duration;
755 $this->task->timespent_fk_product = $product_id;
756 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
757 $this->task->timespent_note = $note;
759 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
761 throw new RestException(304,
'Error nothing done.');
764 throw new RestException(500,
'Error when updating time spent: ' . $this->task->error);
770 'message' =>
'Time spent updated'
791 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
792 throw new RestException(403);
797 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
800 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
801 throw new RestException(500,
'Error when deleting time spent: ' . $this->task->error);
807 'message' =>
'Time spent deleted'
823 dol_syslog(
"API Rest request::timespentRecordChecks", LOG_DEBUG);
824 if ($this->task->fetch(
$id) <= 0) {
825 throw new RestException(404,
'Task not found');
827 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
828 throw new RestException(404,
'Timespent not found');
829 } elseif ($this->task->id !=
$id) {
830 throw new RestException(404,
'Timespent not found in selected task');
851 unset(
$object->barcode_type_code);
852 unset(
$object->barcode_type_label);
853 unset(
$object->barcode_type_coder);
854 unset(
$object->cond_reglement_id);
855 unset(
$object->cond_reglement);
856 unset(
$object->fk_delivery_address);
857 unset(
$object->shipping_method_id);
861 unset(
$object->label_incoterms);
862 unset(
$object->location_incoterms);
867 unset(
$object->mode_reglement_id);
879 unset(
$object->total_localtax1);
880 unset(
$object->total_localtax2);
905 $saving_fk_element =
$object->fk_element;
912 unset(
$object->barcode_type_code);
913 unset(
$object->barcode_type_label);
914 unset(
$object->barcode_type_coder);
915 unset(
$object->cond_reglement_id);
916 unset(
$object->cond_reglement);
917 unset(
$object->fk_delivery_address);
918 unset(
$object->shipping_method_id);
921 unset(
$object->label_incoterms);
922 unset(
$object->location_incoterms);
927 unset(
$object->mode_reglement_id);
935 unset(
$object->actiontypecode);
936 unset(
$object->array_languages);
940 unset(
$object->cond_reglement_supplier_id);
943 unset(
$object->contacts_ids_internal);
945 unset(
$object->date_validation);
946 unset(
$object->demand_reason_id);
947 unset(
$object->deposit_percent);
950 unset(
$object->fk_multicurrency);
956 unset(
$object->linkedObjectsIds);
958 unset(
$object->multicurrency_code);
959 unset(
$object->multicurrency_total_ht);
960 unset(
$object->multicurrency_total_localtax1);
961 unset(
$object->multicurrency_total_localtax2);
962 unset(
$object->multicurrency_total_ttc);
963 unset(
$object->multicurrency_total_tva);
964 unset(
$object->multicurrency_tx);
971 unset(
$object->retained_warranty_fk_cond_reglement);
973 unset(
$object->shipping_method);
979 unset(
$object->transport_mode_id);
982 unset(
$object->user_closing_id);
984 unset(
$object->user_creation_id);
985 unset(
$object->user_modification);
986 unset(
$object->user_modification_id);
988 unset(
$object->user_validation);
989 unset(
$object->user_validation_id);
994 unset(
$object->total_localtax1);
995 unset(
$object->total_localtax2);
1000 if (!
$object->date_creation) {
1003 if (!
$object->date_modification) {
1007 $object->fk_element = $saving_fk_element;
1023 if ($data ===
null) {
1027 foreach (self::$FIELDS as $field) {
1028 if (!isset($data[$field])) {
1029 throw new RestException(400,
"$field field missing");
1031 $object[$field] = $data[$field];
1053 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1054 throw new RestException(403);
1057 $result = $this->task->fetch(
$id);
1059 throw new RestException(404,
'Task not found');
1063 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1066 $contacts = $this->task->liste_contact(-1,
'external', 0, $type);
1067 $socpeoples = $this->task->liste_contact(-1,
'internal', 0, $type);
1069 $contacts = array_merge($contacts, $socpeoples);
1094 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
1096 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1097 throw new RestException(403);
1100 $result = $this->task->fetch(
$id);
1102 throw new RestException(404,
'Task not found');
1106 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1109 $result = $this->task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1111 throw new RestException(500,
'Error : ' . $this->task->error);
1114 $result = $this->task->fetch(
$id);
1116 throw new RestException(404,
'Task not found');
1141 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1142 throw new RestException(403);
1145 $result = $this->task->fetch(
$id);
1147 throw new RestException(404,
'Task not found');
1151 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1154 foreach (array(
'internal',
'external') as $source) {
1155 $contacts = $this->task->liste_contact(-1, $source);
1157 foreach ($contacts as $contact) {
1158 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
1159 $result = $this->task->delete_contact($contact[
'rowid']);
1161 throw new RestException(500,
'Error when deleted the contact');
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $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 sensitive object data @phpstan-template T.
_validate($data)
Validate fields before create or update object.
deleteContact($id, $contactid, $type)
Delete a contact type of given task.
post($request_data=null)
Create task object.
addTimeSpent($id, $date, $duration, $product_id=null, $user_id=0, $note='', $progress=-1)
Add time spent to a task of a project.
put($id, $request_data=null)
Add a task to given project.
getTimeSpentByID($id, $timespent_id)
Get time spent of a task.
_cleanTimeSpentObjectDatas($object)
Clean sensitive object data @phpstan-template T of Object.
getRoles($id, $userid=0)
Get roles a user is assigned to a task with.
addContact($id, $fk_socpeople, $type_contact, $source, $notrigger=0)
Adds a contact to a task.
getContacts($id, $type='')
Get contacts of given task.
getTimespent($id)
Get time spent of a task.
__construct()
Constructor.
putTimeSpent($id, $timespent_id, $date, $duration, $product_id=null, $user_id=0, $note='')
Update time spent for a task of a project.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false)
List tasks.
timespentRecordChecks($id, $timespent_id)
Validate task & timespent IDs for timespent API methods.
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...
dol_now($mode='gmt')
Return date for now.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.