So, here is the "REAL" plugin code:
Code: Select all
<?php
function onAfterRoute()
{
$user =& JFactory::getUser();
$isAuthor = ($user->usertype == 'Author');
$isSaving = (JRequest::getCmd( 'task' ) == 'save') && (JRequest::getCmd( 'option' ) == 'com_content');
if ($isAuthor && $isSaving) {
$lang =& JFactory::getLanguage();
$lang->load( 'plg_system_notifyadmin' , JPATH_ADMINISTRATOR );
$msg = $user->name . ' <' . $user->email .'> ' . JText::_( 'has edited article' ) . ' "' . JRequest::getVar( 'title' ) . ""\n";
$URI =& JFactory::getURI();
$msg .= 'URL: ' . $URI->current() . '?view=article&catid=' . JRequest::getInt( 'catid' ) . '&id=' . JRequest::getInt( 'id' ) . '&Itemid=' . JRequest::getInt( 'Itemid' ) . '&option=com_content' . "\n";
$mail =& JFactory::getMailer();
$mail->setSubject( '[' . $this->params->get( 'subject' , JText::_( 'ARTICLE EDITED' ) ) . '] ' . JRequest::getVar( 'title' ) );
$mail->setBody($msg);
$mail->addRecipient( $this->params->get( 'recipient' , $mail->From ) );
$mail->Send();
}
}
?>
Let´s see step by step:
$user =& JFactory::getUser();
$isAuthor = ($user->usertype == 'Author');
$isSaving = (JRequest::getCmd( 'task' ) == 'save') && (JRequest::getCmd( 'option' ) == 'com_content');
What are the conditions to send the notification e-mail?
First, the user must be
an Author.
Second, he/she must be
saving an existing article.
Looking at the finished code, it seems easy... but, how did I find that?
Before, I used "file_put_contents" php function to show me some information about
$_REQUEST and some Joomla! objects... as
JUser,
JSession,
JMail... and the global
$mainframe!
(And I also did a lot of
Joomla! 1.5 API Reference searches ans researches.)
global $mainframe;
$user =& JFactory::getUser();
$session =& JFactory::getSession();
$mail =& JFactory::getMailer();
$info = "\nRequest:\n" . print_r($_REQUEST, true) . "\n";
$info .= "\nUser:\n" . print_r($user, true) . "\n";
$info .= "\nSessiont:\n" . print_r($session, true) . "\n";
$info .= "\nMail:\n" . print_r($mail, true) . "\n";
$info .= "\nMainframe:\n" . print_r($mainframe, true) . "\n\n";
file_put_contents("/path/to/a/directory/notifyadmin.txt", $info, FILE_APPEND);
Yes, I installed the plugin very BEFORE it was finished, so I could TEST & DEVELOP. With a minimum package, I installed the plugin via admin interface, and then started working in the code, and uploading the new versions of
notifyadmin.php via FTP.
Then, with the snippet above inside OnAfterRoute function, I worked in the front-end, logging as an author, and editing an article.
With the output generated through "file_put_contents", I was able to identify how the conditions I needed (Author saving an article) were registered in the variables.
Answer: usertype == "Author" and task == "save" and option == "com_content"
This was "discovered" this way: using the front-end and looking at the "log" file generated inside the event handler (OnAfterRoute).
So, I was able to code:
$user =& JFactory::getUser();
$isAuthor = ($user->usertype == 'Author');
$isSaving = (JRequest::getCmd( 'task' ) == 'save') && (JRequest::getCmd( 'option' ) == 'com_content');
And the condition to send the notification e-mail was:
if ($isAuthor && $isSaving) {
//SEND THE NOTIFICATION
}
Now, let´s see the code that SENDS the notification, once the conditions are TRUE (an Author is saving an article)
What comes next was in fact the LAST piece of code I made. Everything was already working, except the TRANSLATION... the strings in the language file were not being used. Why?! Hummmm... I didn´t know
I needed to explicitly LOAD the plugin language file. As I wasn´t successfull with the JPlugin loadLanguage function, I arranged myself with the following solution, found in a forum thread:
$lang =& JFactory::getLanguage();
$lang->load( '
plg_system_notifyadmin' ,
JPATH_ADMINISTRATOR );
Why the plugin language file goes into the Admin... don´t ask me.
Following, we have some lines to build the
e-mail body, with some useful information:
- author name
- author e-mail
- name of the edited article
- link to the article
For example: "John
edited "Love is all you need", link: http://...."
Researching the API, and the "log" file, and analyzing an article URL, one can find everything needed:
- author name = $user->name
- author e-mail = $user->email
- name of the edited article = $_REQUEST['title'] or better: JRequest::getVar( 'title' )
- link to the article... for this we need the catid, the id and the Itemid, all provided in JRequest...
$msg = $user->name . ' <' . $user->email .'> ' . JText::_( 'has edited article' ) . ' "' . JRequest::getVar( 'title' ) . "\"\n";
$URI =& JFactory::getURI();
$msg .= 'URL: ' . $URI->current() . '?view=article&catid=' . JRequest::getInt( 'catid' ) . '&id=' . JRequest::getInt( 'id' ) . '&Itemid=' . JRequest::getInt( 'Itemid' ) . '&option=com_content' . "\n";
Note the use of JText. If that string is present in the language file... it will be translated! :laugh:
And what comes next? Let´s generate and send the e-mail? Yes, it´s time to use the plugin parameters! The "SUBJECT" string and the "RECIPIENT"!
To have access to the plugin parameters, use the following code pattern: (thanks, Pentacle!)
$this->params->get( '[i]parameter[i]' )
Finally, the easiest piece of code, which sends the e-mail using JMail:
$mail =& JFactory::getMailer();
$mail->setSubject( '[' . $this->params->get( 'subject' , JText::_( 'ARTICLE EDITED' ) ) . '] ' . JRequest::getVar( 'title' ) );
$mail->setBody($msg);
$mail->addRecipient( $this->params->get( 'recipient' , $mail->From ) );
$mail->Send();
What should be noticed?
The use of $this->params->get to access the plugin parameters.
$this->params->get( 'subject' , JText::_( 'ARTICLE EDITED' ) )
$this->params->get( 'recipient' , $mail->From )
First, the name of the parameter (as defined in the XML file); second, an optional DEFAULT value.
That´s it. You have your "notificator" plugin.
I´d like to thank Nicolas Ogier, from "Website Name" plugin, from which I learned a lot.