I have copied a function from phpBB so that I could make some slight adjustments to the return object. It works fine, creates the group returns the ID of the group etc. The only problem is when it fails (in the instance the group name exists) I get the following error:
[phpBB Debug] PHP Notice: in file C:/.../phpbb_library.php on line 586: Undefined index: GROUP_NAME_TAKEN
Here's my code.
Constructor:
class Phpbb_library
{
protected $_user;
// http://wiki.phpbb.com/Add_users
/**
* Constructor.
*/
public function __construct()
{
// Set the variables scope
global $phpbb_root_path, $phpEx, $user, $auth, $cache, $db, $config, $template, $table_prefix;
define('IN_PHPBB', TRUE);
define('FORUM_ROOT_PATH', Kohana::$config->load('global.FORUM_ABSOLUTE_PATH'));
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : FORUM_ROOT_PATH;
$phpEx = substr(strrchr(__FILE__, '.'), 1);
// Include needed files
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'config.' . $phpEx);
include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
//include($phpbb_root_path . 'language/en/acp/groups.' . $phpEx);
// Initialize phpBB user session
$user->session_begin();
$auth->acl($user->data);
$user->setup();
// Save user data into $_user variable
$this->_user = $user;
}
group_create() - Modification:
/**
* Dynamically create a group and return group id or error.
* Modified version of group_create from includes/functions_user.php
*
* @return array - array object containing group id or error message.
*/
function group_create_mod(&$group_id, $type, $name, $desc, $group_attributes, $allow_desc_bbcode = FALSE, $allow_desc_urls = FALSE, $allow_desc_smilies = FALSE)
{
global $phpbb_root_path, $config, $db, $user, $file_upload;
$error = array();
// Attributes which also affect the users table
$user_attribute_ary = array('group_colour', 'group_rank', 'group_avatar', 'group_avatar_type', 'group_avatar_width', 'group_avatar_height');
// Check data. Limit group name length.
if (!utf8_strlen($name) || utf8_strlen($name) > 60)
{
$error[] = (!utf8_strlen($name)) ? $user->lang['GROUP_ERR_USERNAME'] : $user->lang['GROUP_ERR_USER_LONG'];
}
$err = group_validate_groupname($group_id, $name);
if (!empty($err))
{
$error[] = $user->lang[$err];
}
if (!in_array($type, array(GROUP_OPEN, GROUP_CLOSED, GROUP_HIDDEN, GROUP_SPECIAL, GROUP_FREE)))
{
$error[] = $user->lang['GROUP_ERR_TYPE'];
}
if (!sizeof($error))
{
$user_ary = array();
$sql_ary = array(
'group_name' => (string) $name,
'group_desc' => (string) $desc,
'group_desc_uid' => '',
'group_desc_bitfield' => '',
'group_type' => (int) $type,
);
// Parse description
if ($desc)
{
generate_text_for_storage($sql_ary['group_desc'], $sql_ary['group_desc_uid'], $sql_ary['group_desc_bitfield'], $sql_ary['group_desc_options'], $allow_desc_bbcode, $allow_desc_urls, $allow_desc_smilies);
}
if (sizeof($group_attributes))
{
// Merge them with $sql_ary to properly update the group
$sql_ary = array_merge($sql_ary, $group_attributes);
}
// Setting the log message before we set the group id (if group gets added)
$log = ($group_id) ? 'LOG_GROUP_UPDATED' : 'LOG_GROUP_CREATED';
$query = '';
if ($group_id)
{
$sql = 'SELECT user_id
FROM ' . USERS_TABLE . '
WHERE group_id = ' . $group_id;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$user_ary[] = $row['user_id'];
}
$db->sql_freeresult($result);
if (isset($sql_ary['group_avatar']) && !$sql_ary['group_avatar'])
{
remove_default_avatar($group_id, $user_ary);
}
if (isset($sql_ary['group_rank']) && !$sql_ary['group_rank'])
{
remove_default_rank($group_id, $user_ary);
}
$sql = 'UPDATE ' . GROUPS_TABLE . '
SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
WHERE group_id = $group_id";
$db->sql_query($sql);
// Since we may update the name too, we need to do this on other tables too...
$sql = 'UPDATE ' . MODERATOR_CACHE_TABLE . "
SET group_name = '" . $db->sql_escape($sql_ary['group_name']) . "'
WHERE group_id = $group_id";
$db->sql_query($sql);
// One special case is the group skip auth setting. If this was changed we need to purge permissions for this group
if (isset($group_attributes['group_skip_auth']))
{
// Get users within this group...
$sql = 'SELECT user_id
FROM ' . USER_GROUP_TABLE . '
WHERE group_id = ' . $group_id . '
AND user_pending = 0';
$result = $db->sql_query($sql);
$user_id_ary = array();
while ($row = $db->sql_fetchrow($result))
{
$user_id_ary[] = $row['user_id'];
}
$db->sql_freeresult($result);
if (!empty($user_id_ary))
{
global $auth;
// Clear permissions cache of relevant users
$auth->acl_clear_prefetch($user_id_ary);
}
}
}
else
{
$sql = 'INSERT INTO ' . GROUPS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
$db->sql_query($sql);
}
if (!$group_id)
{
$group_id = $db->sql_nextid();
if (isset($sql_ary['group_avatar_type']) && $sql_ary['group_avatar_type'] == AVATAR_UPLOAD)
{
group_correct_avatar($group_id, $sql_ary['group_avatar']);
}
}
// Set user attributes
$sql_ary = array();
if (sizeof($group_attributes))
{
// Go through the user attributes array, check if a group attribute matches it and then set it. ;)
foreach ($user_attribute_ary as $attribute)
{
if (!isset($group_attributes[$attribute]))
{
continue;
}
// If we are about to set an avatar, we will not overwrite user avatars if no group avatar is set...
if (strpos($attribute, 'group_avatar') === 0 && !$group_attributes[$attribute])
{
continue;
}
$sql_ary[$attribute] = $group_attributes[$attribute];
}
}
if (sizeof($sql_ary) && sizeof($user_ary))
{
group_set_user_default($group_id, $user_ary, $sql_ary);
}
$name = ($type == GROUP_SPECIAL) ? $user->lang['G_' . $name] : $name;
add_log('admin', $log, $name);
group_update_listings($group_id);
}
// MOD: sort out return object
if (sizeof($error))
{
return array(
"success" => FALSE,
"error" => $error,
);
}
else
{
return array(
"success" => TRUE,
"group_id" => $group_id,
);
}
//return (sizeof($error)) ? $error : false;
}
The error I receive comes from this line: $error[] = $user->lang[$err];
It looks like you're just missing a language definition for GROUP_NAME_TAKEN.
For example in (or whichever language file) language/en/acp/groups.php
'GROUP_NAME_TAKEN' => 'The group name you entered is already in use, please select an alternative.',
Edit: try this to include the groups language file:
$user->add_lang('acp/groups');
Add it in your constructor right after the global
line (so that $user
exists).