ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 $ar;
72  protected static $key_maps = array(
73  self::FIELDTYPE => 'type',
74  self::IS_NOTNULL => 'notnull',
75  );
76 
77 
83  public function __construct(ActiveRecord $ar) {
84  $this->ar = $ar;
85  }
86 
87 
93  public static function mapKey($key) {
94  if (isset(self::$key_maps[$key])) {
95  return self::$key_maps[$key];
96  }
97 
98  return $key;
99  }
100 
101 
105  public static function getAllowedConnectorFields() {
106  return self::$allowed_connector_fields;
107  }
108 
109 
113  public static function getAllowedDescriptionFields() {
114  return self::$allowed_description_fields;
115  }
116 
117 
118 
124  public static function getInstance(ActiveRecord $ar) {
125  $arFieldList = new self($ar);
126  $arFieldList->initRawFields($ar);
127  $arFieldList->initFields();
128 
129  return $arFieldList;
130  }
131 
132 
139  public static function getInstanceFromStorage($ar) {
140  $arFieldList = new self();
141  $arFieldList->initRawFields($ar);
142  $arFieldList->initFields();
143 
144  return $arFieldList;
145  }
146 
147 
151  public function getArrayForConnector() {
152  $return = array();
153  foreach ($this->getFields() as $field) {
154  $return[$field->getName()] = $field->getAttributesForConnector();
155  }
156 
157  return $return;
158  }
159 
160 
161  protected function initFields() {
162  foreach ($this->getRawFields() as $fieldname => $attributes) {
163  if (self::checkAttributes($attributes)) {
164  $arField = new arField();
165  $arField->getHasField(true);
166  $arField->loadFromArray($fieldname, $attributes);
167  $this->fields[] = $arField;
168  if ($arField->getPrimary()) {
169  $this->setPrimaryField($arField);
170  }
171  }
172  }
173  }
174 
175 
181  public function getFieldByName($field_name) {
182  $field = NULL;
183  static $field_map;
184  $field_key = $this->ar->getConnectorContainerName() . '.' . $field_name;
185  if ($field_map[$field_key]) {
186  return $field_map[$field_key];
187  }
188  foreach ($this->getFields() as $field) {
189  if ($field->getName() == $field_name) {
190  $field_map[$field_key] = $field;
191 
192  return $field;
193  }
194  }
195  }
196 
197 
203  public function isField($field_name) {
204  $is_field = false;
205  foreach ($this->getFields() as $field) {
206  if ($field->getName() == $field_name) {
207  $is_field = true;
208  }
209  }
210 
211  return $is_field;
212  }
213 
214 
218  public function getPrimaryFieldName() {
219  return $this->getPrimaryField()->getName();
220  }
221 
222 
226  public function getPrimaryFieldType() {
227  return $this->getPrimaryField()->getFieldType();
228  }
229 
230 
236  protected function initRawFields(arStorageInterface $ar) {
237  $regex = "/[ ]*\\* @(" . implode('|', self::$prefixes) . ")_([a-zA-Z0-9_]*)[ ]*([a-zA-Z0-9_]*)/u";
238  $reflectionClass = new ReflectionClass($ar);
239  $raw_fields = array();
240  foreach ($reflectionClass->getProperties() as $property) {
241  if (in_array($property->getName(), self::$protected_names)) {
242  continue;
243  }
244  $properties_array = array();
245  $has_property = false;
246  foreach (explode("\n", $property->getDocComment()) as $line) {
247  if (preg_match($regex, $line, $matches)) {
248  $has_property = true;
249  $properties_array[(string)$matches[2]] = $matches[3];
250  }
251  }
252  if ($has_property) {
253  $raw_fields[$property->getName()] = $properties_array;
254  }
255  }
256 
257  $this->setRawFields($raw_fields);
258  }
259 
260 
266  protected static function isAllowedAttribute($attribute_name) {
267  return in_array($attribute_name, array_merge(self::$allowed_description_fields, array( self::HAS_FIELD )));
268  }
269 
270 
276  protected static function checkAttributes(array $attributes) {
277  if ($attributes[self::HAS_FIELD] === 'true') {
278  foreach (array_keys($attributes) as $atr) {
279  if (!self::isAllowedAttribute($atr)) {
280  return false;
281  }
282  }
283  } else {
284  return false;
285  }
286 
287  return true;
288  }
289 
290 
294  public function setFields($fields) {
295  $this->fields = $fields;
296  }
297 
298 
302  public function getFields() {
303  return $this->fields;
304  }
305 
306 
310  public function setPrimaryField($primary_field) {
311  $this->primary_field = $primary_field;
312  }
313 
314 
318  public function getPrimaryField() {
319  return $this->primary_field;
320  }
321 
322 
326  public function setRawFields($raw_fields) {
327  $this->raw_fields = $raw_fields;
328  }
329 
330 
334  public function getRawFields() {
335  return $this->raw_fields;
336  }
337 
338 
343  $this->primary_fields = $primary_fields;
344  }
345 
346 
350  public function getPrimaryFields() {
351  return $this->primary_fields;
352  }
353 }
354 
355 ?>
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()
Add rich text string
The name of the decorator.
initRawFields(arStorageInterface $ar)
static isAllowedAttribute($attribute_name)
setPrimaryField($primary_field)
$errors fields
Definition: imgupload.php:52
static $allowed_connector_fields
static getInstanceFromStorage($ar)
Create styles array
The data for the language used.
Class arFieldList.
static checkAttributes(array $attributes)
isField($field_name)
static getAllowedDescriptionFields()
__construct(ActiveRecord $ar)
arFieldList constructor.