/**
* Method to delete a user, user groups, and any other necessary data from the database.
*
* @param integer $userId An optional user id.
*
* @return boolean True on success, false on failure.
*
* @since 1.7.0
*/
public function delete($userId = null)
{
// Set the primary key to delete.
$k = $this->_tbl_key;
if ($userId) {
$this->{$k} = (int) $userId;
}
$key = (int) $this->{$k};
// Delete the user.
$query = $this->_db->getQuery(true)->delete($this->_db->quoteName($this->_tbl))->where($this->_db->quoteName($this->_tbl_key) . ' = :key')->bind(':key', $key, ParameterType::INTEGER);
$this->_db->setQuery($query);
$this->_db->execute();
// Delete the user group maps.
$query->clear()->delete($this->_db->quoteName('#__user_usergroup_map'))->where($this->_db->quoteName('user_id') . ' = :key')->bind(':key', $key, ParameterType::INTEGER);
$this->_db->setQuery($query);
$this->_db->execute();
/*
* Clean Up Related Data.
*/
$query->clear()->delete($this->_db->quoteName('#__messages_cfg'))->where($this->_db->quoteName('user_id') . ' = :key')->bind(':key', $key, ParameterType::INTEGER);
$this->_db->setQuery($query);
$this->_db->execute();
$query->clear()->delete($this->_db->quoteName('#__messages'))->where($this->_db->quoteName('user_id_to') . ' = :key')->bind(':key', $key, ParameterType::INTEGER);
$this->_db->setQuery($query);
$this->_db->execute();
$query->clear()->delete($this->_db->quoteName('#__user_keys'))->where($this->_db->quoteName('user_id') . ' = :username')->bind(':username', $this->username);
$this->_db->setQuery($query);
$this->_db->execute();
return true;
}