ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilDclFieldFactory.php
Go to the documentation of this file.
1<?php
2
11{
12
16 public static $field_base_path_patter = "./Modules/DataCollection/classes/Fields/%s/";
20 public static $default_prefix = "ilDcl";
24 public static $record_field_class_patter = "%sRecordFieldModel";
28 public static $field_class_patter = "%sFieldModel";
32 public static $record_class_patter = "%sRecordModel";
36 public static $record_representation_class_pattern = "%sRecordRepresentation";
40 public static $field_representation_class_pattern = "%sFieldRepresentation";
44 protected static $record_field_cache = array();
45
46
56 public static function getRecordFieldInstance(ilDclBaseFieldModel $field, ilDclBaseRecordModel $record)
57 {
58 if (!empty(self::$record_field_cache[$field->getId()][$record->getId()])) {
59 return self::$record_field_cache[$field->getId()][$record->getId()];
60 }
61
62 $path = self::getClassPathByInstance($field, self::$record_field_class_patter);
63 if (file_exists($path)) {
64 require_once($path);
65 $class = self::getClassByInstance($field, self::$record_field_class_patter);
66 $instance = new $class($record, $field);
67 if ($instance instanceof ilDclBaseRecordFieldModel) {
68 if (!$instance->getFieldRepresentation()) {
69 $instance->setFieldRepresentation(self::getFieldRepresentationInstance($field));
70 }
71
72 if (!$instance->getRecordRepresentation()) {
73 $instance->setRecordRepresentation(self::getRecordRepresentationInstance($instance));
74 }
75 self::$record_field_cache[$field->getId()][$record->getId()] = $instance;
76
77 return $instance;
78 }
79 }
80 }
81
82
86 protected static $field_class_cache = array();
87
88
97 public static function getFieldClass($datatype, $class_pattern)
98 {
99 if (!empty(self::$field_class_cache[$datatype . $class_pattern])) {
100 return self::$field_class_cache[$datatype . $class_pattern];
101 }
102
103 $fieldtype = $datatype;
104
105 $class = sprintf($class_pattern, $fieldtype);
106 self::$field_class_cache[$datatype . $class_pattern] = $class;
107
108 return $class;
109 }
110
111
120 public static function getFieldClassFile($datatype, $class_pattern)
121 {
122 return "class." . self::getFieldClass($datatype, $class_pattern) . ".php";
123 }
124
125
129 protected static $field_representation_cache = array();
130
131
141 {
142 // when the datatype overview is generated no field-models are available, so an empty instance is used => no caching there
143 if ($field->getId() != null && !empty(self::$field_representation_cache[$field->getId()])) {
144 return self::$field_representation_cache[$field->getId()];
145 }
146
147 $class_path = self::getClassPathByInstance($field, self::$field_representation_class_pattern);
148
149 $instance = null;
150 if (file_exists($class_path)) {
151 require_once($class_path);
152 $class = self::getClassByInstance($field, self::$field_representation_class_pattern);
153 $instance = new $class($field);
154 } else {
155 throw new ilDclException("Path for FieldRepresentation with file " . $class_path . " does not exists!");
156 }
157
158 if ($instance == null) {
159 throw new ilDclException("Could not create FieldRepresentation of " . $class . " with file " . $class_path);
160 }
161
162 if ($field->getId() != null) {
163 self::$field_representation_cache[$field->getId()] = $instance;
164 }
165
166 return $instance;
167 }
168
169
173 protected static $record_representation_cache = array();
174
175
184 public static function getRecordRepresentationInstance(ilDclBaseRecordFieldModel $record_field)
185 {
186 // there are some field types which have no recordFieldModel object (e.g rating) => no caching
187 if ($record_field->getId() != null && !empty(self::$record_representation_cache[$record_field->getId()])) {
188 return self::$record_representation_cache[$record_field->getId()];
189 }
190
191 $class_path = self::getClassPathByInstance($record_field->getField(), self::$record_representation_class_pattern);
192 $instance = null;
193
194 if (file_exists($class_path)) {
195 require_once($class_path);
196 $class = self::getClassByInstance($record_field->getField(), self::$record_representation_class_pattern);
197 } else {
198 $class = self::getFieldClass(self::$default_prefix . "Base", self::$record_representation_class_pattern);
199 }
200
201 $instance = new $class($record_field);
202
203 if ($instance == null) {
204 throw new ilDclException("Could not create RecordRepresentation of " . $class_path . " " . $record_field->getField()->getDatatype()->getTitle());
205 }
206
207 if ($record_field->getId() != null) {
208 self::$record_representation_cache[$record_field->getId()] = $instance;
209 }
210
211 return $instance;
212 }
213
214
224 public static function getFieldModelInstance($field_id, $datatype = null)
225 {
226 $base = new ilDclBaseFieldModel($field_id);
227 if ($datatype != null) {
228 $base->setDatatypeId($datatype);
229 }
230
231 $ilDclBaseFieldModel = self::getFieldModelInstanceByClass($base, $field_id);
232
233 return $ilDclBaseFieldModel;
234 }
235
236
240 protected static $field_model_cache = array();
241
242
253 public static function getFieldModelInstanceByClass(ilDclBaseFieldModel $field, $field_id = null)
254 {
255 if ($field->getId() != null && !empty(self::$field_model_cache[$field->getId()])) {
256 return self::$field_model_cache[$field->getId()];
257 }
258
259 $path_type = self::getClassPathByInstance($field, self::$field_class_patter);
260
261 if (file_exists($path_type)) {
262 require_once($path_type);
263 $class = self::getClassByInstance($field, self::$field_class_patter);
264 } else {
265 $class = self::getFieldClass(self::$default_prefix . "Base", self::$field_class_patter);
266 }
267
268 if ($field_id) {
269 $instance = new $class($field_id);
270 } else {
271 $instance = new $class();
272 }
273
274 if ($instance == null) {
275 throw new ilDclException("Could not create FieldModel of " . $class);
276 }
277
278 if ($field->getId() != null) {
279 self::$field_model_cache[$field->getId()] = $instance;
280 }
281
282 return $instance;
283 }
284
285
289 protected static $field_type_cache = array();
290
291
298 public static function getFieldTypeByInstance(ilDclBaseFieldModel $field)
299 {
300 $datatype = $field->getDatatype();
301
302 if (!empty(self::$field_type_cache[$datatype->getId()])) {
303 if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
304 if (!empty(self::$field_type_cache[$datatype->getId()][$field->getId()])) {
305 return self::$field_type_cache[$datatype->getId()][$field->getId()];
306 }
307 } else {
308 return self::$field_type_cache[$datatype->getId()];
309 }
310 }
311
312 if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
314 $plugin_data
316 $fieldtype = $plugin_data->getPluginClassPrefix() . ucfirst($plugin_data->getPluginName());
317 } else {
318 $fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
319 }
320 self::$field_type_cache[$datatype->getId()][$field->getId()] = $fieldtype;
321 } else {
322 $fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
323 self::$field_type_cache[$datatype->getId()] = $fieldtype;
324 }
325
326 return $fieldtype;
327 }
328
329
336 public static function getClassByInstance(ilDclBaseFieldModel $field, $class_pattern)
337 {
338 $fieldtype = self::getFieldTypeByInstance($field);
339
340 return self::getFieldClass($fieldtype, $class_pattern);
341 }
342
343
347 protected static $class_path_cache = array();
348
349
357 public static function getClassPathByInstance(ilDclBaseFieldModel $field, $class_pattern)
358 {
359 $datatype = $field->getDatatype();
360
361 if ($field->getId() != null && !empty(self::$class_path_cache[$field->getId()][$class_pattern])) {
362 return self::$class_path_cache[$field->getId()][$class_pattern];
363 }
364
365 if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
367 $plugin_data
369 if ($plugin_data == null) {
370 throw new ilDclException(
371 "Something went wrong by initializing the FieldHook-Plugin '"
372 . $field->getProperty(ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME) . "' on Component '"
373 . ilDclFieldTypePlugin::COMPONENT_NAME . "' with slot '" . ilDclFieldTypePlugin::SLOT_ID . "' on field: "
374 . $field->getTitle()
375 );
376 }
377
378 $class_path = $plugin_data->getDirectory() . "/classes/";
379 } else {
380 $class_path = sprintf(self::$field_base_path_patter, ucfirst(self::parseDatatypeTitle($datatype->getTitle())));
381 }
382 } else {
383 $class_path = sprintf(self::$field_base_path_patter, ucfirst(self::parseDatatypeTitle($datatype->getTitle())));
384 }
385
386 $return = $class_path . self::getFieldClassFile(self::getFieldTypeByInstance($field), $class_pattern);
387
388 if ($field->getId() != null) {
389 self::$class_path_cache[$field->getId()][$class_pattern] = $return;
390 }
391
392 return $return;
393 }
394
395
404 public static function parseDatatypeTitle($title)
405 {
406 $parts = explode("_", $title);
407 $func = function ($value) {
408 return ucfirst($value);
409 };
410
411 $parts = array_map($func, $parts);
412 $title = implode("", $parts);
413
414 return $title;
415 }
416
417
425 public static function getRecordModelInstance($record_id)
426 {
427 return new ilDclBaseRecordModel($record_id);
428 }
429
430
438 public static function getPluginNameFromFieldModel(ilDclBaseFieldModel $object)
439 {
440 $class_name = get_class($object);
441 $class_name = substr($class_name, 2, -(strlen(self::$field_class_patter) - 2));
442
443 return $class_name;
444 }
445}
$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(string $a_ctype, string $a_cname, string $a_slot_id, string $a_pname)
$base
Definition: index.php:4