Hi,

I've done an extensive search, but don't seem to come up with a solution.

Is it possible to have a little star (image) next to a specific user role (weight)? Basically in my views I would like to define Platinum users by a little star to show visitors that they have a full profile.

Any help would be most appreciated.

Lilian

Comments

panis’s picture

you may have to work with your themes files to achieve this depending on where exactly you want to display the little star. Assuming you want to display the star next to the user name do the following in your template file. This will display a star next to wherever the username is displayed..

function phptemplate_username($object) {
   $user = user_load(array('uid'=>$object->uid));
   if (in_array('platinumuser', $user->roles)) {
      return theme_username($object) . '<img src="/images/star.gif">';
   }
   return theme_username($object);
}

The exact code will change based on where you are thinking of displaying your star.. If you wish to only display this on the profile page i.e. when you go to http://www.mysite.com/user/ then you will need to create a theme for the user page and add code like the above in it at the appropriate location.

liliplanet’s picture

Thank you so much for your reply Panis.

I've tried platinummembers as the role is called Platinum Members ...

function phptemplate_username($object) {
$user = user_load(array('uid'=>$object->uid));
if (in_array('platinummembers', $user->roles)) {
return theme_username($object) . 'Only local images are allowed.';
}
return theme_username($object);
}

but nothing happening this side. Is it also perhaps possible to use field_name instead of username although username would be fantastic.

Most most appreciate your help.
Lilian

panis’s picture

where exactly do you need the start to appear - do you have a screenshot - maybe the location yuo want the image to appear is not where the username is being displayed.

liliplanet’s picture

So much appreciated, please see http://www.filmmaker.co.za/planet/member_directory.php

Would like to have the * (start) next to the username, also it would be great that where the username appeared anywhere on the site to show the star next to Platinum Members.

So kind .. thank you.
Lilian

panis’s picture

what are you using to generate this directory?
Is that a view? if so where are you pulling the author name from? if that is not the standard node author for the profile node name then the mechanism I described to you will not work. If this is custom code used to generate the table then you need to update the custom code to include the '*'.

liliplanet’s picture

Hi Panis,

Thank you for your reply. Panis, yes it is in views, but would like it throughout the site next to the username everywhere as well.

I had to create a 'authorship' field as doing an import through the node import module saved as 'author' user/1 or admin

Is it possible to have the * next to a node_field ie field_name? but just username throughout the site would be wonderful.

What I'm trying to do is have Platinum Members appear different to normal users in the views. Some way to differentiate the members.

Look forward to your response, and again, thank you Panis.

Lilian

panis’s picture

not an easy fix then...

Options:
a) update the author for each of these items to the actual user - this will have other implications because now they can edit their own profiles - however you could disable "edit own " through permissions - if you do not want that to happen.

If you have access to mysql you can do this via: (take a backup of the database before you do this)

update node,profile_table,user set node.uid=user.uid where node.nid=profile_table.nid and profile_table.authorship=user.name;

replace profile_table with the cck table name (something like content_type_profile_table) and the authorship field name with the appropriate field name in the table - usually it is of the form field_authorship_value

if you have table prefixes in your drupal settings then you will need to prefix the table names appropriately.

This should reset the username field to the actual user and then in the view display the "Node: Author name".. Now override the username theme with the code snippet I gave you and it should work..

You could also resort to updating the authorship field to have a star in the end through mysql commands based on which of these users are platinum members. Anywhere in your site now instead of displaying "authorship" field now display the "Node: Author field" and things should work as you want them to.

b) this is a very different approach with no table changes.. You will need to override the theme of the table.. in your template.php file copy paste the theme_views_view_table function from the views.module file. rename the function to: phptemplate_views_view_table_machine_name_of_view_here()...

Now you know that the first field that is being generated in the code is the authorship field - use that to figure out the role for the user and print a * next to it..

This is not as flexible as option (a) but you do not have to mess around in the databases.

mooffie’s picture

Panis, as always, you have thorough answers!
:-)

(I wonder why she has three names associated with a row. perhaps she should use several userreferences.)

mooffie’s picture

