source: trunk/Common/Model.php@ 373

Last change on this file since 373 was 373, checked in by chronos, 13 years ago
  • Přidáno: Různé knihovny pro zpracování HTML kódu, tabulek, stránkování, zasílání emailů, aj.
  • Přidáno: Kód zpracování fronty emailů přesunut z Global do modulu EmailQueue.
File size: 7.5 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
34 function __construct($Database, $System)
35 {
36 $this->Database = &$Database;
37 $this->System = &$System;
38 $this->AddPropertyDateTime('TimeCreate');
39 $this->AddPropertyOneToMany('UserCreate', 'User');
40 $this->AddPropertyDateTime('TimeModify');
41 $this->AddPropertyOneToMany('UserModify', 'User');
42 $this->AddPropertyOneToMany('Module', 'Module');
43 }
44
45 function AddPropertyDateTime($Name)
46 {
47 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDateTime);
48 }
49
50 function AddPropertyDate($Name)
51 {
52 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDate);
53 }
54
55 function AddPropertyTime($Name)
56 {
57 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyTime);
58 }
59
60 function AddPropertyOneToMany($Name, $TargetModel)
61 {
62 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyOneToMany, 'TargetModel' => $TargetModel);
63 }
64
65 function AddPropertyText($Name)
66 {
67 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText);
68 }
69
70 function AddPropertyText8($Name)
71 {
72 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText8);
73 }
74
75 function AddPropertyText16($Name)
76 {
77 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText16);
78 }
79
80 function AddPropertyText24($Name)
81 {
82 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText24);
83 }
84
85 function AddPropertyText32($Name)
86 {
87 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyText32);
88 }
89
90 function AddPropertyString($Name)
91 {
92 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyString);
93 }
94
95 function AddPropertyInteger($Name)
96 {
97 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger);
98 }
99
100 function AddPropertyInteger8($Name)
101 {
102 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger8);
103 }
104
105 function AddPropertyInteger16($Name)
106 {
107 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger16);
108 }
109
110 function AddPropertyInteger24($Name)
111 {
112 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger24);
113 }
114
115 function AddPropertyInteger32($Name)
116 {
117 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger32);
118 }
119
120 function AddPropertyInteger64($Name)
121 {
122 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyInteger64);
123 }
124
125 function AddPropertyFloat($Name)
126 {
127 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyFloat);
128 }
129
130 function AddPropertyDouble($Name)
131 {
132 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyDouble);
133 }
134
135 function AddPropertyBoolean($Name)
136 {
137 $this->Properties[] = array('Name' => $Name, 'Type' => PropertyBoolean);
138 }
139
140 function AddPropertyManyToMany($TargetModel, $Relation, $ColumOwn, $ColumnTarget)
141 {
142 $this->Properties[] = array('Type' => PropertyManyToMany,
143 'TargetModel' => $TargetModel, 'Relation' => $Relation, 'ColumnOwn' => $ColumnOwn,
144 'ColumnTarget' => $ColumnTarget);
145 }
146
147 function Install()
148 {
149 $this->Database->insert('SystemModel', array('Name' => $this->Name, 'Module' => $this->Module->Id));
150 $this->Id = $this->Database->insert_id;
151
152 $Query = 'CREATE TABLE IF NOT EXISTS `'.$this->Name.'` ('.
153 '`Id` int(11) NOT NULL AUTO_INCREMENT,';
154 foreach($this->Properties as $Property)
155 {
156 $this->Database->insert('SystemModelProperty', array('Name' => $Property['Name'],
157 'Model' => $this->Id, 'Type' => $Property['Type']));
158 if($Property['Type'] == PropertyDateTime)
159 $Query .= '`'.$Property['Name'].'` DATETIME NOT NULL,';
160 else if($Property['Type'] == PropertyDate)
161 $Query .= '`'.$Property['Name'].'` DATE NOT NULL,';
162 else if($Property['Type'] == PropertyTime)
163 $Query .= '`'.$Property['Name'].'` TIME NOT NULL,';
164 else if($Property['Type'] == PropertyString)
165 $Query .= '`'.$Property['Name'].'` VARCHAR(255) COLLATE utf8_general_ci NOT NULL,';
166 else if($Property['Type'] == PropertyText8)
167 $Query .= '`'.$Property['Name'].'` TINYTEXT COLLATE utf8_general_ci NOT NULL,';
168 else if($Property['Type'] == PropertyText16)
169 $Query .= '`'.$Property['Name'].'` TEXT COLLATE utf8_general_ci NOT NULL,';
170 else if($Property['Type'] == PropertyText24)
171 $Query .= '`'.$Property['Name'].'` MEDIUMTEXT COLLATE utf8_general_ci NOT NULL,';
172 else if($Property['Type'] == PropertyText32)
173 $Query .= '`'.$Property['Name'].'` LONGTEXT COLLATE utf8_general_ci NOT NULL,';
174 else if($Property['Type'] == PropertyInteger8)
175 $Query .= '`'.$Property['Name'].'` TINYINT(4) NOT NULL,';
176 else if($Property['Type'] == PropertyInteger16)
177 $Query .= '`'.$Property['Name'].'` SMALLINT(6) NOT NULL,';
178 else if($Property['Type'] == PropertyInteger24)
179 $Query .= '`'.$Property['Name'].'` MEDIUMINT(9) NOT NULL,';
180 else if($Property['Type'] == PropertyInteger32)
181 $Query .= '`'.$Property['Name'].'` INT(11) NOT NULL,';
182 else if($Property['Type'] == PropertyInteger64)
183 $Query .= '`'.$Property['Name'].'` BIGINT(20) NOT NULL,';
184 else if($Property['Type'] == PropertyBoolean)
185 $Query .= '`'.$Property['Name'].'` BOOLEAN NOT NULL,';
186 else if($Property['Type'] == PropertyFloat)
187 $Query .= '`'.$Property['Name'].'` FLOAT NOT NULL,';
188 else if($Property['Type'] == PropertyDouble)
189 $Query .= '`'.$Property['Name'].'` DOUBLE NOT NULL,';
190 else if($Property['Type'] == PropertyOneToMany)
191 $Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'.
192 'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),';
193 else if($Property['Type'] == PropertyManyToMany) ;
194 // Create many-to-many table
195 //$Query .= '`'.$Property['Name'].'` INT(255) NOT NULL,'.
196 // 'KEY `'.$Property['Name'].'` (`'.$Property['Name'].'`),';
197 }
198 $Query .= 'PRIMARY KEY (`Id`)'.
199 ') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=1 ;';
200 $this->Database->query($Query);
201 foreach($this->Properties as $Property)
202 {
203 if($Property['Type'] == PropertyOneToMany)
204 $this->Database->query('ALTER TABLE `'.$this->Name.'` ADD CONSTRAINT '.
205 '`'.$this->Name.'_ibfk_'.$Property['Name'].'` FOREIGN KEY (`'.$Property['TargetModel'].'`) REFERENCES `'.$Property['TargetModel'].'` (`Id`);');
206 }
207 }
208
209 function UnInstall()
210 {
211 foreach($Model->Properties as $Property)
212 {
213 if($Property['Type'] == PropertyManyToMany)
214 ; // Delete many-to-many table
215 }
216 $this->Database->query('DROP TABLE IF EXISTS `'.$this->Name.'`');
217 }
218}
219
220?>
Note: See TracBrowser for help on using the repository browser.