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

    As I ended my migration from CMSMadeSimple 1.12 to Modx Revolution 2.3.5 , I'm going to share here my snippet in case it can help someones. Not guarantee it will work on every platform as it's really basic and can be more clever.

    <?php
    /**
     * 
     * USAGE : call this snippet from a page
     * 
     * BECAREFUL because it's a mass import if you have huge database
     * 
     * Create users in modx from cmsms user's database, with email as username and email
     * 
     * 
     **/
    
     
    if (!$activated == 1) {
    return 'Snippet ImportUsersCmsms deactivated for security reasons !!! ';
    }
     
     
    $output = '';
    
    
    
    /** @var array $userFields The internal MODX user field names (from modUser and modProfile) */
        $userFields = array(
            'username',    // varchar 100
            'password',
            'hash_class',
            'email',       // varchar 100
            'fullname',    // varchar 100
            'comment',     // text
        );
        
        // options
        $option = array(
        'hash_class'    => 'hashing.modMD5',
        'active'        => '0',             // 0 = inactive in Modx || 1 = active in Modx
        'groupe'        => 'Members',             // name or id group
        'role'          => 'member',             // name or id role
        );
        
        // simplify the array
        $groupe = $option['groupe']; 
        $role = $option['role'];
        
    
    /**
     * 
     * DB request
     * 
     **/
    
    // prepare
    $dsn = 'mysql:dbname=mydatabasename;host=myhost;port=3306;charset=utf8;'; //charset=utf8
    // some params
    $xpdo_params = array(
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'
            );
    // 
    $xpdo = new xPDO($dsn,'username','password',$xpdo_params);
    $xpdo->exec("set names utf8");
    
    // are we connected to DB ?
    $o=($xpdo->connect()) ? 'Connected to db' : 'Connection to db failed';
    
    //print $o; // uncomment to verify
    
    
    // the request
    $sql = "
    -- CMSMS module_feusers_users table have thix prefix : cmsms_
    -- in CMSMS : group name = id
    -- members = 6
    
    SELECT DISTINCT
    	U.username AS fullname,
    	U.password AS password,
    	B.groupid AS groupid,
    	P.data AS email
    FROM 
    	cmsms_module_feusers_users as U
    	
    LEFT JOIN 
    	cmsms_module_feusers_properties AS P
    	ON U.id = P.userid
    	
    LEFT JOIN 
    	cmsms_module_feusers_belongs as B
    	ON U.id = B.userid
    	
    	WHERE 
    	B.groupid = '6' -- group
    	AND
    	P.data 
    	LIKE '%@%' -- looking for the email
    
    -- LIMIT 3 -- for testing purpose
    
    ";
    
    $fields = $xpdo->query($sql);
    
    /**
     * 
     * Results
     * 
     * 
     **/
    
    foreach ($fields as $fieldvalues) {
        
            $userSaved = false;
            
            
            /**
             * 
             * 
             * First create the user
             * 
             * 
             **/
             
             
             
            $user = $modx->newObject('modUser');
            
            // Add modUser -> required fields
            $user->set('username', $fieldvalues['email']);
    		$user->set('password', $fieldvalues['password']);
    		$user->set('hash_class', $option['hash_class']);
    		$user->set('salt', '');
    		$user->set('active',   $option['active']);
    		$user->set('blocked',  0);
    		
    		// Add modUserProfile -> required fields
            $userProfile = $modx->newObject('modUserProfile');
            
                
                
                $userProfile->set('fullname',    $fieldvalues['fullname']);
            
            
            $userProfile->set('email',       $fieldvalues['email']);
    
    		// Add modUserProfile -> import info to comment field
            $importedon = strftime('%d/%m/%Y %H:%M:%S');
            $userProfile->set('comment', 'Imported on : '.$importedon);
            
            $user->addOne($userProfile);
            
            
        		if ($user->save()) {
                    $userSaved = true;
                    $userId = $user->get('id'); // preserve id of new user for later use
                    
                    //$user->joinGroup('UserGroupNameOrId','OptionalRoleNameOrId');
                    $user->joinGroup($groupe,$role);
        		}
        		
        		if (!$userSaved) {
                    // Rollback if one of the savings failed!
                    $user->remove();
                    $output .= date('Y-m-d H:i:s') . ' <strong>ERRoR</strong> : User creation failde for : ' . $fieldvalues['username'].' '.$fieldvalues['email'] . '<br />';
        		} else { //$userSaved
            		$output .= date('Y-m-d H:i:s') . ' <strong>User created</strong> : ' . $fieldvalues['fullname'].' '.$fieldvalues['email'] . '(' . $userId . ')<br />';
        		}
        		$output .= 'OK. End of work  '.$fieldvalues['fullname'].' - Username : '.$fieldvalues['email']. '<hr />';
    		
            }// end foreach
    
    /* send a mail to report */
    $modx->getService('mail', 'mail.modPHPMailer');
    $modx->mail->set(modMail::MAIL_BODY,$output);
    $modx->mail->set(modMail::MAIL_FROM,'[email protected]');
    $modx->mail->set(modMail::MAIL_FROM_NAME,'MyName');
    $modx->mail->set(modMail::MAIL_SUBJECT,'My subject');
    $modx->mail->address('to',$report);
    $modx->mail->setHTML(true);
    if (!$modx->mail->send()) {
        $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
    }
    $modx->mail->reset();
    
    return $output;


    CAUTION : everytime the page is called, the import is done. So it's made to be used once, and the deactivated !

    Then in the page of you choice, put the following lines.
    [[!importUsersCmsms?
    &activated=`0`
    &report=`[email protected]`]]


    Change &activated to 1 to activate the snippet. Need some customization to correspond to your needs in particular for usernames. In my case, we where changing the username wich was a fullname (DOE Jhon) to email ([email protected]).