|
1
|
|
|
<?php |
|
2
|
|
|
namespace ModPleio; |
|
3
|
|
|
|
|
4
|
|
|
class Import { |
|
5
|
|
|
public static function run($csv_location, $columns, \ElggUser $initiator) { |
|
6
|
|
|
global $CONFIG; |
|
7
|
|
|
|
|
8
|
|
|
$site = elgg_get_site_entity(); |
|
9
|
|
|
$profile_fields = get_config("profile_fields"); |
|
10
|
|
|
|
|
11
|
|
|
$fh = fopen($csv_location, "r"); |
|
12
|
|
|
if (!$fh) { |
|
13
|
|
|
throw new Exception("Invalid import file"); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
$stats = [ |
|
17
|
|
|
"created" => 0, |
|
18
|
|
|
"updated" => 0, |
|
19
|
|
|
"error" => 0 |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
$skip_first_line = true; |
|
23
|
|
|
while (($data = fgetcsv($fh, 0, ";")) !== false) { |
|
24
|
|
|
if ($skip_first_line) { |
|
25
|
|
|
$skip_first_line = false; |
|
26
|
|
|
continue; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
$data = Import::extractData($columns, $data); |
|
30
|
|
|
|
|
31
|
|
|
$user = Import::getUserByAttributes($data); |
|
32
|
|
|
if (!$user) { |
|
33
|
|
|
$user = Import::registerUser($data); |
|
34
|
|
|
if ($user) { |
|
35
|
|
|
$stats["created"] += 1; |
|
36
|
|
|
} else { |
|
37
|
|
|
$stats["error"] += 1; |
|
38
|
|
|
} |
|
39
|
|
|
} else { |
|
40
|
|
|
$stats["updated"] += 1; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if (!$user) { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($data["email"] && $user->email !== $data["email"]) { |
|
48
|
|
|
$user->email = $data["email"]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($data["name"] && $user->name !== $data["name"]) { |
|
52
|
|
|
$user->name = $data["name"]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
foreach ($columns as $id => $metadata_name) { |
|
56
|
|
|
$value = $data[$metadata_name]; |
|
57
|
|
|
if (empty($value)) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (!array_key_exists($metadata_name, $profile_fields)) { |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$field_type = $profile_fields[$metadata_name]; |
|
66
|
|
|
|
|
67
|
|
|
if ($profile_fields[$metadata_name] == "tags") { |
|
68
|
|
|
elgg_delete_metadata([ |
|
69
|
|
|
"guid" => $user->guid, |
|
70
|
|
|
"metadata_name" => $metadata_name, |
|
71
|
|
|
"limit" => 0 |
|
72
|
|
|
]); |
|
73
|
|
|
|
|
74
|
|
|
foreach (string_to_tag_array($value) as $v) { |
|
75
|
|
|
create_metadata($user->guid, $metadata_name, $v, "", $user->guid, get_default_access(), true, $site->guid); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} else { |
|
78
|
|
|
create_metadata($user->guid, $metadata_name, $value, "", $user->guid, get_default_access(), false, $site->guid); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$user->save(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
unlink($csv_location); |
|
86
|
|
|
|
|
87
|
|
|
return $stats; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function extractData($columns, $data) { |
|
91
|
|
|
$result = []; |
|
92
|
|
|
foreach ($columns as $id => $metadata_name) { |
|
93
|
|
|
if (!$metadata_name) { |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$result[$metadata_name] = $data[$id]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $result; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function registerUser($data) { |
|
104
|
|
|
if (!$data["name"] || !$data["email"]) { |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$user = false; |
|
109
|
|
|
|
|
110
|
|
|
try { |
|
111
|
|
|
$username = Import::generateUniqueUsername($data); |
|
112
|
|
|
$password = generate_random_cleartext_password(); |
|
113
|
|
|
$guid = register_user($username, $password, $data["name"], $data["email"]); |
|
114
|
|
|
elgg_set_user_validation_status($guid, true, "email"); |
|
|
|
|
|
|
115
|
|
|
$user = get_entity($guid); |
|
|
|
|
|
|
116
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
117
|
|
|
elgg_log("Could not register user " . $e->getMessage(), "ERROR"); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $user; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function generateUniqueUsername($data) { |
|
124
|
|
|
$email = $data["email"]; |
|
125
|
|
|
list($name, $email) = explode("@", $email); |
|
|
|
|
|
|
126
|
|
|
$name = preg_replace("/[^a-zA-Z0-9\.]+/", "", trim($name)); |
|
127
|
|
|
|
|
128
|
|
|
while (strlen($name) < 4) { |
|
129
|
|
|
$name .= "0"; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$hidden = access_show_hidden_entities(true); |
|
133
|
|
|
|
|
134
|
|
View Code Duplication |
if (get_user_by_username($name)) { |
|
135
|
|
|
$i = 1; |
|
136
|
|
|
|
|
137
|
|
|
while (get_user_by_username($name . $i)) { |
|
138
|
|
|
$i++; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$result = $name . $i; |
|
142
|
|
|
} else { |
|
143
|
|
|
$result = $name; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
access_show_hidden_entities($hidden); |
|
147
|
|
|
|
|
148
|
|
|
return $result; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getUserByAttributes($data) { |
|
152
|
|
|
$user = false; |
|
153
|
|
|
|
|
154
|
|
|
if ($data["guid"]) { |
|
155
|
|
|
$user = get_entity($data["guid"]); |
|
156
|
|
|
} elseif ($data["username"]) { |
|
157
|
|
|
$user = get_user_by_username($data["username"]); |
|
158
|
|
|
} elseif ($data["email"]) { |
|
159
|
|
|
$users = get_user_by_email($data["email"]); |
|
160
|
|
|
$user = $users[0]; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return $user; |
|
164
|
|
|
} |
|
165
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.