From 39574c1b3a1f8454bfaaa9647679e42baee7768a Mon Sep 17 00:00:00 2001 From: SimonGschnell Date: Tue, 30 Jul 2024 15:27:24 +0200 Subject: [PATCH] adds delete endpoint for the dashboard bookmark widget --- .../controllers/api/frontend/v1/Bookmark.php | 99 +++++++++++++++++++ .../models/dashboard/Bookmark_model.php | 57 +++++++++++ public/js/api/bookmark.js | 21 ++++ public/js/api/fhcapifactory.js | 2 + public/js/apps/Dashboard/Fhc.js | 3 + public/js/components/DashboardWidget/Url.js | 67 ++++++++++--- system/phrasesupdate.php | 65 +++++++++++- 7 files changed, 300 insertions(+), 14 deletions(-) create mode 100644 application/controllers/api/frontend/v1/Bookmark.php create mode 100644 application/models/dashboard/Bookmark_model.php create mode 100644 public/js/api/bookmark.js diff --git a/application/controllers/api/frontend/v1/Bookmark.php b/application/controllers/api/frontend/v1/Bookmark.php new file mode 100644 index 000000000..c18afc751 --- /dev/null +++ b/application/controllers/api/frontend/v1/Bookmark.php @@ -0,0 +1,99 @@ +. + */ + +if (! defined('BASEPATH')) exit('No direct script access allowed'); + +class Bookmark extends FHCAPI_Controller +{ + + /** + * Object initialization + */ + public function __construct() + { + parent::__construct([ + 'getBookmarks' => self::PERM_LOGGED, + 'delete' => self::PERM_LOGGED, + ]); + + $this->load->model('dashboard/Bookmark_model', 'BookmarkModel'); + + $this->uid = getAuthUID(); + $this->pid = getAuthPersonID(); + + } + + //------------------------------------------------------------------------------------------------------------------ + // Public methods + + + /** + * gets the bookmarks associated to a user + * @access public + * @return void + */ + public function getBookmarks() + { + $bookmarks = $this->BookmarkModel->getAll($this->uid); + + if(isError($bookmarks)){ + $this->terminateWithError(getError($bookmarks)); + } + + $bookmarks = $this->getDataOrTerminateWithError($bookmarks); + + $this->terminateWithSuccess($bookmarks); + } + + /** + * deletes bookmark from associated user + * @access public + * @return void + */ + public function delete($bookmark_id) + { + if(!isset($bookmark_id)) $this->terminateWithError("missing required parameters"); + + $bookmark = $this->BookmarkModel->get($bookmark_id); + + if(isError($bookmark)){ + $this->terminateWithError(getError($bookmark)); + } + + $bookmark = current($this->getDataOrTerminateWithError($bookmark)); + + // only delete bookmark if the user is the owner of the bookmark + $this->load->library('PermissionLib'); + + if($bookmark->uid == $this->uid || $this->permissionlib->isBerechtigt('admin')){ + + $delete_result = $this->BookmarkModel->delete($bookmark_id); + + if(isError($delete_result)){ + $this->terminateWithError(getError($delete_result)); + } + + $delete_result = $this->getDataOrTerminateWithError($delete_result); + + $this->terminateWithSuccess($delete_result); + }else{ + $this->terminateWithError("You are not authorized to delete this bookmark"); + } + } +} + diff --git a/application/models/dashboard/Bookmark_model.php b/application/models/dashboard/Bookmark_model.php new file mode 100644 index 000000000..3b44d055c --- /dev/null +++ b/application/models/dashboard/Bookmark_model.php @@ -0,0 +1,57 @@ +dbTable = 'dashboard.tbl_bookmark'; + $this->pk = 'bookmark_id'; + } + + + /** + * Get Bookmarks of UID. + * @param string user uid + * @return array + */ + public function getAll($uid) + { + return $this->execReadOnlyQuery(" + SELECT * + FROM dashboard.tbl_bookmark + WHERE uid = ? + ",[$uid]); + } + + /** + * Gets Bookmark by bookmark_id. + * @param int $bookmark_id + * @return array + */ + public function get($bookmark_id) + { + return $this->execReadOnlyQuery(" + SELECT * + FROM dashboard.tbl_bookmark + WHERE bookmark_id = ? + ",[$bookmark_id]); + } + + /** + * Gets Bookmark by bookmark_id. + * @param int $bookmark_id + * @return array + */ + public function delete($bookmark_id) + { + return $this->execReadOnlyQuery(" + DELETE + FROM dashboard.tbl_bookmark + WHERE bookmark_id = ? + ",[$bookmark_id]); + } +} diff --git a/public/js/api/bookmark.js b/public/js/api/bookmark.js new file mode 100644 index 000000000..6a6b3c309 --- /dev/null +++ b/public/js/api/bookmark.js @@ -0,0 +1,21 @@ +export default { + + getBookmarks: function (uid) { + return this.$fhcApi.get( + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + `/api/frontend/v1/Bookmark/getBookmarks` + ,{} + ); + }, + + deleteBookmark: function (bookmark_id) { + + return this.$fhcApi.get( + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + `/api/frontend/v1/Bookmark/delete/${bookmark_id}` + ,{} + ); + }, +} \ No newline at end of file diff --git a/public/js/api/fhcapifactory.js b/public/js/api/fhcapifactory.js index 73ce0dd7f..99f154992 100644 --- a/public/js/api/fhcapifactory.js +++ b/public/js/api/fhcapifactory.js @@ -22,6 +22,7 @@ import filter from "./filter.js"; import studstatus from "./studstatus.js"; import profil from "./profil.js"; import profilUpdate from "./profilUpdate.js"; +import bookmark from "./bookmark.js"; export default { search, @@ -31,4 +32,5 @@ export default { studstatus, profil, profilUpdate, + bookmark, }; diff --git a/public/js/apps/Dashboard/Fhc.js b/public/js/apps/Dashboard/Fhc.js index ebcbb4b78..824f03bd2 100755 --- a/public/js/apps/Dashboard/Fhc.js +++ b/public/js/apps/Dashboard/Fhc.js @@ -1,4 +1,6 @@ import FhcDashboard from '../../components/Dashboard/Dashboard.js'; +import Phrasen from "../../plugin/Phrasen.js" +import FhcAlert from "../../plugin/FhcAlert.js" const app = Vue.createApp({ data: () => ({ @@ -9,4 +11,5 @@ const app = Vue.createApp({ } }); app.config.unwrapInjectedRef = true; +app.use(Phrasen).use(FhcAlert); app.mount('#content'); diff --git a/public/js/components/DashboardWidget/Url.js b/public/js/components/DashboardWidget/Url.js index f374b7ed8..23631c88c 100755 --- a/public/js/components/DashboardWidget/Url.js +++ b/public/js/components/DashboardWidget/Url.js @@ -3,7 +3,7 @@ import AbstractWidget from './Abstract'; export default { name: 'WidgetsUrl', data: () => ({ - links: [] + links: null }), mixins: [ AbstractWidget @@ -14,6 +14,14 @@ export default { } }, methods: { + async fetchBookmarks(){ + await this.$fhcApi.factory.bookmark.getBookmarks() + .then(res => res.data) + .then(result => { + this.links = result; + }) + .catch(); + }, addLink(){ let linkId = this.links.length; @@ -24,37 +32,70 @@ export default { url: this.url }) }, - removeLink(linkId){ - let indexToRemove = this.links.findIndex((obj => obj.id === linkId)); - this.links.splice(indexToRemove, 1); + removeLink(bookmark_id){ + this.$fhcApi.factory.bookmark.deleteBookmark(bookmark_id) + .then(res => res.data) + .then(result => { + this.$fhcAlert.alertInfo(this.$p.t('bookmark','bookmarkDeleted')); + }) + .catch(); } }, + created() { - this.links = TEST_LINKS; + //this.links = TEST_LINKS; // this.links = TEST_KEINE_LINKS; }, - template: ` + async mounted(){ + await this.fetchBookmarks(); + }, + template: /*html*/`
-
Neuer Link

+
{{$p.t('bookmark','newLink')}}

- +
-
{{ widgetTag }}
- + + +
` } + +/* +Link JSON structure: +{ + "bookmark_id": number, + "uid": string, + "url": string, + "title": string, + "tag": string, + "insertamum": "2024-07-30 14:33:03.699318", + "insertvon": null, + "updateamum": null, + "updatevon": null +} +*/ + + const TEST_KEINE_LINKS = []; const TEST_LINKS = [ { diff --git a/system/phrasesupdate.php b/system/phrasesupdate.php index 8dc1d163f..688023295 100755 --- a/system/phrasesupdate.php +++ b/system/phrasesupdate.php @@ -27489,7 +27489,70 @@ array( 'insertvon' => 'system' ) ) - ) + ), + // BOOKMARK PHRASEN ---------------------------------------------------------------------- + array( + 'app' => 'core', + 'category' => 'bookmark', + 'phrase' => 'newLink', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Neuer Link', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'New Link', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + array( + 'app' => 'core', + 'category' => 'bookmark', + 'phrase' => 'saveLink', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Link speichern', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'Save link', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + array( + 'app' => 'core', + 'category' => 'bookmark', + 'phrase' => 'bookmarkDeleted', + 'insertvon' => 'system', + 'phrases' => array( + array( + 'sprache' => 'German', + 'text' => 'Link gelöscht', + 'description' => '', + 'insertvon' => 'system' + ), + array( + 'sprache' => 'English', + 'text' => 'Link deleted', + 'description' => '', + 'insertvon' => 'system' + ) + ) + ), + + );