summaryrefslogtreecommitdiff
path: root/model/Article.php
diff options
context:
space:
mode:
Diffstat (limited to 'model/Article.php')
-rw-r--r--model/Article.php125
1 files changed, 125 insertions, 0 deletions
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 @@
1<?php
2// model/Article.php
3
4// structure de données de la variable $fileList
5// on a 3 possibilités (on prendra la 3ème):
6
7// - un tableau simple contenant des tableaux associatifs (avec des index façon BDD)
8// $fileList[0] => [$fileCode => 'code', $fileName => 'nom, $content => 'contenu'],
9
10// - un tableau associatif contenant des tableaux simples
11// les catégories sont au premier niveau
12// mauvaise idée, risque de croiser les fichiers
13// $fileList['fileCode'] => [$fileCode1, $fileCode2]
14// $fileList['fileName'] => [$fileName1, $fileName2], etc
15
16// - un tableau associatif contenant des tableaux associatifs
17// les catégories sont au deuxième niveau
18// $fileList[$fileCode] => [$fileName => 'code', $content => 'contenu'], etc
19
20
21class Article
22{
23 // pour tous les articles
24 public $page; // page et donc dossier concerné
25 public $format = 'html'; // vaut 'html' ou 'json'
26 public $fileListCount;
27 public $fileList; // = toutes les données
28 //protected $articles; // contenu de toute la page
29 //protected $nbArticles; // un fichier = un article
30
31 // pour un article (ou album) spécifique
32 //public $fileName = ''; // = $_SESSION['nomFichier']
33 public $fileCode = ''; // $_SESSION['target']
34 protected $time; // timestamp pour noms des fichiers créés
35
36 public function __construct($page)
37 {
38 $this->page = $page;
39 $this->time = time();
40 $this->makeFileList();
41 }
42
43 // tableaux des noms des fichiers
44 // noter que le chemin et l'extension ne varient pas
45 public function makeFileList()
46 {
47 // globbing = utiliser un pattern pour cibler des fichiers
48 $nameList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format);
49
50 $this->fileListCount = count($nameList);
51
52 for($i = 0; $i < $this->fileListCount; $i++)
53 {
54 $pathInfo = pathinfo($nameList[$i]);
55 $fileCode = $pathInfo['filename'];
56 $this->fileList[$i] = [
57 'fileCode' => $fileCode,
58 'fileName' => $nameList[$i],
59 'content' => '',
60 //'date' => getdate() // peut-être utile plus tard
61 ];
62 }
63 //var_dump($this->fileList); die();
64 }
65
66 // GET
67
68 // SET
69 public function setFileName()
70 {
71 if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format))
72 {
73 $this->fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
74 }
75 }
76
77 // fonctions CRUD (create - read - update - delete)
78
79 // create
80 public function create($content)
81 {
82 //$format = 'html';
83
84 // nommer les fichiers avec le timestamp pour:
85 // - les trier par ordre chronologique
86 // - rendre quasi impossible d'avoir deux fois le même nom (à la condition de gérer la "concurrence")
87 $nom_fichier = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format;
88
89 $fichier = fopen($nom_fichier, 'w'); // w pour créer ou écraser
90 fputs($fichier, $content);
91 fclose($fichier);
92 chmod($nom_fichier, 0666);
93 }
94
95 // read
96 public function readAll() // ajoute le contenu à FileList
97 {
98 for($i = 0; $i < $this->fileListCount; $i++)
99 {
100 $this->fileList[$i]['content'] = file_get_contents($this->fileList[$i]['fileName']);
101 }
102 }
103 public function readOne()
104 {
105 //return(file_get_contents($this->fileName));
106 return(file_get_contents('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format));
107 }
108
109 // update
110 public function update($content)
111 {
112 // 'w' crée ou écrase
113 $file = fopen('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format, 'w');
114 fputs($file, $content);
115 fclose($file);
116 //chown($this->fileName, 'http');
117 chmod($this->fileName, 0666);
118 }
119
120 // delete
121 public function delete()
122 {
123 unlink('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format);
124 }
125}