diff --git a/application/controllers/api/frontend/v1/Bookmark.php b/application/controllers/api/frontend/v1/Bookmark.php index 84c5406a9..aa45709d4 100644 --- a/application/controllers/api/frontend/v1/Bookmark.php +++ b/application/controllers/api/frontend/v1/Bookmark.php @@ -18,6 +18,8 @@ if (! defined('BASEPATH')) exit('No direct script access allowed'); +use \DateTime as DateTime; + class Bookmark extends FHCAPI_Controller { @@ -28,7 +30,7 @@ class Bookmark extends FHCAPI_Controller { parent::__construct([ 'getBookmarks' => self::PERM_LOGGED, - 'delete' => self::PERM_LOGGED, + 'delete' => self::PERM_LOGGED, 'insert' => self::PERM_LOGGED, 'update' => self::PERM_LOGGED, 'changeOrder' => self::PERM_LOGGED @@ -38,96 +40,104 @@ class Bookmark extends FHCAPI_Controller $this->uid = getAuthUID(); $this->pid = getAuthPersonID(); - } //------------------------------------------------------------------------------------------------------------------ // Public methods - /** - * gets the bookmarks associated to a user + /** + * gets the bookmarks associated to a user * @access public * @return void */ public function getBookmarks() { - $this->BookmarkModel->addOrder("sort"); + $this->BookmarkModel->addOrder("sort"); $bookmarks = $this->BookmarkModel->loadWhere(["uid"=>$this->uid]); - $bookmarks = $this->getDataOrTerminateWithError($bookmarks); + $bookmarks = $this->getDataOrTerminateWithError($bookmarks); - $this->terminateWithSuccess($bookmarks); - } + $this->terminateWithSuccess($bookmarks); + } - /** - * deletes bookmark from associated user + /** + * deletes bookmark from associated user * @access public * @return void */ - public function delete($bookmark_id) + public function delete($bookmark_id) { - $bookmark = $this->BookmarkModel->load($bookmark_id); + $bookmark = $this->BookmarkModel->load($bookmark_id); - $bookmark = current($this->getDataOrTerminateWithError($bookmark)); + $bookmark = current($this->getDataOrTerminateWithError($bookmark)); - // only delete bookmark if the user is the owner of the bookmark - if($bookmark->uid == $this->uid || $this->permissionlib->isBerechtigt('admin')){ + // only delete bookmark if the user is the owner of the bookmark + if ($bookmark->uid == $this->uid || $this->permissionlib->isBerechtigt('admin')) { + $delete_result = $this->BookmarkModel->delete($bookmark_id); - $delete_result = $this->BookmarkModel->delete($bookmark_id); + $delete_result = $this->getDataOrTerminateWithError($delete_result); - $delete_result = $this->getDataOrTerminateWithError($delete_result); + $this->terminateWithSuccess($delete_result); + } else { + $this->_outputAuthError(['delete' => ['admin:rw']]); + } + } - $this->terminateWithSuccess($delete_result); - }else{ - $this->_outputAuthError(['delete' => ['admin:rw']]); - } - } - - /** - * inserts new bookmark into the bookmark table + /** + * inserts new bookmark into the bookmark table * @access public * @return void */ - public function insert() + public function insert() { - // form validation - $this->load->library('form_validation'); - $this->form_validation->set_rules('url', 'URL', 'required|valid_url|max_length[511]'); - $this->form_validation->set_rules('title', 'Title', 'required|max_length[255]'); - if($this->form_validation->run() == FALSE) $this->terminateWithValidationErrors($this->form_validation->error_array()); + // form validation + $this->load->library('form_validation'); + $this->form_validation->set_rules('url', 'URL', 'required|valid_url|max_length[511]'); + $this->form_validation->set_rules('title', 'Title', 'required|max_length[255]'); + if (!$this->form_validation->run()) + $this->terminateWithValidationErrors($this->form_validation->error_array()); - $url = $this->input->post('url',true); - $title = $this->input->post('title',true); - $tag = $this->input->post('tag', true); + $url = $this->input->post('url', true); + $title = $this->input->post('title', true); + $tag = $this->input->post('tag', true); if (is_array($tag)) { $tag = json_encode($tag); // convert PHP array to JSON string } $sort = $this->input->post('sort', true); - $insert_into_result = $this->BookmarkModel->insert(['uid'=>$this->uid, 'url'=>$url, 'title'=>$title,'tag'=>$tag, 'insertvon'=>$this->uid, 'updateamum'=>NULL, 'updatevon'=>NULL, 'sort'=>$sort,]); + $insert_into_result = $this->BookmarkModel->insert([ + 'uid' => $this->uid, + 'url' => $url, + 'title' => $title, + 'tag' => $tag, + 'insertvon' => $this->uid, + 'updateamum' => null, + 'updatevon' => null, + 'sort' => $sort + ]); - $insert_into_result = $this->getDataOrTerminateWithError($insert_into_result); + $insert_into_result = $this->getDataOrTerminateWithError($insert_into_result); - $this->terminateWithSuccess($insert_into_result); - - } + $this->terminateWithSuccess($insert_into_result); + } /** - * updates bookmark in the bookmark table + * updates bookmark in the bookmark table * @access public * @return void */ - public function update($bookmark_id) + public function update($bookmark_id) { - // form validation - $this->load->library('form_validation'); - $this->form_validation->set_rules('url', 'URL', 'required|valid_url|max_length[511]'); - $this->form_validation->set_rules('title', 'Title', 'required|max_length[255]'); - if($this->form_validation->run() == FALSE) $this->terminateWithValidationErrors($this->form_validation->error_array()); + // form validation + $this->load->library('form_validation'); + $this->form_validation->set_rules('url', 'URL', 'required|valid_url|max_length[511]'); + $this->form_validation->set_rules('title', 'Title', 'required|max_length[255]'); + if (!$this->form_validation->run()) + $this->terminateWithValidationErrors($this->form_validation->error_array()); - $url = $this->input->post('url',true); - $title = $this->input->post('title',true); + $url = $this->input->post('url', true); + $title = $this->input->post('title', true); $tag = $this->input->post('tag', true); if (is_array($tag)) { $tag = json_encode($tag); @@ -136,21 +146,26 @@ class Bookmark extends FHCAPI_Controller $now = new DateTime(); $now = $now->format('Y-m-d H:i:s'); - $update_result = $this->BookmarkModel->update($bookmark_id,['url'=>$url, 'title'=>$title, 'tag'=>$tag, 'updateamum'=>$now]); + $update_result = $this->BookmarkModel->update($bookmark_id, [ + 'url' => $url, + 'title' => $title, + 'tag' => $tag, + 'updateamum' => $now + ]); - $update_result = $this->getDataOrTerminateWithError($update_result); + $update_result = $this->getDataOrTerminateWithError($update_result); - $this->terminateWithSuccess($update_result); - - } + $this->terminateWithSuccess($update_result); + } /** * changes sort of two bookmarks in the bookmark table * @access public * @return void */ - public function changeOrder($bookmark_id1, $bookmark_id2) + public function changeOrder($bookmark_id1, $bookmark_id2) { + $update_result = []; $result1 = $this->BookmarkModel->load($bookmark_id1); $data1 = $this->getDataOrTerminateWithError($result1); @@ -160,13 +175,17 @@ class Bookmark extends FHCAPI_Controller $data2 = $this->getDataOrTerminateWithError($result2); $sort2 = current($data2)->sort; - $update_result1 = $this->BookmarkModel->update($bookmark_id1,['sort'=>$sort2,]); - $update_result[] = $this->getDataOrTerminateWithError($update_result1); + $update_result1 = $this->BookmarkModel->update($bookmark_id1, [ + 'sort' => $sort2 + ]); + $update_result[] = $this->getDataOrTerminateWithError($update_result1); - $update_result2 = $this->BookmarkModel->update($bookmark_id2,['sort'=>$sort1,]); - $update_result[] = $this->getDataOrTerminateWithError($update_result2); + $update_result2 = $this->BookmarkModel->update($bookmark_id2, [ + 'sort' => $sort1 + ]); + $update_result[] = $this->getDataOrTerminateWithError($update_result2); - $this->terminateWithSuccess($update_result); - } + $this->terminateWithSuccess($update_result); + } } diff --git a/public/js/components/DashboardWidget/Url.js b/public/js/components/DashboardWidget/Url.js index 869c75b4d..5493a267f 100644 --- a/public/js/components/DashboardWidget/Url.js +++ b/public/js/components/DashboardWidget/Url.js @@ -7,13 +7,6 @@ import { useUrlStore } from '../../composables/Pseudostore/DashboardWidget/UrlSt export default { name: "WidgetsUrl", - mixins: [AbstractWidget], - inject: { - sectionName: { - type: String, - default: false - } - }, components:{ CoreForm, FormInput, @@ -22,7 +15,9 @@ export default { PvMultiSelect: primevue.multiselect, PvAutoComplete: primevue.autocomplete, }, + mixins: [AbstractWidget], data: () => ({ + ready: false, bookmark_id: null, title_input: "", url_input: "", @@ -31,9 +26,7 @@ export default { invalidTitel: false, }, selectedTags: [], - newTag: null, selectedFilters: [], - filter: [], filteredArray: [] }), computed: { @@ -55,27 +48,27 @@ export default { return tags.some(tag => this.config.tags.includes(tag)); }); }, - newSort(){ - if(this.bookmarks.length == 0) + newSort() { + if (this.bookmarks.length == 0) return 1; else return Math.max(...this.bookmarks.map(b => b.sort)) + 1; }, - maxSort(){ - if(this.bookmarks.length == 0) + maxSort() { + if (this.bookmarks.length == 0) return 0; else return Math.max(...this.filteredBookmarks.map(b => b.sort)); }, - minSort(){ - if(this.bookmarks.length == 0) + minSort() { + if (this.bookmarks.length == 0) return 0; else return Math.min(...this.filteredBookmarks.map(b => b.sort)); } }, methods: { - clearInputs(){ + clearInputs() { this.title_input = ""; this.url_input = ""; this.selectedTags = []; @@ -91,9 +84,9 @@ export default { this.$refs.editModal.show(); }, - editBookmark(event){ - event.preventDefault(); - if(!this.bookmark_id || !this.url_input || !this.title_input) return; + editBookmark() { + if (!this.bookmark_id || !this.url_input || !this.title_input) + return; this.actions .update( @@ -111,10 +104,7 @@ export default { }) .catch(this.$fhcAlert.handleSystemError); }, - - insertBookmark(event) { - event.preventDefault(); - + insertBookmark() { // reset is-invalid css on url input field for (let key of Object.keys(this.validation)) { this.validation[key] = false; @@ -158,16 +148,16 @@ export default { let isConfirmed = await this.$fhcAlert.confirmDelete(); // early return if the confirm dialog was not confirmed - if (!isConfirmed) return; + if (!isConfirmed) + return; - this.actions - .remove(bookmark_id) - .then(() => { - this.$fhcAlert.alertInfo(this.$p.t("bookmark", "bookmarkDeleted")); - }) - .catch(this.$fhcAlert.handleSystemError); + const errors = await this.actions.remove(bookmark_id); + + if (!errors) { + this.$fhcAlert.alertInfo(this.$p.t("bookmark", "bookmarkDeleted")); + } }, - sortDown(bookmark_id){ + sortDown(bookmark_id) { const current = this.filteredBookmarks.find(b => b.bookmark_id === bookmark_id); const next = this.filteredBookmarks @@ -178,9 +168,9 @@ export default { console.log("lowest sort item, no change"); return; } - this.changeOrder(current.bookmark_id, next.bookmark_id); + this.actions.swap(current.bookmark_id, next.bookmark_id); }, - sortUp(bookmark_id){ + sortUp(bookmark_id) { const current = this.filteredBookmarks.find(b => b.bookmark_id === bookmark_id); const next = this.filteredBookmarks @@ -191,10 +181,7 @@ export default { console.log("highest sort item, no change"); return; } - this.changeOrder(current.bookmark_id, next.bookmark_id); - }, - changeOrder(bookmark_id_1, bookmark_id_2) { - this.actions.swap(bookmark_id_1, bookmark_id_2); + this.actions.swap(current.bookmark_id, next.bookmark_id); }, hasTags(link) { if (!link || !link.tag) return false; @@ -211,18 +198,6 @@ export default { return tags.join(' '); } }, - prepareTag(bookmarkArr){ - const parsedArr = Array.isArray(bookmarkArr) - ? bookmarkArr - : JSON.parse(bookmarkArr); - - const result = parsedArr.map(tag => ({ - tag, - code: tag - })); - - return result; - }, openFilterModal() { if (this.config.tags && this.config.tags.length) this.selectedFilters = [ ...this.config.tags ]; @@ -230,7 +205,7 @@ export default { this.selectedFilters = []; this.$refs.filterModal.show(); }, - async handleAddingTagFilter(widgetId) { + async handleAddingTagFilter() { this.config.tags = this.selectedFilters; this.$emit('change'); this.$fhcAlert.alertInfo(this.$p.t("bookmark", "filterUpdated")); @@ -248,7 +223,7 @@ export default { if (this.filteredArray.length === 0 && query) { this.filteredArray = [query]; } - }, + } }, setup() { const { @@ -260,7 +235,8 @@ export default { return { bookmarks, tags, actions } }, async mounted() { - this.actions.fetch(); + await this.actions.fetch(); + this.ready = true; }, template: /*html*/ `
@@ -290,89 +266,87 @@ export default {
- - -
- {{ $p.t('bookmark', 'emptyBookmarks') }} -
-