if (in_array('platinummembers', $user->roles)) {
[...]
I've tried platinummembers as the role is called Platinum Members ...

No, you must type 'Platinum Members' there. Not 'platinummembers'.

Panis' code should work.

liliplanet’s picture

Yes! All is good and the the * appears!

Panis, just a small thing (I hope) .. the image appears after the username*, is it perhaps possible to have it before the *username. I think it would just look prettier.

Thank you so much for your support.
Lilian

panis’s picture

just change the code to reorder where the <img src=...> or * is in the print function call - and thanks to moofie for pointing out the correct role to use.

liliplanet’s picture

Hi Mooffie and Panis,

Thank you so kindly for helping me a couple of weeks ago. Please, I now have 2 objects that I would to include in the phptemplate_username

your fabulous script:

function phptemplate_username($object) {
   $user = user_load(array('uid'=>$object->uid));
   if (in_array('Platinum Members', $user->roles)) {
      return theme_username($object) . '<img src="/images/star.gif">';
   }
   return theme_username($object);
}

and username to first name and last name:

function phptemplate_username($object, $link = TRUE) {
if ( $object->uid ) {
$type = "profile";
  $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";
  $nid = db_result(db_query($sql, $object->uid, $type));
   if ( $nid ) {
   $profile = node_load($nid);
  }
}
$name = $profile->field_name[0]['value'] . ' ' . $profile->field_lastname[0]['value'] ;
if ( $name ) { 
    if ( $link && user_access('access user profiles')) {
      return l($name, 'user/'. $object->uid, array('title' => t('View Member Profile.')));
    }
    else {
      return check_plain($name);
    }
}

// Profile field not set, default to standard behaviour

  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }

    if ( $link && user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', 'Guest');
  }

  return $output;
}

function phptemplate_user_list($users, $title = NULL) {
  if (!empty($users)) {
    foreach ($users as $user) {
      $items[] = theme('username', $user);
    }
  }
  return theme('item_list', $items, $title);
}

I really have tried to somehow put them together, and only get one to work at a time :)

Please Mooffie and Panis, would you be so kind to show the in the right direction?

Look forward to your reply.
Liilan

panis’s picture

rename the second function to something else.. e.g.

mytheme_firstlastname_theme()...

and replace calls to theme_username() in the first function to call mytheme_firstlastname_theme()

make sure that your first function has the signature phptemplate_username($object, $links) just like the second one and pass the $links variable on to the mytheme_firstlastname_theme()..

liliplanet’s picture

Thank you for your super-fast reply.

I've tried changing username to starname and do not receive errors :), but then again, do not see the star * either .. is this not correct? so sorry, have very little experience with templating.

See second function lower down the page ...

function phptemplate_username($object, $link = TRUE) {
if ( $object->uid ) {
$type = "profile";
  $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";
  $nid = db_result(db_query($sql, $object->uid, $type));
   if ( $nid ) {
   $profile = node_load($nid);
  }
}
$name = $profile->field_name[0]['value'] . ' ' . $profile->field_lastname[0]['value'] ;
if ( $name ) { 
    if ( $link && user_access('access user profiles')) {
      return l($name, 'user/'. $object->uid, array('title' => t('View Member Profile.')));
    }
    else {
      return check_plain($name);
    }
}

// Profile field not set, default to standard behaviour

  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }

    if ( $link && user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', 'Guest');
  }

  return $output;
}

function phptemplate_user_list($users, $title = NULL) {
  if (!empty($users)) {
    foreach ($users as $user) {
      $items[] = theme('username', $user);
    }
  }
  return theme('item_list', $items, $title);
}

///this is the changes

function phptemplate_starname($object) {
   $user = user_load(array('uid'=>$object->uid));
   if (in_array('Platinum Members', $user->roles)) {
      return theme_starname($object) . '<img src="/images/star.gif">';
   }
   return theme_username($object);
}

You mention mytheme in your reply ... my theme is called zen_planet. Sorry Panis, quite lost here.

Look forward to your reply and again, thank you so much.
Lilian

panis’s picture

2 Options below not knowing exactly your requirements

Option 1 - replace the last two functions with

function phptemplate_user_list($users, $title = NULL) {
  if (!empty($users)) {
    foreach ($users as $user) {
      $items[] = theme('starname', $user, $title); /** CHANGE username with starname **/
    }
  }
  return theme('item_list', $items, $title);
}

