ILIAS  release_7 Revision v7.30-3-g800a261c036
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 $class = self::getClassByInstance($field, self::$record_field_class_patter);
65 $instance = new $class($record, $field);
66 if ($instance instanceof ilDclBaseRecordFieldModel) {
67 if (!$instance->getFieldRepresentation()) {
68 $instance->setFieldRepresentation(self::getFieldRepresentationInstance($field));
69 }
70
71 if (!$instance->getRecordRepresentation()) {
72 $instance->setRecordRepresentation(self::getRecordRepresentationInstance($instance));
73 }
74 self::$record_field_cache[$field->getId()][$record->getId()] = $instance;
75
76 return $instance;
77 }
78 }
79 }
80
81
85 protected static $field_class_cache = array();
86
87
96 public static function getFieldClass($datatype, $class_pattern)
97 {
98 if (!empty(self::$field_class_cache[$datatype . $class_pattern])) {
99 return self::$field_class_cache[$datatype . $class_pattern];
100 }
101
102 $fieldtype = $datatype;
103
104 $class = sprintf($class_pattern, $fieldtype);
105 self::$field_class_cache[$datatype . $class_pattern] = $class;
106
107 return $class;
108 }
109
110
119 public static function getFieldClassFile($datatype, $class_pattern)
120 {
121 return "class." . self::getFieldClass($datatype, $class_pattern) . ".php";
122 }
123
124
128 protected static $field_representation_cache = array();
129
130
140 {
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 $class = self::getClassByInstance($field, self::$field_representation_class_pattern);
151 $instance = new $class($field);
152 } else {
153 throw new ilDclException("Path for FieldRepresentation with file " . $class_path . " does not exists!");
154 }
155
156 if ($instance == null) {
157 throw new ilDclException("Could not create FieldRepresentation of " . $class . " with file " . $class_path);
158 }
159
160 if ($field->getId() != null) {
161 self::$field_representation_cache[$field->getId()] = $instance;
162 }
163
164 return $instance;
165 }
166
167
171 protected static $record_representation_cache = array();
172
173
182 public static function getRecordRepresentationInstance(ilDclBaseRecordFieldModel $record_field)
183 {
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 $class = self::getClassByInstance($record_field->getField(), self::$record_representation_class_pattern);
194 } else {
195 $class = self::getFieldClass(self::$default_prefix . "Base", self::$record_representation_class_pattern);
196 }
197
198 $instance = new $class($record_field);
199
200 if ($instance == null) {
201 throw new ilDclException("Could not create RecordRepresentation of " . $class_path . " " . $record_field->getField()->getDatatype()->getTitle());
202 }
203
204 if ($record_field->getId() != null) {
205 self::$record_representation_cache[$record_field->getId()] = $instance;
206 }
207
208 return $instance;
209 }
210
211
221 public static function getFieldModelInstance($field_id, $datatype = null)
222 {
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 {
252 if ($field->getId() != null && !empty(self::$field_model_cache[$field->getId()])) {
253 return self::$field_model_cache[$field->getId()];
254 }
255
256 $path_type = self::getClassPathByInstance($field, self::$field_class_patter);
257
258 if (file_exists($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 {
296 $datatype = $field->getDatatype();
297
298 if (!empty(self::$field_type_cache[$datatype->getId()])) {
299 if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
300 if (!empty(self::$field_type_cache[$datatype->getId()][$field->getId()])) {
301 return self::$field_type_cache[$datatype->getId()][$field->getId()];
302 }
303 } else {
304 return self::$field_type_cache[$datatype->getId()];
305 }
306 }
307
308 if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
310 $plugin_data
312 $fieldtype = $plugin_data->getPluginClassPrefix() . ucfirst($plugin_data->getPluginName());
313 } else {
314 $fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
315 }
316 self::$field_type_cache[$datatype->getId()][$field->getId()] = $fieldtype;
317 } else {
318 $fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
319 self::$field_type_cache[$datatype->getId()] = $fieldtype;
320 }
321
322 return $fieldtype;
323 }
324
325
332 public static function getClassByInstance(ilDclBaseFieldModel $field, $class_pattern)
333 {
334 $fieldtype = self::getFieldTypeByInstance($field);
335
336 return self::getFieldClass($fieldtype, $class_pattern);
337 }
338
339
343 protected static $class_path_cache = array();
344
345
353 public static function getClassPathByInstance(ilDclBaseFieldModel $field, $class_pattern)
354 {
355 $datatype = $field->getDatatype();
356
357 if ($field->getId() != null && !empty(self::$class_path_cache[$field->getId()][$class_pattern])) {
358 return self::$class_path_cache[$field->getId()][$class_pattern];
359 }
360
361 if ($datatype->getId() == ilDclDatatype::INPUTFORMAT_PLUGIN) {
363 $plugin_data
365 if ($plugin_data == null) {
366 throw new ilDclException(
367 "Something went wrong by initializing the FieldHook-Plugin '"
368 . $field->getProperty(ilDclBaseFieldModel::PROP_PLUGIN_HOOK_NAME) . "' on Component '"
369 . ilDclFieldTypePlugin::COMPONENT_NAME . "' with slot '" . ilDclFieldTypePlugin::SLOT_ID . "' on field: "
370 . $field->getTitle()
371 );
372 }
373
374 $class_path = $plugin_data->getDirectory() . "/classes/";
375 } else {
376 $class_path = sprintf(self::$field_base_path_patter, ucfirst(self::parseDatatypeTitle($datatype->getTitle())));
377 }
378 } else {
379 $class_path = sprintf(self::$field_base_path_patter, ucfirst(self::parseDatatypeTitle($datatype->getTitle())));
380 }
381
382 $return = $class_path . self::getFieldClassFile(self::getFieldTypeByInstance($field), $class_pattern);
383
384 if ($field->getId() != null) {
385 self::$class_path_cache[$field->getId()][$class_pattern] = $return;
386 }
387
388 return $return;
389 }
390
391
400 public static function parseDatatypeTitle($title)
401 {
402 $parts = explode("_", $title);
403 $func = function ($value) {
404 return ucfirst($value);
405 };
406
407 $parts = array_map($func, $parts);
408 $title = implode("", $parts);
409
410 return $title;
411 }
412
413
421 public static function getRecordModelInstance($record_id)
422 {
423 return new ilDclBaseRecordModel($record_id);
424 }
425
426
434 public static function getPluginNameFromFieldModel(ilDclBaseFieldModel $object)
435 {
436 $class_name = get_class($object);
437 $class_name = substr($class_name, 2, -(strlen(self::$field_class_patter) - 2));
438
439 return $class_name;
440 }
441}
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