Friday, November 11, 2011

How to show modal popup window on login in joomla

Wanted to show an article in a modal popup window to the user when log-in to the site first time & the article is updated as well. So that created a system plugin & triggered the onAfterRender event. Checked the userlastvisitdate. If its 0000-00-00 00:00:00 excuted a javascript code which shows popup modal to the logged in user. Its easy to write what i did but very diffcult at the time of coding. code is as follows
function onAfterRender()
{
$user = &JFactory::getUser();
$js = '';
$plugin = JPluginHelper::getPlugin( 'system', 'popupmodal' );
$plugin_params = new JParameter( $plugin->params );
$first_article = $plugin_params->get('firstlogin_article');
if($user->lastvisitDate == "0000-00-00 00:00:00")
{

$js = <<<EOT
<script>
var dummylink = new Element('a', {
href: "index.php?option=com_content&view=article&id=<?php echo $first_article; ?>&tmpl=component" ,
rel: "{handler: 'iframe', size: {x: 350, y: 350}}"
});
SqueezeBox.fromElement(dummylink);
</script>
EOT;
}
$user =''; // i stuck at this line cause when user logged in still i was getting lastvisitdate 0.
$body = JResponse::getBody();
$body = str_replace('</body>', $js.'</body>', $body);
JResponse::setBody($body);

}
Half work was done here. Now needed to show this popup modal to all the user of site when they log-in when article which showing in popup modal updated. It was tricky first we need to check whether that article has been updated. To check that we triggered onAfterRoute & execute the follwing code which checks article id & whether the task is save if it is then update lastvisitDate for all user so that our above condition satisfied & every user can see popup modal on log-in.
function onAfterRoute()
{
$id = JRequest::getVar('id');
$task = JRequest::getVar('task');
$plugin = JPluginHelper::getPlugin( 'system', 'popupmodal' );
$plugin_params = new JParameter( $plugin->params );
$frst_art = $plugin_params->get('firstlogin_article');
if($id == $first_article && $task == "save")
{
$db = JFactory :: getDBO();
$query = 'UPDATE #__users SET lastvisitDate = "0000-00-00 00:00:00"';
$db->setQuery($query);
$db->query();
}
}
I'll save your work you can download the plugin here

No comments:

Post a Comment