From 787d03e48471ba62cd830379428f04d996f0b74b Mon Sep 17 00:00:00 2001 From: polo Date: Thu, 17 Feb 2022 18:13:00 +0100 Subject: model update --- model/Album.php | 111 ++++++++++++++++++++++++++++++++++++------------ model/Article.php | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ model/Image.php | 2 + model/Page.php | 104 --------------------------------------------- 4 files changed, 211 insertions(+), 131 deletions(-) create mode 100644 model/Article.php delete mode 100644 model/Page.php (limited to 'model') diff --git a/model/Album.php b/model/Album.php index 1631c46..386a50f 100644 --- a/model/Album.php +++ b/model/Album.php @@ -1,14 +1,17 @@ page = $page; - $this->albumCode = ''; // désigne un fichier json et un html + $this->fileCode = ''; // désigne un fichier json et un html $this->format = 'json'; // vaut 'html' dans la classe mère $this->time = time(); + $this->makeFileList(); } // GET @@ -24,17 +27,24 @@ class Album extends Page // classe "objet" if($pochette != '') { + //echo($pochette); die(); // = nom sans chemin + // télécharger la pochette require('model/Image.php'); $Image = new Image(false); $Image->upload(); - /*$erreur = $Image->erreur; + $erreur = $Image->erreur; if(!empty($erreur)) - {}*/ + {} } + // encodage avec un tableau simple $albumJSON = json_encode([$titre, $annee, $pochette]); + //var_dump($albumJSON); die(); + + // encodage avec un tableau associatif + //$albumJSON = json_encode(['titre' => $titre, 'annee' => $annee,'pochette' => $pochette]); $nom_fichier = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format; @@ -43,35 +53,78 @@ class Album extends Page // classe "objet" fclose($fichier); chmod($nom_fichier, 0666); - return($Image->erreur); + //return($Image->erreur); } // read public function readAll() { - // lecture des données - $albumsJSON = parent::readAll(); + // mettre le JSON dans fileList[$i]['content'] + parent::readAll(); - // conversion des chaines JSON en tableaux: titre, année, pochette - for($i = 0; $i < count($albumsJSON); $i++) + $this->format = 'html'; + for($i = 0; $i < $this->fileListCount; $i++) { - $albumsJSON[$i] = json_decode($albumsJSON[$i], true); + // ajout du JSON + /*$content = file_get_contents($this->fileList[$i]['fileName']); + $content = json_decode($content, true);*/ + $content = json_decode($this->fileList[$i]['content'], true); + //var_dump($content); die(); + + // lecture de $content étant un tableau simple + $this->fileList[$i]['titre'] = $content[0]; + $this->fileList[$i]['annee'] = $content[1]; + $this->fileList[$i]['pochette'] = $content[2]; + + // ajout du HTML si il existe + if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileList[$i]['fileCode'] . '.' . $this->format)) + { + $this->fileList[$i]['HTMLfileName'] = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileList[$i]['fileCode'] . '.' . $this->format; + $this->fileList[$i]['HTMLcontent'] = file_get_contents($this->fileList[$i]['HTMLfileName']); + } + // utile? + else + { + $this->fileList[$i]['HTMLfileName'] = ''; + $this->fileList[$i]['HTMLcontent'] = ''; + } } - - return($albumsJSON); + // on remet comme avant + $this->format = 'json'; } - public function getVignetteJSON() + public function readOne() { - return(json_decode(file_get_contents('data/discographie/json/' . $this->albumCode . '.json'))); + for($i = 0; $i < $this->fileListCount; $i++) + { + if($_SESSION['target'] == $this->fileList[$i]['fileCode']) + { + // json + $this->oneAlbum['fileCode'] = $this->fileList[$i]['fileCode']; + $this->oneAlbum['fileName'] = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; + $this->oneAlbum['content'] = file_get_contents($this->fileList[$i]['fileName']); + $content = json_decode($this->oneAlbum['content']); + $this->oneAlbum['titre'] = $content[0]; + $this->oneAlbum['annee'] = $content[1]; + $this->oneAlbum['pochette'] = $content[2]; + + // html + $this->format = 'html'; + if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format)) + { + $this->oneAlbum['HTMLfileName'] = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; + $this->oneAlbum['HTMLcontent'] = file_get_contents($this->oneAlbum['HTMLfileName']); + } + } + } } - // page de l'album - public static function readOneHTML($albumCode) + // page d'un album + public static function readOneHTML($fileCode) { - if(file_exists('data/discographie/html/' . $albumCode . '.html')) + if(file_exists('data/discographie/html/' . $fileCode . '.html')) { - return(file_get_contents('data/discographie/html/' . $albumCode . '.html')); + return(file_get_contents('data/discographie/html/' . $fileCode . '.html')); } else { @@ -79,13 +132,10 @@ class Album extends Page // classe "objet" } } - // pour afficher des dates - /*public function getDate($fileNumber) + public function getVignetteJSON() { - // le 2è paramètre exclut le suffixe .html - $timestamp = basename($this->files[$fileNumber], '.html'); - return getdate($timestamp); - }*/ + return(json_decode(file_get_contents('data/discographie/json/' . $this->fileCode . '.json'))); + } // update public function updateVignetteJSON($titre, $annee, $pochette) @@ -103,6 +153,12 @@ class Album extends Page // classe "objet" require('model/Image.php'); $Image = new Image(false); $Image->upload(); + $erreur = $Image->erreur; + if(!empty($erreur)) + { + // remettre pochette d'avant + //$pochette + } } $albumJSON = json_encode([$titre, $annee, $pochette]); @@ -120,11 +176,12 @@ class Album extends Page // classe "objet" // delete public function delete() { - unlink('data/' . $this->page . '/' . $this->format . '/' . $this->albumCode . '.json'); + parent::delete(); - if(file_exists('data/' . $this->page . '/html/' . $this->albumCode . '.html')); + if(file_exists('data/' . $this->page . '/html/' . $this->fileCode . '.' . $this->format)); { - unlink('data/' . $this->page . '/html/' . $this->albumCode . '.html'); + $this->format = 'html'; + parent::delete(); } } } diff --git a/model/Article.php b/model/Article.php new file mode 100644 index 0000000..e640174 --- /dev/null +++ b/model/Article.php @@ -0,0 +1,125 @@ + [$fileCode => 'code', $fileName => 'nom, $content => 'contenu'], + +// - un tableau associatif contenant des tableaux simples +// les catégories sont au premier niveau +// mauvaise idée, risque de croiser les fichiers +// $fileList['fileCode'] => [$fileCode1, $fileCode2] +// $fileList['fileName'] => [$fileName1, $fileName2], etc + +// - un tableau associatif contenant des tableaux associatifs +// les catégories sont au deuxième niveau +// $fileList[$fileCode] => [$fileName => 'code', $content => 'contenu'], etc + + +class Article +{ + // pour tous les articles + public $page; // page et donc dossier concerné + public $format = 'html'; // vaut 'html' ou 'json' + public $fileListCount; + public $fileList; // = toutes les données + //protected $articles; // contenu de toute la page + //protected $nbArticles; // un fichier = un article + + // pour un article (ou album) spécifique + //public $fileName = ''; // = $_SESSION['nomFichier'] + public $fileCode = ''; // $_SESSION['target'] + protected $time; // timestamp pour noms des fichiers créés + + public function __construct($page) + { + $this->page = $page; + $this->time = time(); + $this->makeFileList(); + } + + // tableaux des noms des fichiers + // noter que le chemin et l'extension ne varient pas + public function makeFileList() + { + // globbing = utiliser un pattern pour cibler des fichiers + $nameList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format); + + $this->fileListCount = count($nameList); + + for($i = 0; $i < $this->fileListCount; $i++) + { + $pathInfo = pathinfo($nameList[$i]); + $fileCode = $pathInfo['filename']; + $this->fileList[$i] = [ + 'fileCode' => $fileCode, + 'fileName' => $nameList[$i], + 'content' => '', + //'date' => getdate() // peut-être utile plus tard + ]; + } + //var_dump($this->fileList); die(); + } + + // GET + + // SET + public function setFileName() + { + if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format)) + { + $this->fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; + } + } + + // fonctions CRUD (create - read - update - delete) + + // create + public function create($content) + { + //$format = 'html'; + + // nommer les fichiers avec le timestamp pour: + // - les trier par ordre chronologique + // - rendre quasi impossible d'avoir deux fois le même nom (à la condition de gérer la "concurrence") + $nom_fichier = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format; + + $fichier = fopen($nom_fichier, 'w'); // w pour créer ou écraser + fputs($fichier, $content); + fclose($fichier); + chmod($nom_fichier, 0666); + } + + // read + public function readAll() // ajoute le contenu à FileList + { + for($i = 0; $i < $this->fileListCount; $i++) + { + $this->fileList[$i]['content'] = file_get_contents($this->fileList[$i]['fileName']); + } + } + public function readOne() + { + //return(file_get_contents($this->fileName)); + return(file_get_contents('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format)); + } + + // update + public function update($content) + { + // 'w' crée ou écrase + $file = fopen('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format, 'w'); + fputs($file, $content); + fclose($file); + //chown($this->fileName, 'http'); + chmod($this->fileName, 0666); + } + + // delete + public function delete() + { + unlink('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format); + } +} diff --git a/model/Image.php b/model/Image.php index 51d2fa4..8ebcd16 100644 --- a/model/Image.php +++ b/model/Image.php @@ -19,6 +19,8 @@ class Image // SET + // miniatures des photos + public function upload() { // traitement et enregistrement de l'image diff --git a/model/Page.php b/model/Page.php deleted file mode 100644 index 63730d8..0000000 --- a/model/Page.php +++ /dev/null @@ -1,104 +0,0 @@ -page = $page; - $this->time = time(); - $this->makeFileList(); - } - - // tableaux des noms des fichiers - public function makeFileList() - { - $this->fileList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format); - //$this->files = glob('*.' . $this->format); - } - /*public function makeFilePath() - {}*/ - - // nom d'un fichier - public function findFileName($numArticle) - { - if($numArticle > 0) - { - $this->fileName = $this->fileList[$numArticle - 1]; - } - } - - 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 - - // fonctions CRUD (create - read - update - delete) - - // create - public function create($content) - { - //$format = 'html'; - - // nommer les fichiers avec le timestamp pour: - // - les trier par ordre chronologique - // - rendre quasi impossible d'avoir deux fois le même nom (à la condition de gérer la "concurrence") - $nom_fichier = 'data/' . $this->page . '/' . $format . '/' . $this->time . '.' . $format; - - $fichier = fopen($nom_fichier, 'w'); // w pour créer ou écraser - fputs($fichier, $content); - fclose($fichier); - chmod($nom_fichier, 0666); - } - - // read - public function readAll() - { - $i = 0; - $articles = []; - foreach ($this->fileList as $oneFile) - { - $articles[$i] = file_get_contents($oneFile); - $i++; - } - //print_r($articles); - return $articles; - } - public function readOne() - { - return(file_get_contents($this->fileName)); - } - - // update - public function update($content) - { - $file = fopen($this->fileName, 'w'); // crée ou écrase - fputs($file, $content); - fclose($file); - //chown($this->fileName, 'http'); - chmod($this->fileName, 0666); - } - - // delete - public function delete() - { - unlink($this->fileName); - } -} \ No newline at end of file -- cgit v1.2.3