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 >
2023-01-09 10:23:32 +01:00
* @ copyright Copyright ( C ) 2018 - 2023 , Frédéric Tempez
2022-12-29 17:02:20 +01:00
* @ license CC Attribution - NonCommercial - NoDerivatives 4.0 International
2020-11-11 19:48:07 +01:00
* @ link http :// zwiicms . fr /
*/
2023-07-19 18:03:00 +02:00
class language extends common
2022-09-29 08:45:59 +02:00
{
2020-11-11 19:48:07 +01:00
2023-08-07 20:28:09 +02:00
// URL langues de l'UI en ligne
const ZWII_UI_URL = 'https://forge.chapril.org/ZwiiCMS-Team/zwiicms-translations/raw/branch/master/v13/' ;
2023-08-06 11:17:36 +02:00
2020-11-11 19:48:07 +01:00
public static $actions = [
2021-06-04 13:57:44 +02:00
'index' => self :: GROUP_ADMIN ,
'copy' => self :: GROUP_ADMIN ,
2023-02-04 17:39:50 +01:00
'add' => self :: GROUP_ADMIN ,
// Ajouter une langue de contenu
'edit' => self :: GROUP_ADMIN ,
// Éditer une langue de l'UI
'locale' => self :: GROUP_ADMIN ,
// Éditer une langue de contenu
'delete' => self :: GROUP_ADMIN ,
// Effacer une langue de contenu ou de l'interface
2022-09-29 19:08:32 +02:00
'content' => self :: GROUP_VISITOR ,
2022-12-20 15:24:52 +01:00
'update' => self :: GROUP_ADMIN ,
2023-04-24 20:46:14 +02:00
'default' => self :: GROUP_ADMIN
2020-11-11 19:48:07 +01:00
];
2020-11-26 08:59:04 +01:00
2022-10-15 09:00:53 +02:00
const PAGINATION = '20' ;
2022-09-27 11:25:05 +02:00
// Language contents
2021-06-04 13:57:44 +02:00
public static $translateOptions = [];
2022-10-15 09:00:53 +02:00
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 = [];
2022-10-15 09:00:53 +02:00
public static $pages = '' ;
2021-06-04 13:57:44 +02:00
// Liste des langues installées
2022-10-14 19:18:48 +02:00
public static $languagesUiInstalled = [];
2021-06-04 13:57:44 +02:00
public static $languagesInstalled = [];
2022-12-28 16:20:55 +01:00
public static $languagesStore = [];
public static $dialogues = [];
2022-10-15 09:00:53 +02:00
2021-06-04 13:57:44 +02:00
// Liste des langues cibles
public static $languagesTarget = [];
2022-10-15 09:00:53 +02:00
2021-06-04 13:57:44 +02:00
// Activation du bouton de copie
2022-09-28 15:58:35 +02:00
public static $siteCopy = true ;
2022-10-15 09:00:53 +02:00
2022-09-27 11:25:05 +02:00
// Localisation en cours d'édition
public static $locales = [];
2021-06-04 13:57:44 +02:00
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
2022-12-20 15:24:52 +01:00
/**
2022-12-27 17:59:04 +01:00
* Met à jour les traduction du site depuis le store
2022-12-20 15:24:52 +01:00
*/
public function update ()
{
2022-12-27 17:59:04 +01:00
if (
2023-06-29 17:01:27 +02:00
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) !== true
2022-12-27 17:59:04 +01:00
) {
// Valeurs en sortie
$this -> addOutput ([
2023-06-29 17:01:27 +02:00
'access' => false
2022-12-27 17:59:04 +01:00
]);
2023-06-29 17:01:27 +02:00
} else {
$lang = $this -> getUrl ( 2 );
// Action interdite ou URl avec le code langue incorrecte
if (
array_key_exists ( $lang , self :: $languages ) === false
) {
// Valeurs en sortie
$this -> addOutput ([
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2023-06-29 17:01:27 +02:00
'state' => false ,
'notification' => helper :: translate ( 'Action interdite' )
]);
}
2022-12-27 17:59:04 +01:00
2023-07-19 17:56:19 +02:00
// Télécharger le descripteur en ligne
2023-08-07 20:28:09 +02:00
$languageData = json_decode ( helper :: getUrlContents ( self :: ZWII_UI_URL . $lang . '.json' ), true );
2023-08-06 22:31:44 +02:00
$descripteur = json_decode ( helper :: getUrlContents ( self :: ZWII_UI_URL . 'language.json' ), true );
$success = false ;
2023-07-19 17:56:19 +02:00
if (
is_array ( $languageData ) &&
2023-08-06 11:17:36 +02:00
is_array ( $descripteur [ 'language' ][ $lang ])
2023-07-24 13:13:28 +02:00
) {
2023-08-06 22:31:44 +02:00
if ( $this -> setData ([ 'language' , $lang , $descripteur [ 'language' ][ $lang ]])) {
$success = file_put_contents ( self :: I18N_DIR . $lang . '.json' , json_encode ( $languageData , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ));
$success = is_int ( $success ) ? true : false ;
}
2023-06-29 17:01:27 +02:00
}
// Valeurs en sortie
$this -> addOutput ([
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2023-08-06 22:31:44 +02:00
'notification' => $success ? helper :: translate ( 'Copie terminée avec succès' ) : 'Copie terminée avec des erreurs' ,
'state' => $success
2022-12-28 16:20:55 +01:00
]);
2022-12-27 17:59:04 +01:00
}
2022-12-20 15:24:52 +01:00
}
2021-06-04 13:57:44 +02:00
/**
* Configuration avancée des langues
*/
2022-09-29 08:45:59 +02:00
public function copy ()
{
2021-06-04 13:57:44 +02:00
// Soumission du formulaire
2023-06-29 17:01:27 +02:00
if (
2023-06-30 09:09:39 +02:00
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) === true &&
2023-06-29 17:01:27 +02:00
$this -> isPost ()
) {
2021-06-04 13:57:44 +02:00
// Initialisation
$success = false ;
$copyFrom = $this -> getInput ( 'translateFormCopySource' );
$toCreate = $this -> getInput ( 'translateFormCopyTarget' );
if ( $copyFrom !== $toCreate ) {
// Création du dossier
2022-09-29 08:45:59 +02:00
if ( is_dir ( self :: DATA_DIR . $toCreate ) === false ) { // Si le dossier est déjà créé
2023-02-04 17:39:50 +01:00
$success = mkdir ( self :: DATA_DIR . $toCreate , 0755 );
$success = mkdir ( self :: DATA_DIR . $toCreate . '/content' , 0755 );
2021-06-04 13:57:44 +02:00
} else {
$success = true ;
}
2022-09-29 10:55:20 +02:00
// Copier les données par défaut
2023-02-04 17:39:50 +01:00
$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 ;
$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 ) {
2023-02-04 17:39:50 +01:00
$notification = sprintf ( helper :: translate ( 'Données %s copiées vers %s' ), self :: $languages [ $copyFrom ], self :: $languages [ $toCreate ]);
2021-06-04 13:57:44 +02:00
} else {
2022-10-11 10:33:44 +02:00
$notification = helper :: translate ( 'Erreur de copie, vérifiez les permissions' );
2021-06-04 13:57:44 +02:00
}
} else {
$success = false ;
2022-10-11 10:33:44 +02:00
$notification = helper :: translate ( 'Les langues sélectionnées sont identiques' );
2021-06-04 13:57:44 +02:00
}
// Valeurs en sortie
$this -> addOutput ([
2023-02-04 17:39:50 +01:00
'notification' => $notification ,
'title' => 'Utilitaire de copie' ,
'view' => 'index' ,
'state' => $success
2021-06-04 13:57:44 +02:00
]);
}
2022-09-29 10:55:20 +02:00
2021-06-04 13:57:44 +02:00
// Tableau des langues installées
2022-09-26 14:54:15 +02:00
foreach ( self :: $languages as $key => $value ) {
2022-09-29 10:55:20 +02:00
// tableau des langues installées
if ( is_dir ( self :: DATA_DIR . $key )) {
2022-09-29 19:08:32 +02:00
self :: $languagesTarget [ $key ] = self :: $languages [ $key ];
2021-06-04 13:57:44 +02:00
}
}
2022-09-29 10:55:20 +02:00
2021-06-04 13:57:44 +02:00
// Langues cibles fr en plus
2022-09-29 10:55:20 +02:00
self :: $languagesInstalled = self :: $languagesTarget ;
2021-06-04 13:57:44 +02:00
// Valeurs en sortie
$this -> addOutput ([
2022-10-02 10:59:42 +02:00
'title' => helper :: translate ( 'Copie de contenus localisés' ),
2021-06-04 13:57:44 +02:00
'view' => 'copy'
]);
}
2020-11-11 19:48:07 +01:00
/**
* Configuration
*/
2022-09-29 08:45:59 +02:00
public function index ()
{
2020-11-25 08:21:52 +01:00
2023-04-13 10:50:00 +02:00
// --------------------------------------------------------------------------------------------------
// Langues du site
// --------------------------------------------------------------------------------------------------
2022-09-26 14:54:15 +02:00
2022-09-29 10:55:20 +02:00
foreach ( self :: $languages as $key => $value ) {
2022-09-26 14:54:15 +02:00
// tableau des langues installées
2023-04-18 20:37:15 +02:00
if ( is_dir ( self :: DATA_DIR . $key )) {
2023-06-29 17:01:27 +02:00
if (
file_exists ( self :: DATA_DIR . $key . '/page.json' ) &&
2023-04-18 20:37:15 +02:00
file_exists ( self :: DATA_DIR . $key . '/module.json' ) &&
file_exists ( self :: DATA_DIR . $key . '/locale.json' )
2023-04-18 20:42:51 +02:00
) {
2023-04-22 09:53:04 +02:00
if ( file_exists ( self :: DATA_DIR . $key . '/.default' )) {
2023-06-29 17:01:27 +02:00
$messageLocale = helper :: translate ( 'Langue par défaut' );
} elseif ( isset ( $_SESSION [ 'ZWII_CONTENT' ]) && $_SESSION [ 'ZWII_CONTENT' ] === $key ) {
$messageLocale = helper :: translate ( 'Langue du site sélectionnée' );
} else {
$messageLocale = '' ;
}
self :: $languagesInstalled [] = [
template :: flag ( $key , '20 %' ) . ' ' . $value . ' (' . $key . ')' ,
$messageLocale ,
template :: button ( 'translateContentLanguageLocaleEdit' . $key , [
'class' => file_exists ( self :: DATA_DIR . $key . '/locale.json' ) ? '' : ' disabled' ,
'href' => helper :: baseUrl () . $this -> getUrl ( 0 ) . '/locale/' . $key ,
'value' => template :: ico ( 'pencil' ),
'help' => 'Éditer'
]),
template :: button ( 'translateContentLanguageLocaleDelete' . $key , [
'class' => 'translateDelete buttonRed' . ( $messageLocale ? ' disabled' : '' ),
'href' => helper :: baseUrl () . $this -> getUrl ( 0 ) . '/delete/locale/' . $key ,
'value' => template :: ico ( 'trash' ),
'help' => 'Supprimer' ,
])
];
2022-10-26 15:53:26 +02:00
}
2022-09-26 14:54:15 +02:00
}
2023-04-18 20:37:15 +02:00
2022-09-26 16:30:40 +02:00
}
2022-09-28 15:58:35 +02:00
// Activation du bouton de copie
self :: $siteCopy = count ( self :: $languagesInstalled ) > 1 ? false : true ;
2022-12-18 11:34:06 +01:00
2022-12-27 09:49:29 +01:00
// --------------------------------------------------------------------------------------------------
2023-04-13 10:50:00 +02:00
// Langues de l'UI
// --------------------------------------------------------------------------------------------------
2022-12-18 11:34:06 +01:00
2022-12-27 09:49:29 +01:00
// Langues attachées à des utilisateurs non effaçables
2022-12-26 12:05:01 +01:00
$usersUI = [];
$users = $this -> getData ([ 'user' ]);
foreach ( $users as $key => $value ) {
array_push ( $usersUI , $this -> getData ([ 'user' , $key , 'language' ]));
}
2022-12-27 09:49:29 +01:00
// Langues installées
2023-03-01 15:50:41 +01:00
$installedUI = $this -> getData ([ 'language' ]);
2023-04-13 10:50:00 +02:00
if ( array_key_exists ( 'language' , $installedUI )) {
2023-03-01 15:50:41 +01:00
$installedUI = $installedUI [ 'language' ];
}
2023-04-13 10:50:00 +02:00
2022-12-28 16:20:55 +01:00
// Langues disponibles en ligne
2023-08-06 22:31:44 +02:00
$storeUI = json_decode ( helper :: getUrlContents ( self :: ZWII_UI_URL . 'language.json' ), true );
2023-11-03 10:42:00 +01:00
$storeUI = $storeUI ? $storeUI [ 'language' ] : null ;
2022-12-26 12:05:01 +01:00
2022-12-27 09:49:29 +01:00
// Construction du tableau à partir des langues disponibles dans le store
2022-12-28 16:20:55 +01:00
foreach ( $installedUI as $file => $value ) {
2022-10-14 19:18:48 +02:00
// La langue est-elle référencée ?
2022-12-27 17:59:04 +01:00
if ( array_key_exists ( basename ( $file , '.json' ), $installedUI )) {
2022-12-27 09:49:29 +01:00
// La langue est déjà installée
2023-02-04 17:39:50 +01:00
self :: $languagesUiInstalled [ $file ] = [
2022-12-28 16:20:55 +01:00
template :: flag ( $file , '20 %' ) . ' ' . self :: $languages [ $file ],
$value [ 'version' ],
2023-10-03 17:39:02 +02:00
helper :: dateUTF8 ( '%d/%m/%Y' , $value [ 'date' ], self :: $i18nContent ),
2022-12-28 16:20:55 +01:00
//self::$i18nUI === $file ? helper::translate('Interface') : '',
'' ,
2023-02-07 13:38:53 +01:00
/*
2023-11-03 10:42:00 +01:00
template :: button ( 'translateContentLanguageUIEdit' . $file , [
'href' => helper :: baseUrl () . $this -> getUrl ( 0 ) . '/edit/' . $file ,
'value' => template :: ico ( 'pencil' ),
'help' => 'Éditer' ,
'disabled' => 'fr_FR' === $file
]),
*/
2023-07-19 17:56:19 +02:00
2022-12-27 09:49:29 +01:00
template :: button ( 'translateContentLanguageUIDownload' . $file , [
2023-11-03 10:42:00 +01:00
'class' => isset ( $storeUI [ $file ][ 'version' ]) && version_compare ( $installedUI [ $file ][ 'version' ], $storeUI [ $file ][ 'version' ]) < 0 ? 'buttonGreen' : '' ,
2023-06-19 19:46:00 +02:00
'href' => helper :: baseUrl () . $this -> getUrl ( 0 ) . '/update/' . $file ,
2022-12-27 09:49:29 +01:00
'value' => template :: ico ( 'update' ),
2022-12-27 17:59:04 +01:00
'help' => 'Mettre à jour' ,
2022-12-26 12:05:01 +01:00
]),
2022-12-27 09:49:29 +01:00
template :: button ( 'translateContentLanguageUIDelete' . $file , [
2023-04-13 10:07:18 +02:00
'class' => 'translateDelete buttonRed' . ( in_array ( $file , $usersUI ) ? ' disabled' : '' ),
2023-06-19 19:46:00 +02:00
'href' => helper :: baseUrl () . $this -> getUrl ( 0 ) . '/delete/ui/' . $file ,
2022-12-26 12:05:01 +01:00
'value' => template :: ico ( 'trash' ),
'help' => 'Supprimer' ,
2022-12-27 09:49:29 +01:00
]),
2022-10-14 19:18:48 +02:00
];
2022-12-28 16:20:55 +01:00
}
2023-02-04 17:39:50 +01:00
}
// Construction du tableau à partir des langues disponibles dans le store
2023-11-03 10:42:00 +01:00
if ( $storeUI ) {
foreach ( $storeUI as $file => $value ) {
// La langue est-elle installée ?
if ( array_key_exists ( $file , $installedUI ) === false ) {
self :: $languagesStore [ $file ] = [
template :: flag ( $file , '20 %' ) . ' ' . self :: $languages [ $file ],
$value [ 'version' ],
helper :: dateUTF8 ( '%d/%m/%Y' , $value [ 'date' ], self :: $i18nContent ),
'' ,
template :: button ( 'translateContentLanguageUIDownload' . $file , [
'class' => 'buttonGreen' ,
'href' => helper :: baseUrl () . $this -> getUrl ( 0 ) . '/update/' . $file ,
'value' => template :: ico ( 'shopping-basket' ),
'help' => 'Installer' ,
])
];
}
2022-10-14 19:18:48 +02:00
}
}
2022-09-26 14:54:15 +02:00
2023-02-05 19:10:42 +01:00
2022-09-26 14:54:15 +02:00
// Valeurs en sortie
$this -> addOutput ([
2023-08-07 20:08:48 +02:00
'title' => helper :: translate ( 'Langues' ),
2022-09-26 14:54:15 +02:00
'view' => 'index'
]);
}
2021-06-18 21:10:16 +02:00
2022-09-26 14:54:15 +02:00
/***
2022-09-27 16:42:10 +02:00
* Ajouter une langue de contenu
2022-09-26 14:54:15 +02:00
*/
2022-09-29 08:45:59 +02:00
public function add ()
{
2022-09-26 14:54:15 +02:00
// Soumission du formulaire
2023-06-29 17:01:27 +02:00
if (
2023-06-30 09:09:39 +02:00
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) === true &&
2023-06-29 17:01:27 +02:00
$this -> isPost ()
) {
2022-08-29 22:04:57 +02:00
2022-09-28 14:15:06 +02:00
$lang = $this -> getInput ( 'translateAddContent' );
2023-07-24 13:13:28 +02:00
// Constructeur pour cette langue
$this -> jsonDB ( $lang );
2023-08-06 22:31:44 +02:00
2023-07-24 13:13:28 +02:00
// Création du contenu
2023-04-14 09:00:15 +02:00
$this -> initData ( 'page' , $lang );
$this -> initData ( 'module' , $lang );
$this -> initData ( 'locale' , $lang );
2023-04-13 15:45:02 +02:00
2023-07-24 13:13:28 +02:00
2020-11-11 19:48:07 +01:00
// Valeurs en sortie
$this -> addOutput ([
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2022-10-11 10:33:44 +02:00
'notification' => helper :: translate ( 'Modifications enregistrées' ),
2020-11-11 19:48:07 +01:00
'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
2022-09-28 14:15:06 +02:00
// Tableau des langues non installées
foreach ( self :: $languages as $key => $value ) {
2022-09-29 08:45:59 +02:00
if ( ! is_dir ( self :: DATA_DIR . $key ))
self :: $i18nFiles [ $key ] = $value ;
2021-06-04 13:57:44 +02:00
}
2022-09-26 14:54:15 +02:00
// Valeurs en sortie
$this -> addOutput ([
2022-10-02 10:59:42 +02:00
'title' => helper :: translate ( 'Nouveau contenu localisé' ),
2022-09-26 14:54:15 +02:00
'view' => 'add'
]);
}
2022-12-27 09:49:29 +01:00
2022-10-14 19:18:48 +02:00
/**
* Edition des paramètres de la langue de contenu
*/
public function locale ()
2022-09-29 08:45:59 +02:00
{
2023-06-20 20:35:26 +02:00
// Action interdite ou URl avec le code langue incorrecte
2022-12-30 09:25:09 +01:00
$lang = $this -> getUrl ( 2 );
2022-09-29 08:45:59 +02:00
if (
2022-12-30 09:25:09 +01:00
array_key_exists ( $lang , self :: $languages ) === false
2022-09-27 16:42:10 +02:00
) {
2022-09-27 11:25:05 +02:00
// Valeurs en sortie
$this -> addOutput ([
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2022-09-27 11:25:05 +02:00
'state' => false ,
2022-10-11 10:33:44 +02:00
'notification' => helper :: translate ( 'Action interdite' )
2022-09-27 11:25:05 +02:00
]);
}
2022-09-26 14:54:15 +02:00
// Soumission du formulaire
2023-06-29 17:01:27 +02:00
if (
2023-06-30 09:09:39 +02:00
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) === true &&
2023-06-29 17:01:27 +02:00
$this -> isPost ()
) {
2022-09-26 14:54:15 +02:00
2022-09-28 10:18:58 +02:00
// Sauvegarder les locales
2022-09-29 08:45:59 +02:00
$data = [
'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' ),
2023-04-26 22:54:33 +02:00
'poweredPageLabel' => empty ( $this -> getInput ( 'localePoweredPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Motorisé par' : $this -> getInput ( 'localePoweredPageLabel' , helper :: FILTER_STRING_SHORT ),
2023-02-04 17:39:50 +01:00
'searchPageLabel' => empty ( $this -> getInput ( 'localeSearchPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Rechercher' : $this -> getInput ( 'localeSearchPageLabel' , helper :: FILTER_STRING_SHORT ),
2022-09-29 08:45:59 +02:00
'legalPageLabel' => empty ( $this -> getInput ( 'localeLegalPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Mentions légales' : $this -> getInput ( 'localeLegalPageLabel' , helper :: FILTER_STRING_SHORT ),
2023-02-04 17:39:50 +01:00
'sitemapPageLabel' => empty ( $this -> getInput ( 'localeSitemapPageLabel' , helper :: FILTER_STRING_SHORT )) ? 'Plan du site' : $this -> getInput ( 'localeSitemapPageLabel' , helper :: FILTER_STRING_SHORT ),
2022-09-29 08:45:59 +02:00
'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
2023-02-04 17:39:50 +01:00
'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 )),
2022-09-29 08:45:59 +02:00
'buttonValidLabel' => $this -> getInput ( 'localeCookiesButtonText' , helper :: FILTER_STRING_SHORT , $this -> getInput ( 'configCookieConsent' , helper :: FILTER_BOOLEAN ))
]
2022-09-28 10:18:58 +02:00
]
];
2022-09-28 14:15:06 +02:00
2022-09-28 10:18:58 +02:00
// Sauvegarde hors méthodes si la langue n'est pas celle de l'UI
2023-03-31 07:17:56 +02:00
if ( $lang === self :: $i18nContent ) {
2022-09-27 16:42:10 +02:00
// Enregistrer les données par lecture directe du formulaire
2022-09-29 08:45:59 +02:00
$this -> setData ([ 'locale' , $data [ 'locale' ]]);
2022-09-27 16:42:10 +02:00
} else {
// Sauver sur le disque
2022-12-30 09:25:09 +01:00
file_put_contents ( self :: DATA_DIR . $lang . '/locale.json' , json_encode ( $data , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ), LOCK_EX );
2022-09-27 16:42:10 +02:00
}
2022-09-26 14:54:15 +02:00
// Valeurs en sortie
$this -> addOutput ([
'redirect' => helper :: baseUrl () . $this -> getUrl (),
2022-10-11 10:33:44 +02:00
'notification' => helper :: translate ( 'Modifications enregistrées' ),
2022-09-26 14:54:15 +02:00
'state' => true
]);
}
// Préparation de l'affichage du formulaire
//-----------------------------------------
2022-09-27 16:42:10 +02:00
// La locale est-elle celle de la langue de l'UI ?
2023-03-31 07:17:56 +02:00
if ( $lang === self :: $i18nContent ) {
2023-02-04 17:39:50 +01:00
self :: $locales [ $lang ][ 'locale' ] = $this -> getData ([ 'locale' ]);
2022-09-27 16:42:10 +02:00
} else {
// Lire les locales sans passer par les méthodes
2022-12-30 09:25:09 +01:00
self :: $locales [ $lang ] = json_decode ( file_get_contents ( self :: DATA_DIR . $lang . '/locale.json' ), true );
2022-09-27 16:42:10 +02:00
}
2022-09-27 11:25:05 +02:00
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' ]);
2022-09-29 08:45:59 +02:00
foreach ( self :: $pagesList as $page => $pageId ) {
if (
$this -> getData ([ 'page' , $page , 'block' ]) === 'bar' ||
$this -> getData ([ 'page' , $page , 'disable' ]) === true
) {
2022-08-29 22:04:57 +02:00
unset ( self :: $pagesList [ $page ]);
}
}
2023-02-04 17:39:50 +01:00
self :: $orphansList = $this -> getData ([ 'page' ]);
2022-09-29 08:45:59 +02:00
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
) {
2022-08-29 22:04:57 +02:00
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-12-30 09:25:09 +01:00
'title' => helper :: translate ( 'Paramètres de la localisation' ) . ' ' . template :: flag ( $lang , '20 %' ),
2022-10-14 19:18:48 +02:00
'view' => 'locale'
]);
}
/**
* Edition de la langue de l ' interface
*/
2022-12-28 16:20:55 +01:00
public function edit ()
2022-10-14 19:18:48 +02:00
{
2023-02-04 17:39:50 +01:00
$lang = $this -> getUrl ( 2 );
2023-06-20 20:35:26 +02:00
// Action interdite ou URl avec le code langue incorrecte
2022-10-14 19:18:48 +02:00
if (
2022-12-30 09:25:09 +01:00
array_key_exists ( $lang , self :: $languages ) === false
2022-10-14 19:18:48 +02:00
) {
// Valeurs en sortie
$this -> addOutput ([
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2022-10-14 19:18:48 +02:00
'state' => false ,
'notification' => helper :: translate ( 'Action interdite' )
]);
}
// Soumission du formulaire
2023-06-29 17:01:27 +02:00
if (
2023-06-30 09:09:39 +02:00
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) === true &&
2023-06-29 17:01:27 +02:00
$this -> isPost ()
) {
2022-10-14 19:18:48 +02:00
2022-12-28 16:20:55 +01:00
// Sauvegarder les champs de la langue
$data = json_decode ( file_get_contents ( self :: I18N_DIR . $lang . '.json' ), true );
2022-10-14 19:18:48 +02:00
foreach ( $data as $key => $value ) {
2022-12-28 16:20:55 +01:00
$target = $this -> getInput ( 'translateString' . array_search ( $key , array_keys ( $data )));
if ( empty ( $target ) === false ) {
$data [ $key ] = $target ;
}
2022-10-14 19:18:48 +02:00
}
2022-12-28 16:20:55 +01:00
file_put_contents ( self :: I18N_DIR . $lang . '.json' , json_encode ( $data , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ), LOCK_EX );
2022-10-14 19:18:48 +02:00
2022-12-28 16:20:55 +01:00
// Mettre à jour le descripteur
2023-02-04 17:39:50 +01:00
$this -> setData ([
2023-03-01 15:50:41 +01:00
'language' ,
2023-02-04 17:39:50 +01:00
$lang ,
[
'version' => $this -> getInput ( 'translateEditVersion' ),
'date' => $this -> getInput ( 'translateEditDate' , helper :: FILTER_DATETIME ),
]
]);
2022-10-14 19:18:48 +02:00
// Valeurs en sortie
$this -> addOutput ([
'notification' => helper :: translate ( 'Modifications enregistrées' ),
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2022-12-28 16:20:55 +01:00
'state' => true ,
2022-10-14 19:18:48 +02:00
]);
}
// Construction du formulaire
2022-10-14 19:34:34 +02:00
// Chargement des dialogue de la langue cible
if ( ! isset ( $data )) {
$data = json_decode ( file_get_contents ( self :: I18N_DIR . $this -> getUrl ( 2 ) . '.json' ), true );
}
2022-10-14 19:18:48 +02:00
// Ajout des champs absents selon la langue de référence
$dataFr = json_decode ( file_get_contents ( self :: I18N_DIR . 'fr_FR.json' ), true );
2022-10-14 20:26:37 +02:00
foreach ( $dataFr as $key => $value ) {
2022-10-14 19:18:48 +02:00
if ( ! array_key_exists ( $key , $data )) {
$data [ $key ] = '' ;
}
}
2022-12-28 16:20:55 +01:00
file_put_contents ( self :: I18N_DIR . $lang . '.json' , json_encode ( $data , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ), LOCK_EX );
2022-10-14 19:18:48 +02:00
// Tableau des chaines à traduire dans la langue sélectionnée
foreach ( $data as $key => $value ) {
2022-10-15 09:00:53 +02:00
$dialogues [] = [ 'source' => $key , 'target' => $value ];
}
// Pagination
$pagination = helper :: pagination ( $dialogues , $this -> getUrl (), self :: PAGINATION );
// Liste des pages
self :: $pages = $pagination [ 'pages' ];
// Articles en fonction de la pagination
for ( $i = $pagination [ 'first' ]; $i < $pagination [ 'last' ]; $i ++ ) {
2023-02-04 17:39:50 +01:00
self :: $dialogues [ $i ] = $dialogues [ $i ];
2022-10-14 19:18:48 +02:00
}
// Valeurs en sortie
$this -> addOutput ([
2022-12-28 16:20:55 +01:00
'title' => helper :: translate ( 'Éditer les dialogues' ) . ' ' . template :: flag ( $lang , '20 %' ),
'view' => 'edit' ,
'vendor' => [
'flatpickr' ,
],
2020-11-11 19:48:07 +01:00
]);
2020-11-22 13:32:20 +01:00
}
2022-09-26 14:54:15 +02:00
/***
2022-09-26 16:30:40 +02:00
* Effacer une langue de contenu
2022-09-26 14:54:15 +02:00
*/
2022-09-29 08:45:59 +02:00
public function delete ()
{
if (
2023-06-29 17:01:27 +02:00
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) !== true
2022-09-29 08:45:59 +02:00
) {
// Valeurs en sortie
$this -> addOutput ([
2023-06-29 17:01:27 +02:00
'access' => false
2022-09-29 08:45:59 +02:00
]);
2023-06-29 17:01:27 +02:00
} else {
// Action interdite ou URl avec le code langue incorrecte
$target = $this -> getUrl ( 2 );
$lang = $this -> getUrl ( 3 );
if (
array_key_exists ( $lang , self :: $languages ) === false
) {
2022-12-26 12:05:01 +01:00
// Valeurs en sortie
$this -> addOutput ([
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2023-06-29 17:01:27 +02:00
'state' => false ,
'notification' => helper :: translate ( 'Action interdite' )
2022-12-26 12:05:01 +01:00
]);
2023-06-29 17:01:27 +02:00
}
switch ( $target ) {
case 'locale' :
$success = false ;
// Effacement d'une site dans une langue
if ( is_dir ( self :: DATA_DIR . $lang ) === true ) {
2023-07-19 18:07:12 +02:00
$success = $this -> deleteDir ( self :: DATA_DIR . $lang );
2023-06-29 17:01:27 +02:00
}
// Valeurs en sortie
$this -> addOutput ([
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2023-06-29 17:01:27 +02:00
'notification' => $success ? helper :: translate ( 'Traduction supprimée' ) : helper :: translate ( 'Erreur inconnue' ),
'state' => $success
]);
break ;
case 'ui' :
$success = false ;
// Effacement d'une langue de l'interface
if ( file_exists ( self :: I18N_DIR . $lang . '.json' ) === true ) {
$this -> deleteData ([ 'language' , $lang ]);
$success = unlink ( self :: I18N_DIR . $lang . '.json' );
}
// Valeurs en sortie
$this -> addOutput ([
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2023-06-29 17:01:27 +02:00
'notification' => $success ? helper :: translate ( 'Traduction supprimée' ) : helper :: translate ( 'Erreur inconnue' ),
'state' => $success
]);
break ;
default :
# Do nothing
break ;
}
2022-09-26 14:54:15 +02:00
}
}
2021-06-04 13:57:44 +02:00
2023-04-24 20:46:14 +02:00
/*
* Modifie la langue du site par défaut
*
*/
public function default ()
{
if (
2023-06-29 17:01:27 +02:00
$this -> getUser ( 'permission' , __CLASS__ , __FUNCTION__ ) !== true
2023-04-24 20:46:14 +02:00
) {
// Valeurs en sortie
$this -> addOutput ([
2023-06-29 17:01:27 +02:00
'access' => false
2023-04-24 20:46:14 +02:00
]);
2023-06-29 17:01:27 +02:00
} else {
// Action interdite ou URl avec le code langue incorrecte
$lang = $this -> getUrl ( 2 );
if (
array_key_exists ( $lang , self :: $languages ) === false
) {
// Valeurs en sortie
$this -> addOutput ([
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2023-06-29 17:01:27 +02:00
'state' => false ,
'notification' => helper :: translate ( 'Action interdite' )
]);
}
2023-04-24 20:46:14 +02:00
2023-06-29 17:01:27 +02:00
foreach ( self :: $languages as $key => $value ) {
if ( file_exists ( self :: DATA_DIR . $key . '/.default' )) {
unlink ( self :: DATA_DIR . $key . '/.default' );
touch ( self :: DATA_DIR . $lang . '/.default' );
break ;
}
2023-04-24 20:46:14 +02:00
}
2023-06-29 17:01:27 +02:00
// Valeurs en sortie
$this -> addOutput ([
'notification' => helper :: translate ( 'Modifications enregistrées' ),
2023-07-19 18:03:00 +02:00
'redirect' => helper :: baseUrl () . 'language' ,
2023-06-29 17:01:27 +02:00
'state' => true ,
]);
2023-04-24 20:46:14 +02:00
}
}
2022-12-27 09:49:29 +01:00
2021-06-04 13:57:44 +02:00
/*
* Traitement du changement de langue
* Fonction utilisée par le noyau
*/
2022-09-29 19:08:32 +02:00
public function content ()
2022-09-29 08:45:59 +02:00
{
2023-01-17 12:45:48 +01:00
// Langue sélectionnée
2022-09-29 19:08:32 +02:00
$lang = $this -> getUrl ( 2 );
2023-01-17 12:45:48 +01:00
/**
* Changement de la langue si
* différe de la langue active
* déjà initialisée
2023-04-22 15:38:39 +02:00
* fait partie des langues installées
2023-01-17 12:45:48 +01:00
*/
2023-05-09 15:03:56 +02:00
2023-02-04 17:39:50 +01:00
if (
2023-05-10 09:43:20 +02:00
is_dir ( self :: DATA_DIR . $lang ) &&
2023-02-04 17:39:50 +01:00
array_key_exists ( $lang , self :: $languages ) === true
2023-01-17 12:45:48 +01:00
) {
2023-04-12 18:35:46 +02:00
// Stocker la sélection
2023-04-12 15:29:21 +02:00
$_SESSION [ 'ZWII_CONTENT' ] = $lang ;
2021-06-04 13:57:44 +02:00
}
2022-09-29 19:08:32 +02:00
2021-06-04 13:57:44 +02:00
// Valeurs en sortie
2020-11-24 11:12:33 +01:00
$this -> addOutput ([
2023-04-26 21:40:09 +02:00
'redirect' => helper :: baseUrl ()
2020-11-24 11:12:33 +01:00
]);
2020-11-22 13:32:20 +01:00
}
2022-12-27 09:49:29 +01:00
2023-02-04 17:39:50 +01:00
}