summaryrefslogtreecommitdiff
path: root/model
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2022-02-17 18:13:00 +0100
committerpolo <ordipolo@gmx.fr>2022-02-17 18:13:00 +0100
commit787d03e48471ba62cd830379428f04d996f0b74b (patch)
treee9f98c7b9288c4530b50985688dd82622106ba2d /model
parent29df6f1362745eabf4fbcaedf309eb63795152fa (diff)
downloadmelaine-787d03e48471ba62cd830379428f04d996f0b74b.zip
model update
Diffstat (limited to 'model')
-rw-r--r--model/Album.php111
-rw-r--r--model/Article.php125
-rw-r--r--model/Image.php2
-rw-r--r--model/Page.php104
4 files changed, 211 insertions, 131 deletions
diff --git a/model/Album.php b/model/Album.php
index 1631c46..386a50f 100644
--- a/model/Album.php
+++ b/model/Album.php
@@ -1,14 +1,17 @@
1<?php 1<?php
2// model/Album.php 2// model/Album.php
3 3
4class Album extends Page // classe "objet" 4class Album extends Article
5{ 5{
6 public $oneAlbum;
7
6 public function __construct($page) 8 public function __construct($page)
7 { 9 {
8 $this->page = $page; 10 $this->page = $page;
9 $this->albumCode = ''; // désigne un fichier json et un html 11 $this->fileCode = ''; // désigne un fichier json et un html
10 $this->format = 'json'; // vaut 'html' dans la classe mère 12 $this->format = 'json'; // vaut 'html' dans la classe mère
11 $this->time = time(); 13 $this->time = time();
14 $this->makeFileList();
12 } 15 }
13 16
14 // GET 17 // GET
@@ -24,17 +27,24 @@ class Album extends Page // classe "objet"
24 27
25 if($pochette != '') 28 if($pochette != '')
26 { 29 {
30 //echo($pochette); die(); // = nom sans chemin
31
27 // télécharger la pochette 32 // télécharger la pochette
28 require('model/Image.php'); 33 require('model/Image.php');
29 $Image = new Image(false); 34 $Image = new Image(false);
30 $Image->upload(); 35 $Image->upload();
31 36
32 /*$erreur = $Image->erreur; 37 $erreur = $Image->erreur;
33 if(!empty($erreur)) 38 if(!empty($erreur))
34 {}*/ 39 {}
35 } 40 }
36 41
42 // encodage avec un tableau simple
37 $albumJSON = json_encode([$titre, $annee, $pochette]); 43 $albumJSON = json_encode([$titre, $annee, $pochette]);
44 //var_dump($albumJSON); die();
45
46 // encodage avec un tableau associatif
47 //$albumJSON = json_encode(['titre' => $titre, 'annee' => $annee,'pochette' => $pochette]);
38 48
39 $nom_fichier = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format; 49 $nom_fichier = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format;
40 50
@@ -43,35 +53,78 @@ class Album extends Page // classe "objet"
43 fclose($fichier); 53 fclose($fichier);
44 chmod($nom_fichier, 0666); 54 chmod($nom_fichier, 0666);
45 55
46 return($Image->erreur); 56 //return($Image->erreur);
47 } 57 }
48 58
49 // read 59 // read
50 public function readAll() 60 public function readAll()
51 { 61 {
52 // lecture des dones 62 // mettre le JSON dans fileList[$i]['content']
53 $albumsJSON = parent::readAll(); 63 parent::readAll();
54 64
55 // conversion des chaines JSON en tableaux: titre, année, pochette 65 $this->format = 'html';
56 for($i = 0; $i < count($albumsJSON); $i++) 66 for($i = 0; $i < $this->fileListCount; $i++)
57 { 67 {
58 $albumsJSON[$i] = json_decode($albumsJSON[$i], true); 68 // ajout du JSON
69 /*$content = file_get_contents($this->fileList[$i]['fileName']);
70 $content = json_decode($content, true);*/
71 $content = json_decode($this->fileList[$i]['content'], true);
72 //var_dump($content); die();
73
74 // lecture de $content étant un tableau simple
75 $this->fileList[$i]['titre'] = $content[0];
76 $this->fileList[$i]['annee'] = $content[1];
77 $this->fileList[$i]['pochette'] = $content[2];
78
79 // ajout du HTML si il existe
80 if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileList[$i]['fileCode'] . '.' . $this->format))
81 {
82 $this->fileList[$i]['HTMLfileName'] = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileList[$i]['fileCode'] . '.' . $this->format;
83 $this->fileList[$i]['HTMLcontent'] = file_get_contents($this->fileList[$i]['HTMLfileName']);
84 }
85 // utile?
86 else
87 {
88 $this->fileList[$i]['HTMLfileName'] = '';
89 $this->fileList[$i]['HTMLcontent'] = '';
90 }
59 } 91 }
60 92 // on remet comme avant
61 return($albumsJSON); 93 $this->format = 'json';
62 } 94 }
63 95
64 public function getVignetteJSON() 96 public function readOne()
65 { 97 {
66 return(json_decode(file_get_contents('data/discographie/json/' . $this->albumCode . '.json'))); 98 for($i = 0; $i < $this->fileListCount; $i++)
99 {
100 if($_SESSION['target'] == $this->fileList[$i]['fileCode'])
101 {
102 // json
103 $this->oneAlbum['fileCode'] = $this->fileList[$i]['fileCode'];
104 $this->oneAlbum['fileName'] = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
105 $this->oneAlbum['content'] = file_get_contents($this->fileList[$i]['fileName']);
106 $content = json_decode($this->oneAlbum['content']);
107 $this->oneAlbum['titre'] = $content[0];
108 $this->oneAlbum['annee'] = $content[1];
109 $this->oneAlbum['pochette'] = $content[2];
110
111 // html
112 $this->format = 'html';
113 if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format))
114 {
115 $this->oneAlbum['HTMLfileName'] = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
116 $this->oneAlbum['HTMLcontent'] = file_get_contents($this->oneAlbum['HTMLfileName']);
117 }
118 }
119 }
67 } 120 }
68 121
69 // page de l'album 122 // page d'un album
70 public static function readOneHTML($albumCode) 123 public static function readOneHTML($fileCode)
71 { 124 {
72 if(file_exists('data/discographie/html/' . $albumCode . '.html')) 125 if(file_exists('data/discographie/html/' . $fileCode . '.html'))
73 { 126 {
74 return(file_get_contents('data/discographie/html/' . $albumCode . '.html')); 127 return(file_get_contents('data/discographie/html/' . $fileCode . '.html'));
75 } 128 }
76 else 129 else
77 { 130 {
@@ -79,13 +132,10 @@ class Album extends Page // classe "objet"
79 } 132 }
80 } 133 }
81 134
82 // pour afficher des dates 135 public function getVignetteJSON()
83 /*public function getDate($fileNumber)
84 { 136 {
85 // le 2è paramètre exclut le suffixe .html 137 return(json_decode(file_get_contents('data/discographie/json/' . $this->fileCode . '.json')));
86 $timestamp = basename($this->files[$fileNumber], '.html'); 138 }
87 return getdate($timestamp);
88 }*/
89 139
90 // update 140 // update
91 public function updateVignetteJSON($titre, $annee, $pochette) 141 public function updateVignetteJSON($titre, $annee, $pochette)
@@ -103,6 +153,12 @@ class Album extends Page // classe "objet"
103 require('model/Image.php'); 153 require('model/Image.php');
104 $Image = new Image(false); 154 $Image = new Image(false);
105 $Image->upload(); 155 $Image->upload();
156 $erreur = $Image->erreur;
157 if(!empty($erreur))
158 {
159 // remettre pochette d'avant
160 //$pochette
161 }
106 } 162 }
107 163
108 $albumJSON = json_encode([$titre, $annee, $pochette]); 164 $albumJSON = json_encode([$titre, $annee, $pochette]);
@@ -120,11 +176,12 @@ class Album extends Page // classe "objet"
120 // delete 176 // delete
121 public function delete() 177 public function delete()
122 { 178 {
123 unlink('data/' . $this->page . '/' . $this->format . '/' . $this->albumCode . '.json'); 179 parent::delete();
124 180
125 if(file_exists('data/' . $this->page . '/html/' . $this->albumCode . '.html')); 181 if(file_exists('data/' . $this->page . '/html/' . $this->fileCode . '.' . $this->format));
126 { 182 {
127 unlink('data/' . $this->page . '/html/' . $this->albumCode . '.html'); 183 $this->format = 'html';
184 parent::delete();
128 } 185 }
129 } 186 }
130} 187}
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 @@
1<?php
2// model/Article.php
3
4// structure de données de la variable $fileList
5// on a 3 possibilités (on prendra la 3ème):
6
7// - un tableau simple contenant des tableaux associatifs (avec des index façon BDD)
8// $fileList[0] => [$fileCode => 'code', $fileName => 'nom, $content => 'contenu'],
9
10// - un tableau associatif contenant des tableaux simples
11// les catégories sont au premier niveau
12// mauvaise idée, risque de croiser les fichiers
13// $fileList['fileCode'] => [$fileCode1, $fileCode2]
14// $fileList['fileName'] => [$fileName1, $fileName2], etc
15
16// - un tableau associatif contenant des tableaux associatifs
17// les catégories sont au deuxième niveau
18// $fileList[$fileCode] => [$fileName => 'code', $content => 'contenu'], etc
19
20
21class Article
22{
23 // pour tous les articles
24 public $page; // page et donc dossier concerné
25 public $format = 'html'; // vaut 'html' ou 'json'
26 public $fileListCount;
27 public $fileList; // = toutes les données
28 //protected $articles; // contenu de toute la page
29 //protected $nbArticles; // un fichier = un article
30
31 // pour un article (ou album) spécifique
32 //public $fileName = ''; // = $_SESSION['nomFichier']
33 public $fileCode = ''; // $_SESSION['target']
34 protected $time; // timestamp pour noms des fichiers créés
35
36 public function __construct($page)
37 {
38 $this->page = $page;
39 $this->time = time();
40 $this->makeFileList();
41 }
42
43 // tableaux des noms des fichiers
44 // noter que le chemin et l'extension ne varient pas
45 public function makeFileList()
46 {
47 // globbing = utiliser un pattern pour cibler des fichiers
48 $nameList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format);
49
50 $this->fileListCount = count($nameList);
51
52 for($i = 0; $i < $this->fileListCount; $i++)
53 {
54 $pathInfo = pathinfo($nameList[$i]);
55 $fileCode = $pathInfo['filename'];
56 $this->fileList[$i] = [
57 'fileCode' => $fileCode,
58 'fileName' => $nameList[$i],
59 'content' => '',
60 //'date' => getdate() // peut-être utile plus tard
61 ];
62 }
63 //var_dump($this->fileList); die();
64 }
65
66 // GET
67
68 // SET
69 public function setFileName()
70 {
71 if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format))
72 {
73 $this->fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
74 }
75 }
76
77 // fonctions CRUD (create - read - update - delete)
78
79 // create
80 public function create($content)
81 {
82 //$format = 'html';
83
84 // nommer les fichiers avec le timestamp pour:
85 // - les trier par ordre chronologique
86 // - rendre quasi impossible d'avoir deux fois le même nom (à la condition de gérer la "concurrence")
87 $nom_fichier = 'data/' . $this->page . '/' . $this->format . '/' . $this->time . '.' . $this->format;
88
89 $fichier = fopen($nom_fichier, 'w'); // w pour créer ou écraser
90 fputs($fichier, $content);
91 fclose($fichier);
92 chmod($nom_fichier, 0666);
93 }
94
95 // read
96 public function readAll() // ajoute le contenu à FileList
97 {
98 for($i = 0; $i < $this->fileListCount; $i++)
99 {
100 $this->fileList[$i]['content'] = file_get_contents($this->fileList[$i]['fileName']);
101 }
102 }
103 public function readOne()
104 {
105 //return(file_get_contents($this->fileName));
106 return(file_get_contents('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format));
107 }
108
109 // update
110 public function update($content)
111 {
112 // 'w' crée ou écrase
113 $file = fopen('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format, 'w');
114 fputs($file, $content);
115 fclose($file);
116 //chown($this->fileName, 'http');
117 chmod($this->fileName, 0666);
118 }
119
120 // delete
121 public function delete()
122 {
123 unlink('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format);
124 }
125}
diff --git a/model/Image.php b/model/Image.php
index 51d2fa4..8ebcd16 100644
--- a/model/Image.php
+++ b/model/Image.php
@@ -19,6 +19,8 @@ class Image
19 19
20 // SET 20 // SET
21 21
22 // miniatures des photos
23
22 public function upload() 24 public function upload()
23 { 25 {
24 // traitement et enregistrement de l'image 26 // traitement et enregistrement de l'image
diff --git a/model/Page.php b/model/Page.php
deleted file mode 100644
index 63730d8..0000000
--- a/model/Page.php
+++ /dev/null
@@ -1,104 +0,0 @@
1<?php
2// model/Page.php
3
4class Page // classe "objet"
5{
6 public $page; // page et donc dossier concerné
7 public $format = 'html'; // vaut 'html' ou 'json'
8 public $fileList; // noms des fichiers dans ce dossier
9 protected $articles; // contenu de toute la page
10 protected $nbArticles; // un fichier = un article
11
12 // gestion d'un article spécifique
13 public $fileName = ''; // correspond à $_SESSION['nomFichier']
14 protected $time; // timestamp pour noms des fichiers créés
15
16 public function __construct($page)
17 {
18 $this->page = $page;
19 $this->time = time();
20 $this->makeFileList();
21 }
22
23 // tableaux des noms des fichiers
24 public function makeFileList()
25 {
26 $this->fileList = glob('data/' . $this->page . '/' . $this->format . '/*.' . $this->format);
27 //$this->files = glob('*.' . $this->format);
28 }
29 /*public function makeFilePath()
30 {}*/
31
32 // nom d'un fichier
33 public function findFileName($numArticle)
34 {
35 if($numArticle > 0)
36 {
37 $this->fileName = $this->fileList[$numArticle - 1];
38 }
39 }
40
41 public function setFileName()
42 {
43 if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->albumCode . '.' . $this->format))
44 {
45 $this->fileName = 'data/' . $this->page . '/' . $this->format . '/' . $this->albumCode . '.' . $this->format;
46 }
47 }
48
49 // GET
50
51 // SET
52
53 // fonctions CRUD (create - read - update - delete)
54
55 // create
56 public function create($content)
57 {
58 //$format = 'html';
59
60 // nommer les fichiers avec le timestamp pour:
61 // - les trier par ordre chronologique
62 // - rendre quasi impossible d'avoir deux fois le même nom (à la condition de gérer la "concurrence")
63 $nom_fichier = 'data/' . $this->page . '/' . $format . '/' . $this->time . '.' . $format;
64
65 $fichier = fopen($nom_fichier, 'w'); // w pour créer ou écraser
66 fputs($fichier, $content);
67 fclose($fichier);
68 chmod($nom_fichier, 0666);
69 }
70
71 // read
72 public function readAll()
73 {
74 $i = 0;
75 $articles = [];
76 foreach ($this->fileList as $oneFile)
77 {
78 $articles[$i] = file_get_contents($oneFile);
79 $i++;
80 }
81 //print_r($articles);
82 return $articles;
83 }
84 public function readOne()
85 {
86 return(file_get_contents($this->fileName));
87 }
88
89 // update
90 public function update($content)
91 {
92 $file = fopen($this->fileName, 'w'); // crée ou écrase
93 fputs($file, $content);
94 fclose($file);
95 //chown($this->fileName, 'http');
96 chmod($this->fileName, 0666);
97 }
98
99 // delete
100 public function delete()
101 {
102 unlink($this->fileName);
103 }
104} \ No newline at end of file