source: trunk/Common/Model.php@ 378

Last change on this file since 378 was 378, checked in by chronos, 13 years ago
  • Přidáno: Model pro NetworkSegment.
  • Upraveno: Přepracován model systém pro načítání modelů modulů.
File size: 8.7 KB
Line 
1<?php
2
3define('PropertyInteger8', 'Integer8');
4define('PropertyInteger16', 'Integer16');
5define('PropertyInteger24', 'Integer24');
6define('PropertyInteger32', 'Integer32');
7define('PropertyInteger64', 'Integer64');
8
9define('PropertyText8', 'Text8');
10define('PropertyText16', 'Text16');
11define('PropertyText24', 'Text24');
12define('PropertyText32', 'Text32');
13
14define('PropertyDate', 'Date');
15define('PropertyTime', 'Time');
16define('PropertyDateTime', 'DateTime');
17define('PropertyText', 'Text16');
18define('PropertyString', 'String');
19define('PropertyBoolean', 'Boolean');
20define('PropertyInteger', 'Integer32');
21define('PropertyFloat', 'Float');
22define('PropertyDouble', 'Double');
23define('PropertyOneToMany', 'OneToMany');
24define('PropertyManyToMany', 'ManyToMany');
25
26
27class Model
28{
29 var $Database;
30 var $Name;
31 var $Properties;
32 var $System;
33 var $Module;
34 var $Installed;
35
36 function __construct($Database, $System)
37 {
38 $this->Database = &$Database;
39 $this->System = &$System;
40 $this->AddPropertyOneToMany('ItemOwnerModule', 'SystemModule', true);
41 $this->AddPropertyDateTime('ItemTimeCreate', true);
42 $this->AddPropertyOneToMany('ItemUserCreate', 'User', true);
43 $this->AddPropertyDateTime('ItemTimeModify', true);
44 $this->AddPropertyOneToMany('ItemUserModify', 'User', true);
45 }
46
47 function AddPropertyDateTime($Name, $Null = false)
48 {
49 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDateTime,
50 'Null' => $Null);
51 }
52
53 function AddPropertyDate($Name, $Null = false)
54 {
55 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDate,
56 'Null' => $Null);
57 }
58
59 function AddPropertyTime($Name, $Null = false)
60 {
61 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyTime,
62 'Null' => $Null);
63 }
64
65 function AddPropertyOneToMany($Name, $TargetModel, $Null = false)
66 {
67 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyOneToMany,
68 'TargetModel' => $TargetModel, 'Null' => $Null);
69 }
70
71 function AddPropertyText($Name, $Null = false)
72 {
73 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText,
74 'Null' => $Null);
75 }
76
77 function AddPropertyText8($Name, $Null = false)
78 {
79 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText8,
80 'Null' => $Null);
81 }
82
83 function AddPropertyText16($Name, $Null = false)
84 {
85 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText16,
86 'Null' => $Null);
87 }
88
89 function AddPropertyText24($Name, $Null = false)
90 {
91 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText24,
92 'Null' => $Null);
93 }
94
95 function AddPropertyText32($Name, $Null = false)
96 {
97 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText32,
98 'Null' => $Null);
99 }
100
101 function AddPropertyString($Name, $Null = false)
102 {
103 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyString,
104 'Null' => $Null);
105 }
106
107 function AddPropertyInteger($Name, $Null = false)
108 {
109 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger,
110 'Null' => $Null);
111 }
112
113 function AddPropertyInteger8($Name, $Null = false)
114 {
115 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger8,
116 'Null' => $Null);
117 }
118
119 function AddPropertyInteger16($Name, $Null = false)
120 {
121 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger16,
122 'Null' => $Null);
123 }
124
125 function AddPropertyInteger24($Name, $Null = false)
126 {
127 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger24,
128 'Null' => $Null);
129 }
130
131 function AddPropertyInteger32($Name, $Null = false)
132 {
133 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger32,
134 'Null' => $Null);
135 }
136
137 function AddPropertyInteger64($Name, $Null = false)
138 {
139 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger64,
140 'Null' => $Null);
141 }
142
143 function AddPropertyFloat($Name, $Null = false)
144 {
145 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyFloat,
146 'Null' => $Null);
147 }
148
149 function AddPropertyDouble($Name, $Null = false)
150 {
151 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDouble,
152 'Null' => $Null);
153 }
154
155 function AddPropertyBoolean($Name, $Null = false)
156 {
157 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyBoolean,
158 'Null' => $Null);
159 }
160
161 function AddPropertyManyToMany($TargetModel, $Relation, $ColumnOwn, $ColumnTarget, $Null = false)
162 {
163 $this->Properties[] = array('Type' => PropertyManyToMany,
164 'TargetModel' => $TargetModel, 'Name' => $Relation, 'ColumnOwn' => $ColumnOwn,
165 'ColumnTarget' => $ColumnTarget, 'Null' => $Null);
166 }
167
168 function Install()
169 {
170 if($this->Installed) return;
171 $this->Installed = true;
172 $this->Database->insert('SystemModel', array('Name' => $this->Name, 'Module' => $this->Module->Id));
173 $this->Id = $this->Database->insert_id;
174
175 $Query = 'CREATE TABLE IF NOT EXISTS `'.$this->Name.'` ('.
176 '`Id` int(11) NOT NULL AUTO_INCREMENT,';
177 foreach($this->Properties as $Property)
178 {
179 $this->Database->insert('SystemModelProperty', array('Name' => $Property['Name'],
180 'Model' => $this->Id, 'Type' => $Property['Type']));
181 if($Property['Null']) $Null = 'NULL';
182 else $Null = 'NOT NULL';
183 if($Property['Type'] == PropertyDateTime)
184 $Query .= '`'.$Property['Name'].'` DATETIME '.$Null.',';
185 else if($Property['Type'] == PropertyDate)
186 $Query .= '`'.$Property['Name'].'` DATE '.$Null.',';
187 else if($Property['Type'] == PropertyTime)
188 $Query .= '`'.$Property['Name'].'` TIME '.$Null.',';
189 else if($Property['Type'] == PropertyString)
190 $Query .= '`'.$Property['Name'].'` VARCHAR(255) COLLATE utf8_general_ci '.$Null.',';
191 else if($Property['Type'] == PropertyText8)
192 $Query .= '`'.$Property['Name'].'` TINYTEXT COLLATE utf8_general_ci '.$Null.',';
193 else if($Property['Type'] == PropertyText16)
194 $Query .= '`'.$Property['Name'].'` TEXT COLLATE utf8_general_ci '.$Null.',';
195 else if($Property['Type'] == PropertyText24)
196 $Query .= '`'.$Property['Name'].'` MEDIUMTEXT COLLATE utf8_general_ci '.$Null.',';
197 else if($Property['Type'] == PropertyText32)
198 $Query .= '`'.$Property['Name'].'` LONGTEXT COLLATE utf8_general_ci '.$Null.',';
199 else if($Property['Type'] == PropertyInteger8)
200 $Query .= '`'.$Property['Name'].'` TINYINT(4) '.$Null.',';
201 else if($Property['Type'] == PropertyInteger16)
202 $Query .= '`'.$Property['Name'].'` SMALLINT(6) '.$Null.',';
203 else if($Property['Type'] == PropertyInteger24)
204 $Query .= '`'.$Property['Name'].'` MEDIUMINT(9) '.$Null.',';
205 else if($Property['Type'] == PropertyInteger32)
206 $Query .= '`'.$Property['Name'].'` INT(11) '.$Null.',';
207 else if($Property['Type'] == PropertyInteger64)
208 $Query .= '`'.$Property['Name'].'` BIGINT(20) '.$Null.',';
209 else if($Property['Type'] == PropertyBoolean)
210 $Query .= '`'.$Property['Name'].'` BOOLEAN '.$Null.',';
211 else if($Property['Type'] == PropertyFloat)
212 $Query .= '`'.$Property['Name'].'` FLOAT '.$Null.',';
213 else if($Property['Type'] == PropertyDouble)
214 $Query .= '`'.$Property['Name'].'` DOUBLE '.$Null.',';
215 else if($Property['Type'] == PropertyOneToMany)
216 $Query .= '`'.$Property['Name'].'` INT(255)'.$Null.','.
217 'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),';
218 else if($Property['Type'] == PropertyManyToMany) ;
219 // Create many-to-many table
220 //$Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'.
221 // 'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),';
222
223 }
224 $Query .= 'PRIMARY KEY (`Id`)'.
225 ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1 ;';
226 $this->Database->query($Query);
227 foreach($this->Properties as $Property)
228 {
229 if($Property['Type'] == PropertyOneToMany)
230 {
231 if(array_key_exists($Property['TargetModel'], $this->Module->Models))
232 {
233 if(!$this->Module->Models[$Property['TargetModel']]->Installed)
234 $this->Module->Models[$Property['TargetModel']]->Install();
235 }
236 $this->Database->query('ALTER TABLE `'.$this->Name.'` ADD FOREIGN KEY (`'.$Property['Name'].'`) REFERENCES `'.$Property['TargetModel'].'` (`Id`);');
237 }
238 }
239 }
240
241 function UnInstall()
242 {
243 foreach($this->Properties as $Property)
244 {
245 if($Property['Type'] == PropertyManyToMany)
246 ; // Delete many-to-many table
247 }
248 $this->Database->query('DROP TABLE IF EXISTS `'.$this->Name.'`');
249 $this->Installed = false;
250 }
251}
252
253?>
Note: See TracBrowser for help on using the repository browser.