/**
* Method to delete one or more records.
*
* @param array &$pks An array of record primary keys.
*
* @return boolean True if successful, false if an error occurs.
*
* @since 1.6
*/
public function delete(&$pks)
{
$pks = ArrayHelper::toInteger((array) $pks);
$table = $this->getTable();
// Include the plugins for the delete events.
PluginHelper::importPlugin($this->events_map['delete']);
// Iterate the items to delete each one.
foreach ($pks as $i => $pk) {
if ($table->load($pk)) {
if ($this->canDelete($table)) {
$context = $this->option . '.' . $this->name;
// Trigger the before delete event.
$result = Factory::getApplication()->triggerEvent($this->event_before_delete, array($context, $table));
if (\in_array(false, $result, true)) {
$this->setError($table->getError());
return false;
}
// Multilanguage: if associated, delete the item in the _associations table
if ($this->associationsContext && Associations::isEnabled()) {
$db = $this->getDbo();
$query = $db->getQuery(true)->select(['COUNT(*) AS ' . $db->quoteName('count'), $db->quoteName('as1.key')])->from($db->quoteName('#__associations', 'as1'))->join('LEFT', $db->quoteName('#__associations', 'as2'), $db->quoteName('as1.key') . ' = ' . $db->quoteName('as2.key'))->where([$db->quoteName('as1.context') . ' = :context', $db->quoteName('as1.id') . ' = :pk'])->bind(':context', $this->associationsContext)->bind(':pk', $pk, ParameterType::INTEGER)->group($db->quoteName('as1.key'));
$db->setQuery($query);
$row = $db->loadAssoc();
if (!empty($row['count'])) {
$query = $db->getQuery(true)->delete($db->quoteName('#__associations'))->where([$db->quoteName('context') . ' = :context', $db->quoteName('key') . ' = :key'])->bind(':context', $this->associationsContext)->bind(':key', $row['key']);
if ($row['count'] > 2) {
$query->where($db->quoteName('id') . ' = :pk')->bind(':pk', $pk, ParameterType::INTEGER);
}
$db->setQuery($query);
$db->execute();
}
}
if (!$table->delete($pk)) {
$this->setError($table->getError());
return false;
}
// Trigger the after event.
Factory::getApplication()->triggerEvent($this->event_after_delete, array($context, $table));
} else {
// Prune items that you can't change.
unset($pks[$i]);
$error = $this->getError();
if ($error) {
Log::add($error, Log::WARNING, 'jerror');
return false;
} else {
Log::add(Text::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'), Log::WARNING, 'jerror');
return false;
}
}
} else {
$this->setError($table->getError());
return false;
}
}
// Clear the component's cache
$this->cleanCache();
return true;
}