22
33namespace KLXM \YformLangFields ;
44
5+ use rex_clang ;
56use rex_yform_manager_dataset ;
67use rex_yform_manager_table ;
78
89/**
9- * Erweiterte YOrm Dataset-Klasse mit automatischer Array-Konvertierung für Lang-Felder
10- *
10+ * Erweiterte YOrm Dataset-Klasse mit automatischer Array-Konvertierung für Lang-Felder.
11+ *
1112 * Verwendung:
12- * class Article extends \KLXM\YformLangFields\LangDataset
13- * {
14- * // Automatische Array-Konvertierung für alle lang_* Felder
15- * // $article->getValue('title') gibt direkt ein Array zurück
16- * }
13+ * class Article extends \KLXM\YformLangFields\LangDataset
14+ * {
15+ * // $article->getValue('title') gibt direkt ein Array zurück
16+ * }
1717 */
1818class LangDataset extends rex_yform_manager_dataset
1919{
2020 /**
21- * Überschreibt getValue() um Lang-Felder automatisch als Array zurückzugeben
21+ * Überschreibt getValue() um Lang-Felder automatisch als Array zurückzugeben.
22+ *
23+ * @return mixed
2224 */
23- public function getValue ($ key , $ default = null )
25+ public function getValue (string $ key )
2426 {
25- $ value = parent ::getValue ($ key , $ default );
26-
27- // Prüfen ob es ein Lang-Feld ist
27+ $ value = parent ::getValue ($ key );
28+
2829 if ($ this ->isLangField ($ key )) {
29- // JSON automatisch als Array zurückgeben
3030 return LangHelper::normalizeLanguageData ($ value );
3131 }
32-
32+
3333 return $ value ;
3434 }
3535
3636 /**
37- * Raw-Wert (JSON-String) abrufen ohne Konvertierung
37+ * Raw-Wert (JSON-String) abrufen ohne Konvertierung.
38+ *
39+ * @return mixed
3840 */
39- public function getRawValue ($ key , $ default = null )
41+ public function getRawValue (string $ key )
4042 {
41- return parent ::getValue ($ key, $ default );
43+ return parent ::getValue ($ key );
4244 }
4345
4446 /**
45- * Prüft ob ein Feld ein Lang-Feld ist
47+ * Prüft ob ein Feld ein Lang-Feld ist.
4648 */
4749 protected function isLangField (string $ fieldName ): bool
4850 {
49- static $ langFields = null ;
50-
51- if ($ langFields === null ) {
52- $ langFields = [];
53- $ table = rex_yform_manager_table::get (static ::getTableName ());
54-
51+ /** @var array<string, list<string>> $cache */
52+ static $ cache = [];
53+
54+ $ tableName = static ::getTableName ();
55+
56+ if (!isset ($ cache [$ tableName ])) {
57+ $ cache [$ tableName ] = [];
58+ $ table = rex_yform_manager_table::get ($ tableName );
59+
5560 if ($ table ) {
56- $ fields = $ table ->getValueFields ();
57- foreach ($ fields as $ field ) {
61+ foreach ($ table ->getValueFields () as $ field ) {
5862 $ typeName = $ field ->getTypeName ();
59- if (in_array ($ typeName , ['lang_text ' , 'lang_textarea ' , 'lang_media ' ])) {
60- $ langFields [] = $ field ->getName ();
63+ if (in_array ($ typeName , ['lang_text ' , 'lang_textarea ' , 'lang_media ' ], true )) {
64+ $ cache [ $ tableName ] [] = $ field ->getName ();
6165 }
6266 }
6367 }
6468 }
65-
66- return in_array ($ fieldName , $ langFields );
69+
70+ return in_array ($ fieldName , $ cache [ $ tableName ], true );
6771 }
6872
6973 /**
70- * Convenience-Methode: Wert für aktuelle Sprache
74+ * Convenience-Methode: Wert für aktuelle Sprache.
75+ *
76+ * @return mixed
7177 */
72- public function getLang ($ key )
78+ public function getLang (string $ key )
7379 {
74- $ data = $ this ->getValue ($ key ); // Bereits als Array
80+ /** @var list<array{clang_id: int|string, value: mixed}> $data */
81+ $ data = $ this ->getValue ($ key );
7582 $ currentLang = LangHelper::getCurrentLanguage ();
76-
83+
7784 foreach ($ data as $ item ) {
78- if ($ item ['clang_id ' ] === $ currentLang ->getId ()) {
85+ if (( int ) $ item ['clang_id ' ] === $ currentLang ->getId ()) {
7986 return $ item ['value ' ];
8087 }
8188 }
82-
89+
8390 return '' ;
8491 }
8592
8693 /**
87- * Convenience-Methode: Wert für spezifische Sprache
94+ * Convenience-Methode: Wert für spezifische Sprache.
95+ *
96+ * @return mixed
8897 */
89- public function getLangValue ($ key , int $ clangId )
98+ public function getLangValue (string $ key , int $ clangId )
9099 {
91- $ data = $ this ->getValue ($ key ); // Bereits als Array
92-
100+ /** @var list<array{clang_id: int|string, value: mixed}> $data */
101+ $ data = $ this ->getValue ($ key );
102+
93103 foreach ($ data as $ item ) {
94- if ($ item ['clang_id ' ] === $ clangId ) {
104+ if (( int ) $ item ['clang_id ' ] === $ clangId ) {
95105 return $ item ['value ' ];
96106 }
97107 }
98-
108+
99109 return '' ;
100110 }
101111
102112 /**
103- * Alle verfügbaren Übersetzungen als assoziatives Array [clang_id => value]
113+ * Alle verfügbaren Übersetzungen als assoziatives Array [clang_id => value].
114+ *
115+ * @return array<int, mixed>
104116 */
105- public function getAllLangValues ($ key ): array
117+ public function getAllLangValues (string $ key ): array
106118 {
107- $ data = $ this ->getValue ($ key ); // Bereits als Array
119+ /** @var list<array{clang_id: int|string, value: mixed}> $data */
120+ $ data = $ this ->getValue ($ key );
108121 $ result = [];
109-
122+
110123 foreach ($ data as $ item ) {
111- $ result [$ item ['clang_id ' ]] = $ item ['value ' ];
124+ $ result [( int ) $ item ['clang_id ' ]] = $ item ['value ' ];
112125 }
113-
126+
114127 return $ result ;
115128 }
116129
117130 /**
118- * Setzt Wert für spezifische Sprache
131+ * Setzt Wert für spezifische Sprache.
132+ *
133+ * @param mixed $value
119134 */
120- public function setLangValue ($ key , int $ clangId , $ value ): self
135+ public function setLangValue (string $ key , int $ clangId , $ value ): self
121136 {
122- $ currentData = $ this ->getValue ($ key ); // Bereits als Array
123-
137+ /** @var list<array{clang_id: int|string, value: mixed}> $currentData */
138+ $ currentData = $ this ->getValue ($ key );
139+
124140 // Bestehende Übersetzung entfernen
125- $ currentData = array_filter ($ currentData , function ( $ item ) use ($ clangId ) {
126- return $ item ['clang_id ' ] !== $ clangId ;
127- });
128-
141+ $ currentData = array_values ( array_filter ($ currentData , static function ( array $ item ) use ($ clangId ): bool {
142+ return ( int ) $ item ['clang_id ' ] !== $ clangId ;
143+ })) ;
144+
129145 // Neue Übersetzung hinzufügen
130- if (!empty ($ value ) || $ value === 0 || $ value === ' 0 ' ) {
146+ if (!empty ($ value ) || 0 === $ value || ' 0 ' === $ value ) {
131147 $ currentData [] = [
132148 'clang_id ' => $ clangId ,
133- 'value ' => $ value
149+ 'value ' => $ value,
134150 ];
135151 }
136-
137- // Als JSON speichern (Raw-Wert)
138- $ jsonData = json_encode (array_values ($ currentData ), JSON_UNESCAPED_UNICODE );
152+
153+ $ jsonData = json_encode ($ currentData , JSON_UNESCAPED_UNICODE );
154+ if (false === $ jsonData ) {
155+ $ jsonData = '[] ' ;
156+ }
139157 parent ::setValue ($ key , $ jsonData );
140-
158+
141159 return $ this ;
142160 }
143161
144162 /**
145- * Prüfen ob Übersetzung für Sprache existiert
163+ * Prüfen ob Übersetzung für Sprache existiert.
146164 */
147165 public function hasTranslationForLanguage (string $ field , int $ clangId ): bool
148166 {
167+ /** @var list<array{clang_id: int|string, value: mixed}> $data */
149168 $ data = $ this ->getValue ($ field );
150-
169+
151170 foreach ($ data as $ item ) {
152- if ($ item ['clang_id ' ] === $ clangId && !empty ($ item ['value ' ])) {
171+ if (( int ) $ item ['clang_id ' ] === $ clangId && !empty ($ item ['value ' ])) {
153172 return true ;
154173 }
155174 }
156-
175+
157176 return false ;
158177 }
159178
160179 /**
161- * Mehrsprachige Felder einer Tabelle identifizieren
180+ * Mehrsprachige Felder einer Tabelle identifizieren.
181+ *
182+ * @return list<string>
162183 */
163184 public static function getMultilangFields (string $ tableName ): array
164185 {
@@ -168,67 +189,74 @@ public static function getMultilangFields(string $tableName): array
168189 }
169190
170191 $ multilangFields = [];
171- $ fields = $ table ->getValueFields ();
172-
173- foreach ($ fields as $ field ) {
192+ foreach ($ table ->getValueFields () as $ field ) {
174193 $ typeName = $ field ->getTypeName ();
175- if (in_array ($ typeName , ['lang_text ' , 'lang_textarea ' , 'lang_media ' ])) {
194+ if (in_array ($ typeName , ['lang_text ' , 'lang_textarea ' , 'lang_media ' ], true )) {
176195 $ multilangFields [] = $ field ->getName ();
177196 }
178197 }
179-
198+
180199 return $ multilangFields ;
181200 }
182201
183202 /**
184- * Prüfen ob ein Dataset vollständig übersetzt ist
203+ * Prüfen ob ein Dataset vollständig übersetzt ist.
204+ *
205+ * @param list<int> $requiredLanguages
185206 */
186207 public function isFullyTranslatedFor (array $ requiredLanguages ): bool
187208 {
188209 $ multilangFields = self ::getMultilangFields ($ this ->getTableName ());
189-
210+
190211 foreach ($ multilangFields as $ fieldName ) {
191212 foreach ($ requiredLanguages as $ clangId ) {
192213 if (!$ this ->hasTranslationForLanguage ($ fieldName , $ clangId )) {
193214 return false ;
194215 }
195216 }
196217 }
197-
218+
198219 return true ;
199220 }
200221
201222 /**
202- * Übersetzungsstatus für alle Sprachen abrufen
223+ * Übersetzungsstatus für alle Sprachen abrufen.
224+ *
225+ * @return array<int, array{language: rex_clang, translated_fields: int, total_fields: int, is_complete: bool, percentage: int}>
203226 */
204227 public function getTranslationStatus (): array
205228 {
206229 $ multilangFields = self ::getMultilangFields ($ this ->getTableName ());
230+ $ totalFields = count ($ multilangFields );
207231 $ languages = LangHelper::getActiveLanguages ();
208232 $ status = [];
209-
233+
210234 foreach ($ languages as $ lang ) {
211235 $ clangId = $ lang ->getId ();
212- $ status [$ clangId ] = [
213- 'language ' => $ lang ,
214- 'translated_fields ' => 0 ,
215- 'total_fields ' => count ($ multilangFields ),
216- 'is_complete ' => true
217- ];
218-
236+ $ translated = 0 ;
237+ $ isComplete = true ;
238+
219239 foreach ($ multilangFields as $ fieldName ) {
220240 if ($ this ->hasTranslationForLanguage ($ fieldName , $ clangId )) {
221- $ status [ $ clangId ][ ' translated_fields ' ]++ ;
241+ ++ $ translated ;
222242 } else {
223- $ status [ $ clangId ][ ' is_complete ' ] = false ;
243+ $ isComplete = false ;
224244 }
225245 }
226-
227- $ status [ $ clangId ][ ' percentage ' ] = $ status [ $ clangId ][ ' total_fields ' ] > 0
228- ? round (($ status [ $ clangId ][ ' translated_fields ' ] / $ status [ $ clangId ][ ' total_fields ' ] ) * 100 )
246+
247+ $ percentage = $ totalFields > 0
248+ ? ( int ) round (($ translated / $ totalFields ) * 100 )
229249 : 100 ;
250+
251+ $ status [$ clangId ] = [
252+ 'language ' => $ lang ,
253+ 'translated_fields ' => $ translated ,
254+ 'total_fields ' => $ totalFields ,
255+ 'is_complete ' => $ isComplete ,
256+ 'percentage ' => $ percentage ,
257+ ];
230258 }
231-
259+
232260 return $ status ;
233261 }
234- }
262+ }
0 commit comments