summaryrefslogtreecommitdiff
path: root/model/Page.php
diff options
context:
space:
mode:
Diffstat (limited to 'model/Page.php')
-rw-r--r--model/Page.php97
1 files changed, 97 insertions, 0 deletions
diff --git a/model/Page.php b/model/Page.php
new file mode 100644
index 0000000..e03efe1
--- /dev/null
+++ b/model/Page.php
@@ -0,0 +1,97 @@
1<?php
2// model/Page.php
3
4class Page // classe "objet"
5{
6 public $page; // page et donc dossier concerné
7 public $format = 'html'; // vaut 'html' ou 'json'
8 public $fileList; // noms des fichiers dans ce dossier
9 protected $articles; // contenu de toute la page
10 protected $nbArticles; // un fichier = un article
11
12 // gestion d'un article spécifique
13 public $fileName = ''; // correspond à $_SESSION['nomFichier']
14 protected $time; // timestamp pour noms des fichiers créés
15
16 public function __construct($page, $format)
17 {
18 $this->page = $page;
19 $this->format = $format;
20 $this->time = time();
21 $this->makeFileList();
22 }
23
24 // tableaux des noms des fichiers
25 public function makeFileList()
26 {
27 $this->fileList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format);
28 //$this->files = glob('*.' . $this->format);
29 }
30 /*public function makeFilePath()
31 {}*/
32
33 // nom d'un fichier
34 public function findFileName($numArticle)
35 {
36 if($numArticle > 0)
37 {
38 $this->fileName = $this->fileList[$numArticle - 1];
39 }
40 }
41
42 // GET
43
44 // SET
45
46 // fonctions CRUD (create - read - update - delete)
47
48 // create
49 public function create($content)
50 {
51 $format = 'html';
52
53 // nommer les fichiers avec le timestamp pour:
54 // - les trier par ordre chronologique
55 // - rendre quasi impossible d'avoir deux fois le même nom (à la condition de gérer la "concurrence")
56 $nom_fichier = 'data/' . $this->page . '/' . $format . '/' . $this->time . '.' . $format;
57
58 $fichier = fopen($nom_fichier, 'w'); // w pour créer ou écraser
59 fputs($fichier, $content);
60 fclose($fichier);
61 chmod($nom_fichier, 0666);
62 }
63
64 // read
65 public function readAll()
66 {
67 $i = 0;
68 $articles = array();
69 foreach ($this->fileList as $oneFile)
70 {
71 $articles[$i] = file_get_contents($oneFile);
72 $i++;
73 }
74 //print_r($articles);
75 return $articles;
76 }
77 public function readOne()
78 {
79 return(file_get_contents($this->fileName));
80 }
81
82 // update
83 public function update($content)
84 {
85 $file = fopen($this->fileName, 'w'); // crée ou écrase
86 fputs($file, $content);
87 fclose($file);
88 //chown($this->fileName, 'http');
89 chmod($this->fileName, 0666);
90 }
91
92 // delete
93 public function delete()
94 {
95 unlink($this->fileName);
96 }
97} \ No newline at end of file