ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.arConverter.php
Go to the documentation of this file.
1<?php
2require_once(dirname(__FILE__) . '/../class.arFieldList.php');
3
16class arConverter {
17
18 const REGEX = "/([a-z]*)\\(([0-9]*)\\)/us";
22 protected static $field_map = array(
23 'varchar' => arField::FIELD_TYPE_TEXT,
26 'tinyint' => arField::FIELD_TYPE_INTEGER,
27 'smallint' => arField::FIELD_TYPE_INTEGER,
28 'mediumint' => arField::FIELD_TYPE_INTEGER,
30 );
34 protected static $length_map = array(
42 11 => 4,
43 4 => 1,
44 )
45 );
49 protected $table_name = '';
53 protected $class_name = '';
57 protected $structure = array();
61 protected $ids = array();
62
63
68 public function __construct($table_name, $class_name) {
69 $this->setClassName($class_name);
70 $this->setTableName($table_name);
71 $this->readStructure();
72 }
73
74
75 public function readStructure() {
76 $sql = 'DESCRIBE ' . $this->getTableName();
77 $res = self::getDB()->query($sql);
78 while ($data = self::getDB()->fetchObject($res)) {
79 $this->addStructure($data);
80 }
81 }
82
83
84 public function downloadClassFile() {
85
86
87 $header = "<?php
88require_once('./Services/ActiveRecord/class.ActiveRecord.php');
89
96class {CLASS_NAME} extends ActiveRecord {
97
102 static function returnDbTableName() {
103 return '{TABLE_NAME}';
104 }
105
109 public function getConnectorContainerName() {
110 return '{TABLE_NAME}';
111 }
112";
113 $txt = str_replace('{CLASS_NAME}', $this->getClassName(), $header);
114 $txt = str_replace('{TABLE_NAME}', $this->getTableName(), $txt);
115 $all_members = '';
116 foreach ($this->getStructure() as $str) {
117
118 $member = "/**
119 * @var {DECLARATION}
120 *\n";
121 foreach ($this->returnAttributesForField($str) as $name => $value) {
122 $member .= ' * @con_' . $name . ' ' . $value . "\n";
123 }
124
125 $member .= "*/
126 protected \${FIELD_NAME};
127
128 ";
129
130 $member = str_replace('{FIELD_NAME}', $str->field, $member);
131 $member = str_replace('{DECLARATION}', ' ', $member);
132
133 $all_members .= $member;
134 }
135 $txt = $txt . $all_members . '
136}
137
138?>';
139
140 // echo '<pre>' . print_r(, 1) . '</pre>';
141
142 header('Content-type: application/x-httpd-php');
143 header("Content-Disposition: attachment; filename=\"class." . $this->getClassName() . ".php\"");
144 echo $txt;
145 exit;
146 }
147
148
154 protected function returnAttributesForField(stdClass $field) {
155 $attributes = array();
156 $attributes[arFieldList::HAS_FIELD] = 'true';
157 $attributes[arFieldList::FIELDTYPE] = self::lookupFieldType($field->type);
158 $attributes[arFieldList::LENGTH] = self::lookupFieldLength($field->type);
159
160 if ($field->null == 'NO') {
161 $attributes[arFieldList::IS_NOTNULL] = 'true';
162 }
163
164 if ($field->key == 'PRI') {
165 $attributes[arFieldList::IS_PRIMARY] = 'true';
166 $attributes[arFieldList::IS_UNIQUE] = 'true';
167 }
168
169 return $attributes;
170 }
171
172
178 protected static function lookupFieldType($string) {
179 preg_match(self::REGEX, $string, $matches);
180
181 return self::$field_map[$matches[1]];
182 }
183
184
190 protected static function lookupFieldLength($string) {
191 $field_type = self::lookupFieldType($string);
192
193 preg_match(self::REGEX, $string, $matches);
194
195 if (self::$length_map[$field_type][$matches[2]]) {
196 return self::$length_map[$field_type][$matches[2]];
197 } else {
198 return $matches[2];
199 }
200 }
201
202
206 public static function getDB() {
207 global $ilDB;
208
213 return $ilDB;
214 }
215
216
220 public function setTableName($table_name) {
221 $this->table_name = $table_name;
222 }
223
224
228 public function getTableName() {
229 return $this->table_name;
230 }
231
232
236 public function setStructure($structure) {
237 $this->structure = $structure;
238 }
239
240
244 public function getStructure() {
245 return $this->structure;
246 }
247
248
252 public function addStructure(stdClass $structure) {
253 if(!in_array($structure->field, $this->ids)) {
254 $this->structure[] = $structure;
255 $this->ids[] = $structure->field;
256 }
257 }
258
259
263 public function setClassName($class_name) {
264 $this->class_name = $class_name;
265 }
266
267
271 public function getClassName() {
272 return $this->class_name;
273 }
274}
275
276?>
const FIELD_TYPE_TEXT
const FIELD_TYPE_FLOAT
const FIELD_TYPE_DATE
const FIELD_TYPE_TIME
const FIELD_TYPE_INTEGER
const FIELD_TYPE_TIMESTAMP
const FIELD_TYPE_CLOB
$txt
Definition: error.php:12
$header
$data
exit
Definition: login.php:54
global $ilDB