|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* BB's Zend Framework 2 Components |
|
4
|
|
|
* |
|
5
|
|
|
* AdminModule |
|
6
|
|
|
* |
|
7
|
|
|
* @package [MyApplication] |
|
8
|
|
|
* @package BB's Zend Framework 2 Components |
|
9
|
|
|
* @package AdminModule |
|
10
|
|
|
* @author Björn Bartels <[email protected]> |
|
11
|
|
|
* @link https://gitlab.bjoernbartels.earth/groups/zf2 |
|
12
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 |
|
13
|
|
|
* @copyright copyright (c) 2016 Björn Bartels <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Admin\Model; |
|
17
|
|
|
|
|
18
|
|
|
use Zend\Db\TableGateway\TableGateway; |
|
19
|
|
|
use Admin\Model\User; |
|
20
|
|
|
use Zend\Db\Sql\Select; |
|
21
|
|
|
|
|
22
|
|
|
class UserTable |
|
23
|
|
|
{ |
|
24
|
|
|
protected $tableGateway; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(TableGateway $tableGateway) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->setTableGateway($tableGateway); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function fetchAll() |
|
32
|
|
|
{ |
|
33
|
|
|
$resultSet = $this->tableGateway->select(); |
|
34
|
|
|
return $resultSet; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getUserByEmailOrUsername( $id ) |
|
38
|
|
|
{ |
|
39
|
|
|
if (empty($id) ) { |
|
40
|
|
|
return false; |
|
41
|
|
|
} |
|
42
|
|
|
$resultSet = $this->tableGateway->select( |
|
43
|
|
|
function (Select $select) use ($id) { |
|
44
|
|
|
$select->where( |
|
45
|
|
|
array( |
|
46
|
|
|
"username = '".$id."'", |
|
47
|
|
|
"email = '".$id."'" |
|
48
|
|
|
), |
|
49
|
|
|
'OR' |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
); |
|
53
|
|
|
$user = $resultSet->current(); |
|
54
|
|
|
if (!$user) { |
|
55
|
|
|
throw new \Exception("Could not find user with email or username '$id'"); |
|
56
|
|
|
} |
|
57
|
|
|
return $user; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getUser($id) |
|
61
|
|
|
{ |
|
62
|
|
|
$id = (int) $id; |
|
63
|
|
|
$rowset = $this->tableGateway->select(array('user_id' => $id)); |
|
64
|
|
|
$row = $rowset->current(); |
|
65
|
|
|
if (!$row) { |
|
66
|
|
|
throw new \Exception("Could not find row $id"); |
|
67
|
|
|
} |
|
68
|
|
|
return $row; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function saveUser(User $user) |
|
72
|
|
|
{ |
|
73
|
|
|
$data = array( |
|
74
|
|
|
'display_name' => $user->display_name, |
|
75
|
|
|
'username' => $user->username, |
|
76
|
|
|
'email' => $user->email, |
|
77
|
|
|
'password' => $user->password, |
|
78
|
|
|
'state' => $user->state, |
|
79
|
|
|
'aclrole' => $user->aclrole, |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$id = (int)$user->user_id; |
|
83
|
|
|
if ($id == 0) { |
|
84
|
|
|
$this->tableGateway->insert($data); |
|
85
|
|
|
} else { |
|
86
|
|
|
if ($this->getUser($id)) { |
|
87
|
|
|
$this->tableGateway->update($data, array('user_id' => $id)); |
|
88
|
|
|
} else { |
|
89
|
|
|
throw new \Exception('Form id does not exist'); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function deleteUser($id) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->tableGateway->delete(array('user_id' => $id)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getTableGateway() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->tableGateway; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function setTableGateway(TableGateway $tableGateway) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->tableGateway = $tableGateway; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
} |