diff options
Diffstat (limited to 'model')
| -rw-r--r-- | model/Article.php | 84 | ||||
| -rw-r--r-- | model/melaine-read.php | 49 | ||||
| -rw-r--r-- | model/melaine-write.php | 17 |
3 files changed, 131 insertions, 19 deletions
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 @@ | |||
| 1 | <?php | ||
| 2 | // model/Article.php | ||
| 3 | |||
| 4 | class Article | ||
| 5 | { | ||
| 6 | private $page = ''; // page et donc dossier concerné | ||
| 7 | private $croissant = True; // ordre des éléments du tableau $files | ||
| 8 | private $files; // noms des fichiers d'un dossier | ||
| 9 | private $nbArticles; // peut servir | ||
| 10 | private $articles; // contenu de ces mêmes fichiers | ||
| 11 | |||
| 12 | private function getFiles() | ||
| 13 | { | ||
| 14 | if($this->page == '') | ||
| 15 | { | ||
| 16 | die("debug: la méthode setPage() doit être appelée avant toute autre."); | ||
| 17 | } | ||
| 18 | |||
| 19 | $this->files = glob('data/' . $this->page . '/html/*.html'); | ||
| 20 | |||
| 21 | if($croissant == False) | ||
| 22 | { | ||
| 23 | $files = array_reverse($files); | ||
| 24 | } | ||
| 25 | |||
| 26 | $this->nbArticles = count($this->files); | ||
| 27 | } | ||
| 28 | |||
| 29 | public function getNb() | ||
| 30 | { | ||
| 31 | return $this->nbArticles; | ||
| 32 | } | ||
| 33 | |||
| 34 | public function getAll($croissant) | ||
| 35 | { | ||
| 36 | $this->croissant = $croissant; | ||
| 37 | |||
| 38 | getFiles(); | ||
| 39 | |||
| 40 | $i = 0; | ||
| 41 | foreach ($this->files as $file) | ||
| 42 | { | ||
| 43 | $articles[$i] = file_get_contents($file); | ||
| 44 | $i++; | ||
| 45 | } | ||
| 46 | |||
| 47 | return $articles; | ||
| 48 | } | ||
| 49 | |||
| 50 | public function getOne() | ||
| 51 | {} | ||
| 52 | |||
| 53 | public function getDate($fileNumber) | ||
| 54 | { | ||
| 55 | // le 2è paramètre exclut le suffixe .html | ||
| 56 | $timestamp = basename($this->files[$fileNumber], '.html'); | ||
| 57 | return getdate($timestamp); | ||
| 58 | } | ||
| 59 | |||
| 60 | public function new($content) | ||
| 61 | { | ||
| 62 | if($this->page == '') | ||
| 63 | { | ||
| 64 | die("debug: la méthode setPage() doit être appelée avant toute autre."); | ||
| 65 | } | ||
| 66 | |||
| 67 | $nom_fichier = time() . '.html'; | ||
| 68 | $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 | ||
| 69 | fputs($fichier, $content); | ||
| 70 | fclose($fichier); | ||
| 71 | chmod('data/' . $this->page . '/html/' . $nom_fichier, 0666); | ||
| 72 | } | ||
| 73 | |||
| 74 | public function update() | ||
| 75 | {} | ||
| 76 | |||
| 77 | public function remove() | ||
| 78 | {} | ||
| 79 | |||
| 80 | public function setPage($page) | ||
| 81 | { | ||
| 82 | $this->page = $page; | ||
| 83 | } | ||
| 84 | } \ 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 @@ | |||
| 3 | // | 3 | // |
| 4 | // accès en lecture seule | 4 | // accès en lecture seule |
| 5 | 5 | ||
| 6 | // créer un tableau avec le nom des fichiers html | 6 | function lireArticles($page_actuelle, $croissant) |
| 7 | $files = glob('data/' . $page_actuelle . '/html/*.html'); | 7 | { |
| 8 | // nombre de fichiers (= taille du tableau $files) | 8 | // créer un tableau avec le nom des fichiers html |
| 9 | $nombreDarticles = count($files); | 9 | $files = glob('data/' . $page_actuelle . '/html/*.html'); |
| 10 | 10 | ||
| 11 | // tableau contenant tous les articles au format html | 11 | // nombre de fichiers (= taille du tableau $files) |
| 12 | $articles = [$nombreDarticles]; | 12 | //$nombreDarticles = count($files); |
| 13 | for ($i = 0 ; $i < $nombreDarticles ; $i++) | 13 | |
| 14 | { | 14 | // tableau contenant tous les articles au format html |
| 15 | // les noms de fichiers commencent par 1 | 15 | //$articles = [$nombreDarticles]; |
| 16 | // les articles dans le tableau[] commencent par 0 | 16 | |
| 17 | $numero = $i + 1; | 17 | // inverse l'ordre du tableau retourné |
| 18 | $articles[$i] = file_get_contents('data/' . $page_actuelle . '/html/article' . $numero . '.html'); | 18 | if($croissant == False) |
| 19 | } | 19 | { |
| 20 | $files = array_reverse($files); | ||
| 21 | } | ||
| 20 | 22 | ||
| 23 | // for ($i = 0 ; $i < $nombreDarticles ; $i++) | ||
| 24 | // { | ||
| 25 | // // les noms de fichiers commencent par 1 | ||
| 26 | // // les articles dans le tableau[] commencent par 0 | ||
| 27 | // $numero = $i + 1; | ||
| 28 | // $articles[$i] = file_get_contents('data/' . $page_actuelle . '/html/article' . $numero . '.html'); | ||
| 29 | // } | ||
| 30 | $i = 0; | ||
| 31 | foreach ($files as $files) | ||
| 32 | { | ||
| 33 | $articles[$i] = file_get_contents($files); | ||
| 34 | $i++; | ||
| 35 | } | ||
| 36 | |||
| 37 | //print_r($articles); | ||
| 38 | //var_dump($articles); | ||
| 39 | |||
| 40 | return($articles); | ||
| 41 | } | ||
| 21 | 42 | ||
| 22 | // note: les pros font de l'hydration | 43 | // note: les pros font de l'hydration |
| 23 | // le code du modèle est orienté objet et "refactorisé" d'une manière précise: | 44 | // 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 @@ | |||
| 3 | // | 3 | // |
| 4 | // accès en écriture pour créer ou modifier des articles | 4 | // accès en écriture pour créer ou modifier des articles |
| 5 | 5 | ||
| 6 | function nouvelArticle($page, $article, $contenuHTML) | 6 | function nouvelArticle($page, $contenuHTML) |
| 7 | { | 7 | { |
| 8 | // debuggage | ||
| 9 | //exit(); | ||
| 10 | |||
| 11 | // manipulation du fichier | 8 | // manipulation du fichier |
| 12 | $nom_fichier = 'article' . $article . '.html'; | 9 | //$nom_fichier = 'article' . $article . '.html'; |
| 10 | $nom_fichier = time() . '.html'; | ||
| 13 | $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 | 11 | $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 |
| 14 | fputs($fichier, $contenuHTML); | 12 | fputs($fichier, $contenuHTML); |
| 15 | fclose($fichier); | 13 | fclose($fichier); |
| 16 | chmod('data/' . $page . '/html/' . $nom_fichier, 0666); | 14 | chmod('data/' . $page . '/html/' . $nom_fichier, 0666); |
| 17 | } | 15 | } |
| 16 | |||
| 17 | function modifArticle($page, $nom_fichier, $contenuHTML) | ||
| 18 | { | ||
| 19 | // manipulation du fichier | ||
| 20 | $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 | ||
| 21 | fputs($fichier, $contenuHTML); | ||
| 22 | fclose($fichier); | ||
| 23 | chmod('data/' . $page . '/html/' . $nom_fichier, 0666); | ||
| 24 | } \ No newline at end of file | ||
