From 1df0a84e67f093157db9f1de586badb1d93babb9 Mon Sep 17 00:00:00 2001 From: pollen8 Date: Fri, 21 Mar 2014 13:41:13 +0100 Subject: [PATCH] Fixed #1213 - article plugin if changing category on an already existing article a new article was incorrectly created --- plugins/fabrik_form/article/article.php | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/plugins/fabrik_form/article/article.php b/plugins/fabrik_form/article/article.php index fe247c65c34..1c5f6c553af 100644 --- a/plugins/fabrik_form/article/article.php +++ b/plugins/fabrik_form/article/article.php @@ -51,6 +51,7 @@ public function onAfterProcess() { $cat = $catElement->getFullName() . '_raw'; $categories = (array) JArrayHelper::getValue($this->data, $cat); + $this->mapCategoryChanges($categories, $store); } else { @@ -69,6 +70,42 @@ public function onAfterProcess() return true; } + /** + * If changing selected category on editing a record, the new category id needs to be assigned as a + * property to $store with the existing article id. Not tested if say for example the category element + * is a multi-select + * + * @param array $categories Categories selected by the user + * @param object &$store Previously stored categoryid->articleid map + * + * @return object $store + */ + protected function mapCategoryChanges($categories, &$store) + { + $defaultAricleId = null; + + if (!empty($categories)) + { + foreach ($store as $catid => $articleId) + { + if (!in_array($catid, $categories)) + { + // We've swapped categories for an existing article + $defaultAricleId = $articleId; + } + } + foreach ($categories as $category) + { + if (!isset($store->$category)) + { + $store->$category = $defaultAricleId; + } + } + } + + return $store; + } + /** * Save article *