Problem mit dem Modul navigation_lang_changer

Alles rund um Module und Plugins in CONTENIDO 4.10.
Antworten
lunsen_de
Beiträge: 298
Registriert: Mo 17. Okt 2005, 20:26
Wohnort: Weimar
Kontaktdaten:

Problem mit dem Modul navigation_lang_changer

Beitrag von lunsen_de » Mi 27. Mär 2024, 14:59

Hallo, ich sehe ein Problem beim Modul navigation_lang_changer.

System neueste Dev 4.10 mit PHP 8.1
mit Modrewrite

Bei meiner Seite gibt es zwei Sprachen: de und en

Existiert der Artikel (synchronisiert) in beiden Sprachen kann ich mittels klick auf de oder en zwischen den Sprachen im Artikel wechseln.

Existiert der Artikel in der 2. Sprache (z.B. en) nicht, wird der Link en trotzdem angezeigt und es wird ein Link zur IDart des Artikels in der 2. Sprache generiert, obwohl diesen nicht gibt.
War das früher nicht so, dass der Link (also das en) nur angezeigt wurde, wenn der Artikel und die Kategorie in der 2. Sprache vorhanden und online sind?

Beispiel:
In de ist der Artikel "Start", in en synchronisert "Home" da und online, also wird im Sprachmodul en angezeigt und der Link lautet www.domain.de/en/home.html

In de ist der Artikel "Start", in en gibt es diesen nicht!!! Trotzdem wird im Sprachmodul en angezeigt und der Link lautet www.doman.de/cms/front_content.php?idart=1&changelang=2
Diesen Artikel gibt es nicht und es führt auf die Fehlerseite.

Was muss hier geändert werden, damit der Sprachlink, also das de oder en, nur angezeigt wird, wenn es den Artikel in der 2. Sprache gibt und er online ist?

Hier nochmal der Modulcode:

Code: Alles auswählen

<?php

/**
 * Description: Language changer
 *
 * @package    Module
 * @subpackage NavigationLangChanger
 * @author     alexander.scheider@4fb.de
 * @copyright  four for business AG <www.4fb.de>
 * @license    https://www.contenido.org/license/LIZENZ.txt
 * @link       https://www.4fb.de
 * @link       https://www.contenido.org
 */

// create instances and init vars
$catCollection = new cApiCategoryLanguageCollection();
$artCollection = new cApiArticleLanguageCollection();
$catArtCollection = new cApiCategoryArticleCollection();
$languageCollectionInstance = new cApiLanguageCollection();
$clientsLangInstance = new cApiClientLanguageCollection();
$languageInstance = new cApiLanguage();

$tpl = new cTemplate();
$nextLang = false;
$selectedLang = NULL;
$checkedCatArt = false;
$idcatAuto = cRegistry::getCategoryId();
$artRetItem = NULL;
$urlSet = false;
$currentLanguage = NULL;
$clientId = cRegistry::getClientId();
$catCheck = false;
$artCheck = false;
$startart = NULL;

// get all client language id's
$clientsLangInstance->select("idclient= " . $clientId);
$resultClientLangs = $clientsLangInstance->fetchArray('idlang', 'idlang');

// get all active languages of a client
foreach ($resultClientLangs as $clientLang) {
    $languageInstance->loadByMany(
        [
            'active' => '1',
            'idlang' => $clientLang,
        ]
    );
    if ($languageInstance->get('idlang')) {
        $allLanguages[] = $languageInstance->get('idlang');
    }
    $languageInstance = new cApiLanguage();
}

