In my environment, there are profile fields in the parent (such as last name, city, state, and zip) that I would like to propagate from the parent user when the subuser is created.

I added the following code to subuser.pages.inc (after the watchdog and the plain text password is set in subuser_create_form_submit, around line 209):

  // GMM: copy profile fields from primary account to new account
  $copynames = array("profile_lname", "profile_city", "profile_state", "profile_zip");
  foreach($copynames as $namefid)
  {
	$presult = db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $namefid);
        $copyfid = db_fetch_object($presult)->fid;
        if($copyfid > 0) {
	  $lname = db_query("SELECT value FROM {profile_values} WHERE fid = %d AND uid = %d", $copyfid, $GLOBALS["user"]->uid);
	  db_query("UPDATE {profile_values} SET value = '%s' WHERE fid = %d AND uid = %d", db_fetch_object($lname)->value, $copyfid, $account->uid);
	} else
	  drupal_set_message("Warning: could not copy profile data ".$namefid." from parent profile");
  }

This does a lookup on the profile field name, finds its fid, and copies the profile information from the parent to the subuser.

Caveats: This does not copy the privacy for the copied fields if the profile_privacy module is used.

Would the maintainer consider adding this functionality to the base code, perhaps with a nice profile field select in the admin page to choose which profile fields to propagate from parent to child when the subuser is created.

Enjoy,
--Glen

Comments

glen201’s picture

CAVEATS: Oh, and of course, the subuser's copied information has to be maintained separately, so if the parent's info is ever updated, the child is left with the old info in the database. This is especially a problem if the module profile_role is used and the subuser is prevented from editing the inherited information.

timpiche’s picture

Why wouldn't you just select the information from the parent user and only display it for the sub user? This way the information is always up to date.

zibretni’s picture

I put this :
$copynames = array("profile_klantnummer");
foreach($copynames as $namefid)
{
$presult = db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $namefid);
$copyfid = db_fetch_object($presult)->fid;
if($copyfid > 0) {
$getvalue = "SELECT value FROM profile_values WHERE fid='".$copyfid."' and uid='".$edit['parent_user']."'";
$gettedvalue = db_result(db_query($getvalue));
$inputvalue = "INSERT INTO profile_values (fid, uid, value) VALUES ('$copyfid','$account->uid','$gettedvalue')";
db_query($inputvalue);

} else
drupal_set_message("Warning: could not copy profile data ".$namefid." from parent profile");
}

after :
function subuser_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'insert':
if (isset($edit['origin']) && $edit['origin'] == 'subuser') {
db_query('INSERT INTO {user_relationship} (parent_id, uid)
VALUES (%d, %d)', $edit['parent_user'], $account->uid);

in : subuser.module

massyboy’s picture

Hey zibretni, I understand your code, but it's not importing anything of the value of the parent ID..
Altough your code seams reasonable and true and altough no syntax errors are present... since I had no error message when creating a new subuser.
Ofcourse, my one of the questions would be "when" is this function subuser_user() being called?
I do use the functionality via ".../user/5/subuser/create"... So i Don't see the issue at hand...
Maybe you have some suggestions?

Thanks a bunch

blakehall’s picture

Status: Active » Closed (duplicate)

Closing this issue as a duplicate, since we'll be supporting this type of functionality with the patch in #571660: Creating profile with the user creation