Skip to content

Commit

Permalink
Merge pull request #7617 from Bakual/SaveAssoc
Browse files Browse the repository at this point in the history
Moving association saving to JModelAdmin
  • Loading branch information
rdeutz committed Aug 5, 2015
2 parents f4d413b + d5d960f commit 74d0ff2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 217 deletions.
4 changes: 2 additions & 2 deletions administrator/components/com_categories/models/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ public function save($data)
$db = $this->getDbo();
$query = $db->getQuery(true)
->delete('#__associations')
->where($db->quoteName('context') . ' = ' . $db->quote('com_categories.item'))
->where($db->quoteName('context') . ' = ' . $db->quote($this->associationsContext))
->where($db->quoteName('id') . ' IN (' . implode(',', $associations) . ')');
$db->setQuery($query);
$db->execute();
Expand All @@ -612,7 +612,7 @@ public function save($data)

foreach ($associations as $id)
{
$query->values($id . ',' . $db->quote('com_categories.item') . ',' . $db->quote($key));
$query->values($id . ',' . $db->quote($this->associationsContext) . ',' . $db->quote($key));
}

$db->setQuery($query);
Expand Down
75 changes: 1 addition & 74 deletions administrator/components/com_contact/models/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,80 +423,7 @@ public function save($data)
}
}

if (parent::save($data))
{
$assoc = JLanguageAssociations::isEnabled();

if ($assoc)
{
$id = (int) $this->getState($this->getName() . '.id');
$item = $this->getItem($id);

// Adding self to the association
$associations = $data['associations'];

foreach ($associations as $tag => $id)
{
if (empty($id))
{
unset($associations[$tag]);
}
}

// Detecting all item menus
$all_language = $item->language == '*';

if ($all_language && !empty($associations))
{
JError::raiseNotice(403, JText::_('COM_CONTACT_ERROR_ALL_LANGUAGE_ASSOCIATED'));
}

$associations[$item->language] = $item->id;

// Deleting old association for these items
$db = $this->getDbo();
$query = $db->getQuery(true)
->delete('#__associations')
->where('context=' . $db->quote('com_contact.item'))
->where('id IN (' . implode(',', $associations) . ')');
$db->setQuery($query);
$db->execute();

if ($error = $db->getErrorMsg())
{
$this->setError($error);

return false;
}

if (!$all_language && count($associations))
{
// Adding new association for these items
$key = md5(json_encode($associations));
$query->clear()
->insert('#__associations');

foreach ($associations as $id)
{
$query->values($id . ',' . $db->quote('com_contact.item') . ',' . $db->quote($key));
}

$db->setQuery($query);
$db->execute();

if ($error = $db->getErrorMsg())
{
$this->setError($error);

return false;
}
}
}

return true;
}

return false;
return parent::save($data);
}

