ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMMItemTranslationRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27
32{
36 private const TABLE_NAME = 'il_mm_translation';
37
38 public function __construct(private ilDBInterface $db)
39 {
40 }
41
42 public function store(Translations $translations): Translations
43 {
44 foreach ($translations->get() as $translation) {
46 $translations->getId(),
47 $translation->getLanguageCode(),
48 $translation->getTranslation()
49 );
50 }
51
52 return $translations;
53 }
54
55 private function has(string $identification, string $language_code): bool
56 {
57 $language_identification = "{$identification}|$language_code";
58
59 return $this->db->queryF(
60 'SELECT id FROM ' . self::TABLE_NAME . ' WHERE id = %s',
61 ['text'],
62 [$language_identification]
63 )->numRows() > 0;
64 }
65
66 private function storeSingleTranslation(
67 string $identification,
68 string $language_code,
69 string $translation
70 ): void {
71 $language_identification = "{$identification}|$language_code";
72 if ($this->has($identification, $language_code)) {
73 $this->db->update(
74 self::TABLE_NAME,
75 [
76 'translation' => ['text', $translation],
77 ],
78 [
79 'id' => ['text', $language_identification]
80 ]
81 );
82 } else {
83 $this->db->insert(
84 self::TABLE_NAME,
85 [
86 'id' => ['text', $language_identification],
87 'identification' => ['text', $identification],
88 'translation' => ['text', $translation],
89 'language_key' => ['text', $language_code],
90 ]
91 );
92 }
93 }
94
95 public function get(TranslatableItem $item): Translations
96 {
97 $identification = $item->getId();
98 $r = $this->db->queryF(
99 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE identification = %s',
100 ['text'],
101 [$identification]
102 );
103 $translations = [];
104 while ($row = $this->db->fetchAssoc($r)) {
105 if (empty($row['translation'])) {
106 continue;
107 }
108
109 $translations[] = new TranslationDTO(
110 $row['identification'],
111 $row['language_key'],
112 $row['translation']
113 );
114 }
115
116 return new Translations('en', $item, ...$translations);
117 }
118
119 public function blank(TranslatableItem $item, string $language_code, string $translation): Translation
120 {
121 return new TranslationDTO(
122 $item->getId(),
123 $language_code,
124 $translation
125 );
126 }
127
128 public function reset(): void
129 {
130 $this->db->manipulate('TRUNCATE TABLE ' . self::TABLE_NAME);
131 }
132
133 public function retrieveCurrent(Pons $pons): TranslatableItem
134 {
135 $pons->tabs()->structure()->current();
136
137 $id = $pons->in()->getFirstFromRequest('top_id');
138 return (new ilMMItemRepository())->getItemFacadeForIdentificationString($id);
139 }
140
141}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
storeSingleTranslation(string $identification, string $language_code, string $translation)
has(string $identification, string $language_code)
blank(TranslatableItem $item, string $language_code, string $translation)
Interface ilDBInterface.