/**
* Method to store a row in the database from the Table instance properties.
*
* If a primary key value is set the row with that primary key value will be updated with the instance property values.
* If no primary key value is set a new row will be inserted into the database with the properties from the Table instance.
*
* @param boolean $updateNulls True to update fields even if they are null.
*
* @return boolean True on success.
*
* @since 1.6
*/
public function store($updateNulls = false)
{
if ($this->id) {
// Get the user id
$userId = (int) Factory::getUser()->id;
$notIn = [0, $userId];
// Get the old value of the table
$table = Table::getInstance('Menutype', 'JTable', array('dbo' => $this->getDbo()));
$table->load($this->id);
// 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')->whereNotIn($this->_db->quoteName('checked_out'), $notIn)->bind(':menutype', $table->menutype);
$this->_db->setQuery($query);
if ($this->_db->loadRowList()) {
$this->setError(Text::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', \get_class($this), Text::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT')));
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 :params')->whereNotIn($this->_db->quoteName('checked_out'), $notIn)->bind(':params', $searchParams);
$this->_db->setQuery($query);
if ($this->_db->loadRowList()) {
$this->setError(Text::sprintf('JLIB_DATABASE_ERROR_STORE_FAILED', \get_class($this), Text::_('JLIB_DATABASE_ERROR_MENUTYPE_CHECKOUT')));
return false;
}
// Update the menu items
$query->clear()->update($this->_db->quoteName('#__menu'))->set($this->_db->quoteName('menutype') . ' = :setmenutype')->where($this->_db->quoteName('menutype') . ' = :menutype')->bind(':setmenutype', $this->menutype)->bind(':menutype', $table->menutype);
$this->_db->setQuery($query);
$this->_db->execute();
// Update the module items
$whereParams = '%"menutype":' . json_encode($table->menutype) . '%';
$searchParams = '"menutype":' . json_encode($table->menutype);
$replaceParams = '"menutype":' . json_encode($this->menutype);
$query->clear()->update($this->_db->quoteName('#__modules'))->set($this->_db->quoteName('params') . ' = REPLACE(' . $this->_db->quoteName('params') . ', :search, :value)');
$query->where($this->_db->quoteName('module') . ' = ' . $this->_db->quote('mod_menu'))->where($this->_db->quoteName('params') . ' LIKE :whereparams')->bind(':search', $searchParams)->bind(':value', $replaceParams)->bind(':whereparams', $whereParams);
$this->_db->setQuery($query);
$this->_db->execute();
}
return parent::store($updateNulls);
}