Jump to content

$user and $users colliding?


thomas
 Share

Recommended Posts

Hello,

I have a strange problem here.

Long story:

- I added an Image field to my user template

- On my page I have a little login box in a sidebar, which is displayed whenever the current user isn't guest. It the checks if the user has an image uploaded:

<?php if($user->image->url):?>
  <img src="<?=$user->image->size(60,60)->url;?>" style="float:right;">
 <?php endif;?>

This works. Now to the strange part:

In the main content of the page I want to display a profile page for any givven user registered. I took this approach from Ryan's BlogProfile:

$name = $sanitizer->pageName($input->urlSegment1);
$profile = $users->get($name);
<?php if($profile->image->url):?>
  <img src="<?=$profile->image->size(100,100)->url;?>" style="float:left;margin-right:10px;">
 <?php endif;?>

This is on the same page as the login box.

Now I'm getting very strange results. Checking if a file is uploaded constantly fails, so I get errors when there's no image. It's hard to explain and my only two explanations are I'm either stupid or there's something I need to know about images in a user object, or getting info from $users.

Sorry for the weak explanation, can someone help me out?

Thanks,

thomas

Link to comment
Share on other sites

I'm not good with images, but let's try.

So, in your main content, you're trying to display a profile page for the user whose name is given in the first urlSegment, am I correct? This assumption is based on the code above.

Assuming I was right so far, how about these?

1) You've got a page, say /user-profile/ that's using a template with urlSegments in use? The name of the page doesn't matter, but there should be one.

2) And the code you've pasted above is in that templates template file?

3) And you've got /user-profile/thomas in your browser when you want to see profile for user "thomas"?

You should add a check to see if the profile was actually found before accessing any of its properties, like this:

if ($profile->id) {
// profile with the given user name was found, we're good to go

// your code here

} else {
// profile could not be found, display en error or something
}

Can't help much with the image stuff itself, but just trying to dig out some more information for the ones who can. And possibly the problem is somewhere else and you'll get on track by trying to answer my questions.

Link to comment
Share on other sites

Thanks for the hints.

I actually check if the user exists, sorry for not posting it:

if(!$profile->id) $session->redirect('/');

The thing I don't understand is:

<h1 class="blue"><?=$profile->displayname;?></h1>
 <?php if($profile->image->url):?>
  <img src="<?=$profile->image->first()->size(100,100)->url;?>" style="float:left;margin-right:10px;">
 <?php else:?

This displays the displayname correctly and then exits with "Exception: Method Pageimage::first does not exist or is not callable in this context" even though I'm sure there is an image AND <?php if($profile->image->url):?> works on all other sites and occasions for me.

Another mystery: If I go to the profile page without being logged into the admin, everything shows up fine. I open another tab, log into the admin, reload the profile page, and my profile image throws an error again.

Confused ...

Link to comment
Share on other sites

I think it has to do with the users page which has no outputformatting like regular pages. So even a single (max1) image field will be a wire array.

Ryan correct me, but I think $user has outputformatting on and if you retrieve user through $users it is out. So to test you could add a date field and see what that outputs in both cases.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...