/[drupal]/contributions/modules/import/examples/import_user/import_user.module
ViewVC logotype

Diff of /contributions/modules/import/examples/import_user/import_user.module

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

revision 1.1, Fri Oct 31 17:51:23 2008 UTC revision 1.2, Wed Apr 8 21:42:11 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id$  // $Id: import_user.module,v 1.1 2008/10/31 17:51:23 cyberswat Exp $
3    
4  /**  /**
5   * @file   * @file
6   * An example use case for the import API written to import users from an   * An example use case for the import API written to import users from an
7   * existing Drupal installation.   * existing Drupal installation.  Pulls in one profile field.
8   *   *
9   * In order for this to work you will likely need to setup a second datasource   * In lieu of a second data source we will be pulling from an internal test
10   * in settings.php to perform the selects from.   * table.  A common scenario for imports would involve a second database.
11     * Comments below illustrate where you would normally switch datasources.
12   */   */
13    
14  /**  /**
15   * hook_import_stage()   * Implementation of hook_import_stage()
16   *   *
17   */   */
18  function import_user_import_stage() {  function import_user_import_stage() {
19    if(variable_get('import_user_staged',FALSE) == FALSE) {  
20      // The import_user_staged variable is set to false during the installation of
21      // this module.
22      if (variable_get('import_user_staged', FALSE) == FALSE) {
23    
24        // select all of the old uids we will be importing.  If you were importing
25        // from an external datasource you would use db_set_active() to switch
26        // before running the select statement.
27    
28      // select all of the old uids we will be importing.  Use the datasource      $sql = "SELECT id from {import_example_user}";
     // we are importing from  
   
     db_set_active('old');  
     $sql = "SELECT uid from users";  
29      $result = db_query($sql);      $result = db_query($sql);
     db_set_active('default');  
30    
31      while ($user = db_fetch_object($result)) {      while ($user = db_fetch_object($result)) {
32    
33        // create an array containing identifier and type both of which are used in        // create an array containing identifier and type both of which are used in
34        // this modules process hook        // this modules process hook.  The type is an arbitrary identifer the
35          // developer can use to easily identify which type of data is referenced.
36          // The type chosen by the developer will get passed to this modules
37          // implementation of hook_import_process.
38    
39        $attr = array();        $attr = array();
40        $attr['impid'] = $user->uid;        $attr['impid'] = $user->id;
41        $attr['type'] = 'user';        $attr['type'] = 'user';
42    
43        // pass the array to the import_stage method        // pass the array to the import_stage method
44    
45        import_stage($attr);        import_stage($attr);
46      }      }
47    
48      // set this variable to true to eliminate restaging the data      // set this variable to true to eliminate restaging the data
49        variable_set('import_user_staged', TRUE);
     variable_set('import_user_staged',TRUE);  
50    }    }
51  }  }
52    
53    /**
54     * Implementation hook_import_process()
55     */
56  function import_user_import_process($data) {  function import_user_import_process($data) {
57    drupal_set_message("import_user_import_process");    switch ($data->type) {
58    if($data->type == 'user') {      case 'user':
59      db_set_active('old');        $new_user = _import_user_map_data($data);
60      // get the old users data        $obj = new stdClass();
61      db_set_active('old');        $account = user_save($obj, $new_user);
62      $sql = "SELECT uid, name, pass, mail, mode, sort, threshold, theme, ".        if ($account === FALSE) {
63             "signature, created, access, login, status, timezone, language, ".          import_fail($data, 'User Import failed'. print_r($new_user, TRUE));
64             "picture, init, data from users where uid = %d";        }
65      $sql_insert = "INSERT into users(name, pass, mail, mode, sort, threshold, ".        else {
66        "theme, signature, created, access, login, status, timezone, language, ".          import_pass($data, 'User Import passed'. print_r($account, TRUE));
67        "picture, init, data) values('%s','%s','%s',%d, %d, %d, '%s', '%s', %d, ".        }
68        "%d, %d, %d, '%s', '%s', '%s', '%s', '%s')";        break;
     $user = db_fetch_object(db_query($sql,$data->impid));  
     db_set_active('defualt');  
     $status = FALSE;  
     if($user->uid > 0) {  
       $status = db_query($sql_insert,$user->name, $user->pass, $user->mail,  
         $user->mode, $user->sort, $user->threshold, $user->theme,  
         $user->signature, $user->created, $user->access, $user->login,  
         $user->status, $user->timezone, $user->language, $user->picture,  
         $user->init, $user->data);  
     }  
     if($status == FALSE) {  
       import_fail($data,'User Import failed'.print_r($user,true));  
     } else {  
       import_pass($data,'User Import failed'.print_r($user,true));  
     }  
69    }    }
70    }
71    
72    /**
73     * Helper function to map the old data to a Drupal user object;
74     */
75    function _import_user_map_data($data) {
76    
77      // First we need to select the old record we are dealing with.  Normally this
78      // would be done by using db_set_active() to switch datasources.
79      $sql_old_user = "SELECT id, email, name, random_username, random_password from {import_example_user} WHERE id = %d";
80      $old_user = db_fetch_object(db_query($sql_old_user, $data->impid));
81    
82      $account = array();
83    
84      $account['status'] = 1;
85      $account['roles'] = array(DRUPAL_AUTHENTICATED_RID);
86      $account['name'] = $old_user->random_username;
87      $account['mail'] = $old_user->email;
88      $account['pass'] = $old_user->random_password;
89      $account['profile_name'] = $old_user->name;
90      $account['profile_original_id'] = $old_user->id;
91    
92      return $account;
93    
94  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2