ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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') {
167  }
168 
169  return $attributes;
170  }
171 
172 
178  protected static function lookupFieldType($string)
179  {
180  preg_match(self::REGEX, $string, $matches);
181 
182  return self::$field_map[$matches[1]];
183  }
184 
185 
191  protected static function lookupFieldLength($string)
192  {
193  $field_type = self::lookupFieldType($string);
194 
195  preg_match(self::REGEX, $string, $matches);
196 
197  if (self::$length_map[$field_type][$matches[2]]) {
198  return self::$length_map[$field_type][$matches[2]];
199  } else {
200  return $matches[2];
201  }
202  }
203 
204 
208  public static function getDB()
209  {
210  global $DIC;
211  $ilDB = $DIC['ilDB'];
212 
217  return $ilDB;
218  }
219 
220 
224  public function setTableName($table_name)
225  {
226  $this->table_name = $table_name;
227  }
228 
229 
233  public function getTableName()
234  {
235  return $this->table_name;
236  }
237 
238 
242  public function setStructure($structure)
243  {
244  $this->structure = $structure;
245  }
246 
247 
251  public function getStructure()
252  {
253  return $this->structure;
254  }
255 
256 
261  {
262  if (!in_array($structure->Field, $this->ids)) {
263  $this->structure[] = $structure;
264  $this->ids[] = $structure->Field;
265  }
266  }
267 
268 
272  public function setClassName($class_name)
273  {
274  $this->class_name = $class_name;
275  }
276 
277 
281  public function getClassName()
282  {
283  return $this->class_name;
284  }
285 }
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
returnAttributesForField(stdClass $field)
static lookupFieldType($string)
const FIELD_TYPE_INTEGER
setStructure($structure)
foreach($_POST as $key=> $value) $res
const FIELD_TYPE_DATE
const FIELD_TYPE_TIMESTAMP
setTableName($table_name)
$txt
Definition: error.php:11
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
exit
Definition: backend.php:16
const FIELD_TYPE_TIME
global $ilDB
setClassName($class_name)
addStructure(stdClass $structure)
Class arConverter.
$data
Definition: bench.php:6