From c69f21f5245cff29932569de27be991ee36b0913 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Tue, 12 Nov 2019 10:00:55 -0300 Subject: [PATCH] Fix create, delete and drop orphan tags --- controller/notecontroller.php | 27 +++++++++++++++++++-------- css/style.css | 4 ++++ db/tagmapper.php | 5 +++++ js/qn-dialogs.js | 17 ++++++++++++----- js/script.js | 10 ++++++++-- 5 files changed, 48 insertions(+), 15 deletions(-) diff --git a/controller/notecontroller.php b/controller/notecontroller.php index 4fb0919..4710cae 100644 --- a/controller/notecontroller.php +++ b/controller/notecontroller.php @@ -145,6 +145,7 @@ class NoteController extends Controller { * @param int $id * @param string $title * @param string $content + * @param array $tags * @param string $color */ public function update($id, $title, $content, $tags, $color = "#F7EB96") { @@ -166,24 +167,31 @@ class NoteController extends Controller { } // Delete old tag relations - $noteTags = $this->tagmapper->getTagsForNote($this->userId, $id); - foreach ($noteTags as $tag) { - if (!in_array($tag->getName(), $tags)) { - $hnotetag = $this->notetagmapper->findNoteTag($this->userId, $id, $tag->getId()); + $dbTags = $this->tagmapper->getTagsForNote($this->userId, $id); + foreach ($dbTags as $dbTag) { + $delete = true; + foreach ($tags as $tag) { + if ($dbTag->getId() == $tag['id']) { + $delete = false; + break; + } + } + if ($delete) { + $hnotetag = $this->notetagmapper->findNoteTag($this->userId, $id, $dbTag->getId()); $this->notetagmapper->delete($hnotetag); } } // Add new tags and update relations. - foreach ($tags as $name) { - if (!$this->tagmapper->tagExists($this->userId, $name)) { + foreach ($tags as $tag) { + if (!$this->tagmapper->tagExists($this->userId, $tag['name'])) { $htag = new Tag(); - $htag->setName($name); + $htag->setName($tag['name']); $htag->setUserId($this->userId); $htag = $this->tagmapper->insert($htag); } else { - $htag = $this->tagmapper->getTag($this->userId, $name); + $htag = $this->tagmapper->getTag($this->userId, $tag['name']); } if (!$this->notetagmapper->noteTagExists($this->userId, $id, $htag->getId())) { @@ -195,6 +203,9 @@ class NoteController extends Controller { } } + // Purge orphan tags. + $this->tagmapper->dropOld(); + // Set new info on Note $note->setTitle($title); $note->setContent($content); diff --git a/css/style.css b/css/style.css index 8558914..4cf138c 100644 --- a/css/style.css +++ b/css/style.css @@ -240,6 +240,10 @@ div[contenteditable="true"] { display: block !important; } +.select2-results .select2-selected { + display: none !important; +} + .select2-container { min-width: 200px; } diff --git a/db/tagmapper.php b/db/tagmapper.php index 4cf5927..b4a866f 100644 --- a/db/tagmapper.php +++ b/db/tagmapper.php @@ -43,4 +43,9 @@ class TagMapper extends Mapper { return true; } + public function dropOld () { + $sql = 'DELETE FROM *PREFIX*quicknotes_tags WHERE '; + $sql.= 'id NOT IN (SELECT tag_id FROM *PREFIX*quicknotes_note_tags)'; + return $this->execute($sql, []); + } } \ No newline at end of file diff --git a/js/qn-dialogs.js b/js/qn-dialogs.js index 9f702e5..2260cbd 100644 --- a/js/qn-dialogs.js +++ b/js/qn-dialogs.js @@ -46,12 +46,19 @@ const QnDialogs = { input.select2({ placeholder: t('quicknotes', 'Enter tag name'), tokenSeparators: ',', - tags: currentTags.map(function (value) { return value.name; }), + multiple: false, allowClear: true, - toggleSelect: true + toggleSelect: true, + tags : function () { + var data = []; + currentTags.forEach(function (item, index) { + data.push({id: item.id, text: item.name}); + }); + return data; + } }); - input.val(selectedTags.map(function (value) { return value.name; })); + input.val(selectedTags.map(function (value) { return value.id; })); input.trigger("change"); // wrap callback in _.once(): @@ -66,7 +73,7 @@ const QnDialogs = { click: function () { input.select2('close'); if (callback !== undefined) { - callback(false, input.select2("val")); + callback(false, input.select2("data")); } $(dialogId).ocdialog('close'); } @@ -75,7 +82,7 @@ const QnDialogs = { click: function () { input.select2('close'); if (callback !== undefined) { - callback(true, input.select2("val")); + callback(true, input.select2("data")); } $(dialogId).ocdialog('close'); }, diff --git a/js/script.js b/js/script.js index 3dc3ebe..40c6623 100644 --- a/js/script.js +++ b/js/script.js @@ -330,7 +330,10 @@ View.prototype = { var content = $('#modal-note-div #content-editable').html().trim(); var color = this.colorToHex($("#modal-note-div .quicknote").css("background-color")); var tags = $("#modal-note-div .slim-tag").toArray().map(function (value) { - return value.textContent.trim(); + return { + id: value.getAttribute('data-id'), + name: value.textContent.trim() + }; }); /* @@ -352,6 +355,7 @@ View.prototype = { var modalnote = $("#modal-note-div .quicknote"); var modaltitle = $('#modal-note-div #title-editable'); var modalcontent = $('#modal-note-div #content-editable'); + var modaltags = $('#modal-note-div .note-tags'); self._notes.unsetActive(); @@ -361,6 +365,7 @@ View.prototype = { modalnote.data('id', -1); modaltitle.html(""); modalcontent.html(""); + modaltags.html(""); self.render(); }).fail(function () { @@ -632,7 +637,8 @@ View.prototype = { var modalTags = $('#modal-note-div .note-tags'); modalTags.html(''); newTags.forEach(function (item, index) { - var tag = $('
' + item + '
'); + var noteId = parseInt(item.id) || -1; + var tag = $('
' + item.text + '
'); modalTags.append(tag); }); }