Export du coentnu html ok

This commit is contained in:
Fred Tempez 2024-09-03 18:54:41 +02:00
parent 30df948d81
commit b6d3eb8ec2
5 changed files with 287 additions and 10 deletions

View File

@ -40,6 +40,7 @@ class course extends common
'categoryAdd' => self::GROUP_ADMIN, 'categoryAdd' => self::GROUP_ADMIN,
'categoryEdit' => self::GROUP_ADMIN, 'categoryEdit' => self::GROUP_ADMIN,
'categoryDelete' => self::GROUP_ADMIN, 'categoryDelete' => self::GROUP_ADMIN,
'export' => self::GROUP_ADMIN,
]; ];
public static $courseAccess = [ public static $courseAccess = [
@ -303,14 +304,14 @@ class course extends common
self::$courseCategories = $this->getData(['category']); self::$courseCategories = $this->getData(['category']);
// Liste des pages disponibles // Liste des pages disponibles
$this->initDB('page', $this->getUrl(2)); $this->initDB('page', $courseId);
self::$pagesList = $this->getData(['page']); self::$pagesList = $this->getData(['page']);
foreach (self::$pagesList as $page => $pageId) { foreach (self::$pagesList as $pageId => $page) {
if ( if (
$this->getData(['page', $page, 'block']) === 'bar' || $page['block'] === 'bar' ||
$this->getData(['page', $page, 'disable']) === true $page['disable'] === true
) { ) {
unset(self::$pagesList[$page]); unset(self::$pagesList[$pageId]);
} }
} }
@ -352,14 +353,14 @@ class course extends common
self::$courseCategories = $this->getData(['category']); self::$courseCategories = $this->getData(['category']);
// Liste des pages disponibles // Liste des pages disponibles
$this->initDB('page', $this->getUrl(2)); $this->initDB('page', $courseId);
self::$pagesList = $this->getData(['page']); self::$pagesList = $this->getData(['page']);
foreach (self::$pagesList as $page => $pageId) { foreach (self::$pagesList as $pageId => $page) {
if ( if (
$this->getData(['page', $page, 'block']) === 'bar' || $page['block'] === 'bar' ||
$this->getData(['page', $page, 'disable']) === true $page['disable'] === true
) { ) {
unset(self::$pagesList[$page]); unset(self::$pagesList[$pageId]);
} }
} }
@ -1680,6 +1681,127 @@ class course extends common
} }
// Générer un fichier externe contenant le contenu des pages
public function export()
{
// Espace sélectionné
$courseId = $this->getUrl(2);
// Accès limité aux admins, à l'auteur ou éditeurs inscrits
if (
$this->getUser('permission', __CLASS__, __FUNCTION__) === false
) {
// Valeurs en sortie
$this->addOutput([
'access' => false
]);
}
// Liste des pages disponibles
$this->initDB('page', $courseId);
self::$pagesList = [];
foreach ($this->getData(['page']) as $pageId => $page) {
if (
$page['block'] !== 'bar' &&
$page['disable'] !== true
) {
self::$pagesList[] = template::checkbox('courseManageExport' . $pageId, true, $page['title']);
}
}
// Soumission du formulaire
if ($this->isPost()) {
$datas = '';
$resources = [];
foreach ($this->getData(['page']) as $pageId => $page) {
if ($this->getInput('courseManageExport' . $pageId, helper::FILTER_BOOLEAN) === true) {
$pageContent = $this->getPage($pageId, $courseId);
// Extraction des URLs des ressources (images, fichiers, etc.)
preg_match_all('/<img[^>]+src=["\'](.*?)["\']/i', $pageContent, $imgMatches);
preg_match_all('/<a[^>]+href=["\'](.*?)["\']/i', $pageContent, $linkMatches);
if (!empty($imgMatches[1])) {
$resources = array_merge($resources, $imgMatches[1]);
// Remplacement des chemins pour les images dans $pageContent
foreach ($imgMatches[1] as $resourceUrl) {
$resourcePath = parse_url($resourceUrl, PHP_URL_PATH);
$resourceFile = basename($resourcePath);
$pageContent = str_replace($resourceUrl, $resourceFile, $pageContent);
}
}
if (!empty($linkMatches[1])) {
$resources = array_merge($resources, $linkMatches[1]);
// Remplacement des chemins pour les liens dans $pageContent
foreach ($linkMatches[1] as $resourceUrl) {
$resourcePath = parse_url($resourceUrl, PHP_URL_PATH);
$resourceFile = basename($resourcePath);
$pageContent = str_replace($resourceUrl, $resourceFile, $pageContent);
}
}
$datas .= $pageContent;
}
}
// Créer le dossier d'export
$path = self::FILE_DIR . 'source/' . $courseId . '/';
if (is_dir($path . 'export') === false) {
mkdir($path . 'export');
}
// Copier les ressources dans le dossier d'export
foreach ($resources as $resourceUrl) {
$resourcePath = parse_url($resourceUrl, PHP_URL_PATH);
$resourceFile = basename($resourcePath);
if (file_exists($resourcePath)) { // Utilisation du chemin correct
copy($resourcePath, $path . 'export/' . $resourceFile);
}
}
// Ajouter les balises HTML manquantes
$datas = '<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Export de Pages</title>
<link rel="stylesheet" href="style.css">
</head>
<body>' . $datas . '</body></html>';
// Sauvegarder le fichier HTML
file_put_contents($path . '/export/' . $courseId . '_export.html', $datas, LOCK_EX);
// Copie une feuille de style
copy('core/module/course/ressource/style.css', $path . 'export/style.css');
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . 'course/manage/' . $courseId,
'notification' => helper::translate('Pages exportées dans le dossier de cet espace'),
'state' => true,
]);
}
// Valeurs en sortie
$this->addOutput([
'title' => helper::translate('Exporter les pages'),
'view' => 'export'
]);
}
/** /**
* Sauvegarde d'un cours sans option * Sauvegarde d'un cours sans option
*/ */

