/**
* Method to check the current record to save
*
* @return boolean True on success
*
* @since 1.7.0
*/
public function check()
{
try {
parent::check();
} catch (\Exception $e) {
$this->setError($e->getMessage());
return false;
}
// Validate the title.
if (trim($this->title) == '') {
$this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE'));
return false;
}
// The parent_id can not be equal to the current id
if ($this->id === (int) $this->parent_id) {
$this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_PARENT_ID_NOT_VALID'));
return false;
}
// Check for a duplicate parent_id, title.
// There is a unique index on the (parent_id, title) field in the table.
$db = $this->_db;
$parentId = (int) $this->parent_id;
$title = trim($this->title);
$id = (int) $this->id;
$query = $db->getQuery(true)->select('COUNT(title)')->from($this->_tbl)->where($db->quoteName('title') . ' = :title')->where($db->quoteName('parent_id') . ' = :parentid')->where($db->quoteName('id') . ' <> :id')->bind(':title', $title)->bind(':parentid', $parentId, ParameterType::INTEGER)->bind(':id', $id, ParameterType::INTEGER);
$db->setQuery($query);
if ($db->loadResult() > 0) {
$this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_TITLE_EXISTS'));
return false;
}
// We do not allow to move non public to root and public to non-root
if (!empty($this->id)) {
$table = self::getInstance('Usergroup', 'JTable', array('dbo' => $this->getDbo()));
$table->load($this->id);
if (!$table->parent_id && $this->parent_id || $table->parent_id && !$this->parent_id) {
$this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_PARENT_ID_NOT_VALID'));
return false;
}
} elseif (!$this->parent_id) {
$this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_PARENT_ID_NOT_VALID'));
return false;
}
// The new parent_id has to be a valid group
if ($this->parent_id) {
$table = self::getInstance('Usergroup', 'JTable', array('dbo' => $this->getDbo()));
$table->load($this->parent_id);
if ($table->id != $this->parent_id) {
$this->setError(Text::_('JLIB_DATABASE_ERROR_USERGROUP_PARENT_ID_NOT_VALID'));
return false;
}
}
return true;
}