Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

Group.class.php

Go to the documentation of this file.
00001 <?php
00006 include_once("core/system/JBaseObject.class.php");
00007 include_once("core/db/dbjames.class.php");
00008 include_once("core/system/JSystemError.class.php");
00009 
00013 class Group extends JBaseObject
00014 {
00015         var $data;      
00016         var $modules;   
00017 
00018 
00025         function Group ($idG)
00026         {
00027                 $dbObj = new DBJames();
00028 
00029                 $query = "SELECT * FROM Groups ";
00030                 $query .= "WHERE idG = " . $idG;
00031     
00032                 $dbObj->Query($query);
00033                         
00034                 if ($dbObj->numRows == 1)
00035                 {
00036                         $this->data = $dbObj->row;
00037                         $this->_UpdateModulesList();
00038                 }
00039                 else
00040                 {
00041                         $this->data = null;
00042                         $this->RaiseConstructorError(new JSystemError(ERROR_GROUP_NOT_FOUND));
00043                 }
00044         }
00045         
00049         function _UpdateModulesList()
00050         {
00051                 $dbObj = new DBJames();
00052 
00053                 $query  = " SELECT Modules.*, RelModulesGroups.idRMG, ";
00054                 $query .= " RelModulesGroups.rmgName FROM ";
00055                 $query .= " Modules,RelModulesGroups ";
00056                 $query .= " WHERE RelModulesGroups.idG = ".$this->GetId();
00057                 $query .= " AND Modules.idM = RelModulesGroups.idM";
00058                 $query .= " ORDER BY Modules.idM ";
00059 
00060                 $dbObj->Query($query);
00061                 
00062                 $this->modules = array();
00063                 
00064                 while (!$dbObj->EOF)
00065                 {       
00066                         $this->modules[$dbObj->row["idRMG"]]["id"] = $dbObj->row["idM"];
00067                         $this->modules[$dbObj->row["idRMG"]]["name"] = $dbObj->row["rmgName"];
00068                         $dbObj->Next();
00069                 }               
00070         }
00071 
00076         function GetId()
00077         {
00078                 return $this->data["idG"];
00079         }
00080         
00085         function GetName()
00086         {
00087                 return $this->data["name"];
00088         }
00089         
00094         function IsGlobal()
00095         {
00096                 return $this->data["isGlobal"];
00097         }
00098         
00102         function SetGlobal()
00103         {
00104                 $this->data["isGlobal"]=1;
00105         }
00106         
00111         function &GetParent()
00112         {
00113                 $ugrsystem =& JUgrSystem::GetInstance();
00114                 return $ugrsystem->GetGroup($this->data["idParent"]);
00115         }
00116         
00124         function SetName($name)
00125         {
00126                 $this->data["name"] = $name;
00127         }
00128         
00132         function Commit()
00133         {
00134                 $dbObj = new DBJames();
00135                 
00136                 if ($this->IsGlobal())
00137                 {       
00138                         $glob = 1;
00139                 }
00140                 else
00141                 { 
00142                         $glob = 0;
00143                 }
00144                 
00145                 $query  = " UPDATE Groups SET ";
00146                 $query .= " name = '".$this->GetName()."', ";
00147                 $query .= " isGlobal = ".$glob. " ";
00148                 $query .= " WHERE idG = ".$this->GetId();
00149                 
00150                 $dbObj->Execute($query);
00151         }
00152                 
00153 
00154 //---------------------------- User methods ----------------------------------
00155         
00160         function &GetUsers()
00161         {
00162                 if($this->data == null)
00163                         return JSystem::RaiseError(
00164                                         new JSystemError(ERROR_NOT_INITIALIZED));
00165 
00166                 $dbObj = new DBJames();
00167 
00168                 $query  = " SELECT DISTINCT Users.*, RelUsersGroups.idR FROM ";
00169                 $query .= " Users,RelUsersGroups ";
00170                 $query .= " WHERE RelUsersGroups.idG = ".$this->GetId();
00171                 $query .= " AND   Users.idU = RelUsersGroups.idU";
00172 
00173                 $dbObj->Query($query);
00174 
00175                 return $dbObj;
00176         }
00177         
00183         function HasUser($idU)
00184         {
00185                 $dbObj =& $this->GetUsers();
00186                 
00187                 $result = false;
00188                 
00189                 while (! $dbObj->EOF && !$result)
00190                 {
00191                         if ($dbObj->row["idU"] == $idU)
00192                                 $result = true;
00193                         else
00194                                 $dbObj->Next();
00195                 }
00196                 
00197                 return $result;
00198         }               
00199 
00200 
00208         function &GetUsersWithPermission($permission, $mod)
00209         {
00210                 if($this->data == null)
00211                         return JSystem::RaiseError(
00212                                         new JSystemError(ERROR_NOT_INITIALIZED));
00213 
00214                 $dbObj = new DBJames();
00215 
00216                 $query  = " SELECT DISTINCT Users.*, RelUsersGroups.idR FROM ";
00217                 $query .= " Users,RelUsersGroups,RelRolesPermissions,Permissions ";
00218                 $query .= " WHERE RelUsersGroups.idG = ".$this->GetId();
00219                 $query .= " AND   Users.idU = RelUsersGroups.idU ";
00220                 $query .= " AND   Permissions.name = '".$permission."' ";
00221                 $query .= " AND   Permissions.idM = ".$mod;
00222                 $query .= " AND   RelRolesPermissions.idP = Permissions.idP ";
00223                 $query .= " AND   RelUsersGroups.idR = RelRolesPermissions.idR";
00224 
00225                 $dbObj->Query($query);
00226 
00227                 return $dbObj;
00228         }
00229 
00236         function AddUser($idU, $idR)
00237         {
00238                 $ugrsystem =& JUgrSystem::GetInstance();
00239                 
00240                 if ($this->IsGlobal())
00241                 {
00242                         return JSystem::RaiseError(new JSystemError(ERROR_IS_GLOBAL_GROUP));
00243                 }
00244                 elseif ($this->HasUser($idU))
00245                 {
00246                         return JSystem::RaiseError(
00247                                         new JSystemError(ERROR_ALREADY_IN_GROUP));
00248                 }
00249                 elseif (JSystem::IsError($ugrsystem->GetUser($idU)))
00250                 {
00251                         return JSystem::RaiseError(new JSystemError(ERROR_USER_NOT_FOUND));
00252                 }               
00253                 elseif (JSystem::IsError($ugrsystem->GetRole($idR)))
00254                 {
00255                         return JSystem::RaiseError(new JSystemError(ERROR_ROLE_NOT_FOUND));
00256                 }
00257                 else
00258                 {
00259                         $dbObj = new DBJames();
00260                         
00261                         $query  = " INSERT INTO RelUsersGroups ";
00262                         $query .= " (idU, idG, idR) VALUES ";
00263                         $query .= "(".$idU.", ".$this->GetId().", ".$idR.")";
00264                         
00265                         $dbObj->Execute($query);
00266                 }               
00267                 return null;
00268         }
00269         
00270         
00275         function RemoveUser($idU)
00276         {
00277                 if (! $this->HasUser($idU))
00278                 {
00279                         return JSystem::RaiseError(
00280                                                 new JSystemError(ERROR_NOT_IN_GROUP));
00281                 }
00282                 else
00283                 {
00284                         $dbObj = new DBJames();
00285                         
00286                         $query  = " DELETE FROM RelUsersGroups ";
00287                         $query .= " WHERE idU = ".$idU;
00288                         $query .= " AND idG = ".$this->GetId();
00289                         
00290                         $dbObj->Execute($query);
00291                 }               
00292         }
00293         
00294 
00295 //--------------------------- Modules methods ---------------------------------
00296         
00305         function &GetModules()
00306         {
00307                 $modules =& Modules::GetInstance();
00308                 
00309                 if($this->data == null)
00310                         return JSystem::RaiseError(new JSystemError(ERROR_NOT_INITIALIZED));
00311 
00312                 $result = array();
00313                 
00314                 if (count($this->modules) > 0)
00315                 {
00316                         foreach ($this->modules as $idRmg => $cont)
00317                         {
00318                                 $result[$idRmg] =& $modules->GetModule($cont["id"]);
00319                         }
00320                 }               
00321                         
00322                 return $result;
00323         }
00324 
00330         function HasModule($idM)
00331         {
00332                 if($this->data == null)
00333                         return JSystem::RaiseError(
00334                                         new JSystemError(ERROR_NOT_INITIALIZED));
00335 
00336                 $dbObj = new DBJames();
00337 
00338                 $query  = " SELECT RelModulesGroups.idRMG, ";
00339                 $query .= " RelModulesGroups.rmgName FROM ";
00340                 $query .= " Modules,RelModulesGroups ";
00341                 $query .= " WHERE RelModulesGroups.idG = ".$this->GetId();
00342                 $query .= " AND RelModulesGroups.idM = ".$idM;
00343 
00344                 $dbObj->Query($query);
00345 
00346                 if ($dbObj->numRows > 0)
00347                         return true;
00348                 else                    
00349                         return false;
00350         }
00351         
00357         function GetModuleName($idRmg)
00358         {
00359                 return $this->modules[$idRmg]["name"];
00360         }
00361         
00367         function SetModuleName($idRmg, $name)
00368         {
00369                 // Just set the module name if the module belongs to the group
00370                 if (isset($this->modules[$idRmg]))
00371                 {
00372                         // If no name is given, we will use the specified by the module
00373                         // config file
00374                         if ($name == "")
00375                         {
00376                                 $modules =& Modules::GetInstance();
00377                                 $module =& $modules->GetModule($this->modules[$idRmg]["idM"]);
00378                                 $info =& $module->GetInformation();
00379                                 $name = I18N::Language($info->GetScreenNames());
00380                         }
00381                         
00382                         // Update the values in the db
00383                         $dbObj = new DBJames();
00384                         
00385                         $query  = "UPDATE RelModulesGroups ";
00386                         $query .= " SET rmgName=".$name;
00387                         $query .= " WHERE idRMG=".$idRmg;
00388                                 
00389                         $dbObj->Execute($query);                        
00390                         
00391                         // Update the group object
00392                         $this->_UpdateModulesList();
00393                 }
00394         }
00395         
00396         
00403         function AddModule($idM, $name="")
00404         {
00405                 $modules =& Modules::GetInstance();
00406                 $module =& $modules->GetModule($idM);
00407                 if (JSystem::IsError($modules))
00408                 {
00409                         return JSystem::RaiseError(
00410                                                 new JSystemError(ERROR_MODULE_NOT_FOUND));
00411                 }
00412                 else
00413                 {
00414                         // If no name is given, we will use the specified by the module
00415                         // config file
00416                         if ($name == "")
00417                         {
00418                                 $info =& $module->GetInformation();
00419                                 $name = I18N::Language($info->GetScreenNames());
00420                         }
00421                                 
00422                         // We store it in RelModulesGroups
00423                         $dbObj = new DBJames();
00424         
00425                         $query  = "INSERT INTO RelModulesGroups ";
00426                         $query .= " (idM, idG, rmgName) VALUES ";
00427                         $query .= " (".$idM.", ".$this->GetId().", '".$name."') ";
00428                                 
00429                         $dbObj->Execute($query);
00430                         
00431                         $dbObj->SetInsertId();
00432                         
00433                         $newId = $dbObj->newID;
00434 
00435                         // Updates the group object                     
00436                         $this->_UpdateModulesList();
00437                         
00438                         return $newId;
00439                 }
00440         }
00441         
00442         
00447         function RemoveModule($idRMG)
00448         {
00449                 $dbObj = new DBJames();
00450                 
00451                 $query  = " DELETE FROM RelModulesGroups ";
00452                 $query .= " WHERE idRMG = ".$idRMG;
00453                 
00454                 $dbObj->Execute($query);
00455                 
00456                 $this->_UpdateModulesList();
00457         }
00458 
00459         
00460 //------------------------- Subgroups methods ---------------------------------
00461 
00466         function &GetSubgroups()
00467         {
00468                 if($this->data == null)
00469                         return JSystem::RaiseError(
00470                                         new JSystemError(ERROR_NOT_INITIALIZED));
00471 
00472                 $ugrsystem =& JUgrSystem::GetInstance();
00473                 $dbObj = new DBJames();
00474                 
00475                 $query  = " SELECT * FROM Groups ";
00476                 $query .= " WHERE Groups.idParent = ".$this->GetId();
00477 
00478                 $dbObj->Query($query);
00479 
00480                 $result = array();
00481                 
00482                 while (!$dbObj->EOF)
00483                 {       
00484                         $idG = $dbObj->row["idG"];
00485                         $result[$idG] =& $ugrsystem->GetGroup($idG);
00486                         $dbObj->Next();
00487                 }               
00488                         
00489                 return $result;
00490         }
00491 
00492 
00499         function &GetSubgroupsOfUser($idU)
00500         {
00501                 if($this->data == null)
00502                         return JSystem::RaiseError(
00503                                         new JSystemError(ERROR_NOT_INITIALIZED));
00504 
00505                 $ugrsystem =& JUgrSystem::GetInstance();
00506                 $dbObj = new DBJames();         
00507         
00508                 $query  = " SELECT Groups.*, RelUsersGroups.idR FROM ";
00509                 $query .= " Groups, RelUsersGroups ";
00510                 $query .= " WHERE Groups.idParent = ".$this->GetId();
00511                 $query .= " AND RelUsersGroups.idG = Groups.idG ";
00512                 $query .= " AND RelUsersGroups.idU = " .$idU;
00513                 $query .= " ORDER BY Groups.idG";
00514           
00515                 $dbObj->Query($query);
00516 
00517                 $result = array();
00518                 
00519                 while (!$dbObj->EOF)
00520                 {       
00521                         $idG = $dbObj->row["idG"];
00522                         $result[$idG] =& $ugrsystem->GetGroup($idG);
00523                         $dbObj->Next();
00524                 }               
00525                         
00526                 return $result;
00527         }
00528 
00529         
00535         function &AddSubgroup($name)
00536         {
00537                 return $this->Create($name, $this->IsGlobal(), $this->GetId());
00538         }
00539         
00540 
00541 
00542 //-------------------- Groups admin methods (static) -------------------------
00543         
00551         function &Create($name, $global=false, $parent=null)
00552         {
00553                 $ugrsystem =& JUgrSystem::GetInstance();
00554                 
00555                 if ($parent != null && $parent !=0  &&
00556                         JSystem::IsError($ugrsystem->GetGroup($parent)))
00557                 {
00558                         return JSystem::RaiseError(
00559                                         new JSystemError(ERROR_GROUP_NOT_FOUND));
00560                 }
00561                 else
00562                 {
00563                         // Set the value for the top level groups
00564                         if ($parent === null)
00565                                 $parent = 0;
00566                         
00567                         if ($global) $glob = 1;
00568                         else $glob = 0;
00569                                 
00570                         $dbObj = new DBJames();
00571                         
00572                         $query  = " INSERT INTO Groups ";
00573                         $query .= " (name, idParent, isGlobal) VALUES ";
00574                         $query .= " ('".$name."', ".$parent.", ".$glob.") ";
00575                         
00576                         $dbObj->Execute($query);
00577                         
00578                         $dbObj->SetInsertId();
00579                         
00580                         $group =& new Group($dbObj->newID);
00581                         if (JSystem::IsError($group))
00582                                 return JSystem::GetLastError();
00583                         else
00584                                 return $group;
00585                 }
00586         }
00587         
00588         
00593         function Remove($idG)
00594         {
00595                 $ugrsystem =& JUgrSystem::GetInstance();
00596                 $group =& $ugrsystem->GetGroup($idG);
00597                 
00598                 if (JSystem::IsError($group))
00599                 {
00600                         return JSystem::RaiseError(new JSystemError(ERROR_GROUP_NOT_FOUND));
00601                 }
00602                 elseif (count($group->GetSubgroups()) > 0)
00603                 {
00604                         return JSystem::RaiseError(
00605                                                 new JSystemError(ERROR_GROUP_HAS_SUBGROUPS));
00606                 }
00607                 else
00608                 {
00609                         $dbObj = new DBJames();
00610                         
00611                         // First we remove all users from this group.
00612                         $query  = " DELETE FROM RelUsersGroups ";
00613                         $query .= " WHERE idG=".$group->GetId();
00614                         $dbObj->Execute($query);
00615                         
00616                         // Then, we delete all modules from it.
00617                         $query  = " DELETE FROM RelModulesGroups ";
00618                         $query .= " WHERE idG = ".$group->GetId();
00619                         $dbObj->Execute($query);
00620                         
00621                         // Finally, we remove the group.
00622                         $query  = " DELETE FROM Groups ";
00623                         $query .= " WHERE idG = ". $group->GetId();
00624                         $dbObj->Execute($query);
00625                 }
00626         }
00627         
00636         function GetAllGroupsId($parent = null, $global=null)
00637         {
00638                 $dbObj = new DBJames();         
00639                 $query  = " SELECT * FROM Groups ";
00640                 $query .= " WHERE 1 ";
00641                 
00642                 if ($parent !== null)
00643                         $query .= " AND idParent=".$parent;
00644 
00645                 if ($global !== null)
00646                 {
00647                         if ($global) $glob = 1;
00648                         else $glob = 0;
00649                         $query .= " AND isGlobal=".$glob;
00650                 }
00651                 
00652                 $dbObj->Query($query);
00653                 
00654                 return $dbObj;
00655         }                               
00656 
00657 }
00658 ?>

Generated on Wed Nov 19 20:29:34 2003 for James by doxygen 1.3.4