Question

I am working on my component in joomla 2.5 and i need to create user groups dynamically through my code.

As i know in 2.5 joomla is using nested sets for maintaining hierarchical structure so it may affect the whole table when we insert any new entry.

Does joomla provide any function that can be directly used to create user group ? something in which we can specify the parent group and new group will be created under it.

Était-ce utile?

La solution

I solved it myself

In my case i need to insert the child groups just under a particular group (mentioned as $parent_id in the code below)

// get max right from all the child under parent id
    $child_query = "SELECT max(`rgt`) FROM `#__usergroups` WHERE `id` = ".$parent_id;
$db->setQuery($child_query);
$max_rgt = $db->loadResult();


// calculate left and rgt for new entry
$new_lft = $max_rgt;
$new_rgt = $max_rgt + 1;

// update lft and rgt of all entries having lft , rgt greater than max_rgt
$upd1 = "UPDATE `#__usergroups` SET `lft` = `lft` + 2 WHERE `lft` > ".$max_rgt;
$upd2 = "UPDATE `#__usergroups` SET `rgt` = `rgt` + 2 WHERE `rgt` >= ".$max_rgt;


// insert new  child
$insert = "INSERT INTO `#__usergroups`(`parent_id`,`lft`,`rgt`,`title`) VALUES(".$matrix_parent.",".$new_lft.",".$new_rgt.",'".$title."')";

Autres conseils

Even if the question is a kind of old and already accepted, I use this code to add/update/delete groups in a custom component

in order to get the required model of com_users one needs to:

JModelLegacy::addIncludePath( JPATH_ADMINISTRATOR . 
                '/components/com_users/models/', 'UsersModel' );

$groupModel = JModelLegacy::getInstance( 'Group', 'UsersModel' );

before the code shown below will work.

add*/update:

$groupData = array(
    'title' => <new/old name of group here>,
    'parent_id' => <new/old id of parent group here>,
    'id' => <group id here> );

$groupModel->save( $groupData );

*if the group is newly created than the id should be 0. After the saving one can access the new id with a database query since one knows the name/title of the group

delete:

$groupModel->delete( $groupIds );

The whole code above is included in save(), delete() methods of one of my component's model. Since Joomla stores connections between Users and Groups in a seperate Map table, no user data will be corrupted when deleting a group. Note that the delete() method takes an array of ids as argument.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top