23use Luracast\Restler\RestException;
25require_once DOL_DOCUMENT_ROOT.
'/projet/class/task.class.php';
26require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
27require_once DOL_DOCUMENT_ROOT.
'/core/class/timespent.class.php';
40 public static $FIELDS = array(
58 $this->task =
new Task($this->db);
72 public function get(
$id, $includetimespent = 0)
74 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
75 throw new RestException(403);
78 $result = $this->task->fetch(
$id);
80 throw new RestException(404,
'Task not found');
84 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
87 if ($includetimespent == 1) {
88 $timespent = $this->task->getSummaryOfTimeSpent(0);
90 if ($includetimespent == 2) {
91 $timespent = $this->task->fetchTimeSpentOnTask();
114 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $sqlfilters =
'', $properties =
'')
118 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
119 throw new RestException(403);
125 $socids = DolibarrApiAccess::$user->socid ?: 0;
129 if (!DolibarrApiAccess::$user->hasRight(
'societe',
'client',
'voir') && !$socids) {
130 $search_sale = DolibarrApiAccess::$user->id;
133 $sql =
"SELECT t.rowid";
134 $sql .=
" FROM " . MAIN_DB_PREFIX .
"projet_task AS t";
135 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"projet_task_extrafields AS ef ON (ef.fk_object = t.rowid)";
136 $sql .=
" INNER JOIN " . MAIN_DB_PREFIX .
"projet AS p ON p.rowid = t.fk_projet";
137 $sql .=
' WHERE t.entity IN (' .
getEntity(
'project') .
')';
139 $sql .=
" AND t.fk_soc IN (" . $this->db->sanitize((
string) $socids) .
")";
142 if ($search_sale && $search_sale !=
'-1') {
143 if ($search_sale == -2) {
144 $sql .=
" AND NOT EXISTS (SELECT sc.fk_soc FROM " . MAIN_DB_PREFIX .
"societe_commerciaux as sc WHERE sc.fk_soc = p.fk_soc)";
145 } elseif ($search_sale > 0) {
146 $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) .
")";
154 throw new RestException(400,
'Error when validating parameter sqlfilters -> ' . $errormessage);
158 $sql .= $this->db->order($sortfield, $sortorder);
163 $offset = $limit * $page;
165 $sql .= $this->db->plimit($limit + 1, $offset);
169 $result = $this->db->query($sql);
172 $num = $this->db->num_rows($result);
173 $min = min($num, ($limit <= 0 ? $num : $limit));
176 $obj = $this->db->fetch_object($result);
177 $task_static =
new Task($this->db);
178 if ($task_static->fetch($obj->rowid)) {
184 throw new RestException(503,
'Error when retrieve task list : ' . $this->db->lasterror());
198 public function post($request_data =
null)
201 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
202 throw new RestException(403,
"Insufficiant rights");
205 $result = $this->
_validate($request_data);
207 foreach ($request_data as $field => $value) {
208 if ($field ===
'caller') {
210 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
214 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
225 if ($this->task->ref == -1 || $this->task->ref ===
'auto') {
233 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
234 foreach ($dirmodels as $reldir) {
235 $file =
dol_buildpath($reldir.
"core/modules/project/task/".$modele.
'.php', 0);
236 if (file_exists($file)) {
238 $classname = $modele;
242 if ($filefound && !empty($classname)) {
243 $result =
dol_include_once($reldir .
"core/modules/project/task/" . $modele .
'.php');
244 if ($result !==
false && class_exists($classname)) {
245 $modTask =
new $classname();
246 '@phan-var-force ModeleNumRefTask $modTask';
247 $defaultref = $modTask->getNextValue(
null, $this->task);
249 dol_syslog(
"Failed to include module file or invalid classname: " . $reldir .
"core/modules/project/task/" . $modele .
'.php', LOG_ERR);
252 dol_syslog(
"Module file not found or classname is empty: " . $modele, LOG_ERR);
255 if (is_numeric($defaultref) && $defaultref <= 0) {
259 if (empty($defaultref)) {
263 $this->task->ref = $defaultref;
266 if ($this->task->create(DolibarrApiAccess::$user) < 0) {
267 throw new RestException(500,
"Error creating task", array_merge(array($this->task->error), $this->task->errors));
270 return $this->task->id;
283 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
284 throw new RestException(403);
287 $result = $this->task->fetch(
$id);
289 throw new RestException(404,
'Task not found');
293 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
296 $this->task->fetchTimeSpentOnTask();
299 foreach ($this->task->lines as $line) {
321 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
322 throw new RestException(403);
325 $result = $this->task->fetch(
$id);
327 throw new RestException(404,
'Task not found');
331 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
334 $usert = DolibarrApiAccess::$user;
336 $usert =
new User($this->db);
337 $usert->fetch($userid);
339 $this->task->roles = $this->task->getUserRolesForProjectsOrTasks(
null, $usert,
'0',
$id);
341 foreach ($this->task->roles as $line) {
492 public function put(
$id, $request_data =
null)
494 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
495 throw new RestException(403);
498 $result = $this->task->fetch(
$id);
500 throw new RestException(404,
'Task not found');
504 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
506 foreach ($request_data as $field => $value) {
507 if ($field ==
'id') {
510 if ($field ===
'caller') {
512 $this->task->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
515 if ($field ==
'array_options' && is_array($value)) {
516 foreach ($value as $index => $val) {
517 $this->task->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->task);
522 $this->task->$field = $this->
_checkValForAPI($field, $value, $this->task);
525 if ($this->task->update(DolibarrApiAccess::$user) > 0) {
526 return $this->
get(
$id);
528 throw new RestException(500, $this->task->error);
542 public function delete(
$id)
544 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
545 throw new RestException(403);
547 $result = $this->task->fetch(
$id);
549 throw new RestException(404,
'Task not found');
553 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
556 if ($this->task->delete(DolibarrApiAccess::$user) <= 0) {
557 throw new RestException(500,
'Error when delete task : ' . $this->task->error);
563 'message' =>
'Task deleted'
582 dol_syslog(
"API Rest request::getTimeSpent", LOG_DEBUG);
583 if (! DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
584 throw new RestException(403);
587 $taskresult = $this->task->fetch(
$id);
589 throw new RestException(404,
'Task with id='.
$id.
' not found');
592 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
596 $timeresult = $timespent->fetch($timespent_id);
598 throw new RestException(404,
'Timespent with id='.$timespent_id.
' not found');
601 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
628 public function addTimeSpent(
$id, $date, $duration, $product_id =
null, $user_id = 0, $note =
'', $progress = -1)
630 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
631 throw new RestException(403);
633 $result = $this->task->fetch(
$id);
635 throw new RestException(404,
'Task not found');
639 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
644 $uid = DolibarrApiAccess::$user->id;
649 $this->task->timespent_date = $newdate;
650 $this->task->timespent_datehour = $newdate;
651 $this->task->timespent_withhour = 1;
652 $this->task->timespent_duration = $duration;
653 $this->task->timespent_fk_product = $product_id;
654 $this->task->timespent_fk_user = $uid;
655 $this->task->timespent_note = $note;
656 if (!empty($progress) && $progress >= 0 && $progress <= 100) {
657 $this->task->progress = $progress;
660 $result = $this->task->addTimeSpent(DolibarrApiAccess::$user, 0);
662 throw new RestException(304,
'Error nothing done. May be object is already validated');
665 throw new RestException(500,
'Error when adding time: ' . $this->task->error);
671 'message' =>
'Time spent added'
696 public function putTimeSpent(
$id, $timespent_id, $date, $duration, $product_id =
null, $user_id = 0, $note =
'')
698 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
699 throw new RestException(403);
704 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
708 $this->task->timespent_date = $newdate;
709 $this->task->timespent_datehour = $newdate;
710 $this->task->timespent_withhour = 1;
711 $this->task->timespent_duration = $duration;
712 $this->task->timespent_fk_product = $product_id;
713 $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id;
714 $this->task->timespent_note = $note;
716 $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0);
718 throw new RestException(304,
'Error nothing done.');
721 throw new RestException(500,
'Error when updating time spent: ' . $this->task->error);
727 'message' =>
'Time spent updated'
746 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'supprimer')) {
747 throw new RestException(403);
752 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
755 if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) {
756 throw new RestException(500,
'Error when deleting time spent: ' . $this->task->error);
762 'message' =>
'Time spent deleted'
778 dol_syslog(
"API Rest request::timespentRecordChecks", LOG_DEBUG);
779 if ($this->task->fetch(
$id) <= 0) {
780 throw new RestException(404,
'Task not found');
782 if ($this->task->fetchTimeSpent($timespent_id) <= 0) {
783 throw new RestException(404,
'Timespent not found');
784 } elseif ($this->task->id !=
$id) {
785 throw new RestException(404,
'Timespent not found in selected task');
806 unset(
$object->barcode_type_code);
807 unset(
$object->barcode_type_label);
808 unset(
$object->barcode_type_coder);
809 unset(
$object->cond_reglement_id);
810 unset(
$object->cond_reglement);
811 unset(
$object->fk_delivery_address);
812 unset(
$object->shipping_method_id);
816 unset(
$object->label_incoterms);
817 unset(
$object->location_incoterms);
822 unset(
$object->mode_reglement_id);
834 unset(
$object->total_localtax1);
835 unset(
$object->total_localtax2);
860 $saving_fk_element =
$object->fk_element;
867 unset(
$object->barcode_type_code);
868 unset(
$object->barcode_type_label);
869 unset(
$object->barcode_type_coder);
870 unset(
$object->cond_reglement_id);
871 unset(
$object->cond_reglement);
872 unset(
$object->fk_delivery_address);
873 unset(
$object->shipping_method_id);
876 unset(
$object->label_incoterms);
877 unset(
$object->location_incoterms);
882 unset(
$object->mode_reglement_id);
890 unset(
$object->actiontypecode);
891 unset(
$object->array_languages);
895 unset(
$object->cond_reglement_supplier_id);
898 unset(
$object->contacts_ids_internal);
900 unset(
$object->date_validation);
901 unset(
$object->demand_reason_id);
902 unset(
$object->deposit_percent);
905 unset(
$object->fk_multicurrency);
911 unset(
$object->linkedObjectsIds);
913 unset(
$object->multicurrency_code);
914 unset(
$object->multicurrency_total_ht);
915 unset(
$object->multicurrency_total_localtax1);
916 unset(
$object->multicurrency_total_localtax2);
917 unset(
$object->multicurrency_total_ttc);
918 unset(
$object->multicurrency_total_tva);
919 unset(
$object->multicurrency_tx);
926 unset(
$object->retained_warranty_fk_cond_reglement);
928 unset(
$object->shipping_method);
934 unset(
$object->transport_mode_id);
937 unset(
$object->user_closing_id);
939 unset(
$object->user_creation_id);
940 unset(
$object->user_modification);
941 unset(
$object->user_modification_id);
943 unset(
$object->user_validation);
944 unset(
$object->user_validation_id);
949 unset(
$object->total_localtax1);
950 unset(
$object->total_localtax2);
958 if (!
$object->date_modification) {
962 $object->fk_element = $saving_fk_element;
978 if ($data ===
null) {
982 foreach (self::$FIELDS as $field) {
983 if (!isset($data[$field])) {
984 throw new RestException(400,
"$field field missing");
986 $object[$field] = $data[$field];
1006 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'lire')) {
1007 throw new RestException(403);
1010 $result = $this->task->fetch(
$id);
1012 throw new RestException(404,
'Task not found');
1016 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1019 $contacts = $this->task->liste_contact(-1,
'external', 0, $type);
1020 $socpeoples = $this->task->liste_contact(-1,
'internal', 0, $type);
1022 $contacts = array_merge($contacts, $socpeoples);
1045 public function addContact(
$id, $fk_socpeople, $type_contact, $source, $notrigger = 0)
1047 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1048 throw new RestException(403);
1051 $result = $this->task->fetch(
$id);
1053 throw new RestException(404,
'Task not found');
1057 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1060 $result = $this->task->add_contact($fk_socpeople, $type_contact, $source, $notrigger);
1062 throw new RestException(500,
'Error : ' . $this->task->error);
1065 $result = $this->task->fetch(
$id);
1067 throw new RestException(404,
'Task not found');
1090 if (!DolibarrApiAccess::$user->hasRight(
'projet',
'creer')) {
1091 throw new RestException(403);
1094 $result = $this->task->fetch(
$id);
1096 throw new RestException(404,
'Task not found');
1100 throw new RestException(403,
'Access not allowed for login ' . DolibarrApiAccess::$user->login);
1103 foreach (array(
'internal',
'external') as $source) {
1104 $contacts = $this->task->liste_contact(-1, $source);
1106 foreach ($contacts as $contact) {
1107 if ($contact[
'id'] == $contactid && $contact[
'code'] == $type) {
1108 $result = $this->task->delete_contact($contact[
'rowid']);
1110 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.
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...
dol_now($mode='gmt')
Return date for now.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
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.