Hi,

UR seems to rock! :)
As I'm a designer, I have maybe two stupid questions:

1. I'm theming the profile and content profile templates, so I need a "add to contacts" link if users are not yet in relationship; otherwise a "remove from contacts" link.

I've found this snippet:

$mybuddy = !!($relationships = user_relationships_load(array('between' => array($user->uid, $GLOBALS['user']->uid)))) ? 1 : 0;
print($mybuddy); 

Am I right that '1' should appear if users are in relationship, otherwise '0'?
When I use this snippet I always get '0', whether in relationship or not. I'm a designer, so my php skills are poor.
FYI: I use Drupal 6.10, the core profile in conjunction with content profile (6.x-1.0-beta3).

2. Sorry, if my thoughts are too poorly conceived... I have to hide some personal profile fields from users who are not in relationship.
If the snippet above works, is it possible to replace the field information with the '1' and the 'else show nothing' text with the '0'?
Or how would you realize this? If important to know: There are only cck fields to hide.

It would be so great if somebody could help me.

Thanks a lot in advance guys.

Stefan

Comments

MisterSpeed’s picture

Both the user you have compared above have same uid (as $user->uid and $GLOBALS['user']->uid returns same result) thats why it is always returning 0. Add current user id from $user as u have done and other user id for which you r checking for.

design.er’s picture

Hey, thanks for your reply.

I ended up with this snippets and it works fine for me. I want to post my solutions for other designers without enough php skills. Please, feel free to correct me if you think there are better ways to handle this.
Note: I use the content profile.module, so I use $node->uid instead of $account->uid. It works fine for me. And I only use 1 kind of relationship, so I use a static number (1) in all my links. Maybe somebody could provide a more elegant way to create dynamic links.

To create an "add to contacts"/"remove from contacts" link:

 if ($node->uid != $user->uid) {
		  if ($relationships = user_relationships_load(array('between' => array($user->uid, $node->uid)))) { print "<li class='profil-links'><a href=\"$uid/relationships/1/remove\" class=\"user_relationships_popup_link\">" . t('remove from contacts') . "</li>\n"; }
		else { print ("<li class='profil-links'><a href=\"" . $GLOBALS['base_url'] . "/relationship/$node->uid/request/1\" class=\"user_relationships_popup_link\">" . t('add to contacts') . "</li>\n"); } } 

If you'd like to hide individual profile fields from "foreign" users, you can use this example for phone numbers:

 if ($relationships = user_relationships_load(array('between' => array($user->uid, $node->uid)))) {
             if (!empty($node->field_phone[0]['view'])) { print ("<div><span class=\"bold\">" . t('Phone') . ":</span>\n        " . check_plain($node->field_phone[0]['view']) . "    </div>\n"); }
		     }
		else { if ($node->uid == $user->uid) { if (!empty($node->field_phone[0]['view'])) { print ("<div><span class=\"bold\">" . t('Phone') . ":</span>\n        " . check_plain($node->field_phone[0]['view']) . "    </div>\n"); } }
		     }
   

And this snippet for content taxonomy fields - in my example for "City". You have to change the figure "4" with the id of your vocabulary:

 if ($relationships = user_relationships_load(array('between' => array($user->uid, $node->uid)))) {
	         if (count($taxonomy)):
                 $terms = taxonomy_node_get_terms_by_vocabulary($node, 4);
                 if ($terms) {
                     foreach ($terms as $key => $term) {
                              $items4[] = l($term->name, taxonomy_term_path($term), array('attributes' => array('rel' => 'tag', 'title' => $term->description))); }
                     print "<div><span class=\"bold\">" . t('City') . ":</span> " . implode($items4) . "</div>\n";
                 }
             endif; }
	   else {
		      if ($node->uid == $user->uid) {
				 if (count($taxonomy)):
					 $terms = taxonomy_node_get_terms_by_vocabulary($node, 4);
					 if ($terms) {
						 foreach ($terms as $key => $term) {
								  $items4[] = l($term->name, taxonomy_term_path($term), array('attributes' => array('rel' => 'tag', 'title' => $term->description))); }
						 print "<div><span class=\"bold\">" . t('City') . ":</span> " . implode($items4) . "</div>\n";
					 }
				 endif;
			     }
			} 