if (empty($allLanguages)) {
    // no active languages. handling was moved to include.front_content.php (lines 433 - 439).
} else if (count($allLanguages) != 1) {

    $langName = '';

    // else check if there more as one language
    $currentLanguage = cRegistry::getLanguageId();

    // set next language if exists
    foreach ($allLanguages as $langs) {
        if ($langs > $currentLanguage) {
            $langName = conHtmlSpecialChars($languageCollectionInstance->getLanguageName((int) $langs));
            if ('' === trim($langName)) {
                $langName = mi18n("LANGUAGE_NAME_EMPTY");
            }
            $tpl->set('s', 'label', $langName);
            $tpl->set('s', 'title', $langName);

            $selectedLang = $langs;
            $nextLang = true;
            break;
        }
    }

    // otherwise set first language
    if ($nextLang === false) {
        $languageName = conHtmlSpecialChars($languageCollectionInstance->getLanguageName(reset($allLanguages)));
        if ('' === trim($langName)) {
            $langName = mi18n("LANGUAGE_NAME_EMPTY");
        }

        $tpl->set('s', 'label', $languageName);
        $tpl->set('s', 'title', $languageName);
        $selectedLang = reset($allLanguages);
    }

    // check articles, if article exists and is online and not locked set the check to true
    $artCheck = $artCollection->select("idart = '" . cSecurity::toInteger($idart) . "' AND idlang = '" . cSecurity::toInteger($selectedLang) . "' AND online = '1' AND locked = '0'", NULL, NULL, NULL);

    // check if this article is an startarticle
    $startart = $catCollection->getStartIdartByIdcatAndIdlang($idcatAuto, $selectedLang);

    if ($artCheck !== true || ($startart == $idart)) {

        // check category and articles, if category exists and has start article
        // which is online and not locked the set check to true
        $catCheck = $catCollection->select("idcat = '" . cSecurity::toInteger($idcatAuto) . "' AND idlang = '" . cSecurity::toInteger($selectedLang) . "' AND startidartlang != '0'", NULL, NULL, NULL);

        $catRetItem = new cApiCategoryLanguage();
        $catRetItem->loadByCategoryIdAndLanguageId(cSecurity::toInteger($idcatAuto), cSecurity::toInteger($selectedLang));

        if ($catCheck === true && $catRetItem) {
            $artRetItem = $artCollection->fetchById($catRetItem->get('startidartlang'));
        }
        if ($artRetItem) {
            if ($artRetItem->get('online') == 1 && $artRetItem->get('locked') == 0) {
                $checkedCatArt = true;
            }
        }
    }

    // if check is true then set url, otherwise check for next language
    if ($checkedCatArt === true) {
        $url = $catRetItem->getLink($selectedLang);
    } else {
        $config = cRegistry::getClientConfig(cRegistry::getClientId());
        $url = cRegistry::getFrontendUrl() . 'front_content.php?idart='.cSecurity::toInteger($idart).'&changelang=' . cSecurity::toInteger($selectedLang);
    }

    $tpl->set('s', 'url', conHtmlSpecialChars($url));
    $tpl->generate('get.tpl');

}

?>
Grüße Lars

xmurrix
Beiträge: 3148
Registriert: Do 21. Okt 2004, 11:08
Wohnort: Augsburg
Kontaktdaten:

Re: Problem mit dem Modul navigation_lang_changer

Beitrag von xmurrix » Sa 20. Apr 2024, 10:42

Hallo Lars,

im Modulcode ist folgender Block dafür zuständig, dass der Link zum Startartikel der Kategorie in der anderen Sprache erstellt wird, wenn es den aktuellen Artikel in der anderen Sprache nicht gibt, es nicht online und nicht gesperrt ist.

Code: Alles auswählen

    if ($artCheck !== true || ($startart == $idart)) {
        // check category and articles, if category exists and has start article
        // which is online and not locked the set check to true
        $catCheck = $catCollection->select("idcat = '" . cSecurity::toInteger($idcatAuto) . "' AND idlang = '" . cSecurity::toInteger($selectedLang) . "' AND startidartlang != '0'", NULL, NULL, NULL);

        $catRetItem = new cApiCategoryLanguage();
        $catRetItem->loadByCategoryIdAndLanguageId(cSecurity::toInteger($idcatAuto), cSecurity::toInteger($selectedLang));

        if ($catCheck === true && $catRetItem->isLoaded()) {
            $artRetItem = $artCollection->fetchById($catRetItem->get('startidartlang'));
        }
        if ($artRetItem) {
            if ($artRetItem->get('online') == 1 && $artRetItem->get('locked') == 0) {
                $checkedCatArt = true;
            }
        }
    }

Du kannst den ganzen Block auskommentieren, dann sollte für den Fall, das es den Artikel in der anderen Sprache nicht gibt, die Variable $url mit dem Wert '#' gesetzt werden.

