1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
<?php
// model/Article.php
// éventuellement: utiliser une classe structure de données
// accesssible avec des GET et des SET
// à voir alors si on utilise des variables statiques
class Article
{
// pour tous les articles
public $page; // page du site
public $folder; // dossier des données (vaut souvent $page)
public $format = 'html'; // 'html' ou 'json'
public $fileListCount; // pour les boucles "for"
// pas de "foreach", on a besoin des compteurs $i
public $fileList; // = toutes les données
// pour un article (ou album) spécifique
//public $fileName = ''; // = $_SESSION['nomFichier']
public $fileCode = ''; // = $_SESSION['file_code']
protected $time; // timestamp pour noms des fichiers créés
public function __construct(string $page, string $folder)
{
$this->page = $page;
$this->folder = $folder;
$this->time = time();
$this->makeFileList();
}
// GET
// SET
public function setFolder(string $folder)
{
$this->folder = $folder;
}
// tableaux des noms des fichiers
// noter que le chemin et l'extension ne varient pas
public function makeFileList()
{
// noms des fichiers
// globbing = utiliser un pattern pour cibler des fichiers
$nameList = glob('data/' . $this->folder . '/' . $this->format . '/*.' . $this->format);
$this->fileListCount = count($nameList);
// associations: fileCode => position
$positions = $this->readPositionsJSON();
// $this->fileList[]
$positionMax = 0;
for($i = 0; $i < $this->fileListCount; $i++)
{
$pathInfo = pathinfo($nameList[$i]);
$fileCode = $pathInfo['filename'];
$this->fileList[$i] = [
'fileCode' => $fileCode,
'fileName' => $nameList[$i],
'content' => '',
//'date' => getdate() // peut-être utile plus tard
];
// récupération des positions disponibles
// celles inutilisées sont ignorées
// une case dans $positions: "1677796758" => 2
if(isset($positions[$fileCode]) && $positions[$fileCode] > 0)
{
$this->fileList[$i]['position'] = $positions[$fileCode];
$positionMax = max($positions[$fileCode], $positionMax);
}
else
{
$this->fileList[$i]['position'] = 0;
}
}
$this->updatePositionsJSON($positionMax);
// réordonner "fileList" par rapport aux positions
if(!empty($this->fileList))
{
$this->sortFileListByPositions();
}
}
private function readPositionsJSON() // retourne array ou NULL
{
if(file_exists('data/' . $this->folder . '/positions.json'))
{
// "true" pour retourner un tableau et non un objet
$positions = json_decode(file_get_contents('data/' . $this->folder . '/positions.json'), true);
}
return $positions;
}
private function updatePositionsJSON(int $positionMax = -1)
{
// créer les positions manquantes
$positions = [];
for($i = 0; $i < $this->fileListCount; $i++)
{
// fonction appelée par makeFileList()
if($positionMax >= 0)
{
if($this->fileList[$i]['position'] == 0)
{
$positionMax++;
$this->fileList[$i]['position'] = $positionMax;
}
}
// remplissage comme ceci: "1677796758" => 2
$positions[$this->fileList[$i]['fileCode']] = $this->fileList[$i]['position'];
}
file_put_contents('data/' . $this->folder . '/positions.json', json_encode($positions));
}
private function sortFileListByPositions()
{
// contient les positions seules
$positions = array_column($this->fileList, 'position');
// magique!
array_multisort($positions, SORT_ASC, $this->fileList);
}
// pour modifier l'ordre des éléments de la page
// faire plus tard une version statique pour traitement AJAX
public function inversionPositions(int $direction)
{
for($i = 0; $i < $this->fileListCount; $i++)
{
if($this->fileList[$i]['fileCode'] == $this->fileCode)
{
// si direction vaut 1, inversion avec l'élément au dessus (puisqu'on les affiche dans l'ordre décroissant)
if(($direction === 1 || $direction === -1) &&
isset($this->fileList[$i + $direction]['position']))
{
$c = $this->fileList[$i]['position'];
$this->fileList[$i]['position'] = $this->fileList[$i + $direction]['position'];
$this->fileList[$i + $direction]['position'] = $c;
// sortie de la boucle
$i = $this->fileListCount;
}
else // inversion du premier avec le précédent ou du dernier avec le suivant
{}
}
}
// écriture du positions.json
$this->updatePositionsJSON();
}
// du code html pour utiliser les miniatures
private function makeHtmlMiniImages($content)
{
// insérer -mini au nom du dossier et au fichier
// on choisit pour bien faire de cibler toute la balise <img/>
$pattern = array('#(<img[^>]+/images(?!-mini))([^>]+>)#',
'#(<img[^>]+)((?<!-mini)(\.jpg|\.jpeg|\.png|\.gif)[^>]+>)#');
$remplacement = array('$1-mini$2', '$1-mini$2');
$content = preg_replace($pattern, $remplacement, $content);
return $content;
}
// fonctions CRUD (create - read - update - delete)
// create
public function create($content)
{
// $format dépend de la classe qui a été instanciée
// nommer les fichiers avec le timestamp pour:
// - les trier par ordre chronologique
// - rendre quasi impossible d'avoir deux fois le même nom
if($this->format == 'html')
{
// html version images normales
$contentMaxi = $content;
$fileName = 'data/' . $this->folder . '/' . $this->format . '-maxi-images/' . $this->time . '.' . $this->format;
$file = fopen($fileName, 'w'); // w pour créer ou écraser
fputs($file, $contentMaxi);
fclose($file);
chmod($fileName, 0666);
// html version petites images
$content = self::makeHtmlMiniImages($content);
}
$fileName = 'data/' . $this->folder . '/' . $this->format . '/' . $this->time . '.' . $this->format;
$file = fopen($fileName, 'w'); // w pour créer ou écraser
fputs($file, $content);
fclose($file);
chmod($fileName, 0666);
}
// read
public function readAll() // ajoute le contenu à FileList
{
// lit du html ou du json si appelée depuis la classe Album
for($i = 0; $i < $this->fileListCount; $i++)
{
$this->fileList[$i]['content'] = file_get_contents($this->fileList[$i]['fileName']);
}
}
public function readOne()
{
return(file_get_contents('data/' . $this->folder . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format));
}
// update
public function update($content)
{
if($this->format == 'html')
{
// html version images normales
$contentMaxi = $content;
$fileName = 'data/' . $this->folder . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format;
if(file_exists($fileName) && empty($content))
{
$this->delete();
}
elseif(!empty($content))
{
$file = fopen($fileName, 'w'); // w pour créer ou écraser
fputs($file, $contentMaxi);
fclose($file);
chmod($fileName, 0666);
}
// html version petites images
$content = self::makeHtmlMiniImages($content);
}
// json ou html version petites images
$fileName = 'data/' . $this->folder . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
if(file_exists($fileName) && empty($content))
{
$this->delete();
}
elseif(!empty($content))
{
$file = fopen($fileName, 'w'); // w pour créer ou écraser
fputs($file, $content);
fclose($file);
chmod($fileName, 0666);
}
}
// delete
public function delete()
{
$path = 'data/' . $this->folder . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
unlink($path);
// fichiers html dans html-maxi-images
$path_maxi = 'data/' . $this->folder . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format;
if(file_exists($path_maxi))
{
unlink($path_maxi);
}
}
}
|