summaryrefslogtreecommitdiff
path: root/model/Article.php
blob: fa9fce10c0f1db8dba651c8594181759ade9d893 (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
<?php
// model/Article.php

// structure de données de la variable $fileList
// on a 3 possibilités (on prendra la 3ème):

// - un tableau simple contenant des tableaux associatifs (avec des index façon BDD)
// $fileList[0] => [$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")
		$fileName = 'data/' . $this->page . '/' . $this->format .  '/' . $this->time . '.' . $this->format;

	    $file = fopen($fileName, 'w'); // w pour créer ou écraser
	    fputs($file, $content);
	    fclose($file);
	    chmod($fileName, 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)
	{
		$fileName = 'data/' . $this->page . '/' . $this->format .  '/' . $this->fileCode . '.' . $this->format;
		//var_dump(file_get_contents($fileName)); die();
		if(file_exists($fileName) && empty($content))
		{
			$this->delete();
		}
		elseif(!empty($content))
		{
			$file = fopen($fileName, 'w'); // w pour créer ou écraser
			fputs($file, $content);
			fclose($file);
			//chown($this->fileName, 'http');
		    chmod($fileName, 0666);
		}
	}

	// delete
	public function delete()
	{
		unlink('data/' . $this->page . '/' . $this->format .  '/' . $this->fileCode . '.' . $this->format);
	}
}