Login-logout - changing Joomla menus according to state
Monday, 05 July 2010
Article Index
Login-logout - changing Joomla menus according to state
A Javascript solution

Banner

To use PHP to modify the completely rendered template you would have to create a plug-in to handle the page rendered event and modify the page at the point just before it is served to the browser.

This is possible but once again seems like overkill for a change as small as rewriting a menu item depending on the state of the system.

The only other approach that allows PHP to be used would be to add the code to a custom module that was positioned so as to guarantee that it was processed after the menu module we need to modify. Now this is beginning to sound very messy and not a good way to work.

One possible solution is to do the job on the client - i.e. write a browser script. This sounds good but there is the problem of determining the state of the system After all its very easy to discover if the user is logged in on the server using PHP and the Joomla framework. It's difficult if not impossible to find out if the user is logged in using Javascript on the browser.

Of course there is nothing stopping us from writing some PHP code - most easily in the template - that tests the state of the system and then writes the appropriate Javascript into the page.

This all seems a lot simpler when we actually write out the code.

The first thing to do is get a user object:

<?php 
$user=& JFactory::getUser();

and then test to see if the user is logged on or not:

if(!($user->get('guest'))){

The reason for the odd test is that a user not logged on has the status of guest but a logged on user can have a number of different states.

If the user is logged on then we create the script. If the user isn't logged on there is nothing to do because the menu item says "logon" by default:

$script= <<<EOT
<script>
$('#pillmenu SPAN:contains("Login")')
.html('Logout');
</script>
EOT;

We use HereDoc to create a variable holding the script. In this case the Javascript uses Jquery to find the tag with id "pillmenu", then all of the child SPAN tags of that tag and finally the only SPAN tag that contains Login. You don't have to use Jquery if you don't want to - direct DOM calls can do the same job - but it is quicker. Finally we use the html method to change the content of the tag from Login to Logout. Finally the script is echoed to the page:

 echo $script;
};
?>

Putting all of this together gives:

<?php 
$user=& JFactory::getUser();
if(!($user->get('guest'))){
$script= <<<EOT
<script>
$('#pillmenu SPAN:contains("Login")')
.html('Logout');
</script>
EOT;
echo $script;
};
?>

This can be inserted into the template anywhere that is legal. I placed it close to the menu it modifies complete with some comments to explain how it works but this is just to make maintenance easier.

With this addition to the template the Logon menu item

login

now changes to Logout when the user is logged in.

logout

The same idea can be used to change any menu dynamically. Simply test the state using PHP in the template, generate the Javascript code needed to find and change the menu item and let the client do the rest.

 

Banner

<ASIN:0596804946>

<ASIN:0470438533>

<ASIN:0137012314>

<ASIN:1847191304>



Last Updated ( Monday, 05 July 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.