silverman63 Reply #1, 8 months, 3 weeks ago
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:
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++;
}
