ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.arConverter.php
Go to the documentation of this file.
1 <?php
2 require_once(dirname(__FILE__) . '/../class.arFieldList.php');
3 
17 {
18  const REGEX = "/([a-z]*)\\(([0-9]*)\\)/us";
22  protected static $field_map = array(
23  'varchar' => arField::FIELD_TYPE_TEXT,
24  'char' => arField::FIELD_TYPE_TEXT,
26  'tinyint' => arField::FIELD_TYPE_INTEGER,
27  'smallint' => arField::FIELD_TYPE_INTEGER,
28  'mediumint' => arField::FIELD_TYPE_INTEGER,
29  'bigint' => arField::FIELD_TYPE_INTEGER,
30  );
34  protected static $length_map = array(
35  arField::FIELD_TYPE_TEXT => false,
36  arField::FIELD_TYPE_DATE => false,
37  arField::FIELD_TYPE_TIME => false,
39  arField::FIELD_TYPE_CLOB => false,
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 
69  {
70  $this->setClassName($class_name);
71  $this->setTableName($table_name);
72  $this->readStructure();
73  }
74 
75 
76  public function readStructure()
77  {
78  $sql = 'DESCRIBE ' . $this->getTableName();
79  $res = self::getDB()->query($sql);
80  while ($data = self::getDB()->fetchObject($res)) {
81  $this->addStructure($data);
82  }
83  }
84 
85 
86  public function downloadClassFile()
87  {
88  $header = "<?php
89 require_once('./Services/ActiveRecord/class.ActiveRecord.php');
90 
97 class {CLASS_NAME} extends ActiveRecord {
98 
103  static function returnDbTableName() {
104  return '{TABLE_NAME}';
105  }
106 
110  public function getConnectorContainerName() {
111  return '{TABLE_NAME}';
112  }
113 ";
114  $txt = str_replace('{CLASS_NAME}', $this->getClassName(), $header);
115  $txt = str_replace('{TABLE_NAME}', $this->getTableName(), $txt);
116  $all_members = '';
117  foreach ($this->getStructure() as $str) {
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  {
156  $attributes = array();
158  $attributes[arFieldList::FIELDTYPE] = self::lookupFieldType($field->type);
159  $attributes[arFieldList::LENGTH] = self::lookupFieldLength($field->type);
160 
161  if ($field->null == 'NO') {
163  }
164 
165  if ($field->key == 'PRI') {
168  }
169 
170  return $attributes;
171  }
172 
173 
179  protected static function lookupFieldType($string)
180  {
181  preg_match(self::REGEX, $string, $matches);
182 
183  return self::$field_map[$matches[1]];
184  }
185 
186 
192  protected static function lookupFieldLength($string)
193  {
194  $field_type = self::lookupFieldType($string);
195 
196  preg_match(self::REGEX, $string, $matches);
197 
198  if (self::$length_map[$field_type][$matches[2]]) {
199  return self::$length_map[$field_type][$matches[2]];
200  } else {
201  return $matches[2];
202  }
203  }
204 
205 
209  public static function getDB()
210  {
211  global $DIC;
212  $ilDB = $DIC['ilDB'];
213 
218  return $ilDB;
219  }
220 
221 
225  public function setTableName($table_name)
226  {
227  $this->table_name = $table_name;
228  }
229 
230 
234  public function getTableName()
235  {
236  return $this->table_name;
237  }
238 
239 
243  public function setStructure($structure)
244  {
245  $this->structure = $structure;
246  }
247 
248 
252  public function getStructure()
253  {
254  return $this->structure;
255  }
256 
257 
261  public function addStructure(stdClass $structure)
262  {
263  if (!in_array($structure->field, $this->ids)) {
264  $this->structure[] = $structure;
265  $this->ids[] = $structure->field;
266  }
267  }
268 
269 
273  public function setClassName($class_name)
274  {
275  $this->class_name = $class_name;
276  }
277 
278 
282  public function getClassName()
283  {
284  return $this->class_name;
285  }
286 }
const FIELD_TYPE_FLOAT
__construct($table_name, $class_name)
const FIELD_TYPE_CLOB
static lookupFieldLength($string)
global $DIC
Definition: saml.php:7
const FIELD_TYPE_TEXT
$attributes
returnAttributesForField(stdClass $field)
static lookupFieldType($string)
const FIELD_TYPE_INTEGER
if($format !==null) $name
Definition: metadata.php:146
setStructure($structure)
foreach($_POST as $key=> $value) $res
const FIELD_TYPE_DATE
const FIELD_TYPE_TIMESTAMP
setTableName($table_name)
Add a drawing to the header
Definition: 04printing.php:69
$txt
Definition: error.php:11
Create styles array
The data for the language used.
const FIELD_TYPE_TIME
global $ilDB
setClassName($class_name)
addStructure(stdClass $structure)
Class arConverter.