forked from projectsend/projectsend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.usermeta.php
152 lines (123 loc) · 4.13 KB
/
functions.usermeta.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
function user_meta_exists($user_id = null, $meta_name = null)
{
global $dbh;
if (empty($user_id) or empty($meta_name)) {
return false;
}
if (!is_numeric($user_id)) {
return false;
}
$statement = $dbh->prepare( "SELECT id FROM " . TABLE_USER_META . " WHERE user_id=:user_id AND name=:name");
$statement->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$statement->bindParam(':name', $meta_name);
$statement->execute();
$count = $statement->rowCount();
if ($count > 0) {
return true;
}
return false;
}
function get_user_meta($user_id = null, $meta_name = null)
{
global $dbh;
if (empty($user_id) or empty($meta_name)) {
return false;
}
if (!is_numeric($user_id)) {
return false;
}
$statement = $dbh->prepare( "SELECT * FROM " . TABLE_USER_META . " WHERE user_id=:user_id AND name=:name");
$statement->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$statement->bindParam(':name', $meta_name);
$statement->execute();
$count = $statement->rowCount();
if ($count > 0) {
while ( $row = $statement->fetch() ) {
$meta = array(
'id' => html_output($row['id']),
'name' => html_output($row['name']),
'value' => html_output($row['value']),
'created_date' => html_output($row['timestamp']),
'updated_at' => html_output($row['updated_at']),
);
return $meta;
}
}
return null;
}
function get_all_user_meta($user_id = null)
{
global $dbh;
if (empty($user_id)) {
return false;
}
if (!is_numeric($user_id)) {
return false;
}
$return = [];
$statement = $dbh->prepare( "SELECT * FROM " . TABLE_USER_META . " WHERE user_id=:user_id");
$statement->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$statement->execute();
$count = $statement->rowCount();
if ($count > 0) {
while ( $row = $statement->fetch() ) {
$return[] = array(
'id' => html_output($row['id']),
'name' => html_output($row['name']),
'value' => html_output($row['value']),
'created_date' => html_output($row['timestamp']),
'updated_at' => html_output($row['updated_at']),
);
}
return $return;
}
return null;
}
/**
* Save a value to the database
*
* @param int $user_id
* @param string $meta_name
* @param boolean $update_if_exists Update an existing meta row instead of creating a new one
* @return bool
*/
function save_user_meta($user_id = null, $meta_name = null, $meta_value = null, $update_if_exists = false)
{
global $dbh;
if (empty($user_id) or empty($meta_name)) {
return false;
}
if (!is_numeric($user_id)) {
return false;
}
if ($update_if_exists == true)
{
if (user_meta_exists($user_id, $meta_name)) {
$statement = $dbh->prepare( "UPDATE " . TABLE_USER_META . " SET value=:value, updated_at=NOW() WHERE user_id=:user_id AND name=:name" );
$statement->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$statement->bindValue(':name', $meta_name);
$statement->bindValue(':value', $meta_value);
return $statement->execute();
}
}
$statement = $dbh->prepare("INSERT INTO " . TABLE_USER_META . " (user_id, name, value) VALUES (:user_id, :name, :value)");
$statement->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$statement->bindValue(':name', $meta_name);
$statement->bindValue(':value', $meta_value);
return $statement->execute();
}
function delete_user_meta($user_id = null, $meta_name = null)
{
global $dbh;
if (empty($user_id) or empty($meta_name)) {
return false;
}
if (!is_numeric($user_id)) {
return false;
}
$statement = $dbh->prepare( "DELETE FROM " . TABLE_USER_META . " WHERE user_id=:user_id AND name=:name");
$statement->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$statement->bindParam(':name', $meta_name);
return $statement->execute();
}