CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • User cannot edit permitted related profile via Drupal My Account
Pages: [1]

Author Topic: User cannot edit permitted related profile via Drupal My Account  (Read 3805 times)

ToddW

  • I’m new here
  • *
  • Posts: 16
  • Karma: 1
User cannot edit permitted related profile via Drupal My Account
March 18, 2010, 02:23:05 pm
Hi all,
I've searched through the forums and bug tracker. Installed the latest and also tried this patch:
http://issues.civicrm.org/jira/browse/CRM-5850 but I'm still having the same problem.

I have it duplicated on the demo site right now:
-created a Org Profile called "Related Org Info"
http://drupal.demo.civicrm.org/civicrm/admin/uf/group?action=update&id=8&context=group
It just has the Org Name field and is set to View/Edit Drupal User Account
-created a relationship between the demo user and an Org and checked the flag: "can view and update"
http://drupal.demo.civicrm.org/civicrm/contact/view/rel?action=update&reset=1&cid=102&id=167&rtype=a_b

At this point I would expect to see the Org profile on My Account, as well as edit it by clicking the Edit tab. If I click Edit, I get a "Related Org Info" tab, but no fields.
http://drupal.demo.civicrm.org/user/2/edit/Related%20Org%20Info

I also tried messing with the Civi ACLs. Created a new one with the ability to edit that profile, then added Demo user to the Admin group and assigned the ACL to that group.

Am I hitting a bug or is it a permission issue  ???

Thanks,
Todd

rjeschmi

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
Re: User cannot edit permitted related profile via Drupal My Account
March 19, 2010, 06:58:55 pm
Hey just first guess might be that the field is an Organization field.

There is a limitation with Profiles that the fields need to be of the same type and only of certain types. Contact or Individual would probably be the only ones valid in this situation.

I think what you are trying to do is equivalent to this

http://drupal.demo.civicrm.org/civicrm/profile/edit?reset=1&gid=8

which gives a more useful error.


rjeschmi

  • I’m new here
  • *
  • Posts: 6
  • Karma: 0
Re: User cannot edit permitted related profile via Drupal My Account
March 19, 2010, 07:30:47 pm
Oh do you want to see something more like this integrated in the drupal "My Account" screen?

http://drupal.demo.civicrm.org/civicrm/profile/edit?reset=1&gid=8&id=134


petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: User cannot edit permitted related profile via Drupal My Account
March 19, 2010, 09:19:11 pm
If you go to My Account, and then go to View Dashboard you will find 'related' contact there. If you have permission you will be able to edit the core contact details for that related contact. You will not be able to edit Custom Data 'out of the box' but there are solutions to doing this described in the forum.
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

ToddW

  • I’m new here
  • *
  • Posts: 16
  • Karma: 1
Re: User cannot edit permitted related profile via Drupal My Account
March 21, 2010, 09:05:55 am
Thanks guys. That clears it up. Yes, I would like to have the related Org profile integrated into My Account. I think I was assuming that the system would somehow know which relationship I intend to display/edit.

I supposed I will need to write my own code to display the profile for the related organization.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: User cannot edit permitted related profile via Drupal My Account
March 21, 2010, 09:11:00 am

hey todd:

if you do write custom code for this, can you please publish it so others can potentially reuse it

thanx

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

ToddW

  • I’m new here
  • *
  • Posts: 16
  • Karma: 1
Re: User cannot edit permitted related profile via Drupal My Account
March 22, 2010, 12:00:05 pm
Well it's not pretty, but it looks like it's working. Disclaimer: I used a common design pattern that should never be used but happens all the time. Let's call it Cloned Mutant -- copied code from civicrm.module and changed it just slightly. The goal was primarily to leave the civicrm.module untouched.

What I have is an authenticated user with a relationship of "Primary Contact" with an Organization. I want to add a profile to the /user (My Account) page with the org information. Also, I want an Edit tab where the user can update the org info.

First (or last, if you run into problems), you will probably need to tweak the permissions so the role you want can access profile listings and forms. Also, I was only able to get the Organization fields to show up if the fields were set to Public in the profile.

1) In your custom module, create a function that can look up the org id using the "Primary Contact" relationship for the current user (or an admin who is viewing). Something like this (probably a better way. I'm calling this from a View as well to supply a default argument):
function custommodule_get_primary_contact_org_id() {
  global $user;
 
  if ( $user->uid ) {
      $uid = $user->uid;
       require_once('sites/all/modules/civicrm/CRM/Core/BAO/UFMatch.php');
         if ( $user->roles[3] == 'administrator' && is_integer(arg(1)) && ($user->uid != arg(1)) )
            $uid = arg(1);
         $userID = CRM_Core_BAO_UFMatch::getContactId( $uid );
         if ( $userID ) {
            //find relationship of primary contact; 10 is "Primary Contact"
            $orgID = db_result(db_query('SELECT contact_id_b FROM {civicrm_relationship} WHERE is_active = 1 AND relationship_type_id = 10 AND contact_id_a = %d ', $userID));
          return $orgID;
         }
  }
 
  return FALSE;

}


2) Add your hook_user, with functions for viewing and editing the profile:
function custommodule_user( $type, &$edit, &$user, $category = NULL )
{

    if ( $type == 'load' ) {
        return;
    }     

    switch ( $type ) {

     case 'view':
        return custommodule_show_primary_org_profile($user);

    case 'form':
        $inCategory = true;
        if ( $category ) {
            $inCategory = false;
            $categories = civicrm_categories( $account );
            foreach ( $categories as $cat ) {
                if ( $category == $cat['name'] ) {
                    $inCategory = true;
                    break;
                }
            }
        }
       
        // only return a form to drupal my account page
        if ( $inCategory      &&
             arg(0) == 'user' &&
             arg(2) == 'edit' &&
             arg(3) ) {
            return custommodule_show_form_data($edit, $user, $category, true );
        }
        break;

 
    }
}


