Export WIP

This commit is contained in:
fredtempez 2021-02-17 19:34:08 +01:00
parent d75d78fa31
commit 1eb994368e
2 changed files with 79 additions and 22 deletions

View File

@ -161,19 +161,19 @@ class helper {
if (array_key_exists('UPDATE', $class_constants)) { if (array_key_exists('UPDATE', $class_constants)) {
$update = $value::UPDATE; $update = $value::UPDATE;
} else { } else {
$update = false; $update = true;
} }
// Constante DELETE // Constante DELETE
if (array_key_exists('DELETE', $class_constants)) { if (array_key_exists('DELETE', $class_constants)) {
$delete = $value::DELETE; $delete = $value::DELETE;
} else { } else {
$delete = false; $delete = true;
} }
// Constante DATADIRECTORY // Constante DATADIRECTORY
if (array_key_exists('DATADIRECTORY', $class_constants)) { if (array_key_exists('DATADIRECTORY', $class_constants)) {
$dataDirectory= $value::DATADIRECTORY; $dataDirectory = $value::DATADIRECTORY;
} else { } else {
$dataDirectory = ''; $dataDirectory = ['fr/module.json'];
} }
// Affection // Affection
$modules [$value] = [ $modules [$value] = [

View File

@ -18,7 +18,8 @@ class addon extends common {
public static $actions = [ public static $actions = [
'index' => self::GROUP_ADMIN, 'index' => self::GROUP_ADMIN,
'moduleDelete' => self::GROUP_ADMIN 'moduleDelete' => self::GROUP_ADMIN,
'exportModuleData' => self::GROUP_ADMIN,
]; ];
// Gestion des modules // Gestion des modules
@ -85,17 +86,17 @@ class addon extends common {
$infoModules[$key]['version'], $infoModules[$key]['version'],
implode(', ', array_keys($inPagesTitle,$key)), implode(', ', array_keys($inPagesTitle,$key)),
//array_key_exists('delete',$infoModules[$key]) && $infoModules[$key]['delete'] === true && implode(', ',array_keys($inPages,$key)) === '' //array_key_exists('delete',$infoModules[$key]) && $infoModules[$key]['delete'] === true && implode(', ',array_keys($inPages,$key)) === ''
($infoModules[$key]['delete'] === false || $infoModules[$key]['delete'] !== false ) && implode(', ',array_keys($inPages,$key)) === '' $infoModules[$key]['delete'] === true && implode(', ',array_keys($inPages,$key)) === ''
? template::button('moduleDelete' . $key, [ ? template::button('moduleDelete' . $key, [
'class' => 'moduleDelete buttonRed', 'class' => 'moduleDelete buttonRed',
'href' => helper::baseUrl() . $this->getUrl(0) . '/moduleDelete/' . $key . '/' . $_SESSION['csrf'], 'href' => helper::baseUrl() . $this->getUrl(0) . '/moduleDelete/' . $key . '/' . $_SESSION['csrf'],
'value' => template::ico('cancel') 'value' => template::ico('cancel')
]) ])
: '', : '',
array_key_exists('dataDirectory',$infoModules[$key]) && $infoModules[$key]['dataDirectory'] !== '' is_array($infoModules[$key]['dataDirectory']) && implode(', ',array_keys($inPages,$key)) !== ''
? template::button('moduleExport' . $key, [ ? template::button('moduleExport' . $key, [
'class' => 'buttonBlue', 'class' => 'buttonBlue',
'href' => helper::baseUrl(false).$this->exportZip( $key ), 'href' => helper::baseUrl(). $this->getUrl(0) . '/exportModuleData/' . $key,// appel de fonction vaut exécution, utiliser un paramètre
'value' => template::ico('upload') 'value' => template::ico('upload')
]) ])
: '' : ''
@ -270,23 +271,79 @@ class addon extends common {
} }
/* /*
* Export des données d'un module externes à module.json * Export des données d'un module externes ou interne à module.json
*/ */
private function exportZip( $exportModule ){ public function exportModuleData(){
// Lire les données du module
$infoModules = helper::getModules(); $infoModules = helper::getModules();
// Créer un dossier par défaut
$tmpFolder = self::TEMP_DIR . uniqid();
//$tmpFolder = self::TEMP_DIR . 'test';
if (!is_dir($tmpFolder)) {
mkdir($tmpFolder);
}
// Clés moduleIds dans les pages
$inPages = helper::arrayCollumn($this->getData(['page']),'moduleId', 'SORT_DESC');
// Parcourir les pages utilisant le module
foreach (array_keys($inPages,$this->getUrl(2)) as $pageId) {
foreach ($infoModules[$this->getUrl(2)]['dataDirectory'] as $moduleId) {
/**
* Données module.json ?
*/
if (strpos($moduleId,'module.json')) {
// Création de l'arborescence des langues
// Pas de nom dossier de langue - dossier par défaut
$t = explode ('/',$moduleId);
if ( is_array($t)) {
$path = 'fr';
} else {
$path = $t[0];
}
// Créer le dossier si inexistant
if (!is_dir($tmpFolder . '/' . $path)) {
mkdir ($tmpFolder . '/' . $path);
}
// Sauvegarde si données non vides
$tmpData [$pageId] = $this->getData(['module',$pageId ]);
if ($tmpData [$pageId] !== null) {
file_put_contents($tmpFolder . '/' . $moduleId, json_encode($tmpData));
}
/**
* Données dans un json personnalisé, le sauvegarder
*/
} else {
if (file_exists(self::DATA_DIR . '/' . $moduleId) &&
!file_exists($tmpFolder . '/' . $moduleId ) ) {
copy ( self::DATA_DIR . '/' . $moduleId, $tmpFolder . '/' . $moduleId );
}
}
}
}
// création du zip // création du zip
$zip = new ZipArchive(); $fileName = self::TEMP_DIR . '/' . $this->geturl(2) . 'zip';
if( ! is_dir('tmp/exportDataModules')) mkdir('tmp/exportDataModules',0777, true); $this->createZip($fileName,$tmpFolder);
$filename = 'tmp/exportDataModules/'.$exportModule.'dataExport.zip'; if (file_exists($fileName)) {
if( is_file( $filename )) unlink( $filename); header('Content-Type: application/octet-stream');
$directory = $infoModules[$exportModule]['dataDirectory'].'/'; header('Content-Disposition: attachment; filename="' . $fileName . '"');
if($zip->open( $filename, ZipArchive::CREATE) !== TRUE){ header('Content-Length: ' . filesize($fileName));
exit; readfile( $fileName);
// Valeurs en sortie
$this->addOutput([
'display' => self::DISPLAY_RAW
]);
//unlink($filename);
//$this->removeDir($tmpFolder);
// Valeurs en sortie
} else {
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . 'addon',
'notification' => 'Quelque chose s\'est mal passé',
'state' => false
]);
} }
else{
$this->createZip($zip,$directory);
$zip->close();
}
return( $filename );
} }
} }