ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilDclFieldFactory.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  public static string $field_base_path_patter = "../components/ILIAS/DataCollection/classes/Fields/%s/";
24  public static string $default_prefix = "ilDcl";
25  public static string $record_field_class_patter = "%sRecordFieldModel";
26  public static string $field_class_patter = "%sFieldModel";
27  public static string $record_representation_class_pattern = "%sRecordRepresentation";
28  public static string $field_representation_class_pattern = "%sFieldRepresentation";
29  protected static array $record_field_cache = [];
30 
38  public static function getRecordFieldInstance(object $field, object $record): ilDclBaseRecordFieldModel
39  {
40  if (!empty(self::$record_field_cache[$field->getId()][$record->getId()])) {
41  return self::$record_field_cache[$field->getId()][$record->getId()];
42  }
43 
44  $path = self::getClassPathByInstance($field, self::$record_field_class_patter);
45  if (file_exists($path)) {
46  $class = self::getClassByInstance($field, self::$record_field_class_patter);
47  $instance = new $class($record, $field);
48  if ($instance instanceof ilDclBaseRecordFieldModel) {
49  if (!$instance->getFieldRepresentation()) {
50  $instance->setFieldRepresentation(self::getFieldRepresentationInstance($field));
51  }
52 
53  if (!$instance->getRecordRepresentation()) {
54  $instance->setRecordRepresentation(self::getRecordRepresentationInstance($instance));
55  }
56  self::$record_field_cache[$field->getId()][$record->getId()] = $instance;
57 
58  return $instance;
59  }
60  }
61 
62  throw new RuntimeException("file not found " . $path);
63  }
64 
65  protected static array $field_class_cache = [];
66 
70  public static function getFieldClass(string $datatype, string $class_pattern): string
71  {
72  if (!empty(self::$field_class_cache[$datatype . $class_pattern])) {
73  return self::$field_class_cache[$datatype . $class_pattern];
74  }
75 
76  $fieldtype = $datatype;
77 
78  $class = sprintf($class_pattern, $fieldtype);
79  self::$field_class_cache[$datatype . $class_pattern] = $class;
80 
81  return $class;
82  }
83 
84  public static function getFieldClassFile(string $datatype, string $class_pattern): string
85  {
86  return "class." . self::getFieldClass($datatype, $class_pattern) . ".php";
87  }
88 
89  protected static array $field_representation_cache = [];
90 
92  {
93  // when the datatype overview is generated no field-models are available, so an empty instance is used => no caching there
94  if (!empty($field->getId()) && !empty(self::$field_representation_cache[$field->getId()])) {
95  return self::$field_representation_cache[$field->getId()];
96  }
97 
98  $class_path = self::getClassPathByInstance($field, self::$field_representation_class_pattern);
99 
100  if (file_exists($class_path)) {
101  $class = self::getClassByInstance($field, self::$field_representation_class_pattern);
102  $instance = new $class($field);
103  } else {
104  throw new ilDclException("Path for FieldRepresentation with file " . $class_path . " does not exists!");
105  }
106 
107  if ($instance == null) {
108  throw new ilDclException("Could not create FieldRepresentation of " . $class . " with file " . $class_path);
109  }
110 
111  if ($field->getId() != null) {
112  self::$field_representation_cache[$field->getId()] = $instance;
113  }
114 
115  return $instance;
116  }
117 
118  protected static array $record_representation_cache = [];
119 
124  public static function getRecordRepresentationInstance(
125  ilDclBaseRecordFieldModel $record_field
127  // there are some field types which have no recordFieldModel object (e.g rating) => no caching
128  if ($record_field->getId() != null && !empty(self::$record_representation_cache[$record_field->getId()])) {
129  return self::$record_representation_cache[$record_field->getId()];
130  }
131 
132  $class_path = self::getClassPathByInstance(
133  $record_field->getField(),
134  self::$record_representation_class_pattern
135  );
136 
137  if (file_exists($class_path)) {
138  $class = self::getClassByInstance($record_field->getField(), self::$record_representation_class_pattern);
139  } else {
140  $class = self::getFieldClass(self::$default_prefix . "Base", self::$record_representation_class_pattern);
141  }
142 
143  $instance = new $class($record_field);
144 
145  if ($instance == null) {
146  throw new ilDclException("Could not create RecordRepresentation of " . $class_path . " " . $record_field->getField()->getDatatype()->getTitle());
147  }
148 
149  if ($record_field->getId() != null) {
150  self::$record_representation_cache[$record_field->getId()] = $instance;
151  }
152 
153  return $instance;
154  }
155 
160  public static function getFieldModelInstance(int $field_id, ?int $datatype = null): ilDclBaseFieldModel
161  {
162  $base = new ilDclBaseFieldModel($field_id);
163  if ($datatype != null) {
164  $base->setDatatypeId($datatype);
165  }
166 
167  return self::getFieldModelInstanceByClass($base, $field_id);
168  }
169 
170  protected static array $field_model_cache = [];
171 
177  public static function getFieldModelInstanceByClass(
178  ilDclBaseFieldModel $field,
179  ?int $field_id = null
181  if ($field->getId() != null && !empty(self::$field_model_cache[$field->getId()])) {
182  return self::$field_model_cache[$field->getId()];
183  }
184 
185  $path_type = self::getClassPathByInstance($field, self::$field_class_patter);
186 
187  if (file_exists($path_type)) {
188  $class = self::getClassByInstance($field, self::$field_class_patter);
189  } else {
190  $class = self::getFieldClass(self::$default_prefix . "Base", self::$field_class_patter);
191  }
192 
193  if ($field_id) {
194  $instance = new $class($field_id);
195  } else {
196  $instance = new $class();
197  }
198 
199  if ($instance == null) {
200  throw new ilDclException("Could not create FieldModel of " . $class);
201  }
202  $instance->setDatatypeId($field->getDatatypeId());
203 
204  if ($field->getId() != null) {
205  self::$field_model_cache[$field->getId()] = $instance;
206  }
207 
208  return $instance;
209  }
210 
211  protected static array $field_type_cache = [];
212 
213  public static function getFieldTypeByInstance(ilDclBaseFieldModel $field): string
214  {
215  global $DIC;
216  $datatype = $field->getDatatype();
217 
218  if (!empty(self::$field_type_cache[$datatype->getId()])) {
219  return self::$field_type_cache[$datatype->getId()];
220  }
221 
222  if (ilDclFieldTypePlugin::isPluginDatatype($datatype->getTitle())) {
223  $plugin_id = ilDclFieldTypePlugin::getPluginId($datatype->getTitle());
224  if ($DIC["component.repository"]->hasActivatedPlugin($plugin_id)) {
225  $fieldtype = 'il' . $DIC["component.repository"]->getPluginById($plugin_id)->getName();
226  } else {
227  $fieldtype = '';
228  }
229  } elseif ($field->getDatatypeId() == ilDclDatatype::INPUTFORMAT_FILEUPLOAD) {
230  // This is for legacy reasons. The fileupload field was replaced with ilDclDatatype::INPUTFORMAT_FILE in
231  // ILIAS 9, but must be available for one more release, since there might be records with this field type
232  // which have not et been migrated.
233  $fieldtype = self::$default_prefix . ucfirst('Fileupload');
234  } else {
235  $fieldtype = self::$default_prefix . ucfirst(self::parseDatatypeTitle($datatype->getTitle()));
236  }
237  self::$field_type_cache[$datatype->getId()] = $fieldtype;
238  return $fieldtype;
239  }
240 
241  public static function getClassByInstance(ilDclBaseFieldModel $field, string $class_pattern): string
242  {
243  $fieldtype = self::getFieldTypeByInstance($field);
244 
245  return self::getFieldClass($fieldtype, $class_pattern);
246  }
247 
248  protected static array $class_path_cache = [];
249 
253  public static function getClassPathByInstance(ilDclBaseFieldModel $field, string $class_pattern): string
254  {
255  global $DIC;
256  $datatype = $field->getDatatype();
257 
258  if ($field->getId() != null && !empty(self::$class_path_cache[$field->getId()][$class_pattern])) {
259  return self::$class_path_cache[$field->getId()][$class_pattern];
260  }
261 
262  if (ilDclFieldTypePlugin::isPluginDatatype($datatype->getTitle())) {
263  $plugin_id = ilDclFieldTypePlugin::getPluginId($datatype->getTitle());
264  if ($DIC["component.repository"]->hasActivatedPlugin($plugin_id)) {
265  $class_path = $DIC["component.repository"]->getPluginById($plugin_id)->getPath() . '/classes/';
266  } else {
267  return '';
268  }
269  } elseif ($field->getDatatypeId() == ilDclDatatype::INPUTFORMAT_FILEUPLOAD) {
270  // This is for legacy reasons. The fileupload field was replaced with ilDclDatatype::INPUTFORMAT_FILE in
271  // ILIAS 9, but must be available for one more release, since there might be records with this field type
272  // which have not et been migrated.
273  $class_path = sprintf(
274  self::$field_base_path_patter,
275  ucfirst(self::parseDatatypeTitle('Fileupload'))
276  );
277 
278  $class_name = sprintf(
279  'class.' . self::$default_prefix . '%s.php',
280  sprintf(
281  $class_pattern,
282  ucfirst('Fileupload'),
283  )
284  );
285 
286  $return = $class_path . $class_name;
287  if ($field->getId() != null) {
288  self::$class_path_cache[$field->getId()][$class_pattern] = $return;
289  }
290 
291  return $return;
292  } else {
293  $class_path = sprintf(
294  self::$field_base_path_patter,
295  ucfirst(self::parseDatatypeTitle($datatype->getTitle()))
296  );
297  }
298 
299  $return = $class_path . self::getFieldClassFile(self::getFieldTypeByInstance($field), $class_pattern);
300 
301  if ($field->getId() != null) {
302  self::$class_path_cache[$field->getId()][$class_pattern] = $return;
303  }
304 
305  return $return;
306  }
307 
312  public static function parseDatatypeTitle(string $title): string
313  {
314  $parts = explode("_", $title);
315  $func = function ($value) {
316  return ucfirst($value);
317  };
318 
319  $parts = array_map($func, $parts);
320  return implode("", $parts);
321  }
322 
323  public static function getRecordModelInstance(?int $record_id): ilDclBaseRecordModel
324  {
325  return new ilDclBaseRecordModel($record_id);
326  }
327 
328  public static function getPluginNameFromFieldModel(ilDclBaseFieldModel $object): string
329  {
330  $class_name = get_class($object);
331  return substr($class_name, 2, -(strlen(self::$field_class_patter) - 2));
332  }
333 }
static array $record_representation_cache
static getFieldClass(string $datatype, string $class_pattern)
Concatenates Classname from datatype and pattern.
if($clientAssertionType !='urn:ietf:params:oauth:client-assertion-type:jwt-bearer'|| $grantType !='client_credentials') $parts
Definition: ltitoken.php:61
static getClassPathByInstance(ilDclBaseFieldModel $field, string $class_pattern)
static getFieldTypeByInstance(ilDclBaseFieldModel $field)
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
static getRecordModelInstance(?int $record_id)
static isPluginDatatype(string $datatype)
static getClassByInstance(ilDclBaseFieldModel $field, string $class_pattern)
static getPluginId(string $datatype)
static string $field_class_patter
static string $field_base_path_patter
static string $record_representation_class_pattern
$path
Definition: ltiservices.php:29
static parseDatatypeTitle(string $title)
Parse string to FieldClass format Replaces _ with camelcase-notation.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getFieldModelInstanceByClass(ilDclBaseFieldModel $field, ?int $field_id=null)
Gets the correct instance of a fieldModel class Checks if a field is a plugin a replaces the fieldMod...
static getFieldModelInstance(int $field_id, ?int $datatype=null)
Get FieldModel from field-id and datatype.
global $DIC
Definition: shib_login.php:22
static getRecordFieldInstance(object $field, object $record)
Creates a RecordField instance and loads the field and record representation.
static getRecordRepresentationInstance(ilDclBaseRecordFieldModel $record_field)
Get RecordRepresentation from RecordFieldModel.
static array $field_representation_cache
static string $record_field_class_patter
static getFieldClassFile(string $datatype, string $class_pattern)
static getPluginNameFromFieldModel(ilDclBaseFieldModel $object)
getDatatypeId()
Get datatype_id.
static string $field_representation_class_pattern