Skip to content

Commit

Permalink
Fixed Fabrik#1213 - article plugin if changing category on an already…
Browse files Browse the repository at this point in the history
… existing article a new article was incorrectly created
  • Loading branch information
pollen8 committed Mar 21, 2014
1 parent e5afce4 commit 1df0a84
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions plugins/fabrik_form/article/article.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function onAfterProcess()
{
$cat = $catElement->getFullName() . '_raw';
$categories = (array) JArrayHelper::getValue($this->data, $cat);
$this->mapCategoryChanges($categories, $store);
}
else
{
Expand All @@ -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
*
Expand Down

0 comments on commit 1df0a84

Please sign in to comment.