Files
le-matelot/client/plugins/traduction/traduction_admin.php
2020-01-27 08:56:08 +01:00

419 lines
12 KiB
PHP

<?php
include_once(dirname(__FILE__) . "/../../../classes/Lang.class.php");
include_once(dirname(__FILE__) . "/Traduction.class.php");
@include_once(dirname(__FILE__) . "/../../../fonctions/authplugins.php");
if (function_exists('autorisation'))
{
autorisation(Traduction::MODULE);
}
class TraductionAdmin extends Traduction
{
const SIMPLESCAN_VAR = 'traduction_simplescan';
public $reference;
public $traduction;
public $show_all;
public $simplescan;
public function __construct()
{
parent::__construct();
$this->basedir = realpath(dirname(__FILE__)) . "/../../..";
$this->charger_langues('request');
$this->show_all = intval($_REQUEST['manquants']) == 0 ? 1 : 0;
$this->simplescan = intval(Variable::lire(self::SIMPLESCAN_VAR));
}
function normaliser_path($path)
{
$path =
str_replace('\\', '/',
str_replace($this->base_thelia, '',
realpath($path)));
if ($path[0] == '/') $path = substr($path, 1);
return $path;
}
function charger_repertoire($dir)
{
if ($dh = opendir($dir))
{
while (($file = readdir($dh)) !== false)
{
$fp = realpath($dir.'/'.$file);
$short_path = $this->normaliser_path($fp);
if ($file != '.' && $file != '..' && is_dir($fp)) $this->charger_repertoire($fp);
if (! is_file($fp) || (! preg_match('/\.html$/', $fp) && ! preg_match('/\.xml$/', $fp))) continue;
if ($content = file_get_contents($fp))
{
$matches = array();
if (preg_match_all('!'.Traduction::MARQUE_DEBUT.'(.*?)'.Traduction::MARQUE_FIN.'!is', $content, $matches))
{
foreach($matches[1] as $match)
{
$hash = hash('md5', $match);
if (isset($this->reference[$hash]))
{
if (! in_array($short_path, $this->reference[$hash]['files']))
{
$this->reference[$hash]['files'][] = $short_path;
}
}
else
$this->reference[$hash] = array('files' => array($short_path), 'chaine' => $match);
}
}
}
}
@closedir($dh);
}
}
// Charger les chaines à traduire
function charger_reference()
{
$this->reference = array();
if ($this->simplescan) {
$this->charger_repertoire($this->basedir."/template");
$this->charger_repertoire($this->basedir."/client/pdf/template");
}
else {
$this->charger_repertoire($this->basedir);
}
uasort($this->reference, 'TraductionAdmin::tri');
}
static function tri($a, $b)
{
$as = implode(',', $a['files']). strtoupper($a['chaine']);
$bs = implode(',', $b['files']). strtoupper($b['chaine']);
return strcmp($as, $bs);
}
// Consituer le tableau des chaines dans la langue courante
function charger_traduction()
{
$transfile = $this->basedir . self::LANGDIR.'/traduction_'.$this->idlangue.'.php';
if (is_file($transfile))
{
include($transfile);
}
else
{
$traduction = array();
}
// Trouver les nouvelles chaines
foreach($this->reference as $hash => $chaine)
{
if (! isset($traduction[$hash]))
{
$traduction[$hash] = '';
}
}
// Supprimer les chaines obsoletes
foreach($traduction as $hash => $chaine)
{
if (! isset($this->reference[$hash]))
{
unset($traduction[$hash]);
}
}
$this->traduction = $traduction;
}
function ecrire_fichier_traduction()
{
$this->charger_reference();
$this->charger_traduction();
$file = $this->basedir . self::LANGDIR.'/traduction_'.$this->idlangue.'.php';
if ($fp = @fopen($file, 'w'))
{
fwrite($fp, '<' . '?php' . "\n");
fwrite($fp, '$traduction = array('."\n");
foreach($this->traduction as $hash => $chaine)
{
if (isset($_REQUEST[$hash]) && trim($_REQUEST[$hash]) != '')
{
$val = $_REQUEST[$hash];
if (! get_magic_quotes_gpc())
$val = str_replace("'", "\'", $val);
else
$val = str_replace('\"', '"', $val);
}
else if (isset($_REQUEST['hidden_' . $hash]))
{
$val = str_replace("'", "\'", $chaine);
}
else
{
continue;
}
fwrite($fp, "\t'$hash' => '".$val."',\n");
}
fwrite($fp, ");\n");
fwrite($fp, '?' . ">\n");
@fclose($fh);
}
else
{
die("Erreur à l'ouverture du fichier $file pour écriture. Vérifiez les droits d'accès de ce fichier et du répertoire qui le contient.");
}
}
public function maj_options() {
$simplescan = intval(lireParam('simplescan', 'int'));
$opt = new Variable();
if ($opt->charger(self::SIMPLESCAN_VAR)) {
$opt->valeur = $simplescan;
$opt->maj();
}
else {
$opt->nom = self::SIMPLESCAN_VAR;
$opt->valeur = $simplescan;
$opt->cache = 1;
$opt->protege = 1;
$opt->add();
}
}
}
$adm = new TraductionAdmin();
if ($commande == 'maj_trans')
{
$adm->ecrire_fichier_traduction();
}
else if ($commande == 'maj_options')
{
$adm->maj_options();
}
$adm->charger_reference();
$adm->charger_traduction();
?>
<script type="text/javascript" src="../lib/jquery/jquery.js"></script>
<script type="text/javascript">
var changed = false;
$(document).ready(function() {
$('textarea').each(function() {
$(this).height($(this).parent().innerHeight());
}).change(function() { changed = true; });
});
function avertissement()
{
if (changed)
return confirm("Vous avez des traductions non enregistrées.\n\nContinuer tout de même ?");
else
return true;
}
function copier(id)
{
$('textarea[name='+id+']').val($('#texte_' + id).html());
changed = true;
}
</script>
<div id="contenu_int">
<p align="left">
<a href="accueil.php" class="lien04">Accueil </a><img src="gfx/suivant.gif" width="12" height="9" border="0" /><a href="module_liste.php" class="lien04">Liste des modules </a><img src="gfx/suivant.gif" width="12" height="9" border="0" /><a href="./module.php?nom=<?php echo Traduction::MODULE ?>" class="lien04">Traductions v<?php echo Traduction::VERSION ?></a>
</p>
<div id="bloc_description" style="width: 100%;">
<form method="get" id="display_form">
<input type="hidden" name="lang" value="<?php echo $adm->idlangue; ?>" />
<input type="hidden" name="nom" value="<?php echo Traduction::MODULE ?>" />
<input type="hidden" name="commande" value="maj_options" />
<div class="entete" style="height: 25px;">
<div class="titre">PARAMETRES</div>
</div>
<table width="100%" cellpadding="5" cellspacing="0">
<tr class="claire">
<td class="designation" style="vertical-align: middle;">Traduire le site en: </td>
<td>
<?php foreach($adm->langues as $langue) { ?>
<a style="text-decoration: none;" href="module.php?nom=<?php echo Traduction::MODULE ?>&lang=<?php echo $langue->id ?>&manquants=<?php echo $adm->show_all ? '0' : '1' ?>" onclick="return avertissement();"><img style="margin-right: 5px; vertical-align: middle; <?php echo ($adm->idlangue == $langue->id ? 'border: 2px solid green; padding: 2px;' : 'padding: 4px;') ?>"; src="gfx/lang<?php echo $langue->id ?>.gif" alt="<?php echo $langue->description; ?>" title="<?php echo $langue->description; ?>" /><?php echo $langue->description; ?></a>
<?php } ?>
</td>
</tr>
<tr class="foncebottom">
<td class="designation" style="vertical-align: middle;">Paramètres: </td>
<td>
<input type="checkbox" name="manquants" value="1" onclick="if (avertissement()) this.form.submit(); else return false;" <?php echo $adm->show_all == 1 ? '' : 'checked="checked"' ?>/>Masquer les traductions terminées.
<input type="checkbox" name="simplescan" value="1" onclick="if (avertissement()) this.form.submit(); else return false;" <?php echo $adm->simplescan != 0 ? 'checked="checked"' : '' ?>/>Examiner uniquement les répertoires 'template' (plus rapide)
</td>
</tr>
</table>
</form>
<?php if (count($adm->reference) == 0) { ?>
<p style="font-weight: bold;text-align: center; margin: 20px;">Aucun message a traduire n'a été trouvé dans vos fichiers HTML.<br />Vous devez les modifier, et
entourer les messages à traduire avec les balises <?php echo Traduction::MARQUE_DEBUT ?> et <?php echo Traduction::MARQUE_FIN ?>.<br />
Par exemple, "Mon Panier" devient "<?php echo Traduction::MARQUE_DEBUT ?>Mon Panier<?php echo Traduction::MARQUE_FIN ?>".
</p>
<?php } else { ?>
<p style="font-style:italic; text-align:center">Attention, les messages à traduire ne doivent pas contenir de variables Thélia. Le cas échéant, la traduction ne pourra pas fonctionner.</p>
<?php } ?>
<form action="module.php" method="post" id="trans_form">
<input type="hidden" name="commande" value="maj_trans" />
<input type="hidden" name="lang" value="<?php echo $adm->idlangue; ?>" />
<input type="hidden" name="nom" value="<?php echo Traduction::MODULE ?>" />
<input type="hidden" name="manquants" value="<?php echo $adm->show_all ? '0' : '1' ?>" />
<?php
$currfile = '';
$nontraduits = 0;
foreach($adm->reference as $hash => $info)
{
if ($adm->traduction[$hash] == '' || $adm->show_all != 0)
{
$nontraduits++;
if (count($info['files']) > 1)
{
$label = 'des pages:';
$file = '<ul style="margin-left: 35px;">';
foreach($info['files'] as $f) $file .= "<li>- $f</li>";
$file .= '</ul>';
}
else
{
$file = $info['files'][0];
$label = 'de la page';
}
if ($file != $currfile)
{
$currfile = $file;
if ($file != '') echo '</table>';
?>
<div class="entete" style="height: auto; min-height 25px;">
<div class="titre"><img style="margin-right: 5px; vertical-align: middle;" src="gfx/lang<?php echo $adm->idlangue ?>.gif">Traduction <?php echo "$label $currfile"; ?></div>
<div class="fonction_valider"><a href="#" onclick="$('#trans_form').submit(); return false;">ENREGISTRER</a></div>
</div>
<table style="width: 100%;">
<tr class="fonce" style="height: 30px;">
<th colspan="2">Texte original</th>
<th>Traduction</th>
</tr>
<?php
$style = 'fonce';
}
if (strstr($info['chaine'], '#'))
{
$warning = '<br /><span style="color: red; font-weight: bold;">Attention, ce message semble contenir une variable Thélia</span>';
}
else
{
$warning = '';
}
?>
<tr class="<?php echo $style ?>">
<td class="designation" style="padding: 5px">
<span id="texte_<?php echo $hash ?>"><?php echo str_replace('<', '&lt;', str_replace('>', '&gt;', $info['chaine'])); ?></span><?php echo $warning; ?>
</td>
<td class="designation" style="width: 15px;" valign="middle" align="center">
<a title="Copier le texte dans la zone de traduction" href="#" style="color: #ddd; font-weight: normal; font-size: 9px;" onclick="copier('<?php echo $hash ?>'); return false">&gt;&gt;</a>
</td>
<td style="padding: 5px; width: 60%;<?php echo $adm->traduction[$hash] == '' ? "background-color: red;" : '' ?>">
<textarea style="width: 99%;" name="<?php echo $hash ?>"><?php echo $adm->traduction[$hash] ?></textarea>
</td>
</tr>
<?php
$style = $style == 'fonce' ? 'claire' : 'fonce';
}
}
?>
</table>
<?php
if ($adm->show_all == 0)
{
foreach($adm->reference as $hash => $info)
{
if ($adm->traduction[$hash] != '')
{
?>
<input type="hidden" name="hidden_<?php echo $hash; ?>" value="1" />
<?php
}
}
if ($nontraduits == 0)
{
?>
<p style="font-weight: bold; margin: 30px;text-align:center;">Félicitations, vous avez traduit tous les messages.</p>
<?php
}
}
?>
</form>
</div>
<p style="color:#505050;font-size:9px;text-align:center;">Plugin Thelia réalisé par <a target="_blank" href="http://www.cqfdev.fr">Franck Allimant - CQFDev</a>. Contact: <a target="_blank" href="http://www.cqfdev.fr">www.cqfdev.fr</a></p>
</div>