Back to MenuType class

Method delete

public bool
delete
(mixed $pk = null)
Method to delete a row from the database table by primary key value.
Parameters
  • mixed $pk An optional primary key value to delete. If not set the instance property value is used.
Returns
  • bool True on success.
Since
  • 1.6
Class: MenuType
Project: Joomla

Method delete - Source code

/**
 * Method to delete a row from the database table by primary key value.
 *
 * @param   mixed  $pk  An optional primary key value to delete.  If not set the instance property value is used.
 *
 * @return  boolean  True on success.
 *
 * @since   1.6
 */
public function delete($pk = null)
{
    $k = $this->_tbl_key;
    $pk = $pk === null ? $this->{$k} : $pk;
    // If no primary key is given, return false.
    if ($pk !== null) {
        // Get the user id
        $userId = (int) Factory::getUser()->id;
        $notIn = [0, $userId];
        $star = '*';
        // Get the old value of the table
        $table = Table::getInstance('Menutype', 'JTable', array('dbo' => $this->getDbo()));
        $table->load($pk);
        // Verify that no items are checked out
        $query = $this->_db->getQuery(true)->select($this->_db->quoteName('id'))->from($this->_db->quoteName('#__menu'))->where($this->_db->quoteName('menutype') . ' = :menutype')->where('(' . $this->_db->quoteName('checked_out') . ' NOT IN (NULL, :id)' . ' OR ' . $this->_db->quoteName('home') . ' = 1' . ' AND ' . $this->_db->quoteName('language') . ' = :star' . ')')->bind(':menutype', $table->menutype)->bind(':id', $userId, ParameterType::INTEGER)->bind(':star', $star);
        $this->_db->setQuery($query);
        if ($this->_db->loadRowList()) {
            $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', \get_class($this), Text::_('JLIB_DATABASE_ERROR_MENUTYPE')));
            return false;
        }
        // Verify that no module for this menu are checked out
        $searchParams = '%"menutype":' . json_encode($table->menutype) . '%';
        $query->clear()->select($this->_db->quoteName('id'))->from($this->_db->quoteName('#__modules'))->where($this->_db->quoteName('module') . ' = ' . $this->_db->quote('mod_menu'))->where($this->_db->quoteName('params') . ' LIKE :menutype')->whereNotIn($this->_db->quoteName('checked_out'), $notIn)->bind(':menutype', $searchParams);
        $this->_db->setQuery($query);
        if ($this->_db->loadRowList()) {
            $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_DELETE_FAILED', \get_class($this), Text::_('JLIB_DATABASE_ERROR_MENUTYPE')));
            return false;
        }
        // Delete the menu items
        $query->clear()->delete('#__menu')->where('menutype=' . $this->_db->quote($table->menutype));
        $this->_db->setQuery($query);
        $this->_db->execute();
        // Update the module items
        $query->clear()->delete('#__modules')->where('module=' . $this->_db->quote('mod_menu'))->where('params LIKE ' . $this->_db->quote('%"menutype":' . json_encode($table->menutype) . '%'));
        $this->_db->setQuery($query);
        $this->_db->execute();
    }
    return parent::delete($pk);
}