/**
* Method to save the form data.
*
* @param array $data The form data.
*
* @return boolean True on success, False on error.
*
* @since 1.6
*/
public function save($data)
{
$table = $this->getTable();
$context = $this->option . '.' . $this->name;
$app = Factory::getApplication();
if (\array_key_exists('tags', $data) && \is_array($data['tags'])) {
$table->newTags = $data['tags'];
}
$key = $table->getKeyName();
$pk = isset($data[$key]) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
$isNew = true;
// Include the plugins for the save events.
PluginHelper::importPlugin($this->events_map['save']);
// Allow an exception to be thrown.
try {
// Load the row if saving an existing record.
if ($pk > 0) {
$table->load($pk);
$isNew = false;
}
// Bind the data.
if (!$table->bind($data)) {
$this->setError($table->getError());
return false;
}
// Prepare the row for saving
$this->prepareTable($table);
// Check the data.
if (!$table->check()) {
$this->setError($table->getError());
return false;
}
// Trigger the before save event.
$result = $app->triggerEvent($this->event_before_save, array($context, $table, $isNew, $data));
if (\in_array(false, $result, true)) {
$this->setError($table->getError());
return false;
}
// Store the data.
if (!$table->store()) {
$this->setError($table->getError());
return false;
}
// Clean the cache.
$this->cleanCache();
// Trigger the after save event.
$app->triggerEvent($this->event_after_save, array($context, $table, $isNew, $data));
} catch (\Exception $e) {
$this->setError($e->getMessage());
return false;
}
if (isset($table->{$key})) {
$this->setState($this->getName() . '.id', $table->{$key});
}
$this->setState($this->getName() . '.new', $isNew);
if ($this->associationsContext && Associations::isEnabled() && !empty($data['associations'])) {
$associations = $data['associations'];
// Unset any invalid associations
$associations = ArrayHelper::toInteger($associations);
// Unset any invalid associations
foreach ($associations as $tag => $id) {
if (!$id) {
unset($associations[$tag]);
}
}
// Show a warning if the item isn't assigned to a language but we have associations.
if ($associations && $table->language === '*') {
$app->enqueueMessage(Text::_(strtoupper($this->option) . '_ERROR_ALL_LANGUAGE_ASSOCIATED'), 'warning');
}
// Get associationskey for edited item
$db = $this->getDbo();
$id = (int) $table->{$key};
$query = $db->getQuery(true)->select($db->quoteName('key'))->from($db->quoteName('#__associations'))->where($db->quoteName('context') . ' = :context')->where($db->quoteName('id') . ' = :id')->bind(':context', $this->associationsContext)->bind(':id', $id, ParameterType::INTEGER);
$db->setQuery($query);
$oldKey = $db->loadResult();
if ($associations || $oldKey !== null) {
// Deleting old associations for the associated items
$query = $db->getQuery(true)->delete($db->quoteName('#__associations'))->where($db->quoteName('context') . ' = :context')->bind(':context', $this->associationsContext);
$where = [];
if ($associations) {
$where[] = $db->quoteName('id') . ' IN (' . implode(',', $query->bindArray(array_values($associations))) . ')';
}
if ($oldKey !== null) {
$where[] = $db->quoteName('key') . ' = :oldKey';
$query->bind(':oldKey', $oldKey);
}
$query->extendWhere('AND', $where, 'OR');
$db->setQuery($query);
$db->execute();
}
// Adding self to the association
if ($table->language !== '*') {
$associations[$table->language] = (int) $table->{$key};
}
if (\count($associations) > 1) {
// Adding new association for these items
$key = md5(json_encode($associations));
$query = $db->getQuery(true)->insert($db->quoteName('#__associations'))->columns([$db->quoteName('id'), $db->quoteName('context'), $db->quoteName('key')]);
foreach ($associations as $id) {
$query->values(implode(',', $query->bindArray([$id, $this->associationsContext, $key], [ParameterType::INTEGER, ParameterType::STRING, ParameterType::STRING])));
}
$db->setQuery($query);
$db->execute();
}
}
if ($app->input->get('task') == 'editAssociations') {
return $this->redirectToAssociations($data);
}
return true;
}