ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilDclFieldFactory.php
Go to the documentation of this file.
1<?php
2require_once('./Modules/DataCollection/classes/Fields/Base/class.ilDclBaseRecordRepresentation.php');
3require_once('./Modules/DataCollection/classes/Fields/Base/class.ilDclBaseFieldModel.php');
4require_once('./Modules/DataCollection/classes/Fields/Plugin/class.ilDclFieldTypePlugin.php');
5require_once('./Modules/DataCollection/exceptions/class.ilDclException.php');
6
15
19 public static $field_base_path_patter = "./Modules/DataCollection/classes/Fields/%s/";
23 public static $default_prefix = "ilDcl";
27 public static $record_field_class_patter = "%sRecordFieldModel";
31 public static $field_class_patter = "%sFieldModel";
35 public static $record_class_patter = "%sRecordModel";
39 public static $record_representation_class_pattern = "%sRecordRepresentation";
43 public static $field_representation_class_pattern = "%sFieldRepresentation";
47 protected static $record_field_cache = array();
48
49
59 public static function getRecordFieldInstance(ilDclBaseFieldModel $field, ilDclBaseRecordModel $record) {
60 if (!empty(self::$record_field_cache[$field->getId()][$record->getId()])) {
61 return self::$record_field_cache[$field->getId()][$record->getId()];
62 }
63
64 $path = self::getClassPathByInstance($field, self::$record_field_class_patter);
65 if (file_exists($path)) {
66 require_once($path);
67 $class = self::getClassByInstance($field, self::$record_field_class_patter);
68 $instance = new $class($record, $field);
69 if ($instance instanceof ilDclBaseRecordFieldModel) {
70 if (!$instance->getFieldRepresentation()) {
71 $instance->setFieldRepresentation(self::getFieldRepresentationInstance($field));
72 }
73
74 if (!$instance->getRecordRepresentation()) {
75 $instance->setRecordRepresentation(self::getRecordRepresentationInstance($instance));
76 }
77 self::$record_field_cache[$field->getId()][$record->getId()] = $instance;
78
79 return $instance;
80 }
81 }
82 }
83
84
88 protected static $field_class_cache = array();
89
90
99 public static function getFieldClass($datatype, $class_pattern) {
100 if (!empty(self::$field_class_cache[$datatype . $class_pattern])) {
101 return self::$field_class_cache[$datatype . $class_pattern];
102 }
103
104 $fieldtype = $datatype;
105
106 $class = sprintf($class_pattern, $fieldtype);
107 self::$field_class_cache[$datatype . $class_pattern] = $class;
108
109 return $class;
110 }
111
112
121 public static function getFieldClassFile($datatype, $class_pattern) {
122 return "class." . self::getFieldClass($datatype, $class_pattern) . ".php";
123 }
124
125
129 protected static $field_representation_cache = array();
130
131
141 // when the datatype overview is generated no field-models are available, so an empty instance is used => no caching there
142 if ($field->getId() != null && !empty(self::$field_representation_cache[$field->getId()])) {
143 return self::$field_representation_cache[$field->getId()];
144 }
145
146 $class_path = self::getClassPathByInstance($field, self::$field_representation_class_pattern);
147
148 $instance = null;
149 if (file_exists($class_path)) {
150 require_once($class_path);
151 $class = self::getClassByInstance($field, self::$field_representation_class_pattern);
152 $instance = new $class($field);
153 } else {
154 throw new ilDclException("Path for FieldRepresentation with file " . $class_path . " does not exists!");
155 }
156
157 if ($instance == null) {
158 throw new ilDclException("Could not create FieldRepresentation of " . $class . " with file " . $class_path);
159 }
160
161 if($field->getId() != null) {
162 self::$field_representation_cache[$field->getId()] = $instance;
163 }
164
165 return $instance;
166 }
167
168
172 protected static $record_representation_cache = array();
173
174
183 public static function getRecordRepresentationInstance(ilDclBaseRecordFieldModel $record_field) {
184 // there are some field types which have no recordFieldModel object (e.g rating) => no caching
185 if ($record_field->getId() != null && !empty(self::$record_representation_cache[$record_field->getId()])) {
186 return self::$record_representation_cache[$record_field->getId()];
187 }
188
189 $class_path = self::getClassPathByInstance($record_field->getField(), self::$record_representation_class_pattern);
190 $instance = null;
191
192 if (file_exists($class_path)) {
193 require_once($class_path);
194 $class = self::getClassByInstance($record_field->getField(), self::$record_representation_class_pattern);
195 } else {
196 $class = self::getFieldClass(self::$default_prefix . "Base", self::$record_representation_class_pattern);
197 }
198
199 $instance = new $class($record_field);
200
201 if ($instance == null) {
202 throw new ilDclException("Could not create RecordRepresentation of " . $class_path . " " . $record_field->getField()->getDatatype()->getTitle());
203 }
204
205 if($record_field->getId() != null) {
206 self::$record_representation_cache[$record_field->getId()] = $instance;
207 }
208
209 return $instance;
210 }
211
212
222 public static function getFieldModelInstance($field_id, $datatype = null) {
223 $base = new ilDclBaseFieldModel($field_id);
224 if ($datatype != null) {
225 $base->setDatatypeId($datatype);
226 }
227
228 $ilDclBaseFieldModel = self::getFieldModelInstanceByClass($base, $field_id);
229
230 return $ilDclBaseFieldModel;
231 }
232
233
237 protected static $field_model_cache = array();
238
239
250 public static function getFieldModelInstanceByClass(ilDclBaseFieldModel $field, $field_id = null) {
251 if ($field->getId() != null && !empty(self::$field_model_cache[$field->getId()])) {
252 return self::$field_model_cache[$field->getId()];
253 }
254
255 $path_type = self::getClassPathByInstance($field, self::$field_class_patter);
256
257 if (file_exists($path_type)) {
258 require_once($path_type);
259 $class = self::getClassByInstance($field, self::$field_class_patter);
260 } else {
261 $class = self::getFieldClass(self::$default_prefix . "Base", self::$field_class_patter);
262 }
263
264 if ($field_id) {
265 $instance = new $class($field_id);
266 } else {
267 $instance = new $class();
268 }
269
270 if ($instance == null) {
271 throw new ilDclException("Could not create FieldModel of " . $class);
272 }
273
274 if($field->getId() != null) {
275 self::$field_model_cache[$field->getId()] = $instance;
276 }
277
278 return $instance;
279 }
280
281
285 protected static $field_type_cache = array();
286
287
294 public static function getFieldTypeByInstance(ilDclBaseFieldModel $field) {
295 $datatype = $field->getDatatype();
296
297 if (!empty(self::$field_type_cache[$datatype->getId()])) {
298 if($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
299 if(!empty(self::$field_type_cache[$datatype->getId()][$field->getId()])) {
300 return self::$field_type_cache[$datatype->getId()][$field->getId()];
301 }
302 } else {
303 return self::$field_type_cache[$datatype->getId()];
304 }
305 }
306
307 if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
310 $fieldtype = $plugin_data->getPluginClassPrefix() . ucfirst($plugin_data->getPluginName());
311 } else {
312 $fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
313 }
314 self::$field_type_cache[$datatype->getId()][$field->getId()] = $fieldtype;
315 } else {
316 $fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
317 self::$field_type_cache[$datatype->getId()] = $fieldtype;
318 }
319
320 return $fieldtype;
321 }
322
323
330 public static function getClassByInstance(ilDclBaseFieldModel $field, $class_pattern) {
331 $fieldtype = self::getFieldTypeByInstance($field);
332
333 return self::getFieldClass($fieldtype, $class_pattern);
334 }
335
336
340 protected static $class_path_cache = array();
341
342
350 public static function getClassPathByInstance(ilDclBaseFieldModel $field, $class_pattern) {
351 $datatype = $field->getDatatype();
352
353 if ($field->getId() != null && !empty(self::$class_path_cache[$field->getId()][$class_pattern])) {
354 return self::$class_path_cache[$field->getId()][$class_pattern];
355 }
356
357 if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
360 if ($plugin_data == null) {
361 throw new ilDclException("Something went wrong by initializing the FieldHook-Plugin '"
362 . $field->getProperty(ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME) . "' on Component '"
363 . ilDclFieldTypePlugin::COMPONENT_NAME . "' with slot '" . ilDclFieldTypePlugin::SLOT_ID . "' on field: "
364 . $field->getTitle());
365 }
366
367 $class_path = $plugin_data->getDirectory() . "/classes/";
368 } else {
369 $class_path = sprintf(self::$field_base_path_patter, ucfirst(self::parseDatatypeTitle($datatype->getTitle())));
370 }
371 } else {
372 $class_path = sprintf(self::$field_base_path_patter, ucfirst(self::parseDatatypeTitle($datatype->getTitle())));
373 }
374
375 $return = $class_path . self::getFieldClassFile(self::getFieldTypeByInstance($field), $class_pattern);
376
377 if($field->getId() != null) {
378 self::$class_path_cache[$field->getId()][$class_pattern] = $return;
379 }
380
381 return $return;
382 }
383
384
393 public static function parseDatatypeTitle($title) {
394 $parts = explode("_", $title);
395 $func = function ($value) {
396 return ucfirst($value);
397 };
398
399 $parts = array_map($func, $parts);
400 $title = implode("", $parts);
401
402 return $title;
403 }
404
405
413 public static function getRecordModelInstance($record_id) {
414 return new ilDclBaseRecordModel($record_id);
415 }
416
417
425 public static function getPluginNameFromFieldModel(ilDclBaseFieldModel $object) {
426 $class_name = get_class($object);
427 $class_name = substr($class_name, 2, - (strlen(self::$field_class_patter) - 2));
428
429 return $class_name;
430 }
431}
sprintf('%.4f', $callTime)
$path
Definition: aliased.php:25
An exception for terminatinating execution or to throw for unit testing.
const IL_COMP_MODULE
Class ilDclBaseFieldModel.
hasProperty($key)
Checks if a certain property for a field is set.
getProperty($key)
Returns a certain property of a field.
Class ilDclBaseRecordModel.
Class ilDclException.
Class ilDclFieldFactory This Class handles the creation of all field-classes.
static getRecordRepresentationInstance(ilDclBaseRecordFieldModel $record_field)
Get RecordRepresentation from RecordFieldModel.
static parseDatatypeTitle($title)
Parse string to FieldClass format Replaces _ with camelcase-notation.
static getFieldModelInstance($field_id, $datatype=null)
Get FieldModel from field-id and datatype.
static getClassByInstance(ilDclBaseFieldModel $field, $class_pattern)
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
Returns FieldRepresentation from BaseFieldModel.
static getFieldClass($datatype, $class_pattern)
Concatenates Classname from datatype and pattern.
static getRecordFieldInstance(ilDclBaseFieldModel $field, ilDclBaseRecordModel $record)
Creates a RecordField instance and loads the field and record representation.
static getFieldModelInstanceByClass(ilDclBaseFieldModel $field, $field_id=null)
Gets the correct instance of a fieldModel class Checks if a field is a plugin a replaces the fieldMod...
static getFieldClassFile($datatype, $class_pattern)
Get Filename from datatype and pattern.
static getRecordModelInstance($record_id)
Creates a RecordModel instance.
static getFieldTypeByInstance(ilDclBaseFieldModel $field)
static getPluginNameFromFieldModel(ilDclBaseFieldModel $object)
Get plugin-name from FieldModel.
static getClassPathByInstance(ilDclBaseFieldModel $field, $class_pattern)
static getPluginObject($a_ctype, $a_cname, $a_slot_id, $a_pname)
Get plugin object.