Author Topic: Silent Registration User Maintenance Code Change  (Read 2399 times)

Dave Withnall

  • Beginner
  • *
  • Posts: 47
Silent Registration User Maintenance Code Change
« on: January 18, 2009, 13:10:40 pm »
Howdy,

I have a site which uses silent registration. As such the user ends up with a username that is the same as their email address. That's all good.
However, when a user goes to their account maintenance they are shown both a username & an email field which are editable separately.

To remove this duplication of form data I made a modification to the back end code which replicates the account creation process when updating the account.

In the code snippet below you will find 2 if statements looking for SILENT_REGISTRATION. First to copy the email address into the username field, the second allows the code to update the username.

For this to work as advertised, turn off the username field in the account maintenance section.

I wasn't sure of the ramifications of this change if it was done in ps_userfield::getSkipFields() so I left it alone.

Cheers,

D.

ps_shopper.php
Code: [Select]
/**
* Function to update a Shopper Entry
* (uses who have perms='shopper')
*/
function update(&$d) {
global $my, $perm, $sess, $vmLogger, $page;

$auth = $_SESSION['auth'];
$db = new ps_DB;

if ( @$d["user_id"] != $my->id && @$d["user_id"] != $auth['user_id'] && $auth["perms"] != "admin") {
$vmLogger->crit( "Tricky tricky, but we know about this one." );
return False;
}

require_once(CLASSPATH. 'ps_user.php' );
  if( !empty($d['username'])) {
$_POST['username'] = $d['username'];
}
else {
$_POST['username'] = $my->username;
}
   
    if( VM_REGISTRATION_TYPE == 'SILENT_REGISTRATION' ) {
     $_POST['username'] = $_POST['email'];
    }
   
$_POST['name'] = $d['first_name']." ". $d['last_name'];
$_POST['id'] = $auth["user_id"];
$_POST['gid'] = $my->gid;
$d['error'] = "";

if ( VM_REGISTRATION_TYPE != 'NO_REGISTRATION' ) {
ps_user::saveUser( $d );
}

if( !empty( $d['error']) ) {

return false;
}

if (!$this->validate_update($d)) {
return false;
}
$user_id = $auth["user_id"];

/* Update Bill To */

// Get all fields which where shown to the user
$userFields = ps_userfield::getUserFields( 'account', false, '', true );
$skip_fields = ps_userfield::getSkipFields();

    if( VM_REGISTRATION_TYPE == 'SILENT_REGISTRATION' ) {
      $skip_fields = array( 'password', 'password2', 'agreed' );
    }
   
$fields = array(
'mdate' => time()
);

foreach( $userFields as $userField ) {
if( !in_array($userField->name, $skip_fields )) {

$fields[$userField->name] = ps_userfield::prepareFieldDataSave( $userField->type, $userField->name, vmGet( $d, $userField->name, strtoupper($userField->name) ));

}
}

$fields['user_email'] = $fields['email'];
unset($fields['email']);

$db->buildQuery('UPDATE', '#__{vm}_user_info', $fields, " WHERE user_id=".$user_id." AND address_type='BT'" );

// Run the query!
$db->query();

// UPDATE #__{vm}_shopper group relationship
$q = "SELECT shopper_group_id FROM #__{vm}_shopper_vendor_xref ";
$q .= "WHERE user_id = '".$user_id."'";
$db->query($q);

if (!$db->num_rows()) {
//add

$shopper_db = new ps_DB;
// get the default shopper group
$q =  "SELECT shopper_group_id from #__{vm}_shopper_group WHERE ";
$q .= "`default`='1'";
$shopper_db->query($q);
if (!$shopper_db->num_rows()) {  // when there is no "default", take the first in the table
$q =  "SELECT shopper_group_id from #__{vm}_shopper_group";
$shopper_db->query($q);
}

$shopper_db->next_record();
$my_shopper_group_id = $shopper_db->f("shopper_group_id");
if (empty($d['customer_number'])) {
$d['customer_number'] = "";
}

$q  = "INSERT INTO #__{vm}_shopper_vendor_xref ";
$q .= "(user_id,vendor_id,shopper_group_id) ";
$q .= "VALUES ('";
$q .= $_SESSION['auth']['user_id'] . "','";
$q .= $_SESSION['ps_vendor_id'] . "','";
$q .= $my_shopper_group_id. "')";
$db->query($q);
}
$q = "SELECT user_id FROM #__{vm}_auth_user_vendor ";
$q .= "WHERE user_id = '".$_SESSION['auth']['user_id']."'";
$db->query($q);
if (!$db->num_rows()) {
// Insert vendor relationship
$q = "INSERT INTO #__{vm}_auth_user_vendor (user_id,vendor_id)";
$q .= " VALUES ";
$q .= "('" . $_SESSION['auth']['user_id'] . "','";
$q .= $_SESSION['ps_vendor_id'] . "') ";
$db->query($q);
}

return True;
}