-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserUpdate.php
147 lines (123 loc) · 5.45 KB
/
UserUpdate.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
<?php
/**
* User class
*
* Here is just one method that shows algorithm for updating user. Might look dirty
* but works as needed. Of corse, there are some things, that I don't like, so I
* hope that client gives more support for upgrades and tuning.
*
* @copyright 2012 Roberts Rozkalns
* @version Release: @1.3.2@
* @since Class available since Release 0.1
*/
class User {
/**
* Update User data
*
* @param array $data Array of data that should be updated
* @param integer $id The ID of user, that Admin might change
* @return bool
*/
function update_member($data, $id = NULL) {
// check if method caller is logged in
if (!is_logged_in()) {
return false;
}
// Input names in front end are called different than in database
$names = array(
'id' => 'id',
'user_pass' => 'password',
'user_email' => 'email',
'user_registered' => 'time_register',
'user_activation_key' => 'activation_key',
'user_status' => 'status',
'user_display_name' => 'full_name',
'user_name' => 'name',
'user_surname' => 'surname',
'user_gender' => 'gender',
'user_phone' => 'mobile',
'input_location_city' => 'city',
'input_location_address' => 'street',
'input_coordinates' => 'coordinates',
'user_image' => 'image',
'user_work' => 'work',
'user_study' => 'study',
'user_exp' => 'experience',
'user_role' => 'role',
'user_last_activity' => 'last_activity'
);
// Admin might call this method and edit data of other user than himself
if ($id) {
$current_user = user($id);
} else {
$current_user = current_user();
}
// Fields that sould be ignored and not troubled
$ignore_fields = array('user_save', 'user_pass_check', 'user_pass_re', 'user_terms');
// Fields that are not stored in users metadata table
$meta_ignore_fields = array('user_email', 'user_pass');
// Fields that are stored in main database
$main_update_fields = array('user_pass', 'user_email', 'user_name', 'user_surname');
// Store fields in array that sould be updated
$main_update = array();
//$n = 0;
foreach ($data as $k => $v) {
// Skip fields that should be ignored
if (in_array($k, $ignore_fields)) {
continue;
}
// Fields that are in main table and pending for update procedure
if (in_array($k, $main_update_fields)) {
$main_update[$k] = $v;
}
// Skip fields that should be ignored for meta table
if (in_array($k, $meta_ignore_fields)) {
continue;
}
// User object consist of variables and data and are checked against
// stored information in database between given information from
// form
// In this step we insert, update or delete Meta data
// If user has something stored in DB
if ($current_user->$names[$k] != NULL) {
// If new form information is different
if ($current_user->$names[$k] != $v) {
// If given information is empty
if ($v == "") {
// Then delete information from DB with this key
$this->delete_member_meta($current_user->id, array($k => $v));
} else {
// Or is different, then find and update DB with this key
$this->update_member_meta($current_user->id, array($k => $v));
}
}
}
// User dont have anything stored with this key
else {
// Make sure if given input is not empty
if ($v != "") {
// Then insert information in DB with this key
$this->insert_member_meta($current_user->id, array($k => $v));
}
}
}
// In this step we insert, update or delete Main data
// Check if Main update array is filled
if (!empty($main_update)) {
// Update display Name
if (isset($main_update['user_name']) && isset($main_update['user_surname']) && $current_user->full_name != $main_update['user_name'] . " " . $main_update['user_surname']) {
$this->update_member_main($current_user->id, array('user_display_name' => $main_update['user_name'] . " " . $main_update['user_surname']));
}
// Update user Password
if (isset($main_update['user_pass']) && $current_user->password != $this->hash($main_update['user_pass'])) {
$this->update_member_main($current_user->id, array('user_pass' => $this->hash($main_update['user_pass'])));
}
// Update user email and update at the same time Session data, to keep user logged
if (isset($main_update['user_email']) && $current_user->email != $data['user_email']) {
$this->update_member_main($current_user->id, array('user_email' => $main_update['user_email']));
$this->session->set_userdata(array('user_email' => $data['user_email']));
}
}
return false;
}
}