ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
class.arFieldList.php
Go to the documentation of this file.
1 <?php
2 require_once('class.arField.php');
3 
11 class arFieldList {
12 
13  const HAS_FIELD = 'has_field';
14  const IS_UNIQUE = 'is_unique';
15  const IS_PRIMARY = 'is_primary';
16  const IS_NOTNULL = 'is_notnull';
17  const FIELDTYPE = 'fieldtype';
18  const LENGTH = 'length';
19  const SEQUENCE = 'sequence';
20  const INDEX = 'index';
24  protected static $prefixes = array( 'db', 'con' );
28  protected static $protected_names = array( 'arConnector', 'arFieldList' );
32  protected static $allowed_description_fields = array(
33  self::IS_UNIQUE,
34  self::IS_PRIMARY,
35  self::IS_NOTNULL,
36  self::FIELDTYPE,
37  self::LENGTH,
38  self::SEQUENCE,
39  self::INDEX,
40  );
44  protected static $allowed_connector_fields = array(
45  self::IS_NOTNULL,
46  self::FIELDTYPE,
47  self::LENGTH,
48  );
52  protected $primary_field;
56  protected $primary_fields = array();
60  protected $raw_fields = array();
64  protected $fields = array();
68  protected static $key_maps = array(
69  self::FIELDTYPE => 'type',
70  self::IS_NOTNULL => 'notnull',
71  );
72 
73 
79  public static function mapKey($key) {
80  if (isset(self::$key_maps[$key])) {
81  return self::$key_maps[$key];
82  }
83 
84  return $key;
85  }
86 
87 
91  public static function getAllowedConnectorFields() {
92  return self::$allowed_connector_fields;
93  }
94 
95 
99  public static function getAllowedDescriptionFields() {
100  return self::$allowed_description_fields;
101  }
102 
103 
109  public static function getInstance(ActiveRecord $ar) {
110  $arFieldList = new self();
111  $arFieldList->initRawFields($ar);
112  $arFieldList->initFields();
113 
114  return $arFieldList;
115  }
116 
117 
124  public static function getInstanceFromStorage($ar) {
125  $arFieldList = new self();
126  $arFieldList->initRawFields($ar);
127  $arFieldList->initFields();
128 
129  return $arFieldList;
130  }
131 
132 
136  public function getArrayForConnector() {
137  $return = array();
138  foreach ($this->getFields() as $field) {
139  $return[$field->getName()] = $field->getAttributesForConnector();
140  }
141 
142  return $return;
143  }
144 
145 
146  protected function initFields() {
147  foreach ($this->getRawFields() as $fieldname => $attributes) {
148  if (self::checkAttributes($attributes)) {
149  $arField = new arField();
150  $arField->getHasField(true);
151  $arField->loadFromArray($fieldname, $attributes);
152  $this->fields[] = $arField;
153  if ($arField->getPrimary()) {
154  $this->setPrimaryField($arField);
155  }
156  }
157  }
158  }
159 
160 
166  public function getFieldByName($field_name) {
167  $field = NULL;
168  static $field_map;
169  if ($field_map[$field_name]) {
170  return $field_map[$field_name];
171  }
172  foreach ($this->getFields() as $field) {
173  if ($field->getName() == $field_name) {
174  $field_map[$field_name] = $field;
175 
176  return $field;
177  }
178  }
179  }
180 
181 
187  public function isField($field_name) {
188  $is_field = false;
189  foreach ($this->getFields() as $field) {
190  if ($field->getName() == $field_name) {
191  $is_field = true;
192  }
193  }
194 
195  return $is_field;
196  }
197 
198 
202  public function getPrimaryFieldName() {
203  return $this->getPrimaryField()->getName();
204  }
205 
206 
210  public function getPrimaryFieldType() {
211  return $this->getPrimaryField()->getFieldType();
212  }
213 
214 
220  protected function initRawFields(arStorageInterface $ar) {
221  $regex = "/[ ]*\\* @(" . implode('|', self::$prefixes) . ")_([a-zA-Z0-9_]*)[ ]*([a-zA-Z0-9_]*)/u";
222  $reflectionClass = new ReflectionClass($ar);
223  $raw_fields = array();
224  foreach ($reflectionClass->getProperties() as $property) {
225  if (in_array($property->getName(), self::$protected_names)) {
226  continue;
227  }
228  $properties_array = array();
229  $has_property = false;
230  foreach (explode("\n", $property->getDocComment()) as $line) {
231  if (preg_match($regex, $line, $matches)) {
232  $has_property = true;
233  $properties_array[(string)$matches[2]] = $matches[3];
234  }
235  }
236  if ($has_property) {
237  $raw_fields[$property->getName()] = $properties_array;
238  }
239  }
240 
241  $this->setRawFields($raw_fields);
242  }
243 
244 
250  protected static function isAllowedAttribute($attribute_name) {
251  return in_array($attribute_name, array_merge(self::$allowed_description_fields, array( self::HAS_FIELD )));
252  }
253 
254 
260  protected static function checkAttributes(array $attributes) {
261  if ($attributes[self::HAS_FIELD] === 'true') {
262  foreach (array_keys($attributes) as $atr) {
263  if (!self::isAllowedAttribute($atr)) {
264  return false;
265  }
266  }
267  } else {
268  return false;
269  }
270 
271  return true;
272  }
273 
274 
278  public function setFields($fields) {
279  $this->fields = $fields;
280  }
281 
282 
286  public function getFields() {
287  return $this->fields;
288  }
289 
290 
294  public function setPrimaryField($primary_field) {
295  $this->primary_field = $primary_field;
296  }
297 
298 
302  public function getPrimaryField() {
303  return $this->primary_field;
304  }
305 
306 
310  public function setRawFields($raw_fields) {
311  $this->raw_fields = $raw_fields;
312  }
313 
314 
318  public function getRawFields() {
319  return $this->raw_fields;
320  }
321 
322 
327  $this->primary_fields = $primary_fields;
328  }
329 
330 
334  public function getPrimaryFields() {
335  return $this->primary_fields;
336  }
337 }
338 
339 ?>
Class arField.
Class arStorageInterface.
getFieldByName($field_name)
setRawFields($raw_fields)
static getInstance(ActiveRecord $ar)
static $protected_names
Class ActiveRecord.
static $allowed_description_fields
setPrimaryFields($primary_fields)
static mapKey($key)
static getAllowedConnectorFields()
$errors fields
Definition: imgupload.php:48
initRawFields(arStorageInterface $ar)
static isAllowedAttribute($attribute_name)
setPrimaryField($primary_field)
static $allowed_connector_fields
static getInstanceFromStorage($ar)
Class arFieldList.
static checkAttributes(array $attributes)
isField($field_name)
static getAllowedDescriptionFields()