|
|
|
Learning Joomla! for the first time? Buy the Joomla! 1.5 Essential training CD-ROM or watch it on Lynda.com.
|
|
Daily Message with E-mail |
|
|
|
|
Page 2 of 3
dailymessage.php<? defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
If someone tries to request this file outside of Mambo, stop them right here.
require_once ( $mainframe->getPath( 'front_html' ) );
For this version of Daily Message, the file dailymessage.html.php has been added. Except for the e-mail function, all of the output is now handled through this file, making it easier for other developers to make quick formatting modifications.
$id = mosGetParam($_REQUEST, 'id', '');
This version allows the user to select one Daily Message to e-mail to friends. To accomodate this, links generated by the component pass an ID number for future reference. If no message is selected, $id is set to null.
switch( $task ) { case "sendemail": SendEmail($option, $id); break;
case "emailform": EmailForm($option, $id); break;
default: ListMessages($option); break; }
From here, the function to call is determined by the $task variable, which is extracted either from a link or a form POST. The "sendmail" task is placed in a variable on the form where e-mail addresses are entered. Links to pop-up windows contain a value of "emailform". If the 'task' variable is not set (for instance, when the component is first loaded by a visitor), a screen containing all of the currently published Daily Messages appears.
function SendEmail($option, $id) { global $database;
$sendername = mosGetParam($_REQUEST, 'sendername', ''); $emailfrom = mosGetParam($_REQUEST, 'emailfrom', ''); $emailto = mosGetParam($_REQUEST, 'emailto', ''); $note = mosGetParam($_REQUEST, 'note', '');
The variables 'sendername', 'emailform', 'emailto', and 'note' are pulled from the current HTTP request. This will allow for some simple error checking.
if(!$sendername || !$emailfrom || !$emailto) { dailymessage_HTML::FillAllFields(); EmailForm($option, $id); return; }
To send a proper e-mail, three things are essential: a name, an email address to "send from", and a destination address. When any of these are missing, the visitor is given an alert and the form is loaded again. The "return;" call ensures that the rest of this function is not processed, as the data necessary for the e-mail is not ready.
$subject = "A message you may find interesting";
if($note) { $message = $note . "\n\n"; } else { $message = "$sendername thought you might find this message interesting.\n\n"; }
When a sender, an e-mail address to send from, and a recipient are present, a message can be put together. To keep the mosMail call simple, the various elements are held in string variables. First, a friendly and appropriate subject is set. Next, the $note variable is checked for a custom message from the visitor. If one has been entered, this is put into the message body. Otherwise, a straightforward, generic introduction containing the visitor's name starts off the e-mail.
$database->SetQuery("SELECT * FROM #__joe_dailymessage WHERE id = '" . $id . "'"); $rows = $database->loadObjectList(); $row = $rows[0];
$message .= $row->message . " " . mosFormatDate($row->date);
To send a Daily Message to the visitor's friend, it is pulled from the database. A query containing the passed ID is built and executed. The first row of the set is loaded (there should only be one). The contents of the Daily Message gets added to the body along with the date, formatted according to the site settings.
mosMail($emailfrom, $sendername, $emailto, $subject, $message);
dailymessage_HTML::Success();
}
Finally, the message is sent to the recipient. The first parameter of mosMail is the address of the visitor, the second is the visitor's name, third is the destination address, fourth is the subject, and the e-mail body is fifth. After sending the message, a success message is displayed.
function EmailForm($option, $id) { $sendername = mosGetParam($_REQUEST, 'sendername', ''); $emailfrom = mosGetParam($_REQUEST, 'emailfrom', ''); $emailto = mosGetParam($_REQUEST, 'emailto', ''); $note = mosGetParam($_REQUEST, 'note', '');
The EmailForm() function is called when the form for entering e-mail addresses is to be displayed. If the form is reloading (when there is an error, for instance), the submitted variables are extracted from the request and put back into the form. This eliminates retyping which makes visitors much happier.
dailymessage_HTML::EmailForm($option, $id, $sendername, $emailfrom, $emailto, $note);
Once the variables are ready, the form is drawn prefilled with any submitted information.
}
function ListMessages ($option) {
global $database;
// get configuration information $database->setQuery("SELECT * FROM #__joe_dailymessage_conf LIMIT 1");
$rows = $database->loadObjectList(); $row = $rows[0];
$bold = $row->bold; $italic = $row->italic; $underline = $row->underline; $showdate = $row->showdate;
$starttags = ""; $endtags = "";
if($bold == 1) { $starttags .= "<b>"; $endtags = "</b>" . $endtags; }
if($italic == 1) { $starttags .= "<i>"; $endtags = "</i>" . $endtags; }
if($underline == 1) { $starttags .= "<u>"; $endtags = "</u>" . $endtags; }
//get data and output accordingly
$database->setQuery("SELECT * FROM #__joe_dailymessage WHERE published = '1'");
$rows = $database->loadObjectList();
dailymessage_HTML::ListMessages ($option, $bold, $italic, $underline, $showdate, $starttags, $endtags, $rows);
Except for this last line, the contents of this function match with the original dailymessage.php file. The rest of the output code is in the new dailymessage.html.php file and has been modified to accommodate the pop-up window links.
}
?>
|
|