1
|
|
|
<?php |
2
|
|
|
namespace Kineo\Model; |
3
|
|
|
|
4
|
|
|
use Kineo\Component\Database; |
5
|
|
|
|
6
|
|
|
class UserModel extends BaseModel |
7
|
|
|
{ |
8
|
|
|
protected $db; |
9
|
|
|
|
10
|
|
|
public $id; |
11
|
|
|
|
12
|
|
|
public $email; |
13
|
|
|
|
14
|
|
|
public $first_name; |
15
|
|
|
|
16
|
|
|
public $surname; |
17
|
|
|
|
18
|
|
|
public $voting; |
19
|
|
|
|
20
|
|
|
public $constituency_id; |
21
|
|
|
|
22
|
|
|
public function __construct(Database $db, $email = null) |
23
|
|
|
{ |
24
|
|
|
$this->db = $db; |
25
|
|
|
|
26
|
|
|
if(isset($email)) { |
27
|
|
|
$this->loadUserByEmail($email); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function loadUserByEmail($email) |
32
|
|
|
{ |
33
|
|
|
$stmt = $this->db->prepare( |
34
|
|
|
'SELECT * FROM `tblUsers` AS a |
35
|
|
|
LEFT JOIN `tblConstituencies` AS b |
36
|
|
|
ON a.constituency_id = b.id |
37
|
|
|
WHERE email = :email' |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$stmt->bindValue(':email', $email, \PDO::PARAM_STR); |
41
|
|
|
|
42
|
|
|
$stmt->execute(); |
43
|
|
|
|
44
|
|
|
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
45
|
|
|
|
46
|
|
|
// Hydrate model |
47
|
|
|
if(isset($result[0])) { |
48
|
|
|
foreach($result[0] as $key => $val) { |
49
|
|
|
$this->{$key} = $val; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return true; |
53
|
|
|
} else { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function saveUser($email, $firstName, $surname, $constituencyId, $voting = false, $candidateId = null) |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
|
|
$this->db->beginTransaction(); |
62
|
|
|
|
63
|
|
|
// Statement 1 - Save user |
64
|
|
|
$stmt = $this->db->prepare( |
65
|
|
|
'INSERT INTO `tblUsers` (email, first_name, surname, constituency_id, voting) |
66
|
|
|
VALUES (:email, :first_name, :surname, :constituency_id, :voting)' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$stmt->bindValue(':email', $email, \PDO::PARAM_STR); |
70
|
|
|
$stmt->bindValue(':first_name', $firstName, \PDO::PARAM_STR); |
71
|
|
|
$stmt->bindValue(':surname', $surname, \PDO::PARAM_STR); |
72
|
|
|
$stmt->bindValue(':constituency_id', $constituencyId, \PDO::PARAM_INT); |
73
|
|
|
$stmt->bindValue(':voting', $voting, \PDO::PARAM_BOOL); |
74
|
|
|
$stmt->execute(); |
75
|
|
|
|
76
|
|
|
if(!$voting) { |
77
|
|
|
$this->db->commit(); |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$userId = $this->db->lastInsertId(); |
82
|
|
|
|
83
|
|
|
// Statement 2 - Save voting intention |
84
|
|
|
$stmt2 = $this->db->prepare( |
85
|
|
|
'INSERT INTO `tblVotes` (user_id, candidate_id) |
86
|
|
|
VALUES (:user_id, :candidate_id)' |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$stmt2->bindValue(':user_id', $userId, \PDO::PARAM_INT); |
90
|
|
|
$stmt2->bindValue(':candidate_id', $candidateId, \PDO::PARAM_INT); |
91
|
|
|
$stmt2->execute(); |
92
|
|
|
|
93
|
|
|
$this->db->commit(); |
94
|
|
|
|
95
|
|
|
return $userId; |
96
|
|
|
} catch(\PDOException $e) { |
97
|
|
|
$this->db->rollback(); |
98
|
|
|
|
99
|
|
|
throw new \Exception('There was a problem saving this user: ' . $e->getMessage()); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |