1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * FeedsUserProcessor class. |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Feeds processor plugin. Create users from feed items. |
---|
10 | */ |
---|
11 | class FeedsUserProcessor extends FeedsProcessor { |
---|
12 | |
---|
13 | /** |
---|
14 | * Implementation of FeedsProcessor::process(). |
---|
15 | */ |
---|
16 | public function process(FeedsImportBatch $batch, FeedsSource $source) { |
---|
17 | |
---|
18 | // Count number of created and updated nodes. |
---|
19 | $created = $updated = $failed = 0; |
---|
20 | |
---|
21 | while ($item = $batch->shiftItem()) { |
---|
22 | |
---|
23 | if (!($uid = $this->existingItemId($batch, $source)) || $this->config['update_existing']) { |
---|
24 | |
---|
25 | // Map item to a term. |
---|
26 | $account = $this->map($batch); |
---|
27 | |
---|
28 | // Check if user name and mail are set, otherwise continue. |
---|
29 | if (empty($account->name) || empty($account->mail) || !valid_email_address($account->mail)) { |
---|
30 | $failed++; |
---|
31 | continue; |
---|
32 | } |
---|
33 | |
---|
34 | // Add term id if available. |
---|
35 | if (!empty($uid)) { |
---|
36 | $account->uid = $uid; |
---|
37 | } |
---|
38 | |
---|
39 | // Save the user. |
---|
40 | user_save($account, (array) $account); |
---|
41 | if ($account->uid && !empty($account->openid)) { |
---|
42 | $authmap = array( |
---|
43 | 'uid' => $account->uid, |
---|
44 | 'module' => 'openid', |
---|
45 | 'authname' => $account->openid, |
---|
46 | ); |
---|
47 | if (SAVED_UPDATED != drupal_write_record('authmap', $authmap, array('uid', 'module'))) { |
---|
48 | drupal_write_record('authmap', $authmap); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | if ($uid) { |
---|
53 | $updated++; |
---|
54 | } |
---|
55 | else { |
---|
56 | $created++; |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | // Set messages. |
---|
62 | if ($failed) { |
---|
63 | drupal_set_message( |
---|
64 | format_plural( |
---|
65 | $failed, |
---|
66 | 'There was @number user 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.', |
---|
67 | '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.', |
---|
68 | array('@number' => $failed) |
---|
69 | ), |
---|
70 | 'error' |
---|
71 | ); |
---|
72 | } |
---|
73 | if ($created) { |
---|
74 | drupal_set_message(format_plural($created, 'Created @number user.', 'Created @number users.', array('@number' => $created))); |
---|
75 | } |
---|
76 | elseif ($updated) { |
---|
77 | drupal_set_message(format_plural($updated, 'Updated @number user.', 'Updated @number users.', array('@number' => $updated))); |
---|
78 | } |
---|
79 | else { |
---|
80 | drupal_set_message(t('There are no new users.')); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Implementation of FeedsProcessor::clear(). |
---|
86 | */ |
---|
87 | public function clear(FeedsBatch $batch, FeedsSource $source) { |
---|
88 | // Do not support deleting users as we have no way of knowing which ones we |
---|
89 | // imported. |
---|
90 | throw new Exception(t('User processor does not support deleting users.')); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Execute mapping on an item. |
---|
95 | */ |
---|
96 | protected function map(FeedsImportBatch $batch, $target_account = NULL) { |
---|
97 | // Prepare user account object. |
---|
98 | if (empty($target_account)) { |
---|
99 | $target_account = new stdClass(); |
---|
100 | } |
---|
101 | //comentada para que se inserte los usuarios |
---|
102 | //$target_account->uid = 0; |
---|
103 | $target_account->roles = array_filter($this->config['roles']); |
---|
104 | $target_account->status = $this->config['status']; |
---|
105 | |
---|
106 | // Have parent class do the iterating. |
---|
107 | return parent::map($batch, $target_account); |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Override parent::configDefaults(). |
---|
112 | */ |
---|
113 | public function configDefaults() { |
---|
114 | return array( |
---|
115 | 'roles' => array(), |
---|
116 | 'update_existing' => FALSE, |
---|
117 | 'status' => 1, |
---|
118 | 'mappings' => array(), |
---|
119 | ); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Override parent::configForm(). |
---|
124 | */ |
---|
125 | public function configForm(&$form_state) { |
---|
126 | $form = array(); |
---|
127 | |
---|
128 | $form['status'] = array( |
---|
129 | '#type' => 'radios', |
---|
130 | '#title' => t('Status'), |
---|
131 | '#description' => t('Select whether users should be imported active or blocked.'), |
---|
132 | '#options' => array(0 => t('Blocked'), 1 => t('Active')), |
---|
133 | '#default_value' => $this->config['status'], |
---|
134 | ); |
---|
135 | |
---|
136 | $roles = user_roles(TRUE); |
---|
137 | unset($roles[2]); |
---|
138 | if (count($roles)) { |
---|
139 | $form['roles'] = array( |
---|
140 | '#type' => 'checkboxes', |
---|
141 | '#title' => t('Additional roles'), |
---|
142 | '#description' => t('Every user is assigned the "authenticated user" role. Select additional roles here.'), |
---|
143 | '#default_value' => $this->config['roles'], |
---|
144 | '#options' => $roles, |
---|
145 | ); |
---|
146 | } |
---|
147 | // @todo Implement true updating. |
---|
148 | $form['update_existing'] = array( |
---|
149 | '#type' => 'checkbox', |
---|
150 | '#title' => t('Replace existing users'), |
---|
151 | '#description' => t('If an existing user is found for an imported user, replace it. Existing users will be determined using mappings that are a "unique target".'), |
---|
152 | '#default_value' => $this->config['update_existing'], |
---|
153 | ); |
---|
154 | return $form; |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * Set target element. |
---|
159 | */ |
---|
160 | public function setTargetElement(&$target_item, $target_element, $value) { |
---|
161 | $target_item->$target_element = $value; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Return available mapping targets. |
---|
166 | */ |
---|
167 | public function getMappingTargets() { |
---|
168 | $targets = array( |
---|
169 | 'name' => array( |
---|
170 | 'name' => t('User name'), |
---|
171 | 'description' => t('Name of the user.'), |
---|
172 | 'optional_unique' => TRUE, |
---|
173 | ), |
---|
174 | 'mail' => array( |
---|
175 | 'name' => t('Email address'), |
---|
176 | 'description' => t('Email address of the user.'), |
---|
177 | 'optional_unique' => TRUE, |
---|
178 | ), |
---|
179 | 'created' => array( |
---|
180 | 'name' => t('Created date'), |
---|
181 | 'description' => t('The created (e. g. joined) data of the user.'), |
---|
182 | ), |
---|
183 | 'pass' => array( |
---|
184 | 'name' => t('Unencrypted Password'), |
---|
185 | 'description' => t('The unencrypted user password.'), |
---|
186 | ), |
---|
187 | ); |
---|
188 | if (module_exists('openid')) { |
---|
189 | $targets['openid'] = array( |
---|
190 | 'name' => t('OpenID identifier'), |
---|
191 | 'description' => t('The OpenID identifier of the user. <strong>CAUTION:</strong> Use only for migration purposes, misconfiguration of the OpenID identifier can lead to severe security breaches like users gaining access to accounts other than their own.'), |
---|
192 | 'optional_unique' => TRUE, |
---|
193 | ); |
---|
194 | } |
---|
195 | |
---|
196 | // Let other modules expose mapping targets. |
---|
197 | self::loadMappers(); |
---|
198 | drupal_alter('feeds_user_processor_targets', $targets); |
---|
199 | |
---|
200 | return $targets; |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Get id of an existing feed item term if available. |
---|
205 | */ |
---|
206 | protected function existingItemId(FeedsImportBatch $batch, FeedsSource $source) { |
---|
207 | |
---|
208 | // Iterate through all unique targets and try to find a user for the |
---|
209 | // target's value. |
---|
210 | foreach ($this->uniqueTargets($batch) as $target => $value) { |
---|
211 | switch ($target) { |
---|
212 | case 'name': |
---|
213 | $uid = db_result(db_query("SELECT uid FROM {users} WHERE name = '%s'", $value)); |
---|
214 | break; |
---|
215 | case 'mail': |
---|
216 | $uid = db_result(db_query("SELECT uid FROM {users} WHERE mail = '%s'", $value)); |
---|
217 | break; |
---|
218 | case 'openid': |
---|
219 | $uid = db_result(db_query("SELECT uid FROM {authmap} WHERE authname = '%s' AND module = 'openid'", $value)); |
---|
220 | break; |
---|
221 | } |
---|
222 | if ($uid) { |
---|
223 | // Return with the first nid found. |
---|
224 | return $uid; |
---|
225 | } |
---|
226 | } |
---|
227 | return 0; |
---|
228 | } |
---|
229 | } |
---|