-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardManager.php
More file actions
111 lines (93 loc) · 2.58 KB
/
CardManager.php
File metadata and controls
111 lines (93 loc) · 2.58 KB
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
<?php
// Card Manager
/**
*
*/
require_once 'BingoCard.php';
class cards {
public $cards;
}
class CardManager
{
protected $db;
function __construct()
{
$this->db = $this->dbConnect();
}
private function dbConnect() {
try {
$db = new PDO('mysql:host=localhost;dbname='.DB_NAME, DB_USERNAME, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
} catch (PDOException $e) {
//echo $e->getMessage();
return $e->getMessage();
}
}
// GET CARDS BY USER EMAIL
// Provided a user email (string)
// Return an array of cards;
public function getCards($email){
$sql = "SELECT `cards` from `cards` WHERE cards.userID=(SELECT `userID` FROM `users` WHERE users.email=".$this->db->quote($email).")";
$result = $this->db->query($sql);
$data = $result->fetch(PDO::FETCH_ASSOC);
if ($data && $data['cards']) {
//preDump($data);
return json_decode($data['cards']);
}
return array();
}
// ADD A NEW CARD FOR A USER
public function addCard($card, $email){
$cards = $this->getCards($email);
if($cards){
//$cards = json_decode($_cards);
array_push($cards, $card);
} else {
$cards = Array($card);
}
$this->setCards($cards, $email);
}
public function replace($cardNumber, $email){
if($cardNumber){
$cardNumber = intval($cardNumber);
if($cardNumber > 0 && $cardNumber < 4){
$cards = $this->getCards($email);
$cards[$cardNumber - 1] = new BingoCard();
$this->setCards($cards, $email);
}
}
}
// UPDATE THE CARDS JSON DATA FOR A USER
// @params $cards is a an object, with a key ['cards']
public function setCards($cards, $email){
$cards = json_encode($cards);
$_cards = $this->getCards($email);
if(!$_cards){
// INSERT INTO
$sql = "INSERT INTO `cards`(`userID`,`cards`) VALUES ((SELECT `userID` FROM `users` WHERE users.email=".$this->db->quote($email)."), ".$this->db->quote($cards).") ";
try {
$result = $this->db->query($sql);
} catch (PDOException $e) {
return $e->getMessage();
}
} else {
// UPDATE
$sql = "UPDATE `cards` SET cards.cards=".$this->db->quote($cards)." WHERE cards.userID=((SELECT `userID` FROM `users` WHERE users.email=".$this->db->quote($email).")) ";
try {
$result = $this->db->query($sql);
} catch (PDOException $e) {
return $e->getMessage();
}
}
}
public function deleteCards($email){
$sql = "DELETE FROM `cards` WHERE cards.userID=(SELECT `userID` FROM `users` WHERE users.email=".$this->db->quote($email).")";
try {
$result = $this->db->query($sql);
} catch (PDOException $e) {
return $e->getMessage();
}
}
}
?>