Weiter unten wird die Variable $url im Template gesetzt und das Template ausgegeben. Das kannst du in wie folgt ändern, wenn du den Sprachwechsler für die andere Sprache nicht anzeigen willst:

Code: Alles auswählen

    if ($url !== '#') {
        $tpl->set('s', 'url', conHtmlSpecialChars($url));
        $tpl->generate('get.html');
    }
Ich habe das alles zwar nicht getestet, aber so sollte das funktionieren.

Gruß
Murat
CONTENIDO Downloads: CONTENIDO 4.10.1
CONTENIDO Links: Dokumentationsportal, FAQ, API-Dokumentation
CONTENIDO @ Github: CONTENIDO 4.10 - Mit einem Entwicklungszweig (develop-branch), das viele Verbesserungen/Optimierungen erhalten hat und auf Stabilität und Kompatibilität mit PHP 8.0 bis 8.2 getrimmt wurde.

lunsen_de
Beiträge: 298
Registriert: Mo 17. Okt 2005, 20:26
Wohnort: Weimar
Kontaktdaten:

Re: Problem mit dem Modul navigation_lang_changer

Beitrag von lunsen_de » Fr 26. Apr 2024, 16:37

Hallo Murat,

leider hht das nicht funktioniert. Nach dem auskommentieren des ganzen Blocks. geht das AMR nicht mehr und alle Links haben den Aufbau:

https://www.domain.de/cms/front_content ... angelang=2

Egal ob online oder nicht. Und da keine # daraus wird, wenn der Artikel nicht online ist hilft auch der Teil mit derVariable $url im Template nicht.

Das Problem ist also immernoch dasselbe, Artikel in anderer Sprache nicht online und trotzdem ein Link der zur Fehlerseite führt und zusätzlich funktioniert das AMR im Sprachwechsel nicht mehr.

Ich hoffe du kannst Dir das Modul nochmal anschauen, da es ja früher so funktioniert hat und in der aktuellsten Version sicher funktionieren soll.

Grüße Lars

xmurrix
Beiträge: 3148
Registriert: Do 21. Okt 2004, 11:08
Wohnort: Augsburg
Kontaktdaten:

Re: Problem mit dem Modul navigation_lang_changer

Beitrag von xmurrix » Sa 27. Apr 2024, 07:17

Morgen,

der Änderungsvorschlag von mir hat so nicht funktioniert, daher habe ich mir das Modul etwas näher angesehen. Hier ist ein Beispiel, in der der Artikel-Link in anderer Sprache nur dann angezeigt wird, wenn es den Artikel in der anderen Sprache gibt (Artikel existiert, ist online und ist nicht gesperrt):

Code: Alles auswählen

<?php

/**
 * Description: Language changer
 *
 * @package    Module
 * @subpackage NavigationLangChanger
 * @author     alexander.scheider@4fb.de
 * @copyright  four for business AG <www.4fb.de>
 * @license    https://www.contenido.org/license/LIZENZ.txt
 * @link       https://www.4fb.de
 * @link       https://www.contenido.org
 */

// create instances and init vars
$catCollection = new cApiCategoryLanguageCollection();
$artCollection = new cApiArticleLanguageCollection();
$catArtCollection = new cApiCategoryArticleCollection();
$languageCollectionInstance = new cApiLanguageCollection();
$clientsLangInstance = new cApiClientLanguageCollection();
$languageInstance = new cApiLanguage();

$tpl = new cTemplate();
$nextLang = false;
$selectedLang = NULL;
$checkedCatArt = false;
$idcatAuto = cRegistry::getCategoryId();
$artRetItem = NULL;
$urlSet = false;
$currentLanguage = NULL;
$clientId = cRegistry::getClientId();
$catCheck = false;
$artCheck = false;
$startart = NULL;

// get all client language id's
$clientsLangInstance->select("idclient= " . $clientId);
$resultClientLangs = $clientsLangInstance->fetchArray('idlang', 'idlang');

// get all active languages of a client
foreach ($resultClientLangs as $clientLang) {
    $languageInstance->loadByMany(
        [
            'active' => '1',
            'idlang' => $clientLang,
        ]
    );
    if ($languageInstance->get('idlang')) {
        $allLanguageIds[] = cSecurity::toInteger($languageInstance->get('idlang'));
    }
}