/**
Expand Down
66 changes: 0 additions & 66 deletions administrator/components/com_content/models/article.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,72 +551,6 @@ public function save($data)
$this->featured($this->getState($this->getName() . '.id'), $data['featured']);
}

$assoc = JLanguageAssociations::isEnabled();
if ($assoc)
{
$id = (int) $this->getState($this->getName() . '.id');
$item = $this->getItem($id);

// Adding self to the association
$associations = $data['associations'];

foreach ($associations as $tag => $id)
{
if (empty($id))
{
unset($associations[$tag]);
}
}

// Detecting all item menus
$all_language = $item->language == '*';

if ($all_language && !empty($associations))
{
JError::raiseNotice(403, JText::_('COM_CONTENT_ERROR_ALL_LANGUAGE_ASSOCIATED'));
}

$associations[$item->language] = $item->id;

// Deleting old association for these items
$db = $this->getDbo();
$query = $db->getQuery(true)
->delete('#__associations')
->where('context=' . $db->quote('com_content.item'))
->where('id IN (' . implode(',', $associations) . ')');
$db->setQuery($query);
$db->execute();

if ($error = $db->getErrorMsg())
{
$this->setError($error);

return false;
}

if (!$all_language && count($associations))
{
// Adding new association for these items
$key = md5(json_encode($associations));
$query->clear()
->insert('#__associations');

foreach ($associations as $id)
{
$query->values($id . ',' . $db->quote('com_content.item') . ',' . $db->quote($key));
}

$db->setQuery($query);
$db->execute();

if ($error = $db->getErrorMsg())
{
$this->setError($error);
return false;
}
}
}

return true;
}

Expand Down
5 changes: 2 additions & 3 deletions administrator/components/com_menus/models/item.php
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,6 @@ public function save($data)
$this->setState('item.menutype', $table->menutype);

// Load associated menu items
$app = JFactory::getApplication();
$assoc = JLanguageAssociations::isEnabled();

if ($assoc)
Expand Down Expand Up @@ -1307,7 +1306,7 @@ public function save($data)
$db = $this->getDbo();
$query = $db->getQuery(true)
->delete('#__associations')
->where('context=' . $db->quote('com_menus.item'))
->where('context=' . $db->quote($this->associationsContext))
->where('id IN (' . implode(',', $associations) . ')');
$db->setQuery($query);

Expand All @@ -1331,7 +1330,7 @@ public function save($data)

foreach ($associations as $id)
{
$query->values($id . ',' . $db->quote('com_menus.item') . ',' . $db->quote($key));
$query->values($id . ',' . $db->quote($this->associationsContext) . ',' . $db->quote($key));
}

$db->setQuery($query);
Expand Down
73 changes: 1 addition & 72 deletions administrator/components/com_newsfeeds/models/newsfeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,78 +325,7 @@ public function save($data)
$data['published'] = 0;
}

if (parent::save($data))
{

$assoc = JLanguageAssociations::isEnabled();
if ($assoc)
{
$id = (int) $this->getState($this->getName() . '.id');
$item = $this->getItem($id);

// Adding self to the association
$associations = $data['associations'];

foreach ($associations as $tag => $id)
{
if (empty($id))
{
unset($associations[$tag]);
}
}

// Detecting all item menus
$all_language = $item->language == '*';

if ($all_language && !empty($associations))
{
JError::raiseNotice(403, JText::_('COM_NEWSFEEDS_ERROR_ALL_LANGUAGE_ASSOCIATED'));
}

$associations[$item->language] = $item->id;

// Deleting old association for these items
$db = $this->getDbo();
$query = $db->getQuery(true)
->delete('#__associations')
->where($db->quoteName('context') . ' = ' . $db->quote('com_newsfeeds.item'))
->where($db->quoteName('id') . ' IN (' . implode(',', $associations) . ')');
$db->setQuery($query);
$db->execute();

if ($error = $db->getErrorMsg())
{
$this->setError($error);
return false;
}

if (!$all_language && count($associations))
{
// Adding new association for these items
$key = md5(json_encode($associations));
$query->clear()
->insert('#__associations');

foreach ($associations as $id)
{
$query->values($id . ',' . $db->quote('com_newsfeeds.item') . ',' . $db->quote($key));
}

$db->setQuery($query);
$db->execute();

if ($error = $db->getErrorMsg())
{
$this->setError($error);
return false;
}
}
}

return true;
}

return false;
return parent::save($data);
}

/**
Expand Down
51 changes: 51 additions & 0 deletions libraries/legacy/model/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,57 @@ public function save($data)

$this->setState($this->getName() . '.new', $isNew);

if ($this->associationsContext && JLanguageAssociations::isEnabled())
{
$associations = $data['associations'];

// Unset any invalid associations
foreach ($associations as $tag => $id)
{
if (!(int) $id)
{
unset($associations[$tag]);
}
}

// Show a notice if the item isn't assigned to a language but we have associations.
if ($associations && ($table->language == '*'))
{
JFactory::getApplication()->enqueueMessage(
JText::_(strtoupper($this->option) . '_ERROR_ALL_LANGUAGE_ASSOCIATED'),
'notice'
);
}

// Adding self to the association
$associations[$table->language] = (int) $table->$key;

// Deleting old association for these items
$db = $this->getDbo();
$query = $db->getQuery(true)
->delete($db->qn('#__associations'))
->where($db->qn('context') . ' = ' . $db->quote($this->associationsContext))
->where($db->qn('id') . ' IN (' . implode(',', $associations) . ')');
$db->setQuery($query);
$db->execute();

if ((count($associations) > 1) && ($table->language != '*'))
{
// Adding new association for these items
$key = md5(json_encode($associations));
$query = $db->getQuery(true)
->insert('#__associations');

foreach ($associations as $id)
{
$query->values($id . ',' . $db->quote($this->associationsContext) . ',' . $db->quote($key));
}

$db->setQuery($query);
$db->execute();
}
}

return true;
}

Expand Down

0 comments on commit 74d0ff2

Please sign in to comment.