Back to Menu class

Method store

public mixed
store
(mixed $updateNulls = true)
Overloaded store function
Parameters
  • bool $updateNulls True to update fields even if they are null.
Returns
  • mixed False on failure, positive integer on success.
Since
  • 1.6
-
  • \Joomla\CMS\Table\Table::store()
Class: Menu
Project: Joomla

Method store - Source code

/**
 * Overloaded store function
 *
 * @param   boolean  $updateNulls  True to update fields even if they are null.
 *
 * @return  mixed  False on failure, positive integer on success.
 *
 * @see     Table::store()
 * @since   1.6
 */
public function store($updateNulls = true)
{
    $db = $this->getDbo();
    // Verify that the alias is unique
    $table = Table::getInstance('Menu', 'JTable', array('dbo' => $db));
    $originalAlias = trim($this->alias);
    $this->alias = !$originalAlias ? $this->title : $originalAlias;
    $this->alias = ApplicationHelper::stringURLSafe(trim($this->alias), $this->language);
    if ($this->parent_id == 1 && $this->client_id == 0) {
        // Verify that a first level menu item alias is not 'component'.
        if ($this->alias === 'component') {
            $this->setError(Text::_('JLIB_DATABASE_ERROR_MENU_ROOT_ALIAS_COMPONENT'));
            return false;
        }
        // Verify that a first level menu item alias is not the name of a folder.
        if (\in_array($this->alias, Folder::folders(JPATH_ROOT))) {
            $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_MENU_ROOT_ALIAS_FOLDER', $this->alias, $this->alias));
            return false;
        }
    }
    // If alias still empty (for instance, new menu item with chinese characters with no unicode alias setting).
    if (empty($this->alias)) {
        $this->alias = Factory::getDate()->format('Y-m-d-H-i-s');
    } else {
        $itemSearch = array('alias' => $this->alias, 'parent_id' => $this->parent_id, 'client_id' => (int) $this->client_id);
        $error = false;
        // Check if the alias already exists. For multilingual site.
        if (Multilanguage::isEnabled() && (int) $this->client_id == 0) {
            // If there is a menu item at the same level with the same alias (in the All or the same language).
            if ($table->load(array_replace($itemSearch, array('language' => '*'))) && ($table->id != $this->id || $this->id == 0) || $table->load(array_replace($itemSearch, array('language' => $this->language))) && ($table->id != $this->id || $this->id == 0) || $this->language === '*' && $this->id == 0 && $table->load($itemSearch)) {
                $error = true;
            } elseif ($this->language === '*' && $this->id != 0) {
                $id = (int) $this->id;
                $query = $db->getQuery(true)->select('id')->from($db->quoteName('#__menu'))->where($db->quoteName('parent_id') . ' = 1')->where($db->quoteName('client_id') . ' = 0')->where($db->quoteName('id') . ' != :id')->where($db->quoteName('alias') . ' = :alias')->bind(':id', $id, ParameterType::INTEGER)->bind(':alias', $this->alias);
                $otherMenuItemId = (int) $db->setQuery($query)->loadResult();
                if ($otherMenuItemId) {
                    $table->load(array('id' => $otherMenuItemId));
                    $error = true;
                }
            }
        } else {
            // If there is a menu item at the same level with the same alias (in any language).
            if ($table->load($itemSearch) && ($table->id != $this->id || $this->id == 0)) {
                $error = true;
            }
        }
        // The alias already exists. Enqueue an error message.
        if ($error) {
            $menuTypeTable = Table::getInstance('MenuType', 'JTable', array('dbo' => $db));
            $menuTypeTable->load(array('menutype' => $table->menutype));
            $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_MENU_UNIQUE_ALIAS', $this->alias, $table->title, $menuTypeTable->title));
            return false;
        }
    }
    if ($this->home == '1') {
        // Verify that the home page for this menu is unique.
        if ($table->load(array('menutype' => $this->menutype, 'client_id' => (int) $this->client_id, 'home' => '1')) && $table->language != $this->language) {
            $this->setError(Text::_('JLIB_DATABASE_ERROR_MENU_HOME_NOT_UNIQUE_IN_MENU'));
            return false;
        }
        // Verify that the home page for this language is unique per client id
        if ($table->load(array('home' => '1', 'language' => $this->language, 'client_id' => (int) $this->client_id))) {
            if ($table->checked_out && $table->checked_out != $this->checked_out) {
                $this->setError(Text::_('JLIB_DATABASE_ERROR_MENU_DEFAULT_CHECKIN_USER_MISMATCH'));
                return false;
            }
            $table->home = 0;
            $table->checked_out = null;
            $table->checked_out_time = null;
            $table->store();
        }
    }
    if (!parent::store($updateNulls)) {
        return false;
    }
    // Get the new path in case the node was moved
    $pathNodes = $this->getPath();
    $segments = array();
    foreach ($pathNodes as $node) {
        // Don't include root in path
        if ($node->alias !== 'root') {
            $segments[] = $node->alias;
        }
    }
    $newPath = trim(implode('/', $segments), ' /\\');
    // Use new path for partial rebuild of table
    // Rebuild will return positive integer on success, false on failure
    return $this->rebuild($this->{$this->_tbl_key}, $this->lft, $this->level, $newPath) > 0;
}