Changeset 341 for trunk/Modules/Module.php
- Timestamp:
- Jan 17, 2012, 8:40:51 AM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/Module.php
r340 r341 1 1 <?php 2 3 include_once(dirname(__FILE__).'/../Model.php'); 2 4 3 5 class Module … … 9 11 var $Description; 10 12 var $Dependencies; 13 var $Models; 14 var $Database; 15 var $Installed; 16 17 function __construct($Database) 18 { 19 $this->Database = &$Database; 20 } 11 21 12 22 function Install() 13 { 23 { 24 DebugLog('Installing module '.$this->Name.'...'); 25 parent::Install(); 26 foreach($this->Models as $ModelName) 27 { 28 $Model = new $ModelName(); 29 $Query = 'CREATE TABLE IF NOT EXISTS `'.$ModelName.'` ( 30 `Id` int(11) NOT NULL AUTO_INCREMENT,'; 31 foreach($Model->Properties as $Property) 32 { 33 if($Property['Type'] == PropertyDateTime) 34 $Query .= '`'.$Property['Name'].'` DATETIME NOT NULL,'; 35 else if($Property['Type'] == PropertyString) 36 $Query .= '`'.$Property['Name'].'` VARCHAR(255) COLLATE utf8_general_ci NOT NULL,'; 37 else if($Property['Type'] == PropertyText) 38 $Query .= '`'.$Property['Name'].'` TEXT COLLATE utf8_general_ci NOT NULL,'; 39 else if($Property['Type'] == PropertyInteger) 40 $Query .= '`'.$Property['Name'].'` INT(11) NOT NULL,'; 41 else if($Property['Type'] == PropertyFloat) 42 $Query .= '`'.$Property['Name'].'` FLOAT NOT NULL,'; 43 else if($Property['Type'] == PropertyOneToMany) 44 $Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'. 45 'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),'; 46 else if($Property['Type'] == PropertyManyToOne) 47 $Query .= ''; 48 else if($Property['Type'] == PropertyManyToMany) 49 ; // Create many-to-many table 50 //$Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'. 51 // 'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),'; 52 } 53 $Query .= 'PRIMARY KEY (`Id`),'. 54 ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1 ;'; 55 $this->Database->query($Query); 56 } 57 unset($Model); 14 58 } 15 59 16 60 function UnInstall() 17 61 { 62 DebugLog('Uninstalling module '.$this->Name.'...'); 63 parent::UnInstall(); 64 foreach($this->Models as $ModelName) 65 { 66 $Model = new $ModelName(); 67 if($Model['Type'] == PropertyManyToMany) 68 ; // Delete many-to-many table 69 $this->Database->query('DROP TABLE IF EXISTS `'.$ModelName.'`'); 70 unset($Model); 71 } 72 } 73 } 74 75 class ModularSystem 76 { 77 var $Database; 78 var $Modules = array(); 79 80 function __construct($Database) 81 { 82 $this->Database = &$Database; 83 $DbResult = $this->Database->query('SELECT * FROM Module WHERE Installed=1'); 84 while($Module = $DbResult->fetch_array()) 85 { 86 include_once('Modules/'.$File.'/'.$File.'.php'); 87 $this->Modules[] = new $Module['Name']($this->Database); 88 } 89 } 90 91 function Install() 92 { 93 DebugLog('Installing modular system core...'); 94 $this->Database->query('CREATE TABLE IF NOT EXISTS `Module` ( 95 `Id` int(11) NOT NULL AUTO_INCREMENT, 96 `Name` varchar(255) COLLATE utf8_czech_ci NOT NULL, 97 `Creator` varchar(255) COLLATE utf8_czech_ci NOT NULL, 98 `Version` varchar(255) COLLATE utf8_czech_ci NOT NULL, 99 `License` varchar(255) COLLATE utf8_czech_ci NOT NULL, 100 `Installed` int(11) NOT NULL, 101 PRIMARY KEY (`Id`) 102 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci AUTO_INCREMENT=1 ;'); 103 foreach($this->Modules as $Module) 104 { 105 $Module->Install(); 106 } 107 } 108 109 function UnInstall() 110 { 111 DebugLog('Uninstalling modular system core...'); 112 foreach($this->Modules as $Module) 113 $Module->UnInstall(); 114 $this->Database->query('DROP TABLE IF EXISTS `Module`'); 115 } 116 117 function ReloadList() 118 { 119 // Load list of modules from database 120 $Modules = array(); 121 $DbResult = $this->Database->query('SELECT * FROM Module'); 122 while($DbRow = $DbResult->fetch_assoc()) 123 $Modules[$DbRow['Name']] = $DbRow; 124 125 // Load list of modules on disk 126 $ModulesOnDisk = array(); 127 $Files = scandir('Modules'); 128 foreach($Files as $File) 129 if(is_dir('Modules/'.$File) and ($File != '.') and ($File != '..')) 130 { 131 DebugLog($File.','); 132 $ModulesOnDisk[] = $File; 133 } 134 135 // Add new 136 foreach($ModulesOnDisk as $ModuleName) 137 if(!array_key_exists($ModuleName, $Modules)) 138 { 139 DebugLog('Adding module '.$ModuleName.' to list'); 140 include_once('Modules/'.$ModuleName.'/'.$ModuleName.'.php'); 141 $ModuleClassName = 'Module'.$ModuleName; 142 if(class_exists($ModuleClassName)) 143 { 144 $Module = new $ModuleClassName($this->Database); 145 $this->Database->insert('Module', array('Name' => $Module->Name, 146 'Version' => $Module->Version, 'Creator' => $Module->Creator, 147 'Description' => $Module->Description, 'License' => $Module->License, 148 'Installed' => 0)); 149 unset($Module); 150 } else throw new Exception('Missing class '.$ModuleClassName.' in module '.$ModuleName); 151 } 152 153 // Remove missing 154 foreach($Modules as $Module) 155 if($Module['Installed'] == 0) 156 { 157 DebugLog('Removing module '.$ModuleName.' from list'); 158 $this->Database->query('DELETE FROM Module WHERE Id = '.$Module['Id']); 159 } 18 160 } 19 161 }
Note:
See TracChangeset
for help on using the changeset viewer.