Newer
Older
Alex Barth
committed
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
// $Id$
/**
* @file
* FeedsUserProcessor class.
*/
/**
* Feeds processor plugin. Create users from feed items.
*/
class FeedsUserProcessor extends FeedsProcessor {
/**
* Implementation of FeedsProcessor::process().
*/
public function process(FeedsParserResult $parserResult, FeedsSource $source) {
// Count number of created and updated nodes.
$created = $updated = $failed = 0;
foreach ($parserResult->value['items'] as $item) {
if (!($uid = $this->existingItemId($item, $source)) || $this->config['update_existing']) {
// Map item to a term.
$account = $this->map($item);
// Check if user name and mail are set, otherwise continue.
if (empty($account->name) || empty($account->mail) || !valid_email_address($account->mail)) {
$failed++;
continue;
}
// Add term id if available.
if (!empty($uid)) {
$account->uid = $uid;
}
// Save the user.
// @todo: is this really the best way to use this function?
user_save($account, (array) $account);
if ($uid) {
$updated++;
}
else {
$created++;
}
}
}
// Set messages.
if ($failed) {
drupal_set_message(t('There were !number users that could not be imported because either their name or their email was empty or not valid. Check import data and mapping settings on User processor.', array('!number' => $failed)), 'error');
}
if ($created) {
drupal_set_message(t('Created !number users.', array('!number' => $created)));
}
elseif ($updated) {
drupal_set_message(t('Updated !number users.', array('!number' => $updated)));
}
else {
drupal_set_message(t('There are no new users.'));
}
}
/**
* Implement clear.
*
* @param $source
* FeedsSource of this term. FeedsTermProcessor does not heed this
* parameter, it deletes all terms from a vocabulary.
*/
public function clear(FeedsSource $source) {
// Do not support deleting users as we have no way of knowing which ones we
// imported.
throw new Exception(t('User processor does not support deleting users.'));
}
/**
* Execute mapping on an item.
*/
protected function map($source_item) {
// Prepare term object.
$target_account = new stdClass();
$target_account->uid = 0;
$target_account->roles = $this->config['roles'];
$target_account->status = $this->config['status'];
// Have parent class do the iterating.
return parent::map($source_item, $target_account);
}
/**
* Override parent::configDefaults().
*/
public function configDefaults() {
return array(
'roles' => array(),
'update_existing' => FALSE,
'status' => 1,
'mappings' => array(),
);
}
/**
* Override parent::configForm().
*/
public function configForm(&$form_state) {
$form = array();
$form['status'] = array(
'#type' => 'radios',
'#title' => t('Status'),
'#description' => t('Select whether users should be imported active or blocked.'),
'#options' => array(0 => t('Blocked'), 1 => t('Active')),
'#default_value' => $this->config['status'],
);
$roles = user_roles(TRUE);
unset($roles[2]);
if (count($roles)) {
$form['roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Additional roles'),
'#description' => t('Every user will be assigned the "autenticated user" role. Select additional roles here.'),
'#default_value' => $this->config['roles'],
'#options' => $roles,
);
}
$form['update_existing'] = array(
'#type' => 'checkbox',
'#title' => t('Update existing users'),
'#description' => t('Check if existing users should be updated from imported data.'),
'#default_value' => $this->config['update_existing'],
);
return $form;
}
/**
* Set target element.
*/
public function setTargetElement(&$target_item, $target_element, $value) {
$target_item->$target_element = $value;
}
/**
* Return available mapping targets.
*/
public function getMappingTargets() {
$targets = array(
'name' => array(
'name' => t('User name'),
'optional_unique' => TRUE,
),
'mail' => array(
'name' => t('E-mail address'),
'optional_unique' => TRUE,
),
'created' => array(
'name' => t('Created date'),
),
);
return $targets;
}
/**
* Get id of an existing feed item term if available.
*/
protected function existingItemId($source_item, FeedsSource $source) {
// Iterate through all unique targets and try to find a user for the
// target's value.
foreach ($this->uniqueTargets($source_item) as $target => $value) {
switch ($target) {
case 'name':
$nid = db_result(db_query('SELECT uid FROM {users} WHERE name = "%s"', $value));
break;
case 'mail':
$nid = db_result(db_query('SELECT uid FROM {users} WHERE mail = "%s"', $value));
break;
}
if ($nid) {
// Return with the first nid found.
return $nid;
}
}
return 0;
}
}