2020-11-11 19:48:07 +01:00
< ? php
/**
* This file is part of Zwii .
* For full copyright and license information , please see the LICENSE
* file that was distributed with this source code .
*
* @ author Rémi Jean < remi . jean @ outlook . com >
* @ copyright Copyright ( C ) 2008 - 2018 , Rémi Jean
2021-02-17 13:49:58 +01:00
* @ author Frédéric Tempez < frederic . tempez @ outlook . com >
2021-12-18 10:25:33 +01:00
* @ copyright Copyright ( C ) 2018 - 2022 , Frédéric Tempez
2020-11-11 19:48:07 +01:00
* @ license GNU General Public License , version 3
* @ link http :// zwiicms . fr /
*/
class translate extends common {
public static $actions = [
2021-06-04 13:57:44 +02:00
'index' => self :: GROUP_ADMIN ,
'copy' => self :: GROUP_ADMIN ,
2022-09-26 14:54:15 +02:00
'add' => self :: GROUP_ADMIN , // Ajouter une langue de contenu
'edit' => self :: GROUP_ADMIN , // Editer une langue de contenu
'delete' => self :: GROUP_ADMIN , // Effacer une langue de contenu
'i18n' => self :: GROUP_VISITOR ,
2020-11-11 19:48:07 +01:00
];
2020-11-26 08:59:04 +01:00
2022-09-21 09:53:50 +02:00
// Language content
2021-06-04 13:57:44 +02:00
public static $translateOptions = [];
2022-09-24 17:27:15 +02:00
// Page pour la configuration dans la langue
2022-08-29 22:04:57 +02:00
public static $pagesList = [];
public static $orphansList = [];
2021-06-04 13:57:44 +02:00
// Liste des langues installées
public static $languagesInstalled = [];
// Liste des langues cibles
public static $languagesTarget = [];
// Activation du bouton de copie
public static $siteTranslate = true ;
2022-09-24 17:27:15 +02:00
//UI
2022-09-21 09:53:50 +02:00
// Fichiers des langues de l'interface
public static $i18nFiles = [];
2022-09-19 11:40:39 +02:00
2021-06-04 13:57:44 +02:00
/**
* Configuration avancée des langues
*/
public function copy () {
// Soumission du formulaire
if ( $this -> isPost ()) {
// Initialisation
$success = false ;
$copyFrom = $this -> getInput ( 'translateFormCopySource' );
$toCreate = $this -> getInput ( 'translateFormCopyTarget' );
if ( $copyFrom !== $toCreate ) {
// Création du dossier
if ( is_dir ( self :: DATA_DIR . $toCreate ) === false ) { // Si le dossier est déjà créé
2021-09-29 14:50:25 +02:00
$success = mkdir ( self :: DATA_DIR . $toCreate , 0755 );
2021-11-02 12:53:55 +01:00
$success = mkdir ( self :: DATA_DIR . $toCreate . '/content' , 0755 );
2021-06-04 13:57:44 +02:00
} else {
$success = true ;
}
// Copier les données par défaut avec gestion des erreurs
$success = ( copy ( self :: DATA_DIR . $copyFrom . '/locale.json' , self :: DATA_DIR . $toCreate . '/locale.json' ) === true && $success === true ) ? true : false ;
$success = ( copy ( self :: DATA_DIR . $copyFrom . '/module.json' , self :: DATA_DIR . $toCreate . '/module.json' ) === true && $success === true ) ? true : false ;
$success = ( copy ( self :: DATA_DIR . $copyFrom . '/page.json' , self :: DATA_DIR . $toCreate . '/page.json' ) === true && $success === true ) ? true : false ;
2021-11-02 12:53:55 +01:00
$success = ( $this -> copyDir ( self :: DATA_DIR . $copyFrom . '/content' , self :: DATA_DIR . $toCreate . '/content' ) === true && $success === true ) ? true : false ;
2021-06-04 13:57:44 +02:00
// Enregistrer la langue
if ( $success ) {
$this -> setData ([ 'config' , 'i18n' , $toCreate , 'site' ]);
2022-09-26 14:54:15 +02:00
$notification = 'Données ' . self :: $languages [ $copyFrom ] . ' copiées vers ' . self :: $languages [ $toCreate ];
2021-06-04 13:57:44 +02:00
} else {
$notification = " Quelque chose n \ 'a pas fonctionné, vérifiez les permissions. " ;
}
} else {
$success = false ;
$notification = 'Les langues doivent être différentes.' ;
}
// Valeurs en sortie
$this -> addOutput ([
'notification' => $notification ,
'title' => 'Utilitaire de copie' ,
'view' => 'index' ,
'state' => $success
]);
}
// Tableau des langues installées
2022-09-26 14:54:15 +02:00
foreach ( self :: $languages as $key => $value ) {
2021-06-04 13:57:44 +02:00
if ( $this -> getData ([ 'config' , 'i18n' , $key ]) === 'site' ) {
self :: $languagesTarget [ $key ] = $value ;
}
}
// Langues cibles fr en plus
self :: $languagesInstalled = array_merge ([ 'fr' => 'Français (fr)' ], self :: $languagesTarget );
// Valeurs en sortie
$this -> addOutput ([
'title' => 'Utilitaire de copie' ,
'view' => 'copy'
]);
}
2020-11-11 19:48:07 +01:00
/**
* Configuration
*/
public function index () {
2020-11-25 08:21:52 +01:00
2020-11-11 19:48:07 +01:00
// Soumission du formulaire
if ( $this -> isPost ()) {
2021-06-18 19:50:41 +02:00
2022-09-26 14:54:15 +02:00
// Sauvegarder les langues de contenu
$this -> setData ([ 'config' , 'i18n' , 'interface' , $this -> getInput ( 'translateUI' )]);
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl () . $this -> getUrl (),
'notification' => 'Modifications enregistrées' ,
'state' => true
]);
}
// Préparation du formulaire
// -------------------------
foreach ( self :: $languages as $keyi18n => $value ) {
// tableau des langues installées
if ( is_dir ( self :: DATA_DIR . $keyi18n ) ) {
self :: $languagesInstalled [] = [
$value . '(' . $keyi18n . ')' ,
template :: button ( 'translateContentLanguageEdit' . $keyi18n , [
'href' => helper :: baseUrl () . $this -> getUrl ( 0 ) . '/edit/' . $keyi18n . '/' . $_SESSION [ 'csrf' ],
'value' => template :: ico ( 'pencil' ),
'help' => 'Editer les locales'
]),
template :: button ( 'translateContentLanguageDelete' . $keyi18n , [
'class' => 'buttonRed' ,
'href' => helper :: baseUrl () . $this -> getUrl ( 0 ) . '/delete/' . $keyi18n . '/' . $_SESSION [ 'csrf' ],
'value' => template :: ico ( 'trash' ),
'help' => 'Supprimer cette langue'
])
];
}
}
// Liste des langues disponibles
if ( is_dir ( self :: I18N_DIR )) {
$dir = getcwd ();
chdir ( self :: I18N_DIR );
$files = glob ( '*.json' );
// Ajouter une clé au tableau avec le code de langue
foreach ( $files as $file ) {
// La langue est-elle référencée ?
if ( array_key_exists ( basename ( $file , '.json' ), self :: $languages )) {
self :: $i18nFiles [ basename ( $file , '.json' )] = self :: $languages [ basename ( $file , '.json' )];
}
2020-11-25 10:01:13 +01:00
}
2022-09-26 14:54:15 +02:00
chdir ( $dir );
}
// Valeurs en sortie
$this -> addOutput ([
'title' => 'Multilangues' ,
'view' => 'index'
]);
}
2021-06-18 21:10:16 +02:00
2022-09-26 14:54:15 +02:00
/***
* Ajouter unelangue de contenu
*/
public function add () {
// Soumission du formulaire
if ( $this -> isPost ()) {
2022-08-29 22:04:57 +02:00
2022-09-24 18:06:32 +02:00
// Configuration dans des langues spécifiques
2022-08-29 22:04:57 +02:00
// Eviter déconnexion automatique après son activation
if ( $this -> getData ([ 'config' , 'connect' , 'autoDisconnect' ]) === false
2022-09-26 14:54:15 +02:00
AND $this -> getInput ( 'configAutoDisconnect' , helper :: FILTER_BOOLEAN ) === true ) {
$this -> setData ([ 'user' , $this -> getuser ( 'id' ), 'accessCsrf' , $_SESSION [ 'csrf' ]]);
2022-08-29 22:04:57 +02:00
}
// Répercuter la suppression de la page dans la configuration du footer
if ( $this -> getData ([ 'theme' , 'footer' , 'displaySearch' ]) === true
AND $this -> getInput ( 'configSearchPageId' ) === 'none'
){
$this -> setData ([ 'theme' , 'footer' , 'displaySearch' , false ]);
}
if ( $this -> getData ([ 'theme' , 'footer' , 'displayLegal' ]) === true
AND $this -> getInput ( 'configLegalPageId' ) === 'none'
){
$this -> setData ([ 'theme' , 'footer' , 'displayLegal' , false ]);
}
// Sauvegarder les locales
$this -> setData ([
'locale' ,
[
'homePageId' => $this -> getInput ( 'localeHomePageId' , helper :: FILTER_ID , true ),
'page404' => $this -> getInput ( 'localePage404' ),
'page403' => $this -> getInput ( 'localePage403' ),
'page302' => $this -> getInput ( 'localePage302' ),
'legalPageId' => $this -> getInput ( 'localeLegalPageId' ),
'searchPageId' => $this -> getInput ( 'localeSearchPageId' ),
'searchPageLabel' => empty ( $this -> getInput ( 'localeSearchPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Rechercher' : $this -> getInput ( 'localeSearchPageLabel' , helper :: FILTER_STRING_SHORT ),
'legalPageLabel' => empty ( $this -> getInput ( 'localeLegalPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Mentions légales' : $this -> getInput ( 'localeLegalPageLabel' , helper :: FILTER_STRING_SHORT ),
'sitemapPageLabel' => empty ( $this -> getInput ( 'localeSitemapPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Plan du site' : $this -> getInput ( 'localeSitemapPageLabel' , helper :: FILTER_STRING_SHORT ),
'metaDescription' => $this -> getInput ( 'localeMetaDescription' , helper :: FILTER_STRING_LONG , true ),
'title' => $this -> getInput ( 'localeTitle' , helper :: FILTER_STRING_SHORT , true ),
'cookies' => [
// Les champs sont obligatoires si l'option consentement des cookies est active
'mainLabel' => $this -> getInput ( 'localeCookiesZwiiText' , helper :: FILTER_STRING_LONG , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN )),
'titleLabel' => $this -> getInput ( 'localeCookiesTitleText' , helper :: FILTER_STRING_SHORT , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN )),
'linkLegalLabel' => $this -> getInput ( 'localeCookiesLinkMlText' , helper :: FILTER_STRING_SHORT , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN )),
'cookiesFooterText' => $this -> getInput ( 'localeCookiesFooterText' , helper :: FILTER_STRING_SHORT , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN )),
'buttonValidLabel' => $this -> getInput ( 'localeCookiesButtonText' , helper :: FILTER_STRING_SHORT , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN ))
]
]
]);
2020-11-11 19:48:07 +01:00
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl () . $this -> getUrl (),
'notification' => 'Modifications enregistrées' ,
'state' => true
]);
}
2022-09-24 17:27:15 +02:00
2022-09-26 14:54:15 +02:00
2022-09-12 22:14:51 +02:00
// Préparation de l'affichage du formulaire
2022-09-24 18:06:32 +02:00
//-----------------------------------------
2022-09-26 14:54:15 +02:00
// Générer la liste des pages disponibles
self :: $pagesList = $this -> getData ([ 'page' ]);
foreach ( self :: $pagesList as $page => $pageId ) {
if ( $this -> getData ([ 'page' , $page , 'block' ]) === 'bar' ||
$this -> getData ([ 'page' , $page , 'disable' ]) === true ) {
unset ( self :: $pagesList [ $page ]);
2022-09-12 22:14:51 +02:00
}
}
2022-09-26 14:54:15 +02:00
self :: $orphansList = $this -> getData ([ 'page' ]);
foreach ( self :: $orphansList as $page => $pageId ) {
if ( $this -> getData ([ 'page' , $page , 'block' ]) === 'bar' ||
$this -> getData ([ 'page' , $page , 'disable' ]) === true ||
$this -> getdata ([ 'page' , $page , 'position' ]) !== 0 ) {
unset ( self :: $orphansList [ $page ]);
2021-06-04 13:57:44 +02:00
}
}
2022-09-26 14:54:15 +02:00
// Valeurs en sortie
$this -> addOutput ([
'title' => 'Ajouter' ,
'view' => 'add'
]);
}
public function edit () {
// Soumission du formulaire
if ( $this -> isPost ()) {
// Configuration dans des langues spécifiques
// Eviter déconnexion automatique après son activation
if ( $this -> getData ([ 'config' , 'connect' , 'autoDisconnect' ]) === false
AND $this -> getInput ( 'configAutoDisconnect' , helper :: FILTER_BOOLEAN ) === true ) {
$this -> setData ([ 'user' , $this -> getuser ( 'id' ), 'accessCsrf' , $_SESSION [ 'csrf' ]]);
}
// Répercuter la suppression de la page dans la configuration du footer
if ( $this -> getData ([ 'theme' , 'footer' , 'displaySearch' ]) === true
AND $this -> getInput ( 'configSearchPageId' ) === 'none'
){
$this -> setData ([ 'theme' , 'footer' , 'displaySearch' , false ]);
}
if ( $this -> getData ([ 'theme' , 'footer' , 'displayLegal' ]) === true
AND $this -> getInput ( 'configLegalPageId' ) === 'none'
){
$this -> setData ([ 'theme' , 'footer' , 'displayLegal' , false ]);
}
// Sauvegarder les locales
$this -> setData ([
'locale' ,
[
'homePageId' => $this -> getInput ( 'localeHomePageId' , helper :: FILTER_ID , true ),
'page404' => $this -> getInput ( 'localePage404' ),
'page403' => $this -> getInput ( 'localePage403' ),
'page302' => $this -> getInput ( 'localePage302' ),
'legalPageId' => $this -> getInput ( 'localeLegalPageId' ),
'searchPageId' => $this -> getInput ( 'localeSearchPageId' ),
'searchPageLabel' => empty ( $this -> getInput ( 'localeSearchPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Rechercher' : $this -> getInput ( 'localeSearchPageLabel' , helper :: FILTER_STRING_SHORT ),
'legalPageLabel' => empty ( $this -> getInput ( 'localeLegalPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Mentions légales' : $this -> getInput ( 'localeLegalPageLabel' , helper :: FILTER_STRING_SHORT ),
'sitemapPageLabel' => empty ( $this -> getInput ( 'localeSitemapPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Plan du site' : $this -> getInput ( 'localeSitemapPageLabel' , helper :: FILTER_STRING_SHORT ),
'metaDescription' => $this -> getInput ( 'localeMetaDescription' , helper :: FILTER_STRING_LONG , true ),
'title' => $this -> getInput ( 'localeTitle' , helper :: FILTER_STRING_SHORT , true ),
'cookies' => [
// Les champs sont obligatoires si l'option consentement des cookies est active
'mainLabel' => $this -> getInput ( 'localeCookiesZwiiText' , helper :: FILTER_STRING_LONG , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN )),
'titleLabel' => $this -> getInput ( 'localeCookiesTitleText' , helper :: FILTER_STRING_SHORT , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN )),
'linkLegalLabel' => $this -> getInput ( 'localeCookiesLinkMlText' , helper :: FILTER_STRING_SHORT , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN )),
'cookiesFooterText' => $this -> getInput ( 'localeCookiesFooterText' , helper :: FILTER_STRING_SHORT , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN )),
'buttonValidLabel' => $this -> getInput ( 'localeCookiesButtonText' , helper :: FILTER_STRING_SHORT , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN ))
]
]
]);
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl () . $this -> getUrl (),
'notification' => 'Modifications enregistrées' ,
'state' => true
]);
}
// Préparation de l'affichage du formulaire
//-----------------------------------------
2022-09-21 15:09:13 +02:00
// Générer la liste des pages disponibles
2022-08-29 22:04:57 +02:00
self :: $pagesList = $this -> getData ([ 'page' ]);
foreach ( self :: $pagesList as $page => $pageId ) {
if ( $this -> getData ([ 'page' , $page , 'block' ]) === 'bar' ||
$this -> getData ([ 'page' , $page , 'disable' ]) === true ) {
unset ( self :: $pagesList [ $page ]);
}
}
self :: $orphansList = $this -> getData ([ 'page' ]);
foreach ( self :: $orphansList as $page => $pageId ) {
if ( $this -> getData ([ 'page' , $page , 'block' ]) === 'bar' ||
$this -> getData ([ 'page' , $page , 'disable' ]) === true ||
$this -> getdata ([ 'page' , $page , 'position' ]) !== 0 ) {
unset ( self :: $orphansList [ $page ]);
}
}
2022-09-26 14:54:15 +02:00
2020-11-11 19:48:07 +01:00
// Valeurs en sortie
$this -> addOutput ([
2022-09-26 14:54:15 +02:00
'title' => 'Edition' ,
'view' => 'edit'
2020-11-11 19:48:07 +01:00
]);
2020-11-22 13:32:20 +01:00
}
2022-09-26 14:54:15 +02:00
/***
* Effacer une langue de contenu
*/
public function delete () {
// Edition des langues
foreach ( self :: $languages as $keyi18n => $value ) {
if ( $keyi18n === 'fr_FR' ) continue ;
// Effacement d'une langue installée
if ( is_dir ( self :: DATA_DIR . $keyi18n ) === true
AND $this -> getInput ( 'translate' . strtoupper ( $keyi18n )) === 'delete' )
{
$this -> removeDir ( self :: DATA_DIR . $keyi18n );
// Au cas ou la langue est sélectionnée
helper :: deleteCookie ( 'ZWII_I18N_SITE' );
}
}
}
2021-06-04 13:57:44 +02:00
/*
* Traitement du changement de langue
* Fonction utilisée par le noyau
*/
2021-11-29 17:29:59 +01:00
public function i18n () {
2021-06-04 13:57:44 +02:00
2021-06-18 19:50:41 +02:00
// Activation du drapeau
2021-06-04 13:57:44 +02:00
if ( $this -> getInput ( 'ZWII_I18N_' . strtoupper ( $this -> getUrl ( 3 ))) !== $this -> getUrl ( 2 ) ) {
// Nettoyer et stocker le choix de l'utilisateur
helper :: deleteCookie ( 'ZWII_I18N_SITE' );
// Sélectionner
2021-06-18 19:50:41 +02:00
setcookie ( 'ZWII_I18N_' . strtoupper ( $this -> getUrl ( 3 )) , $this -> getUrl ( 2 ), time () + 3600 , helper :: baseUrl ( false , false ) , '' , helper :: isHttps (), true );
2021-06-04 13:57:44 +02:00
// Désactivation du drapeau, langue FR par défaut
} else {
setcookie ( 'ZWII_I18N_SITE' , 'fr' , time () + 3600 , helper :: baseUrl ( false , false ) , '' , helper :: isHttps (), true );
}
// Valeurs en sortie
2020-11-24 11:12:33 +01:00
$this -> addOutput ([
2021-12-06 18:26:16 +01:00
'redirect' => helper :: baseUrl () . $this -> getData ([ 'locale' , $this -> getUrl ( 2 ), 'homePageId' ])
2020-11-24 11:12:33 +01:00
]);
2020-11-22 13:32:20 +01:00
}
2020-11-11 19:48:07 +01:00
}