ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilBiblTranslationFactory.php
Go to the documentation of this file.
1 <?php
2 
24 {
25  protected \ILIAS\DI\Container $dic;
26  protected \ilBiblFieldFactoryInterface $field_factory;
27 
31  public function __construct(ilBiblFieldFactoryInterface $field_factory)
32  {
33  global $DIC;
34  $this->dic = $DIC;
35  $this->field_factory = $field_factory;
36  }
37 
38  public function translate(ilBiblFieldInterface $field): string
39  {
40  if ($this->translationExistsForFieldAndUsersLanguage($field)) {
41  return $this->getInstanceForFieldAndUsersLanguage($field)->getTranslation();
42  }
44  return $this->getInstanceForFieldAndSystemsLanguage($field)->getTranslation();
45  }
46 
47  $core_translation = $this->translateInCore($field);
48  if (strpos($core_translation, "-") !== 0) {
49  return $core_translation;
50  }
51 
52  return $field->getIdentifier();
53  }
54 
55  public function translateAttribute(ilBiblAttributeInterface $attribute): string
56  {
57  $field = $this->field_factory->findOrCreateFieldOfAttribute($attribute);
58 
59  return $this->translate($field);
60  }
61 
62 
63  public function translateAttributeString(int $type_id, ilBiblAttributeInterface $attribute): string
64  {
65  $field = $this->getFieldFactory()
66  ->findOrCreateFieldByTypeAndIdentifier($type_id, $attribute->getIdentifier());
67 
68  return $this->translate($field);
69  }
70 
72  {
73  return $this->field_factory;
74  }
75 
76  private function translateInCore(ilBiblFieldInterface $field): string
77  {
78  $prefix = $this->getFieldFactory()->getType()->getStringRepresentation();
79  $middle = "default";
80  $identifier = $field->getIdentifier();
81 
82  $topic = implode("_", [$prefix, $middle, $identifier]);
83 
84  return $this->dic->language()->txt(strtolower($topic));
85  }
86 
91  {
92  return !is_null($this->getInstanceForFieldAndUsersLanguage($field));
93  }
94 
99  {
100  return !is_null($this->getInstanceForFieldAndSystemsLanguage($field));
101  }
102 
106  public function translationExistsForField(ilBiblFieldInterface $field): bool
107  {
108  return $this->getCollectionOfTranslationsForField($field)->hasSets();
109  }
110 
115  {
116  global $DIC;
117 
119  return $this->getCollectionOfTranslationsForField($field)
120  ->where(["language_key" => $DIC->user()->getCurrentLanguage(),])
121  ->first();
122  }
123 
128  {
129  global $DIC;
130  $lng = $DIC->language()->getDefaultLanguage();
131 
133  return $this->getCollectionOfTranslationsForField($field)
134  ->where(["language_key" => $lng,])
135  ->first();
136  }
137 
142  ilBiblFieldInterface $field,
143  string $language_key
145  $inst = $this->getCollectionOfTranslationsForField($field)
146  ->where(["language_key" => $language_key,])
147  ->get();
148 
149  if ($inst === []) {
150  $inst = new ilBiblTranslation();
151  $inst->setFieldId($field->getId());
152  $inst->setLanguageKey($language_key);
153  $inst->create();
154  }
155 
156  return $inst;
157  }
158 
162  public function getAllTranslationsForField(ilBiblFieldInterface $field): array
163  {
164  return $this->getCollectionOfTranslationsForField($field)->get();
165  }
166 
171  {
172  return $this->getCollectionOfTranslationsForField($field)->getArray();
173  }
174 
176  {
177  return ilBiblTranslation::where(['field_id' => $field->getId()])->orderBy('language_key');
178  }
179 
183  public function findById(int $id): \ilBiblTranslationInterface
184  {
186  return ilBiblTranslation::findOrFail($id);
187  }
188 
192  public function deleteById(int $id): bool
193  {
194  self::findById($id)->delete();
195 
196  return true;
197  }
198 }
findArCreateInstanceForFieldAndlanguage(ilBiblFieldInterface $field, string $language_key)
ilBiblFieldFactoryInterface $field_factory
translationExistsForFieldAndUsersLanguage(ilBiblFieldInterface $field)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$lng
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
NullPointerExceptionInspection
static where($where, $operator=null)
global $DIC
Definition: feed.php:28
getAllTranslationsForFieldAsArray(ilBiblFieldInterface $field)
static findOrFail($primary_key, array $add_constructor_args=array())
Tries to find the object and throws an Exception if object is not found, instead of returning null...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilBiblFieldFactoryInterface $field_factory)
ilBiblTranslationFactory constructor.
translate(ilBiblFieldInterface $field)
Translates a field in the current users language in tho folowing order:
getAllTranslationsForField(ilBiblFieldInterface $field)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
translationExistsForField(ilBiblFieldInterface $field)
translateAttribute(ilBiblAttributeInterface $attribute)
translateAttributeString(int $type_id, ilBiblAttributeInterface $attribute)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
translationExistsForFieldAndSystemsLanguage(ilBiblFieldInterface $field)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getInstanceForFieldAndUsersLanguage(ilBiblFieldInterface $field)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
getInstanceForFieldAndSystemsLanguage(ilBiblFieldInterface $field)
translateInCore(ilBiblFieldInterface $field)
getCollectionOfTranslationsForField(ilBiblFieldInterface $field)