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

Modules.class.php

Go to the documentation of this file.
00001 <?php
00008 include_once("core/system/JBaseObject.class.php");
00009 include_once("core/utils/JSession.class.php");
00010 include_once("core/modules/Module.class.php");
00011 
00012 /* Constants */
00013 define(MODULES_SESSION_VAR, "james-modules");
00014 
00020 class Modules extends JBaseObject
00021 {
00022         var $modulesid;         
00023 
00024         var $modulesnames;      
00025         
00032         function Modules()
00033         {
00034                 $this->_RefreshModules();
00035         }
00036         
00041         function &GetInstance()
00042         {
00043                 static $ob;
00044                 
00045                 if (! isset($ob))
00046                 {
00047                         // We look if it is in session
00048                         if (JSession::ExistsVar(MODULES_SESSION_VAR))
00049                         {
00050                                 $ob = JSession::GetVar(MODULES_SESSION_VAR);
00051                         }
00052                         else
00053                         {
00054                                 $ob = new Modules();
00055                                 $ob->_UpdateSession();
00056                         }
00057                         
00058                         // Doing that thing, we make sure that 
00059                         // $ob->_UpdateSession will be called just before the
00060                         // scripts ends its execution, and the changes made in
00061                         // the object will be stored in session.
00062                         register_shutdown_function(array(&$ob, _UpdateSession));
00063                 }
00064                 return $ob;
00065         }
00066         
00070         function _UpdateSession()
00071         {
00072                 JSession::SetVar(MODULES_SESSION_VAR, $this);
00073         }
00074         
00078         function _RefreshModules()
00079         {
00080                 $modules = Module::GetAllModulesId();
00081                 
00082                 $this->modulesid = array();
00083                 $this->modulesnames = array();
00084                 
00085                 while (! $modules->EOF)
00086                 {
00087                         $idM = $modules->row["idM"];
00088                         $module =& new Module($idM);
00089                         $this->modulesid[$idM] =& $module;
00090                         $this->modulesnames[$module->GetName()] = $idM;
00091                         
00092                         $modules->Next();
00093                 }
00094                 
00095                 $this->_UpdateSession();
00096         }
00097         
00102         function &GetAllModules()
00103         {
00104                 return $this->modulesid;
00105         }
00106 
00115         function &GetModule($id, $refresh=false)
00116         {
00117                 $module =& $this->_AccessModule($id, $refresh);
00118                         
00119                 if (JSystem::IsError($module))
00120                 {
00121                         // Try refreshing the info
00122                         $this->_RefreshModules();
00123                         $module =& $this->_AccessModule($id, $refresh);
00124                         
00125                         if (JSystem::IsError($module))
00126                         {
00127                                 return JSystem::RaiseError(
00128                                                                 new JSystemError(ERROR_MODULE_NOT_FOUND));
00129                         }
00130                         else
00131                                 return $module;
00132                 }
00133                 else
00134                         return $module;
00135         }
00136         
00145         function &_AccessModule($id, $refresh=false)
00146         {
00147                 // Check if the id is the idM of the module
00148                 if (isset($this->modulesid[$id]))
00149                 {
00150                         $idM = $id;
00151                 }
00152                 // Or if it is its name
00153                 else if (isset($this->modulesnames[$id]))
00154                 {
00155                         $idM = $this->modulesnames[$id];
00156                 }
00157                 else
00158                 {
00159                         return JSystem::RaiseError(
00160                                         new JSystemError(ERROR_MODULE_NOT_FOUND));
00161                 }
00162                         
00163                 // If it was a valid module...                  
00164                 if ($refresh)
00165                 {
00166                         $module =& new Module($idM);
00167                         $this->modulesid[$idM] =& $module;
00168                         $this->modulesnames[$module->GetName()] = $idM;
00169                         $this->_UpdateSession();
00170                 }
00171                         
00172                 return $this->modulesid[$idM];
00173         }
00174         
00180         function &InstallModule($path)
00181         {
00182                 include_once("core/modules/ModuleInstall.class.php");
00183                 
00184                 $minstall = new ModuleInstall($path);
00185                 if (JSystem::IsError($minstall))
00186                 {
00187                         $err = JSystem::GetLastError();
00188                         return $err;
00189                 }
00190 
00191                 $name = $minstall->Install();
00192                 if (JSystem::IsError($name))
00193                 {
00194                         return $name;
00195                 }
00196                 else
00197                 {
00198                         $this->_RefreshModules();
00199                         $module =& $this->GetModule($name);
00200                 }
00201                 
00202                 return $module;
00203         }
00204 }
00205 ?>

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