From 020d850a3a1990dcf7a34304760d2f555bb517a3 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 4 Jun 2016 16:25:57 +0930 Subject: [PATCH] Disassociate child tag when parent is deleted. fixes flarum/core#962 --- extensions/tags/js/admin/dist/extension.js | 23 +++++++++++++++---- .../js/admin/src/components/EditTagModal.js | 11 ++++++++- .../tags/src/Command/DeleteTagHandler.php | 4 ++++ extensions/tags/src/Tag.php | 2 +- extensions/tags/src/TagRepository.php | 10 ++++++++ 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/extensions/tags/js/admin/dist/extension.js b/extensions/tags/js/admin/dist/extension.js index d401386d2..802bfe51d 100644 --- a/extensions/tags/js/admin/dist/extension.js +++ b/extensions/tags/js/admin/dist/extension.js @@ -793,11 +793,26 @@ System.register('flarum/tags/components/EditTagModal', ['flarum/components/Modal }, { key: 'delete', value: function _delete() { + var _this4 = this; + if (confirm(app.translator.trans('flarum-tags.admin.edit_tag.delete_tag_confirmation'))) { - this.tag.delete().then(function () { - return m.redraw(); - }); - this.hide(); + (function () { + var children = app.store.all('tags').filter(function (tag) { + return tag.parent() === _this4.tag; + }); + + _this4.tag.delete().then(function () { + children.forEach(function (tag) { + return tag.pushData({ + attributes: { isChild: false }, + relationships: { parent: null } + }); + }); + m.redraw(); + }); + + _this4.hide(); + })(); } } }]); diff --git a/extensions/tags/js/admin/src/components/EditTagModal.js b/extensions/tags/js/admin/src/components/EditTagModal.js index 2d762c5ef..ef45c364f 100644 --- a/extensions/tags/js/admin/src/components/EditTagModal.js +++ b/extensions/tags/js/admin/src/components/EditTagModal.js @@ -110,7 +110,16 @@ export default class EditTagModal extends Modal { delete() { if (confirm(app.translator.trans('flarum-tags.admin.edit_tag.delete_tag_confirmation'))) { - this.tag.delete().then(() => m.redraw()); + const children = app.store.all('tags').filter(tag => tag.parent() === this.tag); + + this.tag.delete().then(() => { + children.forEach(tag => tag.pushData({ + attributes: {isChild: false}, + relationships: {parent: null} + })); + m.redraw(); + }); + this.hide(); } } diff --git a/extensions/tags/src/Command/DeleteTagHandler.php b/extensions/tags/src/Command/DeleteTagHandler.php index cac521c40..05a931e0c 100644 --- a/extensions/tags/src/Command/DeleteTagHandler.php +++ b/extensions/tags/src/Command/DeleteTagHandler.php @@ -44,6 +44,10 @@ class DeleteTagHandler $this->assertCan($actor, 'delete', $tag); + $this->tags->query() + ->where('parent_id', $tag->id) + ->update(['parent_id' => null]); + $tag->delete(); return $tag; diff --git a/extensions/tags/src/Tag.php b/extensions/tags/src/Tag.php index fca463692..56336689a 100644 --- a/extensions/tags/src/Tag.php +++ b/extensions/tags/src/Tag.php @@ -143,7 +143,7 @@ class Tag extends AbstractModel foreach ($tags as $tag) { $can = $canForTag($tag); - if ($can && $tag->parent_id) { + if ($can && $tag->parent) { $can = $canForTag($tag->parent); } diff --git a/extensions/tags/src/TagRepository.php b/extensions/tags/src/TagRepository.php index 4cad9deb5..1d4ed6fb8 100644 --- a/extensions/tags/src/TagRepository.php +++ b/extensions/tags/src/TagRepository.php @@ -16,6 +16,16 @@ use Illuminate\Database\Eloquent\Builder; class TagRepository { + /** + * Get a new query builder for the tags table. + * + * @return Builder + */ + public function query() + { + return Tag::query(); + } + /** * Find a tag by ID, optionally making sure it is visible to a certain * user, or throw an exception.