From 90b3efeeb9d939303ff80b180725e06e2e57af79 Mon Sep 17 00:00:00 2001 From: polo Date: Thu, 13 Apr 2023 03:02:57 +0200 Subject: =?UTF-8?q?=C3=A9diteur=20page=20album?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/Article.php | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) (limited to 'model/Article.php') 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 @@ class Article { // pour tous les articles - public $page; // page et donc dossier concerné + 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 @@ -20,20 +21,29 @@ class Article public $fileCode = ''; // $_SESSION['target'] protected $time; // timestamp pour noms des fichiers créés - public function __construct($page) + 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->page . '/' . $this->format . '/*.' . $this->format); + $nameList = glob('data/' . $this->folder . '/' . $this->format . '/*.' . $this->format); $this->fileListCount = count($nameList); @@ -77,10 +87,10 @@ class Article private function readPositionsJSON() // retourne array ou NULL { - if(file_exists('data/' . $this->page . '/positions.json')) + 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->page . '/positions.json'), true); + $positions = json_decode(file_get_contents('data/' . $this->folder . '/positions.json'), true); } return $positions; } @@ -105,7 +115,7 @@ class Article $positions[$this->fileList[$i]['fileCode']] = $this->fileList[$i]['position']; } - file_put_contents('data/' . $this->page . '/positions.json', json_encode($positions)); + file_put_contents('data/' . $this->folder . '/positions.json', json_encode($positions)); } private function sortFileListByPositions() @@ -157,10 +167,6 @@ class Article return $content; } - - // GET - - // SET // fonctions CRUD (create - read - update - delete) @@ -177,7 +183,7 @@ class Article { // html version images normales $contentMaxi = $content; - $fileName = 'data/' . $this->page . '/' . $this->format . '-maxi-images/' . $this->time . '.' . $this->format; + $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); @@ -187,7 +193,7 @@ class Article $content = self::makeHtmlMiniImages($content); } - $fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format; + $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); @@ -207,7 +213,7 @@ class Article } public function readOne() { - return(file_get_contents('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format)); + return(file_get_contents('data/' . $this->folder . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format)); } // update @@ -217,7 +223,7 @@ class Article { // html version images normales $contentMaxi = $content; - $fileName = 'data/' . $this->page . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format; + $fileName = 'data/' . $this->folder . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format; if(file_exists($fileName) && empty($content)) { $this->delete(); @@ -235,7 +241,7 @@ class Article } // json ou html version petites images - $fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; + $fileName = 'data/' . $this->folder . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; if(file_exists($fileName) && empty($content)) { $this->delete(); @@ -252,11 +258,11 @@ class Article // delete public function delete() { - $path = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; + $path = 'data/' . $this->folder . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; unlink($path); // fichiers html dans html-maxi-images - $path_maxi = 'data/' . $this->page . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format; + $path_maxi = 'data/' . $this->folder . '/' . $this->format . '-maxi-images/' . $this->fileCode . '.' . $this->format; if(file_exists($path_maxi)) { unlink($path_maxi); -- cgit v1.2.3