summaryrefslogtreecommitdiff
path: root/model/Article.php
diff options
context:
space:
mode:
Diffstat (limited to 'model/Article.php')
-rw-r--r--model/Article.php40
1 files changed, 23 insertions, 17 deletions
diff --git a/model/Article.php b/model/Article.php
index 33848bb..2ea80f8 100644
--- a/model/Article.php
+++ b/model/Article.php
@@ -9,7 +9,8 @@
9class Article 9class Article
10{ 10{
11 // pour tous les articles 11 // pour tous les articles
12 public $page; // page et donc dossier concerné 12 public $page; // page du site
13 public $folder; // dossier des données (vaut souvent $page)
13 public $format = 'html'; // 'html' ou 'json' 14 public $format = 'html'; // 'html' ou 'json'
14 public $fileListCount; // pour les boucles "for" 15 public $fileListCount; // pour les boucles "for"
15 // pas de "foreach", on a besoin des compteurs $i 16 // pas de "foreach", on a besoin des compteurs $i
@@ -20,20 +21,29 @@ class Article
20 public $fileCode = ''; // $_SESSION['target'] 21 public $fileCode = ''; // $_SESSION['target']
21 protected $time; // timestamp pour noms des fichiers créés 22 protected $time; // timestamp pour noms des fichiers créés
22 23
23 public function __construct($page) 24 public function __construct(string $page, string $folder)
24 { 25 {
25 $this->page = $page; 26 $this->page = $page;
27 $this->folder = $folder;
26 $this->time = time(); 28 $this->time = time();
27 $this->makeFileList(); 29 $this->makeFileList();
28 } 30 }
29 31
32 // GET
33
34 // SET
35 public function setFolder(string $folder)
36 {
37 $this->folder = $folder;
38 }
39
30 // tableaux des noms des fichiers 40 // tableaux des noms des fichiers
31 // noter que le chemin et l'extension ne varient pas 41 // noter que le chemin et l'extension ne varient pas
32 public function makeFileList() 42 public function makeFileList()
33 { 43 {
34 // noms des fichiers 44 // noms des fichiers
35 // globbing = utiliser un pattern pour cibler des fichiers 45 // globbing = utiliser un pattern pour cibler des fichiers
36 $nameList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format); 46 $nameList = glob('data/' . $this->folder . '/' . $this->format . '/*.' . $this->format);
37 47
38 $this->fileListCount = count($nameList); 48 $this->fileListCount = count($nameList);
39 49
@@ -77,10 +87,10 @@ class Article
77 87
78 private function readPositionsJSON() // retourne array ou NULL 88 private function readPositionsJSON() // retourne array ou NULL
79 { 89 {
80 if(file_exists('data/' . $this->page . '/positions.json')) 90 if(file_exists('data/' . $this->folder . '/positions.json'))
81 { 91 {
82 // "true" pour retourner un tableau et non un objet 92 // "true" pour retourner un tableau et non un objet
83 $positions = json_decode(file_get_contents('data/' . $this->page . '/positions.json'), true); 93 $positions = json_decode(file_get_contents('data/' . $this->folder . '/positions.json'), true);
84 } 94 }
85 return $positions; 95 return $positions;
86 } 96 }
@@ -105,7 +115,7 @@ class Article
105 $positions[$this->fileList[$i]['fileCode']] = $this->fileList[$i]['position']; 115 $positions[$this->fileList[$i]['fileCode']] = $this->fileList[$i]['position'];
106 } 116 }
107 117
108 file_put_contents('data/' . $this->page . '/positions.json', json_encode($positions)); 118 file_put_contents('data/' . $this->folder . '/positions.json', json_encode($positions));
109 } 119 }
110 120
111 private function sortFileListByPositions() 121 private function sortFileListByPositions()
@@ -157,10 +167,6 @@ class Article
157 167
158 return $content; 168 return $content;
159 } 169 }
160
161 // GET
162
163 // SET
164 170
165 // fonctions CRUD (create - read - update - delete) 171 // fonctions CRUD (create - read - update - delete)
166 172
@@ -177,7 +183,7 @@ class Article
177 { 183 {
178 // html version images normales 184 // html version images normales
179 $contentMaxi = $content; 185 $contentMaxi = $content;
180 $fileName = 'data/' . $this->page . '/' . $this->format . '-maxi-images/' . $this->time . '.' . $this->format; 186 $fileName = 'data/' . $this->folder . '/' . $this->format . '-maxi-images/' . $this->time . '.' . $this->format;
181 $file = fopen($fileName, 'w'); // w pour créer ou écraser 187 $file = fopen($fileName, 'w'); // w pour créer ou écraser
182 fputs($file, $contentMaxi); 188 fputs($file, $contentMaxi);
183 fclose($file); 189 fclose($file);
@@ -187,7 +193,7 @@ class Article
187 $content = self::makeHtmlMiniImages($content); 193 $content = self::makeHtmlMiniImages($content);
188 } 194 }
189 195
190 $fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format; 196 $fileName = 'data/' . $this->folder . '/' . $this->format . '/' . $this->time . '.' . $this->format;
191 $file = fopen($fileName, 'w'); // w pour créer ou écraser 197 $file = fopen($fileName, 'w'); // w pour créer ou écraser
192 fputs($file, $content); 198 fputs($file, $content);
193 fclose($file); 199 fclose($file);
@@ -207,7 +213,7 @@ class Article
207 } 213 }
208 public function readOne() 214 public function readOne()
209 { 215 {
210 return(file_get_contents('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format)); 216 return(file_get_contents('data/' . $this->folder . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format));
211 } 217 }
212 218
213 // update 219 // update
@@ -217,7 +223,7 @@ class Article
217 { 223 {
218 // html version images normales 224 // html version images normales
219 $contentMaxi = $content; 225 $contentMaxi = $content;
220 $fileName = 'data/' . $this->page . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format; 226 $fileName = 'data/' . $this->folder . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format;
221 if(file_exists($fileName) && empty($content)) 227 if(file_exists($fileName) && empty($content))
222 { 228 {
223 $this->delete(); 229 $this->delete();
@@ -235,7 +241,7 @@ class Article
235 } 241 }
236 242
237 // json ou html version petites images 243 // json ou html version petites images
238 $fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; 244 $fileName = 'data/' . $this->folder . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
239 if(file_exists($fileName) && empty($content)) 245 if(file_exists($fileName) && empty($content))
240 { 246 {
241 $this->delete(); 247 $this->delete();
@@ -252,11 +258,11 @@ class Article
252 // delete 258 // delete
253 public function delete() 259 public function delete()
254 { 260 {
255 $path = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; 261 $path = 'data/' . $this->folder . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
256 unlink($path); 262 unlink($path);
257 263
258 // fichiers html dans html-maxi-images 264 // fichiers html dans html-maxi-images
259 $path_maxi = 'data/' . $this->page . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format; 265 $path_maxi = 'data/' . $this->folder . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format;
260 if(file_exists($path_maxi)) 266 if(file_exists($path_maxi))
261 { 267 {
262 unlink($path_maxi); 268 unlink($path_maxi);