summaryrefslogtreecommitdiff
path: root/model/Album.php
blob: 95dac503ee211b9040b2f5da2574fc0e78a3fef5 (plain)
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
<?php
// model/Album.php

class Album extends Article
{
	public $oneAlbum;

	public function __construct($page)
	{
		$this->format = 'json'; // vaut 'html' dans la classe mère

		// pour: page, fileCode, time et makeFileList()
		parent::__construct($page);
	}

	// GET
	public function getAllJSON() // pour la page album
	{
		// mettre le JSON dans fileList[$i]['content']
		parent::readAll();

		$this->format = 'html';

		for($i = 0; $i < $this->fileListCount; $i++)
		{
			$content = json_decode($this->fileList[$i]['content'], true);
			$this->fileList[$i]['titre'] = $content[0];
			$this->fileList[$i]['annee'] = $content[1];
			$this->fileList[$i]['pochette'] = $content[2];
			$this->fileList[$i]['pochetteMini'] = $content[3];
		}

		// on remet comme avant
		$this->format = 'json';
	}

	// SET

	// fonctions CRUD

	// create
	public function createVignette($titre, $annee, $pochette, $pochetteMini)
    {
    	//$this->format = 'json';
    	
    	if($pochette != '')
    	{
    		//echo($pochette); die(); // = nom sans chemin

    		// télécharger la pochette
	        require('model/Image.php');
	        $Image = new Image(false);
	        $Image->upload();
	        $Image->makeThumbnail(201);

	        $erreur = $Image->erreur;
	        if(!empty($erreur))
	        {}
    	}

    	// encodage avec un tableau simple
    	$albumJSON = json_encode([$titre, $annee, $pochette, $pochetteMini]);
    	//var_dump($albumJSON); die();

    	// encodage avec un tableau associatif
    	//$albumJSON = json_encode(['titre' => $titre, 'annee' => $annee,'pochette' => $pochette]);

    	$nom_fichier = 'data/' . $this->page . '/' . $this->format .  '/' . $this->time . '.' . $this->format;

    	$fichier = fopen($nom_fichier, 'w'); // w pour créer ou écraser
	    fputs($fichier, $albumJSON);
	    fclose($fichier);
	    chmod($nom_fichier, 0666);

	    //return($Image->erreur);
    }

	// read

	// hydratation
	public function readAll()
	{
		// mettre le JSON dans fileList[$i]['content']
		parent::readAll();
		// = surcharge, appeler la méthode de la classe mère
		// permet de ne pas la remplacer par la nouvelle

		$this->format = 'html';

		for($i = 0; $i < $this->fileListCount; $i++)
		{
			// ajout du JSON
			$content = json_decode($this->fileList[$i]['content'], true);
			
			// lecture de $content étant un tableau simple
			$this->fileList[$i]['titre'] = $content[0];
			$this->fileList[$i]['annee'] = $content[1];
			$this->fileList[$i]['pochette'] = $content[2];
			$this->fileList[$i]['pochetteMini'] = $content[3];

			// ajout du HTML si il existe
			if(file_exists('data/' . $this->page . '/' . $this->format .  '/' . $this->fileList[$i]['fileCode'] . '.' . $this->format))
			{
				$this->fileList[$i]['HTMLfileName'] = 'data/' . $this->page . '/' . $this->format .  '/' . $this->fileList[$i]['fileCode'] . '.' . $this->format;
				$this->fileList[$i]['HTMLcontent'] = file_get_contents($this->fileList[$i]['HTMLfileName']);
			}
			// utile?
			else
			{
				$this->fileList[$i]['HTMLfileName'] = '';
				$this->fileList[$i]['HTMLcontent'] = '';
			}
		}
		// on remet comme avant
		$this->format = 'json';
	}

	// hydratation
	public function readOne()
	{
		for($i = 0; $i < $this->fileListCount; $i++)
		{
			if($this->fileCode == $this->fileList[$i]['fileCode'])
			{
				// json
				$this->oneAlbum['fileCode'] = $this->fileList[$i]['fileCode'];
				$this->oneAlbum['fileName'] = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
				$this->oneAlbum['content'] = file_get_contents($this->fileList[$i]['fileName']);
				$content = json_decode($this->oneAlbum['content']);
				$this->oneAlbum['titre'] = $content[0];
				$this->oneAlbum['annee'] = $content[1];
				$this->oneAlbum['pochette'] = $content[2];
				$this->oneAlbum['pochetteMini'] = $content[3];

				// html
				$this->format = 'html';
				if(file_exists('data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format))
				{
					$this->oneAlbum['HTMLfileName'] = 'data/' . $this->page . '/' . $this->format . '/' . $this->fileCode . '.' . $this->format;
					$this->oneAlbum['HTMLcontent'] = file_get_contents($this->oneAlbum['HTMLfileName']);
				}
			}
		}
	}

	// page d'un album
	/*public static function readOneHTML($fileCode)
	{
		if(file_exists('data/discographie/html/' . $fileCode . '.html'))
		{
			return(file_get_contents('data/discographie/html/' . $fileCode . '.html'));
		}
		else
		{
			return('');
		}
	}*/

	public function getVignetteJSON()
	{
		return(json_decode(file_get_contents('data/discographie/json/' . $this->fileCode . '.json')));
	}

	// update
	public function updateVignetteJSON($titre, $annee, $pochette, $pochetteMini)
	{
		// garder l'image si aucune n'est sélectionnée
		// (on ne peut pas préremplir un <input type="file">)
		$albumJSON = self::getVignetteJSON();
		/*var_dump($pochette); var_dump($pochetteMini);
		var_dump($albumJSON); die();*/

		if($pochette == '')
		{
			$pochette = $albumJSON[2];
			$pochetteMini = $albumJSON[3];
		}
		else
    	{
    		// télécharger la pochette
	        require('model/Image.php');
	        $Image = new Image(false);
	        $Image->upload();
	        $Image->makeThumbnail(201);
	        $erreur = $Image->erreur;
	        if(!empty($erreur))
	        {
	        	// remettre pochette d'avant
	        	//$pochette
	        }
    	}

		$albumJSON = json_encode([$titre, $annee, $pochette, $pochetteMini]);

    	// écriture
    	$nom_fichier = 'data/discographie/json/' . $this->albumCode . '.json';
    	$fichier = fopen($nom_fichier, 'w+'); // w pour créer ou écraser
	    fputs($fichier, $albumJSON);
	    fclose($fichier);
	    chmod($nom_fichier, 0666);

	    //return($Image->erreur);
	}

	// delete
	public function delete()
	{
		parent::delete(); // json

		if(file_exists('data/' . $this->page . '/html/' . $this->fileCode . '.' . $this->format));
		{
			$this->format = 'html';
			parent::delete(); // html
		}
	}
}