Admin Model

It is a method to delete one or more records.

  • array &$pks - An array of record primary keys.
  • return boolean - True if successful, false if an error occurs.
public function delete(&$pks)
{
$dispatcher = \JEventDispatcher::getInstance();
$pks = (array) $pks;
$table = $this->getTable();

// Include the plugins for the delete events.
\JPluginHelper::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 = $dispatcher->trigger($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 && \JLanguageAssociations::isEnabled())
{
$db = $this->getDbo();
$query = $db->getQuery(true)
->select('COUNT(*) as count, ' . $db->quoteName('as1.key'))
->from($db->quoteName('#__associations') . ' AS as1')
->join('LEFT', $db->quoteName('#__associations') . ' AS as2 ON ' . $db->quoteName('as1.key') . ' = ' . $db->quoteName('as2.key'))
->where($db->quoteName('as1.context') . ' = ' . $db->quote($this->associationsContext))
->where($db->quoteName('as1.id') . ' = ' . (int) $pk)
->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') . ' = ' . $db->quote($this->associationsContext))
->where($db->quoteName('key') . ' = ' . $db->quote($row['key']));

if ($row['count'] > 2)
{
$query->where($db->quoteName('id') . ' = ' . (int) $pk);
}

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

if (!$table->delete($pk))
{
$this->setError($table->getError());

return false;
}

// Trigger the after event.
$dispatcher->trigger($this->event_after_delete, array($context, $table));
}
else
{
// Prune items that you can't change.
unset($pks[$i]);
$error = $this->getError();

if ($error)
{
\JLog::add($error, \JLog::WARNING, 'jerror');

return false;
}
else
{
\JLog::add(\JText::_('JLIB_APPLICATION_ERROR_DELETE_NOT_PERMITTED'), \JLog::WARNING, 'jerror');

return false;
}
}
}
else
{
$this->setError($table->getError());

return false;
}
}

// Clear the component's cache
$this->cleanCache();

return true;
}