4) Function to view the profile
function custommodule_show_primary_org_profile($user) {
   require_once 'CRM/Core/BAO/UFMatch.php';

    $userID = CRM_Core_BAO_UFMatch::getContactId( $user->uid );
    if ( $userID ) {
        // make sure user has permission to view the record
        require_once 'CRM/Contact/BAO/Contact.php';
        require_once 'CRM/Contact/BAO/Contact/Permission.php';
 
        require_once 'CRM/Profile/Page/Dynamic.php';
        require_once 'CRM/Core/BAO/UFGroup.php';
        require_once 'CRM/Core/BAO/UFField.php';

        $ctype = CRM_Core_DAO::getFieldValue( 'CRM_Contact_DAO_Contact', $userID, 'contact_type' );

        $ufGroups =& CRM_Core_BAO_UFGroup::getModuleUFGroup('User Account', 0, false);
        $weight   = 105;
       
       
        $id = 4; //Profile ID!
        $title = $ufGroups[$id];
       
        $primary_org_id = custommodule_get_primary_contact_org_id();
        if ( !$primary_org_id ) return;
       
        $page = new CRM_Profile_Page_Dynamic( custommodule_get_primary_contact_org_id(), $id, null, true );
        $pageContent = $page->run( );

            // CRM-3537: profile edit link
            if ( user_edit_access($user) ) {
                $editURL = '» ' . l( "Edit '{$title['title']}'",
                                           "user/{$user->uid}/edit/{$title['title']}");
            }

        //strip out all links, we don't want these links to civi
        $pageContent = str_replace('</a>', '', $pageContent);
        $pageContent = preg_replace('/<a[^>]+href[^>]+>/', '', $pageContent);
       
            if ( $pageContent ) {
                $user->content[$title['title']] =
                    array( '#title'  => $title['title'],
                           '#type'   => 'user_profile_category',
                           '#weight' => $weight );
                $user->content[$title['title']][$title['title']] =
                    array(  '#type'  => 'user_profile_item',
                            '#value' => $pageContent . $editURL );

                $weight += 10;
            }
       
    }
    return;

}


3) Function to edit the profile:
function custommodule_show_form_data($edit, &$user, $category, $reset, $doNotProcess = false)
{
    require_once 'CRM/Core/BAO/UFMatch.php';
    require_once 'CRM/Core/BAO/UFGroup.php';

    // lets suppress key generation for all CMS forms
    civicrm_key_disable( );

    $output = array( );

    $userID = CRM_Core_BAO_UFMatch::getContactId( $user->uid );
    if ( ! $userID ) {
        $ctype = civicrm_get_ctype( 'Individual' );
        CRM_Core_BAO_UFMatch::synchronize( $user, false, 'Drupal', $ctype );
        $userID = CRM_Core_BAO_UFMatch::getContactId( $user->uid );
    }
   
     
    // at this point we better have a valid userID
  if ( ! $userID ) {
        // we get into this scenario if we do not like the email address supplied by the user
        return;
    }
   
   //reset the ctype to org
   $ctype = civicrm_get_ctype( 'Organization' );
   
    //to allow Edit any profile if user have permission
    $profileID = CRM_Core_DAO::getFieldValue( "CRM_Core_DAO_UFGroup", $category, 'id', 'title' );
    require_once 'CRM/Core/Permission.php';
    $ufGroupIDs = CRM_Core_Permission::ufGroupClause( CRM_Core_Permission::EDIT, null, true );

    $primary_org_id = custommodule_get_primary_contact_org_id();
    if ( !$primary_org_id ) return;
   
   
    if ( in_array($profileID, $ufGroupIDs) ) {
        // get all the data for this specific category
        $html = CRM_Core_BAO_UFGroup::getEditHTML( $primary_org_id, $category, null, false, $reset, null, $doNotProcess, $ctype );
    } else {
        CRM_Core_Error::fatal( ts( 'The requested Profile (gid=%1) is disabled OR it is not configured to be used for \'Profile\' listings in its Settings OR there is no Profile with that ID OR you do not have permission to access this profile. Please contact the site administrator if you need assistance.',
                                   array( 1 => $profileID )));
    }
   
    if ( $html ) {
        $html = civicrm_add_jquery( $html );
        $index = empty( $category ) ? 'civicrm-profile-my-account' : $category;
        $output[$index][] = array( '#title' => $category,
                                   '#value' => $html,
                                   '#weight' => 1 );
    }

    return $output;
}

crispinbailey

  • I post occasionally
  • **
  • Posts: 30
  • Karma: 1
  • CiviCRM version: 4.0.5
  • CMS version: Drupal 7
  • MySQL version: 5.1
  • PHP version: 5.3
Re: User cannot edit permitted related profile via Drupal My Account
August 16, 2011, 06:39:37 pm
Has anyone managed to get this working in CiviCRM 4 w/ Drupal 7 yet? I tried creating a custom module using ToddW's code but no luck. :(

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: User cannot edit permitted related profile via Drupal My Account
August 16, 2011, 07:09:33 pm
see comment re Webform-CiviCRM options on other thread http://forum.civicrm.org/index.php?topic=4972

(yes i see you are the same person, so this is for others who might find their way here)
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Profiles (Moderator: Dave Greenberg) »
  • User cannot edit permitted related profile via Drupal My Account

This forum was archived on 2017-11-26.