From 787d03e48471ba62cd830379428f04d996f0b74b Mon Sep 17 00:00:00 2001 From: polo Date: Thu, 17 Feb 2022 18:13:00 +0100 Subject: model update --- model/Article.php | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 model/Article.php (limited to 'model/Article.php') 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 @@ + [$fileCode => 'code', $fileName => 'nom, $content => 'contenu'], + +// - un tableau associatif contenant des tableaux simples +// les catégories sont au premier niveau +// mauvaise idée, risque de croiser les fichiers +// $fileList['fileCode'] => [$fileCode1, $fileCode2] +// $fileList['fileName'] => [$fileName1, $fileName2], etc + +// - un tableau associatif contenant des tableaux associatifs +// les catégories sont au deuxième niveau +// $fileList[$fileCode] => [$fileName => 'code', $content => 'contenu'], etc + + +class Article +{ + // pour tous les articles + public $page; // page et donc dossier concerné + public $format = 'html'; // vaut 'html' ou 'json' + public $fileListCount; + public $fileList; // = toutes les données + //protected $articles; // contenu de toute la page + //protected $nbArticles; // un fichier = un article + + // pour un article (ou album) spécifique + //public $fileName = ''; // = $_SESSION['nomFichier'] + public $fileCode = ''; // $_SESSION['target'] + protected $time; // timestamp pour noms des fichiers créés + + public function __construct($page) + { + $this->page = $page; + $this->time = time(); + $this->makeFileList(); + } + + // tableaux des noms des fichiers + // noter que le chemin et l'extension ne varient pas + public function makeFileList() + { + // globbing = utiliser un pattern pour cibler des fichiers + $nameList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format); + + $this->fileListCount = count($nameList); + + 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 + ]; + } + //var_dump($this->fileList); die(); + } + + // GET + + // SET + public function setFileName() + { + if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format)) + { + $this->fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format; + } + } + + // fonctions CRUD (create - read - update - delete) + + // create + public function create($content) + { + //$format = 'html'; + + // nommer les fichiers avec le timestamp pour: + // - les trier par ordre chronologique + // - rendre quasi impossible d'avoir deux fois le même nom (à la condition de gérer la "concurrence") + $nom_fichier = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format; + + $fichier = fopen($nom_fichier, 'w'); // w pour créer ou écraser + fputs($fichier, $content); + fclose($fichier); + chmod($nom_fichier, 0666); + } + + // read + public function readAll() // ajoute le contenu à FileList + { + 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($this->fileName)); + return(file_get_contents('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format)); + } + + // update + public function update($content) + { + // 'w' crée ou écrase + $file = fopen('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format, 'w'); + fputs($file, $content); + fclose($file); + //chown($this->fileName, 'http'); + chmod($this->fileName, 0666); + } + + // delete + public function delete() + { + unlink('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format); + } +} -- cgit v1.2.3