Created
December 12, 2011 01:02
-
-
Save schwarzmx/1463920 to your computer and use it in GitHub Desktop.
Script to create user groups model for NEREID project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This script will be in charge of creating the user groups and setting their | |
* proper permissions as we arranged. That is: | |
* | |
* Students: Will create articles and only edit their own. Inherits from Author | |
* group in Joomla's predefined groups. | |
* Visitors: Anyone that visits this site. No login necessary. Inherits from | |
* Public. | |
* Webmaster: The administrator group of this site. Inherits from SuperUser. | |
* | |
*/ | |
require 'configuration.php'; | |
$config = new JConfig; | |
$usergroups_table = 'usergroups'; | |
// create connection to our database | |
mysql_connect($config->host, $config->user, $config->password) | |
or die(mysql_error()); | |
mysql_select_db($config->db) | |
or die(mysql_error()); | |
/* | |
* this function was taken from the documentation for Joomla 1.5 and adapted to v1.7 | |
* see: http://docs.joomla.org/Custom_user_groups | |
*/ | |
function rebuild_tree($parent_id, $left) { | |
global $config; | |
global $usergroups_table; | |
// the right value of this node is the left value + 1 | |
$right = $left + 1; | |
// get all children of this node | |
$result = mysql_query( | |
'SELECT id FROM ' . $config->dbprefix . $usergroups_table . | |
' WHERE parent_id = ' . $parent_id . ';') | |
or die(mysql_error()); | |
while ($row = mysql_fetch_array($result)) { | |
// recursive execution of this function for each | |
// child of this node | |
// $right is the current right value, which is | |
// incremented by the rebuild_tree function | |
$right = rebuild_tree($row['id'], $right); | |
} | |
// we've got the left value, and now that we've processed | |
// the children of this node we also know the right value | |
mysql_query( | |
'UPDATE ' . $config->dbprefix . $usergroups_table . | |
' SET lft = ' . $left . ', rgt = ' . $right . | |
' WHERE id = ' . $parent_id . ';') | |
or die(mysql_error()); | |
// return the right value of this node + 1 | |
return $right + 1; | |
} | |
// function that creates a group, it's just an insert command | |
function insert_group($group_name, $parent_id){ | |
global $config; | |
global $usergroups_table; | |
mysql_query( | |
'INSERT INTO '. $config->dbprefix . $usergroups_table . ' (title, parent_id)'. | |
' VALUES (\''. $group_name .'\', '. $parent_id .')' | |
) | |
or die(mysql_error()); | |
} | |
// now create user groups | |
insert_group('Students', 3); // 3 is the ID for Author, which is the parent_id we want | |
insert_group('Visitors', 1); // 1 is the ID for Public | |
insert_group('Webmaster', 8); // 8 is the ID of Super Users | |
// from the aforementioned site: | |
/* | |
* 0-> parent_id in Joomla this is the value of the parent_id field of the Root record | |
* 1-> start the left tree at 1 | |
*/ | |
rebuild_tree (0, 1); | |
// print everything | |
Print 'User groups successfully created!<br />'; | |
$user_groups = mysql_query("SELECT * FROM " . $config->dbprefix . $usergroups_table) | |
or die(mysql_error()); | |
while($row = mysql_fetch_array($user_groups)) | |
{ | |
Print "<hr/>"; | |
Print "id:" . $row['id'] . "<br />"; | |
Print "parent_id: " . $row['parent_id']. "<br />"; | |
Print "lft: " . $row['lft']. "<br />"; | |
Print "rgt: " . $row['rgt']. "<br />"; | |
Print "title: " . $row['title']. "<br />"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment