From b7b322e6612d99451faefab99e1379abee4eee61 Mon Sep 17 00:00:00 2001 From: mc2contributor Date: Thu, 27 Apr 2023 11:46:39 -0600 Subject: [PATCH 1/7] API endpoint to update timespent record --- htdocs/projet/class/api_tasks.class.php | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/htdocs/projet/class/api_tasks.class.php b/htdocs/projet/class/api_tasks.class.php index 64690f90950..e77dea9e483 100644 --- a/htdocs/projet/class/api_tasks.class.php +++ b/htdocs/projet/class/api_tasks.class.php @@ -516,6 +516,7 @@ class Tasks extends DolibarrApi * @param string $note Note * * @url POST {id}/addtimespent + * NOTE: Should be "POST {id}/timespent", since POST already implies "add" * * @return array */ @@ -562,6 +563,64 @@ class Tasks extends DolibarrApi ); } + /** + * Update time spent for a task of a project. + * You can test this API with the following input message + * { "date": "2016-12-31 23:15:00", "duration": 1800, "user_id": 1, "note": "My time test" } + * + * @param int $id Task ID + * @param int $timespent_id Time spent ID (llx_projet_task_time.rowid) + * @param datetime $date Date (YYYY-MM-DD HH:MI:SS in GMT) + * @param int $duration Duration in seconds (3600 = 1h) + * @param int $user_id User (Use 0 for connected user) + * @param string $note Note + * + * @url PUT {id}/timespent/{timespent_id} + * + * @return array + */ + public function putTimeSpent($id, $timespent_id, $date, $duration, $user_id = 0, $note = '') + { + if (!DolibarrApiAccess::$user->rights->projet->creer) { + throw new RestException(401); + } + if ($this->task->fetch($id) <= 0) { + throw new RestException(404, 'Task not found'); + } + if ($this->task->fetchTimeSpent($timespent_id) <= 0) { + throw new RestException(404, 'Timespent not found'); + } + elseif ($this->task->id != $id) { + throw new RestException(404, 'Timespent not found in selected task'); + } + + if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + $newdate = dol_stringtotime($date, 1); + $this->task->timespent_date = $newdate; + $this->task->timespent_datehour = $newdate; + $this->task->timespent_withhour = 1; + $this->task->timespent_duration = $duration; + $this->task->timespent_fk_user = $user_id ?? DolibarrApiAccess::$user->id; + $this->task->timespent_note = $note; + + $result = $this->task->updateTimeSpent(DolibarrApiAccess::$user, 0); + if ($result == 0) { + throw new RestException(304, 'Error nothing done.'); + } + if ($result < 0) { + throw new RestException(500, 'Error when updating time spent: '.$this->task->error); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Time spent updated' + ) + ); + } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** From a9775a4413b63b072836de89517604cd9713f218 Mon Sep 17 00:00:00 2001 From: mc2contributor Date: Thu, 27 Apr 2023 11:49:18 -0600 Subject: [PATCH 2/7] API endpoint to delete timespent record --- htdocs/projet/class/api_tasks.class.php | 55 +++++++++++++++++++++---- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/htdocs/projet/class/api_tasks.class.php b/htdocs/projet/class/api_tasks.class.php index e77dea9e483..c239de3709e 100644 --- a/htdocs/projet/class/api_tasks.class.php +++ b/htdocs/projet/class/api_tasks.class.php @@ -584,15 +584,7 @@ class Tasks extends DolibarrApi if (!DolibarrApiAccess::$user->rights->projet->creer) { throw new RestException(401); } - if ($this->task->fetch($id) <= 0) { - throw new RestException(404, 'Task not found'); - } - if ($this->task->fetchTimeSpent($timespent_id) <= 0) { - throw new RestException(404, 'Timespent not found'); - } - elseif ($this->task->id != $id) { - throw new RestException(404, 'Timespent not found in selected task'); - } + $this->_timespentRecordChecks($id, $timespent_id); if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); @@ -622,6 +614,51 @@ class Tasks extends DolibarrApi ); } + /** + * Delete time spent for a task of a project. + * + * @param int $id Task ID + * @param int $timespent_id Time spent ID (llx_projet_task_time.rowid) + * + * @url DELETE {id}/timespent/{timespent_id} + * + * @return array + */ + public function deleteTimeSpent($id, $timespent_id, $user_id) + { + if (!DolibarrApiAccess::$user->rights->projet->supprimer) { + throw new RestException(401); + } + $this->_timespentRecordChecks($id, $timespent_id); + + if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + if ($this->task->delTimeSpent(DolibarrApiAccess::$user, 0) < 0) { + throw new RestException(500, 'Error when deleting time spent: '.$this->task->error); + } + + return array( + 'success' => array( + 'code' => 200, + 'message' => 'Time spent deleted' + ) + ); + } + + protected function _timespentRecordChecks($id, $timespent_id) { + if ($this->task->fetch($id) <= 0) { + throw new RestException(404, 'Task not found'); + } + if ($this->task->fetchTimeSpent($timespent_id) <= 0) { + throw new RestException(404, 'Timespent not found'); + } + elseif ($this->task->id != $id) { + throw new RestException(404, 'Timespent not found in selected task'); + } + } + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore /** * Clean sensible object datas From 034f1167fde30d45fa95408e102bc38d83ca3c9d Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Thu, 27 Apr 2023 18:51:33 +0000 Subject: [PATCH 3/7] Fixing style errors. --- htdocs/projet/class/api_tasks.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/projet/class/api_tasks.class.php b/htdocs/projet/class/api_tasks.class.php index c239de3709e..2fb1908ddc7 100644 --- a/htdocs/projet/class/api_tasks.class.php +++ b/htdocs/projet/class/api_tasks.class.php @@ -647,14 +647,14 @@ class Tasks extends DolibarrApi ); } - protected function _timespentRecordChecks($id, $timespent_id) { + protected function _timespentRecordChecks($id, $timespent_id) + { if ($this->task->fetch($id) <= 0) { throw new RestException(404, 'Task not found'); } if ($this->task->fetchTimeSpent($timespent_id) <= 0) { throw new RestException(404, 'Timespent not found'); - } - elseif ($this->task->id != $id) { + } elseif ($this->task->id != $id) { throw new RestException(404, 'Timespent not found in selected task'); } } From 63d380bde04722eeb28eb9500c0705465fe52a32 Mon Sep 17 00:00:00 2001 From: Thibault Fiacre <57494317+atm-thibaultf@users.noreply.github.com> Date: Fri, 28 Apr 2023 12:22:30 +0200 Subject: [PATCH 4/7] Fix for deleting loan payment --- htdocs/loan/payment/card.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/loan/payment/card.php b/htdocs/loan/payment/card.php index 309e0d99f4a..b291b313e43 100644 --- a/htdocs/loan/payment/card.php +++ b/htdocs/loan/payment/card.php @@ -223,7 +223,7 @@ print '
'; if (empty($action) && !empty($user->rights->loan->delete)) { if (!$disable_delete) { - print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', 1); + print dolGetButtonAction($langs->trans("Delete"), '', 'delete', $_SERVER["PHP_SELF"].'?id='.$id.'&action=delete&token='.newToken(), 'delete', 1); } else { print dolGetButtonAction($langs->trans("CantRemovePaymentWithOneInvoicePaid"), $langs->trans("Delete"), 'delete', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=delete&token='.newToken(), 'delete', 0); } From bb36267be13fa94da779228f25d283ca1ac8ef54 Mon Sep 17 00:00:00 2001 From: VESSILLER Date: Fri, 28 Apr 2023 16:58:16 +0200 Subject: [PATCH 5/7] FIX edit field value of url --- htdocs/core/class/html.form.class.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index 95572a5c443..ef87b5f5081 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -246,7 +246,7 @@ class Form if (empty($notabletag)) { $ret .= ''; } - if (preg_match('/^(string|safehtmlstring|email)/', $typeofdata)) { + if (preg_match('/^(string|safehtmlstring|email|url)/', $typeofdata)) { $tmp = explode(':', $typeofdata); $ret .= ''; } elseif (preg_match('/^(integer)/', $typeofdata)) { @@ -320,6 +320,8 @@ class Form } else { if (preg_match('/^(email)/', $typeofdata)) { $ret .= dol_print_email($value, 0, 0, 0, 0, 1); + } elseif (preg_match('/^url/', $typeofdata)) { + $ret .= dol_print_url($value, '_blank', 32, 1); } elseif (preg_match('/^(amount|numeric)/', $typeofdata)) { $ret .= ($value != '' ? price($value, '', $langs, 0, -1, -1, $conf->currency) : ''); } elseif (preg_match('/^(checkbox)/', $typeofdata)) { From 7a8fd37a4a28562d7644bcc11f39f939a9e1b789 Mon Sep 17 00:00:00 2001 From: mc2contributor Date: Fri, 28 Apr 2023 10:07:46 -0600 Subject: [PATCH 6/7] Fix issues identified by CI in pull request. --- htdocs/projet/class/api_tasks.class.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/htdocs/projet/class/api_tasks.class.php b/htdocs/projet/class/api_tasks.class.php index 2fb1908ddc7..ada4b220020 100644 --- a/htdocs/projet/class/api_tasks.class.php +++ b/htdocs/projet/class/api_tasks.class.php @@ -584,7 +584,7 @@ class Tasks extends DolibarrApi if (!DolibarrApiAccess::$user->rights->projet->creer) { throw new RestException(401); } - $this->_timespentRecordChecks($id, $timespent_id); + $this->timespentRecordChecks($id, $timespent_id); if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); @@ -624,12 +624,12 @@ class Tasks extends DolibarrApi * * @return array */ - public function deleteTimeSpent($id, $timespent_id, $user_id) + public function deleteTimeSpent($id, $timespent_id) { if (!DolibarrApiAccess::$user->rights->projet->supprimer) { throw new RestException(401); } - $this->_timespentRecordChecks($id, $timespent_id); + $this->timespentRecordChecks($id, $timespent_id); if (!DolibarrApi::_checkAccessToResource('task', $this->task->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); @@ -647,7 +647,14 @@ class Tasks extends DolibarrApi ); } - protected function _timespentRecordChecks($id, $timespent_id) + /** + * Validate task & timespent IDs for timespent API methods. + * Loads the selected task & timespent records. + * + * @param int $id Task ID + * @param int $timespent_id Time spent ID (llx_projet_task_time.rowid) + */ + protected function timespentRecordChecks($id, $timespent_id) { if ($this->task->fetch($id) <= 0) { throw new RestException(404, 'Task not found'); From 8e0e134dc5d61b4af1b17aa083f368faf73b0d4b Mon Sep 17 00:00:00 2001 From: mc2contributor Date: Fri, 28 Apr 2023 10:20:48 -0600 Subject: [PATCH 7/7] Add "@return void" to satisfy CI check --- htdocs/projet/class/api_tasks.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/htdocs/projet/class/api_tasks.class.php b/htdocs/projet/class/api_tasks.class.php index ada4b220020..80fede57563 100644 --- a/htdocs/projet/class/api_tasks.class.php +++ b/htdocs/projet/class/api_tasks.class.php @@ -653,6 +653,8 @@ class Tasks extends DolibarrApi * * @param int $id Task ID * @param int $timespent_id Time spent ID (llx_projet_task_time.rowid) + * + * @return void */ protected function timespentRecordChecks($id, $timespent_id) {