I hope that will help other non-programmers.

Regards,
Stefan

MisterSpeed’s picture

Assigned: Unassigned » MisterSpeed
Liliplanet’s picture

Thank you for the snippet Stefan, I'm also using content profile with the following:

<?php if (count(array_intersect($user_roles, $approved_roles)) > 0) { ?>
<div><?php
if ($node->uid != $user->uid) {
          if ($relationships = user_relationships_load(array('between' => array($user->uid, $node->uid)))) { print "<a href=\"$uid/relationships/1/remove\" class=\"user_relationships_popup_link\">" . t('Remove from my Network') . "\n"; }
        else { print ("<a href=\"" . $GLOBALS['base_url'] . "/relationship/$node->uid/request/1\" class=\"user_relationships_popup_link\">" . t('Add to my Network') . "\n"); } }
?>

To 'Add to my Network' works great, but when I try 'Remove from my Network', the popup shows, but does not load anything, just stays stuck.

Any help would be most appreciated. (Actually would even prefer if it was not a popup, but a 'normal link' as with the http://www.drupal.org/project/flags module) in other words, by clicking on the link just reloads the page with a confirmation message.)

But just to make it work, would be fabulous.

Look forward to hearing from you.
Lilian

kvvnn’s picture

MisterSpeed’s picture

Assigned: MisterSpeed » Unassigned
drupalina’s picture

Hi,
I used the first code in comment #2 in views and custom field. It almost works, but I need a little push to make it work with 1-way twitter-style following/followers relationships.
So far I've got:

<?php
          if ($relationships = user_relationships_load(array('between' => array($user->uid, $data->users_user_relationships_uid)))) { print "<li class='profil-links'><a href=\"$data->users_user_relationships_uid/relationships/3/remove\" class=\"user_relationships_popup_link\">" . t('Un-follow') . "</li>\n"; }
        else { print ("<li class='profil-links'><a href=\"" . $GLOBALS['base_url'] . "/relationship/$data->users_user_relationships_uid/request/3\" class=\"user_relationships_popup_link\">" . t('+Follow') . "</li>\n"); }  

?>

The "+ Follow" links work beautifully and open up in an Ajax popup.
But because these are 1 way relationships, the direction of the relationship should also be checked. "I'm following Mike" is not the same as "Mike is following me" - checking for the existence of the relationship is not enough. It should also check the direction of the relationship.

So, if the logged in user is viewing a user whom he is following, it should say "Following - (un-follow)"
Otherwise, it should just give a link "+ Follow".
So even if the logged in user is viewing another user who is already following him, it should still say "+ Follow"

Can anyone help with the above code? Please! (it would be a great use to anybody who is working with twitter-style user relationships)

mrf’s picture

Issue tags: -CCK, -user relationship, -hide fields

You'll need to check if the is_oneway and is_reciprocal values are true then present the link to follow the user.

Something along the lines of (I haven't tested this...)

<?php
if ($relationships = user_relationships_load(array('between' => array($user->uid, $data->users_user_relationships_uid)))) { 
  print "<li class='profil-links'><a href=\"$data->users_user_relationships_uid/relationships/3/remove\" class=\"user_relationships_popup_link\">" . t('Un-follow') . "</li>\n";    
} else if ($relationships = user_relationships_load(array('between' => array($user->uid, $data->users_user_relationships_uid),'is_oneway' => true, 'is_reciprocal' => true))) { 
  print ("<li class='profil-links'><a href=\"" . $GLOBALS['base_url'] . "/relationship/$data->users_user_relationships_uid/request/3\" class=\"user_relationships_popup_link\">" . t('+Follow') . "</li>\n"); }  
} else { print ("<li class='profil-links'><a href=\"" . $GLOBALS['base_url'] . "/relationship/$data->users_user_relationships_uid/request/3\" class=\"user_relationships_popup_link\">" . t('+Follow') . "</li>\n"); }  
?>
mrf’s picture

Status: Active » Fixed
mrf’s picture

Component: Code » Documentation
Category: support » task
Status: Fixed » Active

Would be great to create a documentation page that describes how to do this, some sort of combination of the information in this issue, and over at #902908: How to add (print) custom button: "Add to friends" into custom user-profile.tpl.php