Search code examples
phpjoomlanested-setsjoomla2.5

How to create joomla user group in joomla 2.5


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.


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."')";