We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!

Answered Importing users

    • 28042 ☆ A M B ☆
    • 24,524 Posts
    When importing users via .csv, what would be the best way to actually create the new users? Is there a processor or a service I can load and use to create the new user with three values (username, fullname, email), as if I had entered them in the Manager interface?

    $csvpath = MODX_ASSETS_PATH . 'imports/';
    $csvfile = $csvpath . 'userimport.csv'; // make this a snippet property
    
    $file = new SplFileObject($csvfile);
    $delimiter = ',';
    $enclosure = "\n";
    $file->setCsvControl($delimiter, $enclosure);
    while ($file->valid()) {
        $data = $file->fgetcsv();
        $email = $data[2];
        // check for email already existing
        $result = $modx->query('SELECT id FROM modx_user_attributes WHERE email = ' . $email);
        if (!is_object($result)) {
            // user needs to be created
            $username = strstr($email, '@', true); // As of PHP 5.3.0
            $fullname = $data[0] . ' ' . $data[1];
            // we already have $email   
            $active = '1';
            ...
          }


    This question has been answered by multiple community members. See the first response.

      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 42046
      • 436 Posts
      Does the User Import extra not cover these functions?

      http://modx.com/extras/package/userimport
        • 37523
        • 141 Posts
        You may also find that when you create the new user you have to supply a password for them.

        Below is a routine I have used to create users:

        //create the new user object
        $userToSave = $modx->newObject('modUser', array ('username'=>$theUsersNewName));
        
        //give a password
        $userToSave->set('password', $theUsersNewPassword);
        
        //setup the users profile
        $profile = $modx->newObject('modUserProfile');
        $profile->set('email',$theUsersNewEmail);
        $profile->set('fullname',$theUsersFullName);
        $success = $userToSave->addOne($profile);
        
        if ($success) {
        $userToSave->save();
        }else {
          $output.= '<p>failed to add profile. User not saved.</p>';
        }
        



        Hope this helps
          Glyn Szasz
          Sydney, Australia

          Happy to help (when can) and happy to learn

          If you are a Sydney based MODX developer would love to hear from you. Please contact me.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          @Dan - with several modifications it would, as the user has a different format for his .csv files and I do need extended fields. In any case, I am looking for a processor or a service, rather than the "long" method.

          @Glyn - again, I'm looking for a runProcessor() or a getService() method rather than the user->save() method.

          Only three core services are listed in the rtfm, but so many API features are undocumented, or documented very poorly, that I'm wondering if there aren't more actually available. For example, another page in the rtfm refers to the modSmarty service. And a quick look through the files comes up with a modLexicon service.

          [ed. note: sottwell last edited this post 11 years, 7 months ago.]
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            This is more like what I'm looking for...
            $response = $modx->runProcessor('security/user/create',array(
                'username' => $credentials['username'],
                'fullname' => $juser->name,
                'email' => $juser->email,
                'phone' => $juser->telnr ? $juser->telnr:'0612345678',
                'newpassword' => TRUE,
             
                    /* Changed theses params */
                'specifiedpassword' => $credentials['password'],
                'confirmpassword' => $credentials['password'],
                    /* End of change */
             
                'passwordnotifymethod' => 'e'
            ));

            https://forums.modx.com/thread/31739/security-user-create-processor-keeps-asking-for-password
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
            • discuss.answer
              • 44580
              • 189 Posts
              Susan, Bruno did this for me a few months ago: https://forums.modx.com/thread/94367/migxdb---moduser-question#dis-post-511587. There might be something of use for you in there.
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Thank you! That has all the elements I need, including how to tell it to generate a password and email the user.

                All that's left is to figure out how to get the extended fields in there, but a bit of trial-and-error should work that out. If all else fails, I can add that after the user is created. [ed. note: sottwell last edited this post 11 years, 7 months ago.]
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 44580
                  • 189 Posts
                  I have an "extended" table that I update as part of this process like so:
                   // Update the quote with the new user_id
                      if ($newuser = $response->getObject()){
                          $object->set('user_id',$modx->getOption('id',$newuser));
                          $object->save();
                          // Add the new user to the Parents group
                          $user = $modx->getObject('modUser', $modx->getOption('id',$newuser));
                          $user->joinGroup(3, 1);
                          // Create and save the parent details record also
                          $parent = $modx->newObject('ParentDetails');
                          $parent->set('parent_id',$modx->getOption('id',$newuser));
                          $parent->set('coach_id', $object->get('coach_id'));
                          $parent->set('hourly_rate', $object->get('cost_per_hour'));
                          $parent->set('send_invoice', 'Y');
                          $parent->set('num_lessons', '8');
                          $parent->save();

                  I'm actually doing two additional things at the end of what Bruno provided: adding the new user to a group; and creating a "parent" record which effectively extends the user record. Hope this helps.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Thank you; I'm not doing anything as complicated, just putting values into the user's Extended Fields. I do need to add him to a group as well, though. I'm just trying to stuff as much into the runProcessor() as I can.
                      Studying MODX in the desert - http://sottwell.com
                      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                      Join the Slack Community - http://modx.org
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      This is working well, but it's behaving as if I were creating a new Manager user. I wonder if I should just runSnippet() using the Register snippet instead. It does require a password, but that's just two lines of code.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org