db.php 2.53 KB
Newer Older
stc's avatar
stc committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php

class DB {

	private $conn;

	function __construct () {
		include 'connexion.php';
		try {
			$this->conn = new PDO('pgsql:host=localhost;port=5432;dbname=apisub', $db_user, $db_pass);
		} catch (PDOException $e) {
			die('Connection failed: ' . $e->getMessage());
		}
	}

16
	public function printHtmlBegin () {
stc's avatar
stc committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
		echo '<html>';
		echo '<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head>';
		echo '<title>Api Sub</title>';
		echo '<link href="css/main.css" type="text/css" rel="stylesheet"/>';
		echo '<body>';
		echo '<h1>Inscriptions Api</h1>';
	}

	function printUser($utclogin, $surname, $firstname) {
		echo '<p><i>'.$firstname.' '. $surname.'</i> (<b>'.$utclogin.'</b>)</p>';
	}

	function subList($utclogin) {
		$sql = "SELECT code, name, year, semester, TO_CHAR(subdate,'DD/MM/YYYY') as subdate
						FROM api JOIN subscribe ON api.code=subscribe.api
32
						WHERE	utclogin='".$utclogin."'";
stc's avatar
stc committed
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
		$st = $this->conn->prepare($sql);
		$st->execute();
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
		$st = $this->conn->prepare($sql);
		$st->execute();
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
		if ($res) {
			echo '<h2>Vos inscriptions</h2>';
			foreach ($res as $row) {
				echo '<p>';
				echo ' <a href="index.php?action=unsub&api='.$row['code'].'&utclogin='.$utclogin.'">[désinscription]</a> ';
				echo $row['code']."-".$row['name']." (".$row['semester'].$row['year'].") [le ".$row['subdate']."]";
				echo '</p>';
			}
		}
		else {
			echo '<h2>Aucune inscription</h2>';
		}
	}

	function apiList($utclogin, $semester, $year) {
		$sql = "SELECT code, name
						FROM api
						WHERE semester='".$semester."' AND year=".$year."
						ORDER BY code, name";
		$st = $this->conn->prepare($sql);
		$st->execute();
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
		$st = $this->conn->prepare($sql);
		$st->execute();
		$res = $st->fetchAll(PDO::FETCH_ASSOC);
		if ($res) {
			echo '<h2>Liste des Api '.$semester.$year.'</h2>';
			foreach ($res as $row) {
				echo '<p>';
				echo ' <a href="index.php?action=sub&api='.$row['code'].'&utclogin='.$utclogin.'">[inscription]</a> ';
				echo $row['code']."-".$row['name'];
				echo '</p>';
			}
		}
		else {
			echo '<h2>Aucune inscription</h2>';
		}
	}

	function subToApi($utclogin, $api) {
		$today = date("Ymd");
		$sql = "INSERT INTO subscribe VALUES ('".$utclogin."',".$api.",TO_DATE('".$today."','YYYYMMDD'))";
		$res=$this->conn->exec($sql);
	}

	function unsubToApi($utclogin, $api) {
85
		$sql = "DELETE FROM subscribe WHERE utclogin='".$utclogin."' AND api=".$api;
stc's avatar
stc committed
86
87
88
89
90
		$res=$this->conn->exec($sql);
	}


}