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

Friday, November 4, 2011

How add Javascript in Joomla article easily

Wondering to add Javascript snippet in Joomla article. Here is the easiest way to add.
Goto Joomla Administrator -> site -> Global Configuration.
Select Editor - TinyMCE 2.0 as Default WYSIWYG Editor & save it.
that's it you are now free to write javascript code in articles.
Don't try to change above setting otherwise you will lose Javascript code from joomla article. It worked for me with Joomla 1.5 version.

Somewhere i read following lines but it won't help me at all cause it adds extra tags like mce_href & it changed the code according to it's structure which is nonsense(but it just make sure that your JS code wont lose if you change above settings).
Go to plugin manager & click on tiny TinyMCE 2.0
Go to plugin parameters
select "never" to Code cleanup on save option & save it.

I suggest use this method only when there is no preference to use editor other than TinyMCE. Otherwise you need to find a plugin from joomla extensions.