if (count($allLanguageIds) != 1) {
    $idart = cSecurity::toInteger(cRegistry::getArticleId());
    $langName = '';

    // else check if there is more than one language
    $currentLanguage = cSecurity::toInteger(cRegistry::getLanguageId());

    // set next language if exists
    foreach ($allLanguageIds as $languageId) {
        if ($languageId > $currentLanguage) {
            $langName = conHtmlSpecialChars($languageCollectionInstance->getLanguageName($languageId));
            if ('' === trim($langName)) {
                $langName = mi18n("LANGUAGE_NAME_EMPTY");
            }
            $tpl->set('s', 'label', $langName);
            $tpl->set('s', 'title', $langName);

            $selectedLang = $languageId;
            $nextLang = true;
            break;
        }
    }

    // otherwise set first language
    if ($nextLang === false) {
        $languageName = conHtmlSpecialChars($languageCollectionInstance->getLanguageName(reset($allLanguageIds)));
        if ('' === trim($langName)) {
            $langName = mi18n("LANGUAGE_NAME_EMPTY");
        }

        $tpl->set('s', 'label', $languageName);
        $tpl->set('s', 'title', $languageName);
        $selectedLang = reset($allLanguageIds);
    }

    // check articles, if article exists and is online and not locked set the check to true
    $artCheck = $artCollection->select("idart = '" . $idart . "' AND idlang = '" . cSecurity::toInteger($selectedLang) . "' AND online = '1' AND locked = '0'", NULL, NULL, NULL);

    // check if this article is an startarticle
    $startart = $catCollection->getStartIdartByIdcatAndIdlang($idcatAuto, $selectedLang);

    if ($artCheck !== true || ($startart == $idart)) {
        // check category and articles, if category exists and has start article
        // which is online and not locked the set check to true
        $catCheck = $catCollection->select("idcat = '" . cSecurity::toInteger($idcatAuto) . "' AND idlang = '" . cSecurity::toInteger($selectedLang) . "' AND startidartlang != '0'", NULL, NULL, NULL);

        $catRetItem = new cApiCategoryLanguage();
        $catRetItem->loadByCategoryIdAndLanguageId(cSecurity::toInteger($idcatAuto), cSecurity::toInteger($selectedLang));

        if ($catCheck === true && $catRetItem) {
            $artRetItem = $artCollection->fetchById($catRetItem->get('startidartlang'));
        }
        if ($artRetItem) {
            if ($artRetItem->get('online') == 1 && $artRetItem->get('locked') == 0) {
                $checkedCatArt = true;
            }
        } else {
            $checkedCatArt = true;
            $catRetItem = null;
        }
    }

    // if check is true then set url, otherwise check for next language
    if ($checkedCatArt === true) {
        $url = isset($catRetItem) ? $catRetItem->getLink($selectedLang) : '#';
    } else {
        $config = cRegistry::getClientConfig(cRegistry::getClientId());
        $url = cRegistry::getFrontendUrl() . 'front_content.php?idart=' . cSecurity::toInteger($idart) . '&changelang=' . cSecurity::toInteger($selectedLang);
    }

    if ($url !== '#') {
        $tpl->set('s', 'url', $url);
        $tpl->generate('get.html');
    }

}

?>
Übrigens, beim Modul 'navigation_lang_changer' sehe ich noch etwas Optimierungsbedarf, es generiert nicht Links zu allen Sprachen -sofern es mehr als zwei Sprachen gibt- auch verwendet es die alte Templatelogik, das sollte auf Smarty umgestellt werden. Ich erstelle mal in GitHub einen Task dafür.

Gruß
Murat
CONTENIDO Downloads: CONTENIDO 4.10.1
CONTENIDO Links: Dokumentationsportal, FAQ, API-Dokumentation
CONTENIDO @ Github: CONTENIDO 4.10 - Mit einem Entwicklungszweig (develop-branch), das viele Verbesserungen/Optimierungen erhalten hat und auf Stabilität und Kompatibilität mit PHP 8.0 bis 8.2 getrimmt wurde.

Antworten