ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
FieldSubstitution.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
25 
27 {
28  private static array $field_cache = [];
29  private \ilLanguage $lng;
30 
31  public function __construct(
32  private \ilDclBaseRecordModel $record,
33  private \ilDclBaseFieldModel $field
34  ) {
35  global $DIC;
36  $this->lng = $DIC->language();
37  }
38 
39  public function getFieldId(): int
40  {
41  return (int) $this->field->getId();
42  }
43 
44  public function substituteFieldValue(string $placeholder): string
45  {
46  if (isset(self::$field_cache[$placeholder])) {
47  $field = self::$field_cache[$placeholder];
48  } else {
49  $field = $this->getFieldFromPlaceholder($placeholder);
50  }
51 
52  return $this->record->getRecordFieldFormulaValue($field->getId());
53  }
54 
55  public function substituteFieldValues(array $tokens): array
56  {
57  $replaced = [];
58  foreach ($tokens as $token) {
59  if ($token instanceof Token) {
60  $token = $token->getValue();
61  }
62  if (str_starts_with($token, Tokenizer::FIELD_OPENER)) {
63  $replaced[] = new Token($this->substituteFieldValue($token));
64  } else {
65  $replaced[] = new Token($token);
66  }
67  }
68 
69  return $replaced;
70  }
71 
72  public function getFieldFromPlaceholder(string $placeholder): \ilDclBaseFieldModel
73  {
74  $table = \ilDclCache::getTableCache($this->record->getTableId()); // TODO May need caching per table in future
75  $field_title = preg_replace('#^\[\[(.*)\]\]#', "$1", $placeholder);
76  $field = $table->getFieldByTitle($field_title);
77  if ($field === null) {
78  // Workaround for standardfields - title my be ID
79  $field = $table->getField($field_title);
80  if ($field === null) {
81  throw new \ilException(sprintf($this->lng->txt('dcl_err_formula_field_not_found'), $field_title));
82  }
83  }
84  self::$field_cache[$placeholder] = $field;
85  return $field;
86  }
87 }
Token
The string representation of these tokens must not occur in the names of metadata elements...
Definition: Token.php:27
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct(private \ilDclBaseRecordModel $record, private \ilDclBaseFieldModel $field)
$token
Definition: xapitoken.php:70
global $DIC
Definition: shib_login.php:22
static getTableCache(?int $table_id=null)