/[drupal]/contributions/modules/user_import/supported/user.inc
ViewVC logotype

Diff of /contributions/modules/user_import/supported/user.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.2.6, Sat Mar 21 23:11:50 2009 UTC revision 1.1.2.7, Sun Sep 20 02:02:13 2009 UTC
# Line 13  function user_user_import_form_field_mat Line 13  function user_user_import_form_field_mat
13    $options = array();    $options = array();
14    $options['user']['email'] = t('Email Address*');    $options['user']['email'] = t('Email Address*');
15    $options['user']['password'] = t('Password');    $options['user']['password'] = t('Password');
16      $options['user']['roles'] = t('Roles');
17    return $options;    return $options;
18  }  }
19    
# Line 56  function user_user_import_data($settings Line 56  function user_user_import_data($settings
56      $value = trim($data[$column_id]);      $value = trim($data[$column_id]);
57    }    }
58    
59      if ($field_id == 'roles') {
60        $value = user_user_import_roles_data($data[$column_id], $settings['roles_new']);
61      }
62    
63    return $value;    return $value;
64  }  }
65    
# Line 78  function user_user_import_pre_save($sett Line 82  function user_user_import_pre_save($sett
82    
83        case UPDATE_REPLACE:        case UPDATE_REPLACE:
84          // update roles          // update roles
85          $account_add['roles'] = array();                                  if (!isset($account_add['roles'])) {
86                                            $account_add['roles'] = array();
87                                    }
88    
89          foreach ($settings['roles'] as $rid => $role_set) {          foreach ($settings['roles'] as $rid => $role_set) {
90            if (!empty($role_set)) {            if (!empty($role_set)) {
# Line 145  function user_user_import_edit_roles_fie Line 151  function user_user_import_edit_roles_fie
151    $form['role_selection'] = array(    $form['role_selection'] = array(
152        '#type' => 'fieldset',        '#type' => 'fieldset',
153        '#title' => t('Role Assign'),        '#title' => t('Role Assign'),
       '#description' => t("Select which role(s) imported users should be assigned. The role 'authenticated user' is assigned automatically."),  
154        '#weight' => -80,        '#weight' => -80,
155        '#collapsible' => TRUE,        '#collapsible' => TRUE,
156        '#collapsed' => $collapsed,        '#collapsed' => $collapsed,
157    );    );
158    
159    $form['role_selection']['roles'] = array(    $form['role_selection']['roles'] = array(
160            '#title' => t('Assign Role(s) To All Users'),
161        '#type' => 'checkboxes',        '#type' => 'checkboxes',
162        '#options' => $roles,        '#options' => $roles,
163        '#default_value' => $roles_selected,        '#default_value' => $roles_selected,
164          '#description' => t("Select which role(s) all imported users should be assigned. The role 'authenticated user' is assigned automatically."),
165    
166      );
167    
168      $form['role_selection']['roles_new'] = array(
169          '#type' => 'checkbox',
170          '#title' => t('Add New Roles'),
171          '#default_value' => $import['options']['roles_new'],
172          '#description' => t('Create imported role(s) that are not found and assign it to the user, in addition to any role(s) selected above. Warning: incorrect roles will be created if the incoming data includes typos.'),
173    );    );
174    
175    return;    return;
# Line 225  function _user_import_existing_uid($emai Line 240  function _user_import_existing_uid($emai
240          $uid = db_result(db_query("SELECT uid FROM {users} WHERE mail= '%s' LIMIT 1", $email));          $uid = db_result(db_query("SELECT uid FROM {users} WHERE mail= '%s' LIMIT 1", $email));
241    return $uid;    return $uid;
242  }  }
243    
244    /**
245     * Implementation of hook_user_import_after_save().
246     */
247    function user_user_import_after_save($settings, $account, $password, $fields, $updated, $update_setting_per_module) {
248      /**
249       * @todo change hook_user_import_after_save() so that all changes to data are returned and saved in one hit
250       */
251      user_save($account, array('contact' => $settings['contact']));
252            user_user_import_after_save_role($account, $settings['roles_new'], $account->roles, $fields['user']['roles']);
253      return;
254    }
255    
256    function user_user_import_roles_data($data, $new_roles_allowed) {
257    
258      if (empty($data)) return;
259      $values = explode( ',', $data);
260    //var_dump($values);
261      // check if any roles are specified that don't already exist
262      $existing_roles = user_roles();
263    
264      foreach($values as $piece) {
265        $role = trim($piece);
266        $unrecognised = array();
267    
268                    if (!empty($role)) {
269                // only add if role is recognized or adding new roles is allowed
270                if (empty($new_roles_allowed) && !array_search($role, $existing_roles)) {
271                            $unrecognised[] = $role;
272                }
273                else {
274                            $roles[] = $role;
275                }
276                    }
277    
278      }
279    
280      if (!empty($unrecognised)) {
281                    user_import_errors(t('The following unrecognised roles were specified: ') . implode(',', $unrecognised));
282      }
283    
284      return $roles;
285    }
286    
287    
288    function user_user_import_after_save_role($account, $new_roles_allowed, $account_roles, $roles) {
289    
290      $existing_roles = user_roles();
291    
292      // if roles were specified, add to existing roles
293      $assign_roles = array();
294    
295      if (is_array($roles) && !empty($roles)) {
296    
297                    foreach ($roles as $role) {
298    
299                if (!empty($role)) {
300    
301                              $key = array_search($role, $existing_roles);
302    
303                              if (!empty($new_roles_allowed) && empty($key)) {
304                                      db_query("INSERT INTO {role} (name) VALUES ('%s')", $role);
305                          $key = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s' LIMIT 1", $role));
306                                            $existing_roles[$key] = $role;
307                              }
308    
309                              $assign_roles[$key] = $role;
310                            }
311                    }
312    
313                    $need_update = FALSE;
314    
315              foreach ($assign_roles as $key => $role) {
316                      if (!isset($account_roles[$key])) {
317                        $need_update = TRUE;
318                        $account_roles[$key] = $role;
319                      }
320              }
321    
322                    if ($need_update) {
323                            user_save($account, array('roles' => $account_roles));
324              }
325      }
326    
327      return;
328    }
329    
330    
331    

Legend:
Removed from v.1.1.2.6  
changed lines
  Added in v.1.1.2.7

  ViewVC Help
Powered by ViewVC 1.1.2