Using PHP block to create a personalize messages

Permalink
Hello!

I'm trying to create a welcome message for registered users using the PHP block and $u->getUserName();

I'm not really sure of the syntax for this. I want to include an if/else statement that either welcomes the user or asks him/her to log-in/register.

I tried using some of the code in my footer, which displays the user name, but I get an error message when I try to add it to the block.*

Clearly, I don't know what I'm doing. Any pointers?

Thanks so much for your time and attention on this!!! I really appreciate your help.

Best wishes,

Michelle



* Parse error: syntax error, unexpected $end in /home1/anewdire/public_html/packages/nd_php_package/blocks/nd_php/view.php(4) : eval()'d code on line 4

msmiller
 
johndorsay replied on at Permalink Reply
johndorsay
Here is the easiest way,
Get the login block from the marketplace & install it

http://www.concrete5.org/marketplace/addons/login_block/...

Then look at the source code in the packages/login/blocks/view.php

Here is where you can write a custom message
<?php 
$u = new User();
if ($u->isRegistered()) { ?>
  <?php  
   if (Config::get("ENABLE_USER_PROFILES")) {
   $userName = '<a href="' . $this->url('/profile') . '">' . $u->getUserName() . '</a>';
   } else {
        $userName = $u->getUserName();
   }
?>
<p>Here is where you can say some cool new words to the user <?php echo $userName;?></p>
<a href="<?php echo $this->url('/login', 'logout')?>"><?php echo t('Sign Out')?></a>


You should also ensure your setting are correct in the dashboard - >users and groups -> Login & Registration.

-JD
RadiantWeb replied on at Permalink Reply
RadiantWeb
should just be able to add right b4 that:

$u = new user();
msmiller replied on at Permalink Reply
msmiller
Hello JD,

Thanks so much for the quick reply! I just need a little clarification. What are the correct settings for users and groups?

Do I add the code you gave me to the view file or the php block or both.

I'm sorry, I'm just learning. In the meantime, I'll keep reading this over and playing with it to see if I can figure it out.

Thanks again,

Michelle
johndorsay replied on at Permalink Reply
johndorsay
HI MIchelle,
Sorry if my last message was too cryptic to understand.
The settings I referred to, will basically set up whether new users can register on the site, where they will be directed to once logged in, etc...

The code I pasted above was just a snippet of the view.php file. And where I placed the text:
'Here is were you can say...'
was just to show you where to customize the logged in text.

To add this login block, you should be able to put any page into edit mode and place a new block type 'Login' on the page. This will be at the bottom of the list so scroll all the way down and add the block.

And just to be sure, after you download the Login Block, you upload it to the packages folder, then install the block by going to dashboard ->Add functionality.

And Chad is correct if you are looking to use the user name around the site, you'll need a new instance of the User Class:
<?php $u = new User(); echo $u->getUserName();?>

Hope this helps you out
msmiller replied on at Permalink Reply
msmiller
JD,

Thanks so much for your help with this. I hadn't considered changing the login block, just calling welcoming the user. But, now that I have a little instructions on this, I will see if I can make some changes to it.

Thanks again,

Michelle
msmiller replied on at Permalink Reply
msmiller
Hello Chad,

Thanks for helping. I'm sorry, but I'm still confused. I tried adding this to the php block:

$u = new user(); $u->getUserName();

This isn't what you meant, though. Is it?

I just don't get it. I don't understand why I can reference the username in my footer but not in the PHP Block.

I'll keep looking at it.

Michelle
msmiller replied on at Permalink Reply
msmiller
Is there a way to use the footer php to put the name in memory so I can pick it up and reference it on any page through the PHP blocks?

I'm stumped for now. I do have the Login Add-in through. I'm not sure if I mentioned that earlier.

Thanks for your help!!
jordanlev replied on at Permalink Reply
jordanlev
Hi Michelle,
The Registration settings that John was referring to were just to make sure registration is enabled (so in the "Registration" box at the bottom of that settings page, make sure any of the 3 "Registration is enabled" settings is checked, not the "Registration is disabled" one).

As for the problem with the code, unfortunately johndorsay's code is a tiny bit incomplete. I think what you want to do is start over with a fresh copy of the Login Block add-on (so uninstall the one you have now, re-download the block, then install that new fresh one that doesn't have any modifications to it). Now COPY the packages/login/blocks/view.php file to blocks/login/view.php (you'll probably have to create the "login" folder inside the "blocks" folder to put it in). Now work with THAT file, and paste all of johndorsay's code in just above the "<form class..." line. THEN add this below that code:
<?php } else { ?>

(so the "<form class..." line is now below that "else" line).

Now at the very bottom of that file (below the "</form>" line), add this code:
<?php } ?>


That should do it!

-Jordan
Mnkras replied on at Permalink Reply
Mnkras
well i made a custom template for the login block that can be easily changed to do whatever you want,

<?php  
defined('C5_EXECUTE') or die(_("Access Denied.")); 
Loader::model("user_attributes");  //Load the user attributes model
global $c, $u; //Tap into the global user object
$loginURL= $this->url('/login', 'do_login' );
if (!is_object($u) || is_object($u) && !$u->isLoggedIn() || $c->isEditMode()) {  
?>         
         <style>
         .login_block_form .loginTxt{ font-weight:bold }
         .login_block_form .uNameWrap{ margin:8px 0px; }
         .login_block_form .passwordWrap{ margin-bottom:8px;}
         .login_block_form .login_block_register_link{margin-top:8px; font-size:11px}
         </style>
         <form class="login_block_form" method="post" action="<?php  echo $loginURL?>">
            <?php   if($returnToSamePage ){ ?>


before the else is what non logged in people see after the else is what everyone else sees, in this case its the sidebar from the profile,

(Thanks Green2Go)
msmiller replied on at Permalink Reply
msmiller
Thanks, G2G!

I'll give it try.
msmiller replied on at Permalink Reply
msmiller
Jordan,

Thanks for this. I'm starting to get a better understanding of how powerful and flexible the system is. I'll keep you posted on my progress.

Thanks again,

Michelle
RadiantWeb replied on at Permalink Best Answer Reply
RadiantWeb
I put this into my template...and it worked just fine. I'll test it in the php block:

$u = new user();
if ($u->isLoggedIn()){ 
  echo 'hello '.$u->getUserName().'!';
}


you may have been missing the 'echo'?

Chad
RadiantWeb replied on at Permalink Reply
RadiantWeb
works fine using the php block as well. see attached image.
RadiantWeb replied on at Permalink Reply 1 Attachment
RadiantWeb
sorry. here ya go :-P

yeah...just add the if/else for logged in or not logged in and you are g2g.

C
msmiller replied on at Permalink Reply
msmiller
Chad,

Thanks so much!! This did the trick.

Best wishes,

Michelle