BlogManager.php 2.55 KB
Newer Older
1 2 3
<?php
class BlogManager extends Base
{
4 5 6 7 8
	private $time;
	private $title;
	private $content;
	private $shortContent;
	private $id;
Corentin Bettiol's avatar
Corentin Bettiol committed
9

10
	function __construct($time, $title, $content, $shortContent, $id){
11 12 13 14 15
		$this->time = htmlspecialchars($time); 
		$this->title = htmlspecialchars($title);
		$this->content = htmlspecialchars($content);
		$this->shortContent = htmlspecialchars($shortContent);
		$this->id = htmlspecialchars($id);
16

17
	}
18

19 20 21 22
	public function getPage($page)
	{
		$start = $page*5;
		$stop = $start + 5;
23

24 25
		$db = $this->dbConnect();
		$req = $db->query('SELECT * FROM l3m_blog ORDER BY id DESC LIMIT '. $start .', '. $stop .'');
26

27 28
		return $req;
	}
29

30 31 32
	public function get($link)
	{
		$link = htmlspecialchars($link);
33

34
		$db = $this->dbConnect();
35 36
		$req = $db->prepare('SELECT * FROM l3m_blog WHERE slug = ?');
		$req->execute([$link]);
37 38 39 40 41 42 43 44 45 46 47 48 49
		$post = $req->fetch();

		if($post == false)
			return false;

		$post['datetime'] = new DateTime($post['time']);

		return $post;
	}

	// todo vérifier qu'on poste pas un truc vide
	public function update(){
		$db = $this->dbConnect();
Corentin Bettiol's avatar
Corentin Bettiol committed
50
		$req = $db->prepare('UPDATE l3m_blog SET time = :time, title = :title, content = :content, shortContent = :shortContent WHERE id = :id');
51
		$req->execute([
52 53 54 55 56
			'time' => $this->time,
			'title' => $this->title,
			'content' => $this->content,
			'shortContent' => $this->shortContent,
			'id' => $this->id
57
		]);
58 59 60
	}

	public function send(){
61
		if($this->time == ''){
62 63 64 65
			$this->time = new datetime();
			$this->time = $this->time->format('Y-m-d H:i:s');
		}
		if(strlen($this->title) < 1){
66
			header('Location:/patate');
67 68 69
			exit();
		}
		if(strlen($this->shortContent) < 1){
70
			$this->shortContent = 'Ce billet de blog ne dispose d\'aucune description.';
71
		}
72 73 74

		$this->slug = $this->slugify($this->title);

75
		$db = $this->dbConnect();
76 77 78 79 80 81 82 83 84 85 86 87 88
		$i = 1;
		$slug = $this->slug;
		do{
			$req = $db->prepare('SELECT slug FROM l3m_blog WHERE slug = ?');
			$req->execute([$this->slug]);
			$slugExist = $req->fetch();
			if($slugExist == true){
				$i++;
				$this->slug = $slug . "-" . $i;
			}
		}
		while($slugExist != false);

Corentin Bettiol's avatar
Corentin Bettiol committed
89
		$req = $db->prepare('INSERT INTO l3m_blog(time, title, slug, content, shortContent) VALUES(:time, :title, :slug, :content, :shortContent)');
90
		$req->execute([
91 92
			'time' => $this->time,
			'title' => $this->title,
93
			'slug' => $this->slug,
94
			'content' => $this->content,
95
			'shortContent' => $this->shortContent
96
		]);
97 98 99 100
	}


	public function delete($link){
101
		$link = htmlspecialchars($link);
102 103

		$db = $this->dbConnect();
104 105
		$req = $db->prepare('DELETE FROM l3m_blog WHERE slug = ?');
		$req->execute([$link]);
106
	}
107
}