ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilDclCache.php
Go to the documentation of this file.
1<?php
2
19declare(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) {
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) {
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);
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 }
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 {
147 if (!isset($fields_cache[$field_id])) {
149 }
150
151 return $fields_cache[$field_id];
152 }
153
154 public static function getRecordCache(?int $record_id): ilDclBaseRecordModel
155 {
157 if (!$record_id || !isset($records_cache[$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 $unknown = new ilDclDatatype();
263 if ($datatyp_id > ilDclFieldTypePlugin::ID_BUFFER) {
264 $unknown->setTitle('unknown_plugin');
265 }
266 return $unknown;
267 }
268
269 return self::$datatype_cache[$datatyp_id];
270 }
271
272 public static function buildFieldFromRecord(array $rec): ilDclBaseFieldModel
273 {
274 $fields_cache = &self::$fields_cache;
275 if (isset($fields_cache[$rec["id"]])) {
276 return $fields_cache[$rec["id"]];
277 }
279 $field->setId($rec["id"]);
280 $field->setTableId($rec["table_id"]);
281 if (null !== $rec["title"]) {
282 $field->setTitle($rec["title"]);
283 }
284 if (null !== $rec["description"]) {
285 $field->setDescription($rec["description"]);
286 }
287 $field->setDatatypeId($rec["datatype_id"]);
288 $fields_cache[$rec["id"]] = $field;
289
290 return $field;
291 }
292
296 public static function resetCache(): void
297 {
298 self::$fields_cache = [];
299 self::$record_field_cache = [];
300 self::$records_cache = [];
301 }
302}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
static where($where, $operator=null)
static getRecordCache(?int $record_id)
static getRecordFieldCache(object $record, object $field)
static getFieldProperties(string $field_id)
Cache Field properties.
static array $field_properties_cache
const TYPE_TABLEVIEW
static array $fields_cache
static getTableCache(?int $table_id=null)
static getFieldCache(int $field_id=0)
static resetCache()
Resets all the cache fields.
static buildFieldFromRecord(array $rec)
static getRecordRepresentation(ilDclBaseRecordFieldModel $record_field)
Returns a record representation.
static array $tables_cache
static array $clone_mapping
const TYPE_DATACOLLECTION
static initCloneMapping()
static array $datatype_cache
static array $record_representation_cache
static array $record_field_cache
static array $records_cache
static array $field_representation_cache
static preloadFieldProperties(array $fields)
Preloads field properties.
static getFieldRepresentation(ilDclBaseFieldModel $field)
static setCloneOf(int $old, int $new, string $type)
static getCloneOf(int $id, string $type)
static getDatatype(int $datatyp_id)
Get cached datatypes.
static getAllDatatype(bool $force=false)
Get all possible Datatypes.
static getRecordRepresentationInstance(ilDclBaseRecordFieldModel $record_field)
Get RecordRepresentation from RecordFieldModel.
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
static getRecordModelInstance(?int $record_id)
static getRecordFieldInstance(object $field, object $record)
Creates a RecordField instance and loads the field and record representation.
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.
if(!file_exists('../ilias.ini.php'))