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

class Page // classe "objet"
{
	public $page; // page et donc dossier concerné
	public $format = 'html'; // vaut 'html' ou 'json'
	public $fileList; // noms des fichiers dans ce dossier
	protected $articles; // contenu de toute la page
	protected $nbArticles; // un fichier = un article

	// gestion d'un article spécifique
	public $fileName = ''; // correspond à $_SESSION['nomFichier']
	protected $time; // timestamp pour noms des fichiers créés

	public function __construct($page, $format)
	{
		$this->page = $page;
		$this->format = $format;
		$this->time = time();
		$this->makeFileList();
	}

	// tableaux des noms des fichiers
	public function makeFileList()
	{
		$this->fileList = glob('data/' . $this->page . '/' . $this->format .  '/*.' . $this->format);
		//$this->files = glob('*.' . $this->format);
	}
	/*public function makeFilePath()
	{}*/

	// nom d'un fichier
	public function findFileName($numArticle)
	{
		if($numArticle > 0)
		{
			$this->fileName = $this->fileList[$numArticle - 1];
		}
	}

	// GET

	// SET
	
	// 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 . '/' . $format .  '/' . $this->time . '.' . $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()
	{
		$i = 0;
		$articles = array();
		foreach ($this->fileList as $oneFile)
		{
			$articles[$i] = file_get_contents($oneFile);
			$i++;
		}
		//print_r($articles);
		return $articles;
	}
	public function readOne()
	{
		return(file_get_contents($this->fileName));
	}
	
	// update
	public function update($content)
	{
		$file = fopen($this->fileName, 'w');  // crée ou écrase
		fputs($file, $content);
		fclose($file);
		//chown($this->fileName, 'http');
	    chmod($this->fileName, 0666);
	}

	// delete
	public function delete()
	{
		unlink($this->fileName);
	}
}