I wanted to be able to show archived newsletters on my site, so I created a replacement tag to do it. It is very simple and does not yet work for text-based newsletters, but you're welcome to enhance it further.
First of all I needed to specify that a newsletter is archived. I used the existing database column newsletter_trashed. I made the following changes in message.newsletter.tmpl.php right at the beginning:
Code: Select all
// changed by jens
$sql = "SELECT * FROM ".DB_PREPEND."phpwcms_newsletter WHERE newsletter_trashed in (0,1) ORDER BY newsletter_changed DESC;";
if($result = mysql_query($sql, $db) or die("error while listing newsletters")) {
while($row = mysql_fetch_assoc($result)) {
echo "<tr".( ($row_count % 2) ? " bgcolor=\"#F3F5F8\"" : "" ).">\n<td width=\"25\">";
echo '<img src="img/symbole/newsletter_little_icon.gif" width="25" height="17"></td>'."\n";
echo '<td width="463" class="dir"><a href="phpwcms.php?do=messages&p=3&s='.$row["newsletter_id"]; // changed by jens
echo '"><strong>'.html_specialchars($row["newsletter_subject"])."</strong></a></td>\n".'<td width="50" align="right">'; // changed by jens
// added by jens
$archimg = ($row["newsletter_trashed"] == 0 ? "img/button/active_11x11_0.gif" : "img/button/active_11x11_1.gif");
$archact = ($row["newsletter_trashed"] == 0 ? 1 : 0);
$archtitle = ($row["newsletter_trashed"] == 0 ? "Archive newsletter" : "Unarchive newsletter");
echo '<a href="phpwcms.php?do=messages&p=3&s='.$row["newsletter_id"].'&archive=' . $archact;
echo '" title="'.$archtitle.'"><img src="'.$archimg.'" width="11" height="11" border="0"></a>';
echo '<img src="img/leer.gif" width="2" height="1">';
// ----
Next, I added the following code to the same file to write the changes to the database when the form is submitted:
Code: Select all
// added by jens
if(isset($_GET["archive"]))
{
$sql = "UPDATE ".DB_PREPEND."phpwcms_newsletter SET newsletter_trashed=" . intval($_GET["archive"]);
$sql .= " WHERE newsletter_id=" . intval($_GET["s"]) . " LIMIT 1;";
mysql_query($sql, $db) or die("error while archiving newsletter: " . mysql_error());
header("Location: ".PHPWCMS_URL."phpwcms.php?do=messages&p=3");
exit();
}
// ----
Code: Select all
if(!empty($_GET["del"]) && intval($_GET["del"]) == $newsletter["newsletter_id"]) {
//delete newsletter now
$sql = "UPDATE ".DB_PREPEND."phpwcms_newsletter SET newsletter_trashed=9 ";
$sql .= "WHERE newsletter_id=".intval($_GET["del"])." LIMIT 1;";
mysql_query($sql, $db) or die("error while deleting newsletter");
header("Location: ".PHPWCMS_URL."phpwcms.php?do=messages&p=3");
exit();
}
Then, I wrote some code for the {LISTNEWSLETTERS} replacement tag to be able to show it on the site. Save the following code as listnewsletters.php and place it in phpwcms_template\inc_script\frontend_render:
Code: Select all
<?php
// AUTHOR: Jens Zetterström
// DESCRIPTION: Lists all available newsletters
// USAGE: {LISTNEWSLETTERS}
$lang_reader = "Reader";
function show_newsletter_list()
{
$rs = "";
$query =
"SELECT newsletter_id,
newsletter_subject,
UNIX_TIMESTAMP(newsletter_changed) AS newsletter_changed,
newsletter_vars
FROM " . DB_PREPEND . "phpwcms_newsletter
WHERE newsletter_trashed = 1
ORDER BY newsletter_changed DESC";
$list_rs = mysql_query($query, $GLOBALS["db"]) or die ("Error in query:<br> $query <br><br>" . mysql_error());
if(mysql_num_rows($list_rs))
{
while($row = mysql_fetch_assoc($list_rs))
{
$rs .= date("Y-m-d", $row["newsletter_changed"]) . " ";
$rs .= "<a href=\"" . $_SERVER["REQUEST_URI"] . "&nlid=" . $row["newsletter_id"] . "\">" . $row["newsletter_subject"] . "</a> <br/>\n";
}
}
return $rs;
}
if( ! ( strpos($content["all"],'{LISTNEWSLETTERS}')===false ) )
{
if(isset($_GET["nlid"]))
{
$query =
"SELECT newsletter_id,
newsletter_subject,
UNIX_TIMESTAMP(newsletter_changed) AS newsletter_changed,
newsletter_vars
FROM " . DB_PREPEND . "phpwcms_newsletter
WHERE newsletter_id = " . $_GET["nlid"] . "
ORDER BY newsletter_changed DESC";
$list_rs = mysql_query($query, $GLOBALS["db"]) or die ("Error in query:<br> $query <br><br>" . mysql_error());
if(mysql_num_rows($list_rs))
{
$row = mysql_fetch_assoc($list_rs);
$newsletter = unserialize($row['newsletter_vars']);
if(strlen($newsletter["html"]) > 0)
{
$newsletter["html"] = preg_replace('/###RECIPIENT_NAME###/ei', $lang_reader , $newsletter["html"]);
$newsletter["html"] = preg_replace('/###SITE_URL###/ei', "\$GLOBALS[\"phpwcms\"][\"site\"]", $newsletter["html"]);
$newsletter["html"] = preg_replace('/###DELETE_LINK###/ei', "", $newsletter["html"]);
$newsletter["html"] = preg_replace('/###VERIFY_LINK###/ei', "", $newsletter["html"]);
$newsletter["html"] = preg_replace('/###RECIPIENT_EMAIL###/ei', "your_email@domain.com", $newsletter["html"]);
$content["all"] = $newsletter["html"];
}
else
{
// TODO: fix text newsletters
$newsletter["text"] = preg_replace('/###RECIPIENT_NAME###/ei', $lang_reader , $newsletter["text"]);
$newsletter["text"] = preg_replace('/###SITE_URL###/ei', "\$GLOBALS[\"phpwcms\"][\"site\"]", $newsletter["text"]);
$newsletter["text"] = preg_replace('/###DELETE_LINK###/ei', "", $newsletter["text"]);
$newsletter["text"] = preg_replace('/###VERIFY_LINK###/ei', "", $newsletter["text"]);
$newsletter["text"] = preg_replace('/###RECIPIENT_EMAIL###/ei', "your_email@domain.com", $newsletter["text"]);
//$content["main"] = htmlentities(nl2br($newsletter["text"]));
}
return;
// TODO: open window with newsletter instead
}
}
else
{
$content["all"] = preg_replace('/\{LISTNEWSLETTERS\}/ei', "show_newsletter_list()" , $content["all"]);
}
}
?>
Cheers,
Jens