From 610291f311aeab7363d1dbc39e62cbce8533c73f Mon Sep 17 00:00:00 2001 From: polo Date: Mon, 10 May 2021 03:31:18 +0200 Subject: classe --- model/Article.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ model/melaine-read.php | 49 ++++++++++++++++++++--------- model/melaine-write.php | 17 +++++++--- 3 files changed, 131 insertions(+), 19 deletions(-) create mode 100644 model/Article.php (limited to 'model') diff --git a/model/Article.php b/model/Article.php new file mode 100644 index 0000000..cee594f --- /dev/null +++ b/model/Article.php @@ -0,0 +1,84 @@ +page == '') + { + die("debug: la méthode setPage() doit être appelée avant toute autre."); + } + + $this->files = glob('data/' . $this->page . '/html/*.html'); + + if($croissant == False) + { + $files = array_reverse($files); + } + + $this->nbArticles = count($this->files); + } + + public function getNb() + { + return $this->nbArticles; + } + + public function getAll($croissant) + { + $this->croissant = $croissant; + + getFiles(); + + $i = 0; + foreach ($this->files as $file) + { + $articles[$i] = file_get_contents($file); + $i++; + } + + return $articles; + } + + public function getOne() + {} + + public function getDate($fileNumber) + { + // le 2è paramètre exclut le suffixe .html + $timestamp = basename($this->files[$fileNumber], '.html'); + return getdate($timestamp); + } + + public function new($content) + { + if($this->page == '') + { + die("debug: la méthode setPage() doit être appelée avant toute autre."); + } + + $nom_fichier = time() . '.html'; + $fichier = fopen('data/' . $this->page . '/html/' . $nom_fichier, 'w'); // w peut créer un fichier, si il existe déjà, il est effacé par le nouveau contenu + fputs($fichier, $content); + fclose($fichier); + chmod('data/' . $this->page . '/html/' . $nom_fichier, 0666); + } + + public function update() + {} + + public function remove() + {} + + public function setPage($page) + { + $this->page = $page; + } +} \ No newline at end of file diff --git a/model/melaine-read.php b/model/melaine-read.php index 7e51b2c..801cd3c 100644 --- a/model/melaine-read.php +++ b/model/melaine-read.php @@ -3,21 +3,42 @@ // // accès en lecture seule -// créer un tableau avec le nom des fichiers html -$files = glob('data/' . $page_actuelle . '/html/*.html'); -// nombre de fichiers (= taille du tableau $files) -$nombreDarticles = count($files); - -// tableau contenant tous les articles au format html -$articles = [$nombreDarticles]; -for ($i = 0 ; $i < $nombreDarticles ; $i++) -{ - // les noms de fichiers commencent par 1 - // les articles dans le tableau[] commencent par 0 - $numero = $i + 1; - $articles[$i] = file_get_contents('data/' . $page_actuelle . '/html/article' . $numero . '.html'); -} +function lireArticles($page_actuelle, $croissant) +{ + // créer un tableau avec le nom des fichiers html + $files = glob('data/' . $page_actuelle . '/html/*.html'); + + // nombre de fichiers (= taille du tableau $files) + //$nombreDarticles = count($files); + + // tableau contenant tous les articles au format html + //$articles = [$nombreDarticles]; + + // inverse l'ordre du tableau retourné + if($croissant == False) + { + $files = array_reverse($files); + } + // for ($i = 0 ; $i < $nombreDarticles ; $i++) + // { + // // les noms de fichiers commencent par 1 + // // les articles dans le tableau[] commencent par 0 + // $numero = $i + 1; + // $articles[$i] = file_get_contents('data/' . $page_actuelle . '/html/article' . $numero . '.html'); + // } + $i = 0; + foreach ($files as $files) + { + $articles[$i] = file_get_contents($files); + $i++; + } + + //print_r($articles); + //var_dump($articles); + + return($articles); +} // note: les pros font de l'hydration // le code du modèle est orienté objet et "refactorisé" d'une manière précise: diff --git a/model/melaine-write.php b/model/melaine-write.php index 4f5b441..642bf05 100644 --- a/model/melaine-write.php +++ b/model/melaine-write.php @@ -3,15 +3,22 @@ // // accès en écriture pour créer ou modifier des articles -function nouvelArticle($page, $article, $contenuHTML) +function nouvelArticle($page, $contenuHTML) { - // debuggage - //exit(); - // manipulation du fichier - $nom_fichier = 'article' . $article . '.html'; + //$nom_fichier = 'article' . $article . '.html'; + $nom_fichier = time() . '.html'; $fichier = fopen('data/' . $page . '/html/' . $nom_fichier, 'w'); // w peut créer un fichier, si il existe déjà, il est effacé par le nouveau contenu fputs($fichier, $contenuHTML); fclose($fichier); chmod('data/' . $page . '/html/' . $nom_fichier, 0666); } + +function modifArticle($page, $nom_fichier, $contenuHTML) +{ + // manipulation du fichier + $fichier = fopen('data/' . $page . '/html/' . $nom_fichier, 'w'); // w peut créer un fichier, si il existe déjà, il est effacé par le nouveau contenu + fputs($fichier, $contenuHTML); + fclose($fichier); + chmod('data/' . $page . '/html/' . $nom_fichier, 0666); +} \ No newline at end of file -- cgit v1.2.3