@@ -33,21 +33,38 @@ public static function getActiveDomainId(): int
3333 // Prüfe URL-Parameter
3434 $ requested = \rex_request ('domain_id ' , 'int ' , 0 );
3535 if ($ requested > 0 && self ::domainExists ($ requested )) {
36- \rex_set_session (self ::SESSION_KEY , $ requested );
36+ if (\rex::isBackend ()) {
37+ \rex_set_session (self ::SESSION_KEY , $ requested );
38+ }
3739 return $ requested ;
3840 }
3941
40- // Prüfe Session
41- $ sessionDomainId = (int ) \rex_session (self ::SESSION_KEY , 'int ' , 0 );
42- if ($ sessionDomainId > 0 && self ::domainExists ($ sessionDomainId )) {
43- return $ sessionDomainId ;
42+ // Frontend: ohne Session arbeiten (verhindert Fehler bei nicht eingeloggten Besuchern)
43+ if (!\rex::isBackend ()) {
44+ if (\rex_addon::get ('yrewrite ' )->isAvailable () && class_exists ('rex_yrewrite ' )) {
45+ $ currentDomain = \rex_yrewrite::getCurrentDomain ();
46+ if ($ currentDomain && method_exists ($ currentDomain , 'getId ' )) {
47+ $ currentDomainId = (int ) $ currentDomain ->getId ();
48+ if ($ currentDomainId > 0 ) {
49+ return $ currentDomainId ;
50+ }
51+ }
52+ }
53+ } else {
54+ // Backend: Session-Auswahl berücksichtigen
55+ $ sessionDomainId = (int ) \rex_session (self ::SESSION_KEY , 'int ' , 0 );
56+ if ($ sessionDomainId > 0 && self ::domainExists ($ sessionDomainId )) {
57+ return $ sessionDomainId ;
58+ }
4459 }
4560
4661 // Fallback zur ersten Domain
4762 $ domains = self ::getDomains ();
4863 if (!empty ($ domains )) {
4964 $ fallbackDomainId = (int ) $ domains [0 ]['id ' ];
50- \rex_set_session (self ::SESSION_KEY , $ fallbackDomainId );
65+ if (\rex::isBackend ()) {
66+ \rex_set_session (self ::SESSION_KEY , $ fallbackDomainId );
67+ }
5168 return $ fallbackDomainId ;
5269 }
5370
@@ -70,9 +87,17 @@ public static function getActiveDomain(): ?array
7087
7188 public static function domainExists (int $ domainId ): bool
7289 {
90+ if (!\rex_addon::get ('yrewrite ' )->isAvailable ()) {
91+ return false ;
92+ }
93+
7394 $ sql = \rex_sql::factory ();
74- $ sql ->setQuery ('SELECT id FROM ' . \rex::getTable ('yrewrite_domain ' ) . ' WHERE id = ? ' , [$ domainId ]);
75- return $ sql ->getRows () > 0 ;
95+ try {
96+ $ sql ->setQuery ('SELECT id FROM ' . \rex::getTable ('yrewrite_domain ' ) . ' WHERE id = ? ' , [$ domainId ]);
97+ return $ sql ->getRows () > 0 ;
98+ } catch (\rex_sql_exception $ e ) {
99+ return false ;
100+ }
76101 }
77102
78103 public static function renderDomainTabs (int $ activeDomainId ): string
0 commit comments