Gestion des modules import notification si pages de même nom dans le site et l'archive

This commit is contained in:
SylvainLelievre 2021-02-20 16:36:53 +01:00
parent 20d5c4d221
commit 10ef44aaaf
1 changed files with 80 additions and 43 deletions

View File

@ -120,7 +120,7 @@ class addon extends common {
is_array($infoModules[$key]['dataDirectory']) && implode(', ',array_keys($inPages,$key)) === ''
? template::button('moduleExport' . $key, [
'class' => 'buttonBlue',
'href' => helper::baseUrl(). $this->getUrl(0) . '/import/' . $key,// appel de fonction vaut exécution, utiliser un paramètre
'href' => helper::baseUrl(). $this->getUrl(0) . '/import/' . $key.'/' . $_SESSION['csrf'],// appel de fonction vaut exécution, utiliser un paramètre
'value' => template::ico('upload')
])
: ''
@ -311,10 +311,10 @@ class addon extends common {
file_put_contents($tmpFolder . '/' . $moduleId, json_encode($tmpData));
}
// Export des données localisées dans des dossiers
foreach ($infoModules[$this->getUrl(2)]['dataDirectory'] as $moduleId) {
if ( file_exists(self::DATA_DIR . '/' . $moduleId)
&& !file_exists($tmpFolder . '/' . $moduleId ) ) {
$this->custom_copy ( self::DATA_DIR . '/' . $moduleId, $tmpFolder . '/' . $moduleId );
foreach ($infoModules[$this->getUrl(2)]['dataDirectory'] as $dirId) {
if ( file_exists(self::DATA_DIR . '/' . $dirId)
&& !file_exists($tmpFolder . '/' . $dirId ) ) {
$this->custom_copy ( self::DATA_DIR . '/' . $dirId, $tmpFolder . '/' . $dirId );
}
}
}
@ -353,6 +353,16 @@ class addon extends common {
* Importer des données d'un module externes ou interne à module.json
*/
public function import(){
// Jeton incorrect
if ($this->getUrl(3) !== $_SESSION['csrf']) {
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . 'addon',
'state' => false,
'notification' => 'Action non autorisée'
]);
}
else{
// Soumission du formulaire
if($this->isPost()) {
// Récupérer le fichier et le décompacter
@ -367,6 +377,8 @@ class addon extends common {
// Pour chaque dossier localisé
$dataTarget = array();
$dataSource = array();
// Liste des pages de même nom dans l'archive et le site
$list = '';
foreach (self::$i18nList as $key=>$value) {
// Les Pages et les modules
foreach (['page','module'] as $fileTarget){
@ -374,9 +386,19 @@ class addon extends common {
// Le dossier de langue existe
// faire la fusion
$dataSource = json_decode(file_get_contents(self::TEMP_DIR . $tempFolder . '/' .$key . '/' . $fileTarget . '.json'), true);
// Des pages de même nom que celles de l'archive existent
if( $fileTarget === 'page' ){
foreach( $dataSource as $keydataSource=>$valuedataSource ){
foreach( $this->getData(['page']) as $keypage=>$valuepage ){
if( $keydataSource === $keypage) $list .= ' '.$this->getData(['page', $keypage, 'title']);
}
}
}
$dataTarget = json_decode(file_get_contents(self::DATA_DIR . $key . '/' . $fileTarget . '.json'), true);
$data [$fileTarget] = array_merge($dataTarget[$fileTarget], $dataSource);
if( $list === ''){
file_put_contents(self::DATA_DIR . '/' .$key . '/' . $fileTarget . '.json', json_encode( $data ,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|LOCK_EX) );
}
// Supprimer les fichiers importés
unlink (self::TEMP_DIR . $tempFolder . '/' .$key . '/' . $fileTarget . '.json');
}
@ -389,6 +411,20 @@ class addon extends common {
// Supprimer le dossier temporaire
$this->removeDir(self::TEMP_DIR . $tempFolder);
$zip->close();
if( $list !== '' ){
$success = false;
$notification = 'Import impossible les pages suivantes doivent être renommées :'.$list;
}
else{
$success = true;
$notification = 'Import réussi';
}
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . 'addon',
'state' => $success,
'notification' => $notification
]);
}
// Valeurs en sortie
$this->addOutput([
@ -397,3 +433,4 @@ class addon extends common {
]);
}
}
}