diff options
author | polo <ordipolo@gmx.fr> | 2023-03-03 23:44:43 +0100 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2023-03-03 23:44:43 +0100 |
commit | c0b176ae142624d6a6daa0f65ea6fead448b8b47 (patch) | |
tree | e06445897db82e0f3cddd13bb2a67860fcf9fc3c /model | |
parent | a1a9c5d80d800f6c38b900c66c77db05801a6f1d (diff) | |
download | melaine-c0b176ae142624d6a6daa0f65ea6fead448b8b47.zip |
inversion de positions
Diffstat (limited to 'model')
-rw-r--r-- | model/Article.php | 102 |
1 files changed, 100 insertions, 2 deletions
diff --git a/model/Article.php b/model/Article.php index d0fb019..f4fe65a 100644 --- a/model/Article.php +++ b/model/Article.php | |||
@@ -10,8 +10,9 @@ class 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 et donc dossier concerné |
13 | public $format = 'html'; // vaut 'html' ou 'json' | 13 | public $format = 'html'; // 'html' ou 'json' |
14 | public $fileListCount; | 14 | public $fileListCount; // pour les boucles "for" |
15 | // pas de "foreach", on a besoin des compteurs $i | ||
15 | public $fileList; // = toutes les données | 16 | public $fileList; // = toutes les données |
16 | 17 | ||
17 | // pour un article (ou album) spécifique | 18 | // pour un article (ou album) spécifique |
@@ -30,11 +31,17 @@ class Article | |||
30 | // noter que le chemin et l'extension ne varient pas | 31 | // noter que le chemin et l'extension ne varient pas |
31 | public function makeFileList() | 32 | public function makeFileList() |
32 | { | 33 | { |
34 | // noms des fichiers | ||
33 | // globbing = utiliser un pattern pour cibler des fichiers | 35 | // globbing = utiliser un pattern pour cibler des fichiers |
34 | $nameList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format); | 36 | $nameList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format); |
35 | 37 | ||
36 | $this->fileListCount = count($nameList); | 38 | $this->fileListCount = count($nameList); |
37 | 39 | ||
40 | // associations: fileCode => position | ||
41 | $positions = $this->readPositionsJSON(); | ||
42 | |||
43 | // $this->fileList[] | ||
44 | $positionMax = 0; | ||
38 | for($i = 0; $i < $this->fileListCount; $i++) | 45 | for($i = 0; $i < $this->fileListCount; $i++) |
39 | { | 46 | { |
40 | $pathInfo = pathinfo($nameList[$i]); | 47 | $pathInfo = pathinfo($nameList[$i]); |
@@ -45,9 +52,100 @@ class Article | |||
45 | 'content' => '', | 52 | 'content' => '', |
46 | //'date' => getdate() // peut-être utile plus tard | 53 | //'date' => getdate() // peut-être utile plus tard |
47 | ]; | 54 | ]; |
55 | |||
56 | // récupération des positions disponibles | ||
57 | // celles inutilisées sont ignorées | ||
58 | // une case dans $positions: "1677796758" => 2 | ||
59 | if(isset($positions[$fileCode]) && $positions[$fileCode] > 0) | ||
60 | { | ||
61 | $this->fileList[$i]['position'] = $positions[$fileCode]; | ||
62 | $positionMax = max($positions[$fileCode], $positionMax); | ||
63 | } | ||
64 | else | ||
65 | { | ||
66 | $this->fileList[$i]['position'] = 0; | ||
67 | } | ||
68 | } | ||
69 | $this->updatePositionsJSON($positionMax); | ||
70 | |||
71 | // réordonner "fileList" par rapport aux positions | ||
72 | if(!empty($this->fileList)) | ||
73 | { | ||
74 | $this->sortFileListByPositions(); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | private function readPositionsJSON() // retourne array ou NULL | ||
79 | { | ||
80 | if(file_exists('data/' . $this->page . '/positions.json')) | ||
81 | { | ||
82 | // "true" pour retourner un tableau et non un objet | ||
83 | $positions = json_decode(file_get_contents('data/' . $this->page . '/positions.json'), true); | ||
84 | } | ||
85 | return $positions; | ||
86 | } | ||
87 | |||
88 | private function updatePositionsJSON(int $positionMax = -1) | ||
89 | { | ||
90 | // créer les positions manquantes | ||
91 | $positions = []; | ||
92 | for($i = 0; $i < $this->fileListCount; $i++) | ||
93 | { | ||
94 | // fonction appelée par makeFileList() | ||
95 | if($positionMax >= 0) | ||
96 | { | ||
97 | if($this->fileList[$i]['position'] == 0) | ||
98 | { | ||
99 | $positionMax++; | ||
100 | $this->fileList[$i]['position'] = $positionMax; | ||
101 | } | ||
102 | } | ||
103 | |||
104 | // remplissage comme ceci: "1677796758" => 2 | ||
105 | $positions[$this->fileList[$i]['fileCode']] = $this->fileList[$i]['position']; | ||
106 | } | ||
107 | |||
108 | file_put_contents('data/' . $this->page . '/positions.json', json_encode($positions)); | ||
109 | } | ||
110 | |||
111 | private function sortFileListByPositions() | ||
112 | { | ||
113 | // contient les positions seules | ||
114 | $positions = array_column($this->fileList, 'position'); | ||
115 | |||
116 | // magique! | ||
117 | array_multisort($positions, SORT_ASC, $this->fileList); | ||
118 | } | ||
119 | |||
120 | // pour modifier l'ordre des éléments de la page | ||
121 | // faire plus tard une version statique pour traitement AJAX | ||
122 | public function inversionPositions(int $direction) | ||
123 | { | ||
124 | for($i = 0; $i < $this->fileListCount; $i++) | ||
125 | { | ||
126 | if($this->fileList[$i]['fileCode'] == $this->fileCode) | ||
127 | { | ||
128 | // si direction vaut 1, inversion avec l'élément au dessus (puisqu'on les affiche dans l'ordre décroissant) | ||
129 | if(($direction === 1 || $direction === -1) && | ||
130 | isset($this->fileList[$i + $direction]['position'])) | ||
131 | { | ||
132 | $c = $this->fileList[$i]['position']; | ||
133 | $this->fileList[$i]['position'] = $this->fileList[$i + $direction]['position']; | ||
134 | $this->fileList[$i + $direction]['position'] = $c; | ||
135 | |||
136 | // sortie de la boucle | ||
137 | $i = $this->fileListCount; | ||
138 | } | ||
139 | else // inversion du premier avec le précédent ou du dernier avec le suivant | ||
140 | {} | ||
141 | } | ||
48 | } | 142 | } |
143 | |||
144 | // écriture du positions.json | ||
145 | $this->updatePositionsJSON(); | ||
49 | } | 146 | } |
50 | 147 | ||
148 | // du code html pour utiliser les miniatures | ||
51 | private function makeHtmlMiniImages($content) | 149 | private function makeHtmlMiniImages($content) |
52 | { | 150 | { |
53 | // insérer -mini au nom du dossier et au fichier | 151 | // insérer -mini au nom du dossier et au fichier |