///this is the changes
function phptemplate_starname($object, $title) {
   $user = user_load(array('uid'=>$object->uid));
   if (in_array('Platinum Members', $user->roles)) {
      return theme_username($object, $title) . '<img src="/images/star.gif">'; /** CHANGE starname to username **/
   }
   return theme_username($object, $title);
}

Option 2 - If you want to make the username show with a star everywhere then you will need to flip it around

function phptemplate_fullname($users, $title = NULL) { /** RENAME your username function to fullname */
    /* CODE for function does not CHANGE - keep your original code here.. */
}

function phptemplate_user_list($users, $title = NULL) {
  if (!empty($users)) {
    foreach ($users as $user) {
      $items[] = theme('username', $user, $title); /** No change here **/
    }
  }
  return theme('item_list', $items, $title);
}

///this is the changes
function phptemplate_username($object, $title = NULL) {
   $user = user_load(array('uid'=>$object->uid));
   if (in_array('Platinum Members', $user->roles)) {
      return theme_fullname($object, $title) . '<img src="/images/star.gif">'; /** CHANGE fullname **/
   }
   return theme_fullname($object, $title); /** CHANGE to fullname **/
}
liliplanet’s picture

Sorry Panis still getting the error when selecting your second option (in other words for the star to appear everywhere)

Fatal error: Cannot redeclare phptemplate_username() (previously declared in template.php:186

function phptemplate_username($object, $link = TRUE) {
if ( $object->uid ) {
$type = "profile";
  $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";
  $nid = db_result(db_query($sql, $object->uid, $type));
   if ( $nid ) {
   $profile = node_load($nid);
  }
}
$name = $profile->field_name[0]['value'] . ' ' . $profile->field_lastname[0]['value'] ;
if ( $name ) { 
    if ( $link && user_access('access user profiles')) {
      return l($name, 'user/'. $object->uid, array('title' => t('View Member Profile.')));
    }
    else {
      return check_plain($name);
    }
}

// Profile field not set, default to standard behaviour

  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }

    if ( $link && user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', 'Guest');
  }

  return $output;
}

function phptemplate_fullname($users, $title = NULL) { /** RENAME your username function to fullname */
    /* CODE for function does not CHANGE - keep your original code here.. */
}

function phptemplate_user_list($users, $title = NULL) {
  if (!empty($users)) {
    foreach ($users as $user) {
      $items[] = theme('username', $user, $title); /** No change here **/
    }
  }
  return theme('item_list', $items, $title);
}

///these are the changes

function phptemplate_username($object, $title = NULL) {
   $user = user_load(array('uid'=>$object->uid));
   if (in_array('Platinum Members', $user->roles)) {
      return theme_fullname($object, $title) . '<img src="/images/star.gif">'; /** CHANGE fullname **/
   }
   return theme_fullname($object, $title); /** CHANGE to fullname **/
}

I tried replacing phptemplate_username with fullname, but got the same error.

Thank you Panis, and look forward to your reply.
Liilan

panis’s picture

a) The very first line of the code you posted - rename the function to phptemplate_fullname.
b) the function immediately after that is a blank function that you misunderstood my instructions - delete that function..

liliplanet’s picture

Panis, I'm really starting to feel quite blonde here and apologize for not getting this. I've tried everything and still get errors.

Currently the error is : warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/mysite/themes/zen_planet/template.php on line 196.

// Function: phptemplate_loadprofile($type, $uid)
// $type is a drupal content type used
// as a node profile

function phptemplate_loadprofile($type, $uid) {
  $profile = NULL;
  $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";
  $nid = db_result(db_query($sql, $uid, $type));
if ( $nid ) {
   $profile = node_load($nid);
  }
 
  return $profile;
}


function phptemplate_fullname($object, $link = TRUE) {
if ( $object->uid ) {
$type = "profile";
  $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";
  $nid = db_result(db_query($sql, $object->uid, $type));
   if ( $nid ) {
   $profile = node_load($nid);
  }
}
$name = $profile->field_name[0]['value'] . ' ' . $profile->field_lastname[0]['value'] ;
if ( $name ) {
    if ( $link && user_access('access user profiles')) {
      return l($name, 'user/'. $object->uid, array('title' => t('View Member Profile.')));
    }
    else {
      return check_plain($name);
    }
}

// Profile field not set, default to standard behaviour

  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }

    if ( $link && user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', 'Guest');
  }

  return $output;
}