View File

@ -0,0 +1,98 @@
/* Réinitialisation de base pour supprimer les marges et les espacements par défaut */
body, h1, h2, h3, h4, h5, h6, p, ul, ol, li {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
line-height: 1.6;
}
body {
margin: 20px;
padding: 0;
background-color: #f5f5f5;
color: #333;
}
h1, h2, h3, h4, h5, h6 {
margin-bottom: 10px;
color: #2c3e50;
}
p {
margin-bottom: 10px;
}
ul, ol {
margin-bottom: 10px;
padding-left: 20px;
}
a {
color: #3498db;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
img {
max-width: 100%;
height: auto;
display: block;
margin: 0 auto 10px;
}
.container {
max-width: 800px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.header, .footer {
background-color: #34495e;
color: #fff;
text-align: center;
padding: 10px 0;
}
.header h1, .footer p {
margin: 0;
}
.content {
padding: 20px;
}
code {
background-color: #f4f4f4;
border: 1px solid #e1e1e1;
padding: 2px 4px;
font-family: "Courier New", Courier, monospace;
color: #c7254e;
display: block;
margin-bottom: 10px;
white-space: pre-wrap;
}
pre {
background-color: #f4f4f4;
border: 1px solid #e1e1e1;
padding: 10px;
font-family: "Courier New", Courier, monospace;
overflow-x: auto;
color: #333;
}
/* Media Queries pour rendre la page responsive */
@media (max-width: 600px) {
body {
margin: 10px;
}
.container {
padding: 10px;
}
}

View File

@ -0,0 +1,18 @@
/**
* 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
* @author Frédéric Tempez <frederic.tempez@outlook.com>
* @copyright Copyright (C) 2018-2024, Frédéric Tempez
* @license CC Attribution-NonCommercial-NoDerivatives 4.0 International
* @link http://zwiicms.fr/
*/
/** NE PAS EFFACER
* admin.css
*/

View File

@ -0,0 +1,30 @@
<?php echo template::formOpen('courseExportForm'); ?>
<div class="row">
<div class="col1">
<?php echo template::button('courseExportBack', [
'class' => 'buttonGrey',
'href' => helper::baseUrl() . 'course/manage/' . $this->getUrl(2),
'value' => template::ico('left')
]); ?>
</div>
<div class="col2 offset9">
<?php echo template::submit('courseExportSubmit', [
'value' => 'Valider'
]); ?>
</div>
</div>
<div class="row">
<div class="col12">
<div class="block">
<h4><?php echo helper::translate('Sélection des pages de l\'espace') ?></h4>
<div class='row'>
<div class='col10 offset2'>
<?php foreach ($module::$pagesList as $key => $value) {
echo $value;
}
?>
</div>
</div>
</div>
</div>
</div>

View File

@ -55,6 +55,15 @@
]); ?> ]); ?>
</div> </div>
<?php endif; ?> <?php endif; ?>
<?php if ($this->getUser('permission', 'course', 'export') === true): ?>
<div class="col2">
<?php echo template::button('courseManageExport' . $this->getUrl(2), [
'href' => helper::baseUrl() . 'course/export/' . $this->getUrl(2),
'value' => 'Exporter',
'ico' => 'upload'
]); ?>
</div>
<?php endif; ?>
</div> </div>
<div class="row"> <div class="row">