Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Display UserPhoto in "Menu"

2

Answers

  • peregrineperegrine MVP
    edited April 2012 Answer ✓

    In the continuing saga you can see which works best for you the one above or this one, if any.

    <?php
    $Session = Gdn::Session();
    if ($Session->IsValid()) {
    if(isset($Session->User->Photo)) {
    $User = UserBuilder($Session->User);
    echo UserPhoto($User, array('LinkClass'=>'','ImageClass'=>'ProfilePhotoSmall')) ; 
        }
     }
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Both solutions worked well! I went for your last suggestion above because of the user avatar beeing displayed in right proportions without beeing stretched/skewed.
    Thank you for your time, pregrine! I wish to bother you with one last question.

    What is the exact code for displaying the users' username as a clickable link?
    My code for displaying the username for now, is:
    <?php echo $Session->User->Name ?> in default.master.php.

  • chjohan said:
    What is the exact code for displaying the users' username as a clickable link?

    I managed to do this myself by using: <?php echo UserAnchor($User); ?>

    Thank you all for your answers!

  • peregrineperegrine MVP
    edited April 2012

    Great. You're learning, just experiment and things gradually become clearer and clearer.
    whew!

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Just used your final solution on my forum - works like a charm, thanks so much for sharing ;)

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited March 2013

    This is a great Thread, I was looking for ways to make the user name in the menu display the user image instead of the text/name...Using this method gave me the avatar but not replacing the text link , just added it above the menu. I also pilfered the who's online plugin for snippets but my results have not been satisfactory :(

    I want to add the user image to this link

    $this->Menu->AddLink('User',  $Name,  '/profile/{UserID}/{Username}',array('Garden.SignIn.Allow'), array('class' => 'UserNotifications'));  
    
    

    tried different combos with odd results...what would be the best way to add the user image to that link?

    I thought I could do it like this but I know it is not right:

    $this->Menu->AddLink('User',  Img('unknown link to user photo', array('title' => T('{Username}'),  '/profile/{UserID}/{Username}',array('img class' =>'ProfilePhotoSmall'),array('Garden.SignIn.Allow'), array('class' => 'UserNotifications'));     
    
    
  • KasperKasper Scholar of the Bits Copenhagen Vanilla Staff
    edited March 2013

    A pretty neat Smarty variable was assigned for this sort of stuff: {$User}. It's only accesible through the Smarty templates though, but the function can easily be rewritten so you can use it elsewhere:

    public function User($Sender) {
      $Session = Gdn::Session();
      if($Session->IsValid()) {
         $User = array(
            'Name' => $Session->User->Name,
            'Photo' => '',
            'CountNotifications' => (int)GetValue('CountNotifications', $Session->User, 0),
            'CountUnreadConversations' => (int)GetValue('CountUnreadConversations', $Session->User, 0),
            'SignedIn' => TRUE);
    
         $Photo = $Session->User->Photo;
         if ($Photo) {
            if (!preg_match('`^https?://`i', $Photo)) {
               $Photo = Gdn_Upload::Url(ChangeBasename($Photo, 'n%s'));
            }
         } else {
            if (function_exists('UserPhotoDefaultUrl'))
               $Photo = UserPhotoDefaultUrl($Session->User, 'ProfilePhoto');
            elseif ($ConfigPhoto = C('Garden.DefaultAvatar'))
               $Photo = Gdn_Upload::Url($ConfigPhoto);
            else
               $Photo = Asset('/applications/dashboard/design/images/defaulticon.png', TRUE);
         }
         $User['Photo'] = $Photo;
      } else {
         $User = FALSE; /*array(
            'Name' => '',
            'CountNotifications' => 0,
            'SignedIn' => FALSE);*/
      }
      return $User;
    }
    

    Put this in your theme hooks and you can to the following (tried and tested):

    $this->User()['Photo'];
    

    Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    lol that wiped out everything , I am not getting this treat me like a noob lmao

  • KasperKasper Scholar of the Bits Copenhagen Vanilla Staff
    edited March 2013

    Lol, no worries :-) What you need to do is simply copy/paste the function I posted into your theme hooks inside the []ThemeHooks class:

    <?php if (!defined('APPLICATION')) exit();
    
    class YourThemeHooks implements Gdn_IPlugin {
    
        [User() function goes here]
    
        /**
         * No setup required
         */
        public function Setup() {
            return TRUE;
        }
    
        /**
         * No cleanup required
         */
        public function OnDisable() {
            return TRUE;
        }
    
    }
    

    Then simply write the following to get the profile picture for the currently logged in user:

    $this->User()['Photo'];
    

    ...or if you want to do it directly in the master view (this should work in theory):

    YourThemeHooks::User()['Photo'];
    

    Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    I am able to add the image but what I get is the link not the image lmao Sometimes I get this stuff and sometimes I am lost....this is a php based theme not tpl or smarty. No themehooks except for the master.default.php .. That is where I am trying to put this stuff arg!

  • KasperKasper Scholar of the Bits Copenhagen Vanilla Staff
    edited March 2013

    If you want to turn the image src into an <img> tag, do this:

    Img($this->User()['Photo'])
    

    ...or this:

    Img(YourThemeHooks::User()['Photo'])
    

    Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited March 2013

    no it did not like the [ ] gives me syntax errors and unexpected this and that , I have been trying to figure this out for a while and thought maybe by now I could get a handle on it but seems I am still too green to be successful... I will keep trying some day it will dawn on me I hope thanks for the time and effort I really should understand this by now and it is embarrassing . how is it that I can replace anything with images and this I can't ?

    Funny thing is it does give me the image but in text form as in the url of the image.
    This is the section I am trying to add this to, I reverted the Photo changes I made this is how it looks original.

     $Authenticator = Gdn::Authenticator();
                            if ($Session->IsValid()) {
                                $Name = $Session->User->Name;
                                $CountNotifications = $Session->User->CountNotifications;
                                if (is_numeric($CountNotifications) && $CountNotifications > 0)
                                    $Name .= ' '.$CountNotifications.'';
                                
                
          
                        
                                $this->Menu->AddLink('User', $Name, '/profile/{UserID}/{Username}',array('Garden.SignIn.Allow'), array('class' => 'UserNotifications'));  
    
    

    I replaced $Name with $Photo and tried a variety of things as well as your advice. Where in that section would you insert the changes, Maybe I need to make a theme hooks file for this theme....but I want to simplify not more complex, the point of the user image on the menu is to save menu space...specially if user has a long name..smells like what I need is a plugin called UNPhoto

  • KasperKasper Scholar of the Bits Copenhagen Vanilla Staff

    You'll need to put the code I posted in a theme hooks file yes - as for simplicity, more is often less. A core concept of OOP is keeping your data in different and easily accesible objects - keeping your objects in separate files is a good idea after which you can access them using methods in e.g. your views. Here the ThemeHooks is your object and it lets you access user data using the User() method.

    Kasper Kronborg Isager (kasperisager) | Freelance Developer @Vanilla | Hit me up: Google Mail or Vanilla Mail | Find me on GitHub

  • You'll need to put the code I posted in a theme hooks file - as for simplicity, more is often less

    seconded.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    I went to dinner , needed a break , ok so the solution is to make a theme hooks.php for my theme to put the code above in there, ok I shall give it a whack and report...

  • vrijvlindervrijvlinder Papillon-Sauvage MVP
    edited March 2013

    more is often less

    wow the opposite of make up application...bass lines, food intake, I was trained to think less is more !! I must retrain my brain to think more is less. I learned that about the plugin relationship, more modules, less work along the line. The more work you put into a relationship the less you like the other person...I guess I could learn to think in those terms! :)

  • peregrineperegrine MVP
    edited March 2013

    @vrijvlinder said:

    I was trained to think less is more !!

    think of it as "More || Less" (maybe translation "mas o menos")

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    Sad to report I was too dense to get it to work...I added the class.vrijthemehooks.php as instructed with Kasper's code, tried to add the user part to the default but it rejected the [ ] and from then on just a minefield of syntax errors ... If I could understand the flowchart , but frustration has got the best of me on this matter..... I just can't seem to do it :(

  • peregrineperegrine MVP
    edited March 2013

    post what you got and which files you put it in.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    this is the vrijhooks I put it in the theme folder

    
    <?php if (!defined('APPLICATION')) exit();
    
    class VrijThemeHooks implements Gdn_IPlugin {
    
    public function User($Sender) {
      $Session = Gdn::Session();
      if($Session->IsValid()) {
         $User = array(
            'Name' => $Session->User->Name,
            'Photo' => '',
            'CountNotifications' => (int)GetValue('CountNotifications', $Session->User, 0),
            'CountUnreadConversations' => (int)GetValue('CountUnreadConversations', $Session->User, 0),
            'SignedIn' => TRUE);
         $Photo = $Session->User->Photo;
         if ($Photo) {
            if (!preg_match('`^https?://`i', $Photo)) {
               $Photo = Gdn_Upload::Url(ChangeBasename($Photo, 'n%s'));
            }
         } else {
            if (function_exists('UserPhotoDefaultUrl'))
               $Photo = UserPhotoDefaultUrl($Session->User, 'ProfilePhoto');
            elseif ($ConfigPhoto = C('Garden.DefaultAvatar'))
               $Photo = Gdn_Upload::Url($ConfigPhoto);
            else
               $Photo = Asset('/applications/dashboard/design/images/defaulticon.png', TRUE);
         }
         $User['Photo'] = $Photo;
      } else {
         $User = FALSE; /*array(
            'Name' => '',
            'CountNotifications' => 0,
            'SignedIn' => FALSE);*/
      }
      return $User;
    }
     /**
         * No setup required
         */
        public function Setup() {
            return TRUE;
        }
        /**
         * No cleanup required
         */
        public function OnDisable() {
            return TRUE;
        }
    }
    
    

    then tried using

    Img($this->User()['Photo'])

    ...or this:

    Img(VrijThemeHooks::User()['Photo']) it did not like the [ ] tried removing them rewriting it no luck

    in the section for the link in the default.php

    $this->Menu->AddLink('User', $Name,(put the img code above here) '/profile/{UserID}/{Username}',array('img class' =>'ProfilePhotoSmall'),array('Garden.SignIn.Allow'), array('class' => 'UserNotifications'));  
    
    
Sign In or Register to comment.