We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49499
    • 48 Posts
    Due to an offline registration process for an event, I now need to add about 500 users (this is only a small selection of our web users) to a User Group or two. I have been given a CSV of email addresses of everyone's accounts that should be added to those groups.

    How should I go about this?

    My initial thinking is to put the email addresses into an array and maybe use getIterator or getCollection to grab the User objects?

    I can't seem to construct a query that works... Here is what I have tried:

    $c = $xpdo->newQuery('modUser');
    
    $c->where(array(
    	'email:IN' => array('[email protected]','[email protected]')
    ));
    
    $userObjs = $xpdo->getIterator('modUser',$c);
    
    foreach($userObjs as $userObj) {
    	$userObj->joinGroup('GROUP1');
    	$userObj->joinGroup('GROUP2');
    }
     
    return 'Success.';

    This question has been answered by patrickappelman. See the first response.

    • discuss.answer
      • 49499
      • 48 Posts
      The following worked for me. Create a script called add_users_to_group:

      $emails = $modx->getOption('emails',$scriptProperties,'');
      $group = $modx->getOption('group',$scriptProperties,'');
      
      if ($emails) {
      	$email_array = explode('||',$emails);
      
      	$c = $modx->newQuery('modUser');
      	$c->select($modx->getSelectColumns('modUser','modUser','',array('id','username')));
      	$c->innerJoin('modUserProfile','Profile');
      	$c->where(array(
      		'Profile.email:IN' => $email_array,
      		'modUser.active' => true,
      	));
      	
      	$count = $modx->getCount('modUser',$c);
      	$users = $modx->getIterator('modUser',$c);
      	
      	
      	foreach ($users as $user) {
      		$user->joinGroup($group);
      	}
      
      
      	return "<p>" . $count . " Users Updated</p>";
      }


      Then call it using:

      [[!add_users_to_group? 
          &emails=`[email protected]||[email protected]`
          &group=`GROUP1`
      ]]
        • 3749
        • 24,544 Posts
        Very nice solution -- and much faster and more efficient than what I was going to suggest.

        Do you mind if I do a blog post on it?
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 49499
          • 48 Posts
          Hi BobRay. Thank you! I don't mind at all... In fact, I based the solution on an answer you gave here and an answer splittingred gave here.