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';
29require_once DOL_DOCUMENT_ROOT.
'/core/lib/company.lib.php';
43 public static $FIELDS = array(
61 $this->task =
new Task($this->db);
77 public function get(
$id, $includetimespent = 0)
79 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
80 throw new RestException(403);
83 $result = $this->task->fetch(
$id);
85 throw new RestException(404,
'Task not found');
89 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
92 if ($includetimespent == 1) {
93 $timespent = $this->task->getSummaryOfTimeSpent(0);
95 if ($includetimespent == 2) {
96 $timespent = $this->task->fetchTimeSpentOnTask();
123 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'', $pagination_data =
false, $includetimespent = 0)
127 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
128 throw new RestException(403);
134 $socids = DolibarrApiAccess::$user->socid ?: 0;
138 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
139 $search_sale = DolibarrApiAccess::$user->id;
142 $sql =
"SELECT t.rowid";
143 $sql .=
" FROM " . MAIN_DB_PREFIX .
"projet_task AS t";
144 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"projet_task_extrafields AS ef ON (ef.fk_object = t.rowid)";
145 $sql .=
" INNER JOIN " . MAIN_DB_PREFIX .
"projet AS p ON p.rowid = t.fk_projet";
146 $sql .=
' WHERE t.entity IN (' .
getEntity(
'project') .
')';
148 $sql .=
" AND p.fk_soc IN (" . $this->db->sanitize((
string) $socids) .
")";
151 if ($search_sale && $search_sale !=
'-1') {
152 if ($search_sale == -2) {
153 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
'p.fk_soc', 0, 1);
154 } elseif ($search_sale > 0) {
155 $sql .=
" AND ".getSalesRepresentativeSqlFilter(
'p.fk_soc', (
int) $search_sale);
163 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
168 $sqlTotals = str_replace(
'SELECT t.rowid',
'SELECT count(t.rowid) as total', $sql);
170 $sql .= $this->db->order($sortfield, $sortorder);
175 $offset = $limit * $page;
177 $sql .= $this->db->plimit($limit + 1, $offset);
181 $result = $this->db->query($sql);
184 $num = $this->db->num_rows($result);
185 $min = min($num, ($limit <= 0 ? $num : $limit));
188 $obj = $this->db->fetch_object($result);
189 $task_static =
new Task($this->db);
190 if ($task_static->fetch($obj->rowid)) {
191 if ($includetimespent == 1) {
192 $task_static->getSummaryOfTimeSpent(0);
194 if ($includetimespent == 2) {
195 $task_static->fetchTimeSpentOnTask();
202 throw new RestException(503,
'Error when retrieve task list : ' . $this->db->lasterror());
206 if ($pagination_data) {
207 $totalsResult = $this->db->query($sqlTotals);
208 $total = $this->db->fetch_object($totalsResult)->total;
213 $obj_ret[
'data'] = $tmp;
214 $obj_ret[
'pagination'] = [
215 'total' => (int) $total,
217 'page_count' => ceil((
int) $total / $limit),
235 public function post($request_data =
null)
238 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
239 throw new RestException(403,
"Insufficiant rights");
242 $result = $this->
_validate($request_data);
244 foreach ($request_data as $field => $value) {
245 if ($field ===
'caller') {
247 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
251 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
262 if ($this->task->ref == -1 || $this->task->ref ===
'auto') {
270 $dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
271 foreach ($dirmodels as $reldir) {
272 $file =
dol_buildpath($reldir.
"core/modules/project/task/".$modele.
'.php', 0);
273 if (file_exists($file)) {
275 $classname = $modele;
279 if ($filefound && !empty($classname)) {
280 $result =
dol_include_once($reldir .
"core/modules/project/task/" . $modele .
'.php');
281 if ($result !==
false && class_exists($classname)) {
282 $modTask =
new $classname();
283 '@phan-var-force ModeleNumRefTask $modTask';
284 $defaultref = $modTask->getNextValue(
null, $this->task);
286 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/task/" . $modele .
'.php', LOG_ERR);
289 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
292 if (is_numeric($defaultref) && $defaultref <= 0) {
296 if (empty($defaultref)) {
300 $this->task->ref = $defaultref;
303 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
304 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
307 return $this->task->id;
322 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
323 throw new RestException(403);
326 $result = $this->task->fetch(
$id);
328 throw new RestException(404,
'Task not found');
332 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
335 $this->task->fetchTimeSpentOnTask();
338 foreach ($this->task->lines as $line) {
362 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
363 throw new RestException(403);
366 $result = $this->task->fetch(
$id);
368 throw new RestException(404,
'Task not found');
372 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
375 $usert = DolibarrApiAccess::$user;
377 $usert =
new User($this->db);
378 $usert->fetch($userid);
380 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(
null, $usert,
'0',
$id);
382 foreach ($this->task->roles as $line) {
535 public function put(
$id, $request_data =
null)
537 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
538 throw new RestException(403);
541 $result = $this->task->fetch(
$id);
543 throw new RestException(404,
'Task not found');
547 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
549 foreach ($request_data as $field => $value) {
550 if ($field ==
'id') {
553 if ($field ===
'caller') {
555 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
558 if ($field ==
'array_options' && is_array($value)) {
559 foreach ($value as $index => $val) {
560 $this->task->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->task);
565 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
568 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
569 return $this->
get(
$id);
571 throw new RestException(500, $this->task->error);
587 public function delete(
$id)
589 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
590 throw new RestException(403);
592 $result = $this->task->fetch(
$id);
594 throw new RestException(404,
'Task not found');
598 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
601 if ($this->task->delete(DolibarrApiAccess::$user) <= 0) {
602 throw new RestException(500,
'Error when delete task : ' . $this->task->error);
608 'message' =>
'Task deleted'
629 dol_syslog(
"API Rest request::getTimeSpent", LOG_DEBUG);
630 if (! DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
631 throw new RestException(403);
634 $taskresult = $this->task->fetch(
$id);
636 throw new RestException(404,
'Task with id='.
$id.
' not found');
639 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
643 $timeresult = $timespent->fetch($timespent_id);
645 throw new RestException(404,
'Timespent with id='.$timespent_id.
' not found');
648 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
677 public function addTimeSpent(
$id, $date, $duration, $product_id =
null, $user_id = 0, $note =
'', $progress = -1)
679 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
680 throw new RestException(403);
682 $result = $this->task->fetch(
$id);
684 throw new RestException(404,
'Task not found');
688 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
693 $uid = DolibarrApiAccess::$user->id;
698 $this->task->timespent_date = $newdate;
699 $this->task->timespent_datehour = $newdate;
700 $this->task->timespent_withhour = 1;
701 $this->task->timespent_duration = $duration;
702 $this->task->timespent_fk_product = $product_id;
703 $this->task->timespent_fk_user = $uid;
704 $this->task->timespent_note = $note;
705 if (!empty($progress) && $progress >= 0 && $progress <= 100) {
706 $this->task->progress = $progress;
709 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
711 throw new RestException(304,
'Error nothing done. May be object is already validated');
714 throw new RestException(500,
'Error when adding time: ' . $this->task->error);
720 'message' =>
'Time spent added'
747 public function putTimeSpent(
$id, $timespent_id, $date, $duration, $product_id =
null, $user_id = 0, $note =
'')
749 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
750 throw new RestException(403);
755 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
759 $this->task->timespent_date = $newdate;
760 $this->task->timespent_datehour = $newdate;
761 $this->task->timespent_withhour = 1;
762 $this->task->timespent_duration = $duration;
763 $this->task->timespent_fk_product = $product_id;
764 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
765 $this->task->timespent_note = $note;
767 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
769 throw new RestException(304,
'Error nothing done.');
772 throw new RestException(500,
'Error when updating time spent: ' . $this->task->error);
778 'message' =>
'Time spent updated'
799 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
800 throw new RestException(403);
805 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
808 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
809 throw new RestException(500,
'Error when deleting time spent: ' . $this->task->error);
815 'message' =>
'Time spent deleted'
831 dol_syslog(
"API Rest request::timespentRecordChecks", LOG_DEBUG);
832 if ($this->task->fetch(
$id) <= 0) {
833 throw new RestException(404,
'Task not found');
835 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
836 throw new RestException(404,
'Timespent not found');
837 } elseif ($this->task->id !=
$id) {
838 throw new RestException(404,
'Timespent not found in selected task');
859 unset(
$object->barcode_type_code);
860 unset(
$object->barcode_type_label);
861 unset(
$object->barcode_type_coder);
862 unset(
$object->cond_reglement_id);
863 unset(
$object->cond_reglement);
864 unset(
$object->fk_delivery_address);
865 unset(
$object->shipping_method_id);
869 unset(
$object->label_incoterms);
870 unset(
$object->location_incoterms);
875 unset(
$object->mode_reglement_id);
887 unset(
$object->total_localtax1);
888 unset(
$object->total_localtax2);
913 $saving_fk_element =
$object->fk_element;
920 unset(
$object->barcode_type_code);
921 unset(
$object->barcode_type_label);
922 unset(
$object->barcode_type_coder);
923 unset(
$object->cond_reglement_id);
924 unset(
$object->cond_reglement);
925 unset(
$object->fk_delivery_address);
926 unset(
$object->shipping_method_id);
929 unset(
$object->label_incoterms);
930 unset(
$object->location_incoterms);
935 unset(
$object->mode_reglement_id);
943 unset(
$object->actiontypecode);
944 unset(
$object->array_languages);
948 unset(
$object->cond_reglement_supplier_id);
951 unset(
$object->contacts_ids_internal);
953 unset(
$object->date_validation);
954 unset(
$object->demand_reason_id);
955 unset(
$object->deposit_percent);
958 unset(
$object->fk_multicurrency);
964 unset(
$object->linkedObjectsIds);
966 unset(
$object->multicurrency_code);
967 unset(
$object->multicurrency_total_ht);
968 unset(
$object->multicurrency_total_localtax1);
969 unset(
$object->multicurrency_total_localtax2);
970 unset(
$object->multicurrency_total_ttc);
971 unset(
$object->multicurrency_total_tva);
972 unset(
$object->multicurrency_tx);
979 unset(
$object->retained_warranty_fk_cond_reglement);
981 unset(
$object->shipping_method);
987 unset(
$object->transport_mode_id);
990 unset(
$object->user_closing_id);
992 unset(
$object->user_creation_id);
993 unset(
$object->user_modification);
994 unset(
$object->user_modification_id);
996 unset(
$object->user_validation);
997 unset(
$object->user_validation_id);
1002 unset(
$object->total_localtax1);
1003 unset(
$object->total_localtax2);
1008 if (!
$object->date_creation) {
1011 if (!
$object->date_modification) {
1015 $object->fk_element = $saving_fk_element;
1031 if ($data ===
null) {
1035 foreach (self::$FIELDS as $field) {
1036 if (!isset($data[$field])) {
1037 throw new RestException(400,
"$field field missing");
1039 $object[$field] = $data[$field];
1061 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1062 throw new RestException(403);
1065 $result = $this->task->fetch(
$id);
1067 throw new RestException(404,
'Task not found');
1071 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1074 $contacts = $this->task->liste_contact(-1,
'external', 0, $type);
1075 $socpeoples = $this->task->liste_contact(-1,
'internal', 0, $type);
1077 $contacts = array_merge($contacts, $socpeoples);
1102 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
1104 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1105 throw new RestException(403);
1108 $result = $this->task->fetch(
$id);
1110 throw new RestException(404,
'Task not found');
1114 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1117 $result = $this->task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1119 throw new RestException(500,
'Error : ' . $this->task->error);
1122 $result = $this->task->fetch(
$id);
1124 throw new RestException(404,
'Task not found');
1149 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1150 throw new RestException(403);
1153 $result = $this->task->fetch(
$id);
1155 throw new RestException(404,
'Task not found');
1159 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1162 foreach (array(
'internal',
'external') as $source) {
1163 $contacts = $this->task->liste_contact(-1, $source);
1165 foreach ($contacts as $contact) {
1166 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
1167 $result = $this->task->delete_contact($contact[
'rowid']);
1169 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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
_cleanObjectDatas($object)
Clean sensitive object data @phpstan-template T.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $sqlfilters='', $properties='', $pagination_data=false, $includetimespent=0)
List tasks.
_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.
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.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
forgeSQLFromUniversalSearchCriteria
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.