function phptemplate_user_list($users, $title = NULL) {
  if (!empty($users)) {
    foreach ($users as $user) {
      $items[] = theme('username', $user, $title); /** No change here **/
    }
  }
  return theme('item_list', $items, $title);
}

function phptemplate_username($object, $title = NULL) {
   $user = user_load(array('uid'=>$object->uid));
   if (in_array('Platinum Members', $user->roles)) {
      return theme_fullname($object, $title) . '<img src="/images/star.gif">'; 
   }
   return theme_fullname($object, $title); 
}

Please, I can imagine it's in there, but honestly have moved, changed, renamed and :) doesn't work.

Would most appreciate your kind help again. Thank you Panis.

panis’s picture

in the function below - the object being passed in may not be a real object sometimes - which would make the in_array function get a null parameter and hence your error.

Look at how the phptemplate_fullname() function handles this and you can add the same check. This should help fix the error you are seeing.

function phptemplate_username($object, $title = NULL) {
   if ($object->uid ) {
      $user = user_load(array('uid'=>$object->uid));
      if ($user->uid && in_array('Platinum Members', $user->roles)) {
         return theme_fullname($object, $title) . '<img src="/images/star.gif">';
      }
   }
   return theme_fullname($object, $title);
}
liliplanet’s picture

Hi Panis,

Can you believe that I'm still trying to make this work (since May ..:) and was hoping I'd figure it out eventually ...

I've moved stuff, replaced, deleted, added and receive 'Fatal error: Call to undefined function theme_fullname() in /home/mysite/public_html/sites/all/themes/bluebreeze/template.php on line 97'

With the following it works, but no 'star *' next to the Platinum Member.

Please Panis I'm prepared to pay for your help, if you be so kind to figure this out. It's pretty important to define my members in their roles.

function phptemplate_loadprofile($type, $uid) {
  $profile = NULL;
  $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";
  $nid = db_result(db_query($sql, $uid, $type));
if ( $nid ) {
   $profile = node_load($nid);
  }

  return $profile;
}

function phptemplate_starname($object) {
   $user = user_load(array('uid'=>$object->uid));
   if (in_array('Platinum Members', $user->roles)) {
      return theme_username($object) . '<img src="/images/star.gif">';
   }
   return theme_username($object);
}

function phptemplate_username($object, $link = TRUE) {
if ( $object->uid ) {
$type = "profile";
  $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'";
  $nid = db_result(db_query($sql, $object->uid, $type));
   if ( $nid ) {
   $profile = node_load($nid);
  }
}
$name = $profile->field_name[0]['value'] . ' ' . $profile->field_lastname[0]['value'] ;
if ( $name ) {
    if ( $link && user_access('access user profiles')) {
      return l($name, 'user/'. $object->uid, array('title' => t('View Member Profile.')));
    }
    else {
      return check_plain($name);
    }
}

// Profile field not set, default to standard behaviour

  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }

    if ( $link && user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = variable_get('anonymous', 'Guest');
  }

  return $output;
}

function phptemplate_user_list($users, $title = NULL) {
  if (!empty($users)) {
    foreach ($users as $user) {
      $items[] = theme('username', $user);
    }
  }
  return theme('item_list', $items, $title);
}

function favoritenodes_count ($uid) {
     $sql = "SELECT COUNT(*) FROM {favorite_nodes} WHERE uid = %d";
     return db_result(db_query($sql, uid));
}


// Override the display of the nodeprofile to just show the node itself and not the surrounding markup or the tabs.
// Theming of the nodeprofile will be handled separately. Note that this affects it wherever it is displayed and 
// not only on the user page.
function phptemplate_nodeprofile_display_box($element, $content) {
  $head = isset($element['#title']) ? '<h2 class="nodeprofile-title">'. check_plain($element['#title']) .'</h2>' : '';

  return $content;
}

// Override the entire user account view page to not show the categories or to seperate items into divs. 
// Note that this is set up to still show everything that is thrown on this page. Getting rid of non-nodeprofile 
// sections is done in the node_profile_bonus module by unsetting them but could also be done here by conditionally
// printing them to begin with.
function phptemplate_user_profile($account, $fields) {
  foreach ($fields as $category => $items) {
    foreach ($items as $item) {
      $output .= $item['value'] ;
    }
  }

  return $output;
} 

Look forward to hearing from you, and thank you.
Lilian