-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserActiveRecordModel.php
135 lines (110 loc) · 2.68 KB
/
UserActiveRecordModel.php
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Peto16\User;
use \Anax\Database\ActiveRecordModel;
use \Peto16\User\UserStorageInterface;
/**
* Class to handle a user.
*/
class UserActiveRecordModel extends ActiveRecordModel implements UserStorageInterface
{
/**
* @var string $tableName name of the database table.
*/
protected $tableName = "ramverk1_User";
/**
* Columns in the table.
*
* @var integer $id primary key auto incremented.
*/
public $id;
public $username;
public $password;
public $email;
public $firstname;
public $lastname;
public $administrator;
public $enabled;
public $deleted;
/**
* Create user.
*
* @param User $user a user object.
*
* @return void
*/
public function createUser(User $user)
{
$this->setUserData($user);
$this->save();
}
/**
* Update user.
*
* @param User $user User object.
*
* @return void
*/
public function updateUser(User $user)
{
$this->find("id", $user->id);
$this->setUserData($user);
$this->save();
}
/**
* Delete user.
*
* @param integer $id integer for userid.
*
* @return void
*/
public function deleteUser($id)
{
$user = $this->find("id", $id);
$this->id = $user->id;
date_default_timezone_set("Europe/Stockholm");
$this->deleted = date("Y-m-d H:i:s");
$this->save();
}
/**
* Dynamicly set user properties to its value.
*
* @param User $user user object
*/
public function setUserData(User $user)
{
$userVarArray = get_object_vars($user);
foreach ($userVarArray as $key => $value) {
if ($value !== null) {
$this->{$key} = $value;
}
}
}
/**
* Dynamicly get user by field.
*
* @param string $field Fieldname to search.
*
* @param mixed $data Data to search for in the field.
*
* @return User Returns a user.
*/
public function getUserByField($field, $data)
{
return $this->find($field, $data);
}
/**
* Returns all users stored and that are not deleted.
*
* @return array Array with all users.
*/
public function findAllUsers()
{
$this->checkDb();
return $this->db->connect()
->select()
->from($this->tableName)
->where("deleted IS NULL")
->execute()
->fetchAllClass(get_class($this));
}
}