• Creating a user import snippet! On behalf of the community, let's make this happen!#

  • silverman63 Reply #1, 8 months, 3 weeks ago

    Reply
    Well, one thing I've found lacking in most open CMS's is an actively developed way to upload users by CSV (Drupal has a decent one, couldn't find one for Wordpress or ModX though!).

    So, with that in mind, I set out to try and create a user creation loop from a csv. My problem is I don't really know how the process of user creation is gone about besides the same basic way of creating a new resource or chunk. So I have my loop, and towards the end, I tried to sift through Login's register snippet for information for a usable piece of code to help with the details. I don't know if its useless or not. But also, I still have a hazy idea of how to make an application of it within the site besides as a formit hook, let alone if I got the code right...So I'd love to have you guys take a look! I also want this to eventually be an open snippet or plugin for the community, so yeah.

    csv2users:
    <?php
    $csv = file_get_contents($spreadsheet);
    //Get every row, including headers, on its own
    $rows = explode("\n",$csv);
    $totalusers = (count($rows) - 1); // since we have to subtract the header row
    $headers = explode(",", $rows[0]);
    $columns = count($headers); // gets total number of columns
    $user = 1;
    
    //start user import loop
    while ($user <= $totalusers) {
    $data = explode $rows[$user];
    $col = 0;
    
    //set each of the values in a pair with its proper heading
    for ($col; $col <= $columns; $col++;) {
    $fields = array($headers[$col])($data[$col]);
    }
    //Get first and last name, then combine for fullname field as well as username
    $fname = $fields('first_name');
    $lname = $fields('last_name');
    $username = substr ( $fname, 1, 5) . substr($lname,1,5);
    
    
    //see if username is already in use, and if not create new.
    $user = $modx->getObject('modUser',array('username' => $username));
    if (!empty($user)){
        // get random digits
    	$randts = bcmod(Time(),1000000);
    	$username .= $randts;
    	}
    
    		//Create New User and User Profile
    	$user = $modx->newObject('modUser');
    	$profile = $modx->newObject('modUserProfile');
    	$user->set('username',$username);
    	$user->set('active',1);
    	
    	//Insert UserFields into DB
    $col = 1;	
    for ($col; $col <= $columns; $col++) {
    $profile->set($headers[$col],$userinfo[$col]);
    }	
    	$profile->set('internalKey',0);
    	$user->addOne($profile,'Profile');
    
    //From Login's Register Snippet
     /* get usergroup */
            $pk = array();
            $pk[intval($usergroupMeta[0]) > 0 ? 'id' : 'name'] = $usergroupMeta[0];
            $usergroup = $modx->getObject('modUserGroup',$pk);
            if (!$usergroup) continue;
    
            /* get role */
            $rolePk = !empty($usergroupMeta[1]) ? $usergroupMeta[1] : 'Member';
            $role = $modx->getObject('modUserGroupRole',array('name' => $rolePk));
    
            /* create membership */
            $member = $modx->newObject('modUserGroupMember');
            $member->set('member',0);
            $member->set('user_group',$usergroup->get('id'));
            if (!empty($role)) {
                $member->set('role',$role->get('id'));
            } else {
                $member->set('role',1);
            }
            $user->addMany($member,'UserGroupMembers');
    
    /* save user */
    if (!$user->save()) {
        $modx->log(modX::LOG_LEVEL_ERROR,'[Login] Could not save newly registered user: '.$user->get('id').' with username: '.$user->get('username'));
        return $modx->lexicon('register.user_err_save');
    }
    /* setup persisting parameters */
    $persistParams = $modx->getOption('persistParams',$scriptProperties,'');
    if (!empty($persistParams)) $persistParams = $modx->fromJSON($persistParams);
    if (empty($persistParams) || !is_array($persistParams)) $persistParams = array();
    
    //end of Login's code
    
    $user++;
    }
    
    


  • Lucas Reply #2, 8 months, 3 weeks ago

    Reply
    Have a look at Mark Hamstra's ImportX addon. I wanted a CSV import for a site I run which uses a lot of custom tables and classes - rather than coding something from scratch I simply tweaked this to do what I wanted.

    You could do the same for users quite easily.


  • silverman63 Reply #3, 8 months, 3 weeks ago

    Reply
    Maybe it's my lack of knowledge, but that honestly seem to be an overtly complicated way of going about it. Also, I want to try and develop my own skills. Lastly, I'm not quite sure how that would work to convert as a snippet AND I think it'd be great to make a general public tool for importing.

    Any thoughts on my code though?


  • UltraSEF Reply #4, 6 months, 3 weeks ago

    Reply
    I want to give this snippet a try, but before I do, how does it import the Users' MD5 password?

    The last thing I need is for 300+ Users to use the reset password functionality when they know all too well that they didn't change their PW.


  • David Walker Reply #5, 3 months, 2 weeks ago

    Reply
    Did this ever get packaged up as a finished snippet?


  • silverman63 Reply #6, 3 months, 1 week ago

    Reply
    Hey UltraSEF - sorry for really not communicating on this. I'll be honest - it didn't work when I tried to get it to, which is a big part of why I put it up here. I wanted to finish it on my own for the community, but couldn't figure out how ModX's user system fully worked (in terms of getting this snippet working).

    David, I kind of said it up there but I really haven't gotten any time to work on it since school started up, and also since no one gave me any thoughts on the code. If you have any input or suggestions, I'd love to hear it.

    Same goes to anyone else! If you have thoughts or suggestions, please post them. I'd love to be able to turn this into a package for the community at some point soon in the future.


  • UltraSEF Reply #7, 3 months, 1 week ago

    Reply
    Hi silverman63,

    I'm just a front end developer who uses Sublime 2 and a spreadsheet to do some of the tedious MySQL work via phpmyadmin. I'm too creative to be a programmer, but I'm a pretty good rat, meaning that I can analyze things very well and make progressive enhancements.

    Go back to what Lucas suggested: check out ImportX and see if you can tweak that for users, their MD5 passwords too. Jason's pbkdf2convert allows EVO user's MD5 passwords to work and salts them.


  • whistlemaker Reply #8, 3 months, 1 week ago

    Reply
    I'm too creative to be a programmer


  • UltraSEF Reply #9, 3 months, 1 week ago

    Reply
    ?? Whatever are you suggesting whistlemaker?


  • whistlemaker Reply #10, 3 months, 1 week ago

    Reply
    I'm not suggesting anything.

    IMHO it is possible to be creative and be a programmer, in fact programming is a creative process.