From 29df6f1362745eabf4fbcaedf309eb63795152fa Mon Sep 17 00:00:00 2001 From: polo Date: Mon, 29 Nov 2021 03:25:19 +0100 Subject: discographie --- model/Album.php | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++------- model/Image.php | 16 +++++++------ model/Page.php | 15 ++++++++---- 3 files changed, 84 insertions(+), 20 deletions(-) (limited to 'model') diff --git a/model/Album.php b/model/Album.php index 2254c10..1631c46 100644 --- a/model/Album.php +++ b/model/Album.php @@ -6,6 +6,7 @@ class Album extends Page // classe "objet" public function __construct($page) { $this->page = $page; + $this->albumCode = ''; // désigne un fichier json et un html $this->format = 'json'; // vaut 'html' dans la classe mère $this->time = time(); } @@ -19,7 +20,7 @@ class Album extends Page // classe "objet" // create public function createVignette($titre, $annee, $pochette) { - $this->format = 'json'; + //$this->format = 'json'; if($pochette != '') { @@ -28,7 +29,7 @@ class Album extends Page // classe "objet" $Image = new Image(false); $Image->upload(); - /*$erreur = $Image->getError(); + /*$erreur = $Image->erreur; if(!empty($erreur)) {}*/ } @@ -41,18 +42,41 @@ class Album extends Page // classe "objet" fputs($fichier, $albumJSON); fclose($fichier); chmod($nom_fichier, 0666); + + return($Image->erreur); } // read - public function getVignette() + public function readAll() + { + // lecture des données + $albumsJSON = parent::readAll(); + + // conversion des chaines JSON en tableaux: titre, année, pochette + for($i = 0; $i < count($albumsJSON); $i++) + { + $albumsJSON[$i] = json_decode($albumsJSON[$i], true); + } + + return($albumsJSON); + } + + public function getVignetteJSON() { - return(file_get_contents($this->fileName)); + return(json_decode(file_get_contents('data/discographie/json/' . $this->albumCode . '.json'))); } // page de l'album - public static function readOneAlbum($albumCode) + public static function readOneHTML($albumCode) { - return(file_get_contents('data/discographie/html/' . $albumCode . '.html')); + if(file_exists('data/discographie/html/' . $albumCode . '.html')) + { + return(file_get_contents('data/discographie/html/' . $albumCode . '.html')); + } + else + { + return(''); + } } // pour afficher des dates @@ -64,12 +88,43 @@ class Album extends Page // classe "objet" }*/ // update - public function updateVignette() - {} + public function updateVignetteJSON($titre, $annee, $pochette) + { + // garder l'ancienne image + $albumJSON = self::getVignetteJSON(); + + if($pochette == '') + { + $pochette = $albumJSON[2]; + } + else + { + // télécharger la pochette + require('model/Image.php'); + $Image = new Image(false); + $Image->upload(); + } + + $albumJSON = json_encode([$titre, $annee, $pochette]); + + // écriture + $nom_fichier = 'data/discographie/json/' . $this->albumCode . '.json'; + $fichier = fopen($nom_fichier, 'w+'); // w pour créer ou écraser + fputs($fichier, $albumJSON); + fclose($fichier); + chmod($nom_fichier, 0666); + + return($Image->erreur); + } // delete public function delete() { - unlink($this->fileName); + unlink('data/' . $this->page . '/' . $this->format . '/' . $this->albumCode . '.json'); + + if(file_exists('data/' . $this->page . '/html/' . $this->albumCode . '.html')); + { + unlink('data/' . $this->page . '/html/' . $this->albumCode . '.html'); + } } } diff --git a/model/Image.php b/model/Image.php index efd75cc..51d2fa4 100644 --- a/model/Image.php +++ b/model/Image.php @@ -3,10 +3,10 @@ class Image { - private const MAX_WEIGHT = 2000000; // taille max des images (par défaut 2Mo dans php.ini) + private const MAX_WEIGHT = 2000000; // à adapter au php.ini private $page; - private $ajax = false; - public $erreur = ''; + private $ajax = false; // vaut true avec le ckeditor + public $erreur; public function __construct($ajax) { @@ -37,19 +37,21 @@ class Image } else{$this->erreur = 'mauvais format, veuillez utiliser une image comportant un de ces formats: jpg ou jpeg, png, gif, bmp, webp, tiff
le format svg n\'est pas supporté';} } - else{$this->erreur = 'fichier trop lourd';} + else + { + $this->erreur = 'erreur du serveur: le fichier téléchargé est trop lourd, poids max = ' . self::MAX_WEIGHT . ' octets'; + } } else { - $this->erreur = $_FILES['upload']['error']; + $this->erreur = 'erreur du serveur: le fichier téléchargé est trop lourd, poids max = ' . self::MAX_WEIGHT . ' octets'; } // retour des rêquetes AJAX - if($this->ajax) + if($this->ajax && empty($Image->erreur)) { // nouveau chemin à renvoyer en format json $chemin = '{"url": "data/' . $this->page . '/images/' . $_FILES['upload']['name'] . '"}'; - //echo json_encode($chemin); echo $chemin; } } diff --git a/model/Page.php b/model/Page.php index e03efe1..63730d8 100644 --- a/model/Page.php +++ b/model/Page.php @@ -13,10 +13,9 @@ class Page // classe "objet" public $fileName = ''; // correspond à $_SESSION['nomFichier'] protected $time; // timestamp pour noms des fichiers créés - public function __construct($page, $format) + public function __construct($page) { $this->page = $page; - $this->format = $format; $this->time = time(); $this->makeFileList(); } @@ -39,6 +38,14 @@ class Page // classe "objet" } } + public function setFileName() + { + if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->albumCode . '.' . $this->format)) + { + $this->fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->albumCode . '.' . $this->format; + } + } + // GET // SET @@ -48,7 +55,7 @@ class Page // classe "objet" // create public function create($content) { - $format = 'html'; + //$format = 'html'; // nommer les fichiers avec le timestamp pour: // - les trier par ordre chronologique @@ -65,7 +72,7 @@ class Page // classe "objet" public function readAll() { $i = 0; - $articles = array(); + $articles = []; foreach ($this->fileList as $oneFile) { $articles[$i] = file_get_contents($oneFile); -- cgit v1.2.3