ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilDclCache.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 {
23  public const TYPE_DATACOLLECTION = 'dcl';
24  public const TYPE_TABLE = 'table';
25  public const TYPE_FIELD = 'field';
26  public const TYPE_RECORD = 'record';
27  public const TYPE_TABLEVIEW = 'tableview';
31  protected static array $tables_cache = [];
35  protected static array $fields_cache = [];
39  protected static array $records_cache = [];
44  protected static array $record_field_cache = [];
48  protected static array $field_representation_cache = [];
52  protected static array $record_representation_cache = [];
56  protected static array $field_properties_cache = [];
60  protected static array $datatype_cache = [];
73  protected static array $clone_mapping = [];
74 
75  public static function setCloneOf(int $old, int $new, string $type): void
76  {
77  if (!self::$clone_mapping) {
78  self::initCloneMapping();
79  }
80  self::$clone_mapping[$type][$old] = $new;
81  }
82 
83  protected static function initCloneMapping(): void
84  {
85  self::$clone_mapping = [
86  self::TYPE_DATACOLLECTION => [],
87  self::TYPE_TABLE => [],
88  self::TYPE_FIELD => [],
89  self::TYPE_RECORD => [],
90  self::TYPE_TABLEVIEW => [],
91  ];
92  }
93 
94  public static function getCloneOf(int $id, string $type): ?object
95  {
96  $type_cache = self::$clone_mapping[$type];
97  $clone_id = null;
98 
99  if (!is_array($type_cache)) {
100  return null;
101  }
102 
103  if (isset($type_cache[$id])) {
104  $clone_id = $type_cache[$id];
105  } else {
106  foreach ($type_cache as $key => $mapping) {
107  if ($mapping == $id) {
108  $clone_id = $key;
109  }
110  }
111  }
112 
113  if (!$clone_id) {
114  return null;
115  }
116 
117  switch ($type) {
118  case self::TYPE_DATACOLLECTION:
119  return new ilObjDataCollection($clone_id);
120  case self::TYPE_FIELD:
121  return self::getFieldCache($clone_id);
122  case self::TYPE_TABLE:
123  return self::getTableCache($clone_id);
124  case self::TYPE_RECORD:
125  return self::getRecordCache($clone_id);
126  }
127 
128  return null;
129  }
130 
131  public static function getTableCache(?int $table_id = null): ilDclTable
132  {
133  if (is_null($table_id) === true || $table_id === 0) {
134  return new ilDclTable();
135  }
136  $tables_cache = &self::$tables_cache;
137  if (!isset($tables_cache[$table_id])) {
138  $tables_cache[$table_id] = new ilDclTable($table_id);
139  }
140 
141  return $tables_cache[$table_id];
142  }
143 
144  public static function getFieldCache(int $field_id = 0): ilDclBaseFieldModel
145  {
146  $fields_cache = &self::$fields_cache;
147  if (!isset($fields_cache[$field_id])) {
148  $fields_cache[$field_id] = ilDclFieldFactory::getFieldModelInstance($field_id);
149  }
150 
151  return $fields_cache[$field_id];
152  }
153 
154  public static function getRecordCache(?int $record_id): ilDclBaseRecordModel
155  {
156  $records_cache = &self::$records_cache;
157  if (!$record_id || !isset($records_cache[$record_id])) {
158  $records_cache[$record_id] = ilDclFieldFactory::getRecordModelInstance($record_id);
159  }
160 
161  return $records_cache[$record_id];
162  }
163 
164  public static function getRecordFieldCache(
165  object $record, //object|ilDclBaseRecordModel
166  object $field //object|ilDclBaseFieldModel
168  $fid = $field->getId();
169  $rid = $record->getId();
170  if (!isset(self::$record_field_cache[$rid])) {
171  self::$record_field_cache[$rid] = [];
172  self::$record_field_cache[$rid][$fid] = ilDclFieldFactory::getRecordFieldInstance($field, $record);
173  } elseif (!isset(self::$record_field_cache[$rid][$fid])) {
174  self::$record_field_cache[$rid][$fid] = ilDclFieldFactory::getRecordFieldInstance($field, $record);
175  }
176 
177  return self::$record_field_cache[$rid][$fid];
178  }
179 
184  {
185  if (!isset(self::$field_representation_cache[$field->getId()])) {
186  self::$field_representation_cache[$field->getId()] = ilDclFieldFactory::getFieldRepresentationInstance($field);
187  }
188 
189  return self::$field_representation_cache[$field->getId()];
190  }
191 
196  public static function getRecordRepresentation(
197  ilDclBaseRecordFieldModel $record_field
199 
200  if (!isset(self::$record_representation_cache[$record_field->getId()])) {
201  self::$record_representation_cache[$record_field->getId()] = ilDclFieldFactory::getRecordRepresentationInstance($record_field);
202  }
203 
204  return self::$record_representation_cache[$record_field->getId()];
205  }
206 
211  public static function getFieldProperties(string $field_id): array
212  {
213  if (!isset(self::$field_properties_cache[$field_id])) {
214  self::$field_properties_cache[$field_id] = [];
215  $result = ilDclFieldProperty::where(['field_id' => $field_id])->get();
216  foreach ($result as $prop) {
217  self::$field_properties_cache[$field_id][$prop->getName()] = $prop;
218  }
219  }
220 
221  return self::$field_properties_cache[$field_id];
222  }
223 
228  public static function preloadFieldProperties(array $fields): void
229  {
230  foreach ($fields as $field_key => $field) {
231  if (isset(self::$field_properties_cache[$field->getId()])) {
232  unset($fields[$field_key]);
233  }
234  }
235 
236  if (count($fields) > 0) {
237  $field_ids = [];
238  foreach ($fields as $field) {
239  $field_ids[] = $field->getId();
240  }
241  $result = ilDclFieldProperty::where(['field_id' => $field_ids], 'IN')->get();
242  foreach ($result as $prop) {
243  if (!isset(self::$field_properties_cache[$prop->getFieldId()])) {
244  self::$field_properties_cache[$prop->getFieldId()] = [];
245  }
246  self::$field_properties_cache[$prop->getFieldId()][$prop->getName()] = $prop;
247  }
248  }
249  }
250 
255  public static function getDatatype(int $datatyp_id): ilDclDatatype
256  {
257  if (self::$datatype_cache == null) {
258  self::$datatype_cache = ilDclDatatype::getAllDatatype();
259  }
260 
261  if (!isset(self::$datatype_cache[$datatyp_id])) {
262  return new ilDclDatatype();
263  }
264 
265  return self::$datatype_cache[$datatyp_id];
266  }
267 
268  public static function buildFieldFromRecord(array $rec): ilDclBaseFieldModel
269  {
270  $fields_cache = &self::$fields_cache;
271  if (isset($fields_cache[$rec["id"]])) {
272  return $fields_cache[$rec["id"]];
273  }
275  $field->setId($rec["id"]);
276  $field->setTableId($rec["table_id"]);
277  if (null !== $rec["title"]) {
278  $field->setTitle($rec["title"]);
279  }
280  if (null !== $rec["description"]) {
281  $field->setDescription($rec["description"]);
282  }
283  $field->setDatatypeId($rec["datatype_id"]);
284  $field->setUnique((bool) $rec["is_unique"]);
285  $fields_cache[$rec["id"]] = $field;
286 
287  return $field;
288  }
289 
293  public static function resetCache(): void
294  {
295  self::$fields_cache = [];
296  self::$record_field_cache = [];
297  self::$records_cache = [];
298  }
299 }
const TYPE_DATACOLLECTION
static array $records_cache
static preloadFieldProperties(array $fields)
Preloads field properties.
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
static array $record_field_cache
static initCloneMapping()
static getFieldCache(int $field_id=0)
static array $tables_cache
const TYPE_TABLEVIEW
static getRecordModelInstance(?int $record_id)
static array $field_properties_cache
static array $field_representation_cache
static where($where, $operator=null)
static array $fields_cache
static resetCache()
Resets all the cache fields.
static getFieldRepresentation(ilDclBaseFieldModel $field)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static getDatatype(int $datatyp_id)
Get cached datatypes.
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.
static getAllDatatype(bool $force=false)
Get all possible Datatypes.
static getTableCache(?int $table_id=null)
static getRecordRepresentation(ilDclBaseRecordFieldModel $record_field)
Returns a record representation.
static setCloneOf(int $old, int $new, string $type)
static getRecordFieldInstance(object $field, object $record)
Creates a RecordField instance and loads the field and record representation.
static array $clone_mapping
static getRecordRepresentationInstance(ilDclBaseRecordFieldModel $record_field)
Get RecordRepresentation from RecordFieldModel.
static getRecordCache(?int $record_id)
static array $datatype_cache
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static buildFieldFromRecord(array $rec)
static getCloneOf(int $id, string $type)
static getRecordFieldCache(object $record, object $field)
static getFieldProperties(string $field_id)
Cache Field properties.
static array $record_representation_cache