Files
bm/public_html/public/cms/modules/newsletter.class.php
2025-09-24 13:26:28 +02:00

627 lines
20 KiB
PHP

<?php
require_once(ROOT."include/config.inc.php");
require_once(CMS_DIR."modules/_buildingblockfactory.class.php");
define(NEWSLETTER_MAX_COUNT, 100);
/** Newsletter
*
* @version 2.0.0
* @since 2008-04-07
* @author martin lenzelbauer
*
*/
class Newsletter extends Page{
var $buildingBlocks;
var $creationDate;
var $recipientGroup;
var $recipientList;
var $sent;
var $total;
/** C'tor
*/
//-----------------------------------------------
function Newsletter($id, $parent){
//-----------------------------------------------
parent::Page($id, $parent);
$this->name = "Newsletter";
$this->buildingBlocks = array();
$this->creationDate = date("Y-m-d");
$this->buttonDelete = true;
$this->recipientGroup = 0;
$this->recipientList = array();
$this->sent = 0;
$this->total = 0;
}
/** @see Page::load()
*/
//-----------------------------------------------
function load($path=array()){
//-----------------------------------------------
parent::load($path);
if(!$this->classId){
return;
}
$query = sprintf("SELECT * FROM bruckm_newsletter WHERE id = %d", $this->classId);
$result = dbQuery($query);
$line = mysqli_fetch_array($result, MYSQLI_ASSOC);
$this->creationDate = $line['creationDate'];
$this->status = $line['status'];
$this->recipientGroup = $line['recipientGroup'];
$this->recipientList = explode("\t", $line['recipientList']);
$this->sent = $line['sent'];
$this->total = $line['total'];
$this->buildingBlocks = array();
if(strlen($line['buildingBlocks']) > 0){
$buildingBlocks = explode("\t", $line['buildingBlocks']);
foreach($buildingBlocks as $b){
$block = BuildingBlockFactory::instance($b);
$block->load();
$this->buildingBlocks[] = $block;
}
}
foreach($this->buildingBlocks as $i=>$j){
if(strtolower(get_class($this->buildingBlocks[$i])) == "imageblock"){
$this->buildingBlocks[$i]->setDirectories(NEWSLETTER_DIR, "");
}
else if(strtolower(get_class($this->buildingBlocks[$i])) == "fileblock"){
$this->buildingBlocks[$i]->setDirectory(NEWSLETTER_DIR);
}
}
}
/** @see Page::doSave()
*/
//----------------------------------------------
function doSave(){
//----------------------------------------------
$buildingBlocks = array();
foreach($this->buildingBlocks as $i=>$block){
$this->buildingBlocks[$i]->save();
$buildingBlocks[] = $this->buildingBlocks[$i]->getReference();
}
$query = sprintf("UPDATE bruckm_newsletter SET buildingBlocks = %s, recipientGroup = %d, recipientList = %s, sent = %d, total = %d WHERE id = %d",
sqlstring(implode("\t", $buildingBlocks)),
sqlnum($this->recipients),
sqlstring(implode("\t", $this->recipientList)),
sqlnum($this->sent),
sqlnum($this->total),
sqlnum($this->classId));
dbQuery($query);
parent::doSave();
}
/** @see Page::doCreate()
*/
//----------------------------------------------
function doCreate(){
//----------------------------------------------
$query = sprintf("INSERT INTO bruckm_newsletter (creationDate) VALUES (NOW())");
dbQuery($query);
$this->classId = mysql_insert_id();
parent::doCreate();
}
/** @see Page::doDelete()
*/
//-----------------------------------------------
function doDelete(){
//-----------------------------------------------
parent::doDelete();
$query = sprintf("DELETE FROM bruckm_newsletter WHERE id = %d LIMIT 1", $this->classId);
dbQuery($query);
foreach($this->buildingBlocks as $i=>$block){
$this->buildingBlocks[$i]->delete();
}
}
/** @see Page::canBeDeleted()
*/
//----------------------------------------------
function canBeDeleted(){
//----------------------------------------------
return true;
}
/** @see Page::install()
*/
//-----------------------------------------------
function install(){
//-----------------------------------------------
$query = sprintf("CREATE TABLE IF NOT EXISTS bruckm_newsletter (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
creationDate DATE NOT NULL,
buildingBlocks TEXT NOT NULL DEFAULT '',
recipientGroup INT NOT NULL DEFAULT 0,
recipientList TEXT NOT NULL DEFAULT '',
sent INT UNSIGNED NOT NULL DEFAULT 0,
total INT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
)");
dbQuery($query);
TextBlock::install();
HeadingBlock::install();
ImageBlock::install();
FileBlock::install();
}
/** @see CmsObject::update()
*/
//-----------------------------------------------
function update(){
//-----------------------------------------------
parent::update();
if(!empty($_POST['subject'])){
$this->name= $_POST['subject'];
}
foreach($this->buildingBlocks as $i=>$block){
if (strtolower(get_class($block)) == 'imageblock') {
$block->setSize(630, 630);
}
$this->buildingBlocks[$i]->update();
}
}
/** @see CmsObject::doPrintClassContent()
*/
//-----------------------------------------------
function doPrintClassContent(){
//-----------------------------------------------
$t = new Template(CMS_TEMPLATE_DIR."newsletter.html");
$t->setVar("SUBJECT", htmlspecialchars($this->name));
$t->setVar("CREATION_DATE", $this->creationDate);
$t->setVar("STATUS", $this->sent . "/" . $this->total . " gesendet");
$out = $t->toString();
$out .= $this->doPrintBuildingBlockInsertBar(0);
foreach($this->buildingBlocks as $i=>$block){
$out .= $this->buildingBlocks[$i]->printContent($i);
$out .= $this->doPrintBuildingBlockInsertBar($i+1);
}
return $out;
}
/** @see CmsObject::handleAction()
*/
//-----------------------------------------------
function handleAction($action, $position, $type){
//-----------------------------------------------
switch($action){
case "insertBuildingBlock":
$this->doInsertBuildingBlock($position, $type);
break;
case "moveBuildingBlock":
$this->doMoveBuildingBlock($position, $type);
break;
case "deleteBuildingBlock":
$this->doDeleteBuildingBlock($position);
break;
}
}
/** @see CmsObject::publish()
*/
//---------------------------------------------
function publish(){
//---------------------------------------------
return false;
}
/** @see CmsObject::show()
*/
//---------------------------------------------
function show(){
//---------------------------------------------
header("Location: index.php");
}
/** @see CmsObject::getCssClass()
*/
//-----------------------------------------------
function getCssClass(){
//-----------------------------------------------
return "itemNewsletter";
}
// === ADDITIONAL METHODS ==================================================== //
/** inserts a building block
* @param class building block class
* @param index position
*/
//-----------------------------------------------
function doInsertBuildingBlock($index, $class){
//-----------------------------------------------
$this->buildingBlocks = array_insert($this->buildingBlocks, $index, BuildingBlockFactory::instance("$class,0"));
$this->save();
}
/** moves a building block
* @param direction up or down
* @param index position
*/
//-----------------------------------------------
function doMoveBuildingBlock($index, $direction){
//-----------------------------------------------
if($direction == "up" && $index > 0){
$this->buildingBlocks = array_swap($this->buildingBlocks, $index, $index-1);
}
if($direction == "down" && $index < sizeof($this->buildingBlocks)-1){
$this->buildingBlocks = array_swap($this->buildingBlocks, $index, $index+1);
}
$this->save();
}
/** deletes a building block object
* @param index position
*/
//-----------------------------------------------
function doDeleteBuildingBlock($index){
//-----------------------------------------------
$this->buildingBlocks[$index]->delete();
$this->buildingBlocks = array_remove($this->buildingBlocks, $index);
$this->save();
}
/** prints a bar with buttons for inserting building blocks
* @param index position, where the building block will be inserted
* @return string
*/
//-----------------------------------------------
function doPrintBuildingBlockInsertBar($index){
//-----------------------------------------------
$t = new Template(CMS_TEMPLATE_DIR."insertbar.html");
$button = "<div class=\"insert\" onMouseOver=\"showTooltip('%s einf&uuml;gen')\"".
"onClick=\"setAction('insertBuildingBlock', %d, '%s'); document.forms[0].submit();\">".
"<img src=\"images/%s1.png\" alt=\"%s einf&uuml;gen\" onMouseOver=\"swapImage(this,2)\" onMouseOut=\"swapImage(this,1)\" />".
"</div>";
$out .= sprintf($button, "&Uuml;berschrift", $index, "headingblock", "headingblock", "&Uuml;berschrift");
$out .= sprintf($button, "Text", $index, "textblock", "textblock", "Text");
$out .= sprintf($button, "Bild", $index, "imageblock", "imageblock", "Bild");
$out .= sprintf($button, "Datei", $index, "fileblock", "fileblock", "Datei");
$out .= sprintf($button, "Anmelde-Link", $index, "attendanceblock", "attendanceblock", "Anmelde-Link");
$t->setVar("CLASSES", $out);
return $t->toString();
}
/** initialises the sending process
*/
//-----------------------------------------------
function initSendingProcess(){
//-----------------------------------------------
$this->recipientList = array();
$this->sent = 0;
$this->total = 0;
if ($this->recipientGroup == 0) {
$query = sprintf("SELECT id FROM bruckm_ticketcustomer
WHERE newsletter = 'true' AND email != '' AND firstname != '' AND surname != ''
GROUP BY email");
$result = dbQuery($query);
while ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$this->recipientList[] = $line['id'];
}
$this->total = sizeof($this->recipientList);
$query = sprintf("UPDATE bruckm_newsletter SET recipientList = %s, sent = 0, total = %d WHERE id = %d",
sqlstring(implode("\t", $this->recipientList)),
sqlnum($this->total),
sqlnum($this->classId));
dbQuery($query);
}
else {
$query = sprintf("SELECT customer.id AS cid
FROM bruckm_ticketcustomer AS customer, bruckm_ticketcustomergroupmemberships AS mm
WHERE mm.groupId = %d
AND customer.id = mm.customerId
AND customer.newsletter = 'true'
AND customer.email != ''
AND customer.firstname != ''
AND customer.surname != ''",
sqlnum($this->recipientGroup));
$result = dbQuery($query);
while($line = mysqli_fetch_array($result, MYSQLI_ASSOC)){
$this->recipientList[] = $line['cid'];
$this->total++;
}
$query = sprintf("UPDATE bruckm_newsletter SET recipientList = %s, sent = 0, total = %d WHERE id = %d",
sqlstring(implode("\t", $this->recipientList)),
sqlnum($this->total),
sqlnum($this->classId));
dbQuery($query);
}
}
/** sends the newsletter
*/
//-----------------------------------------------
function sendNewsletter(){
//-----------------------------------------------
// build newsletter
$htmlContent = "";
$textContent = "";
foreach($this->buildingBlocks as $i=>$block){
if(strtolower(get_class($block)) == "imageblock" || strtolower(get_class($block)) == "fileblock"){
$this->buildingBlocks[$i]->useAbsoluteUrl(true);
}
$htmlContent .= $this->buildingBlocks[$i]->publishForNewsletter();
$textContent .= $this->buildingBlocks[$i]->publishAsPlainText();
}
$htmlMail = new Template(NEWSLETTER_DIR."template-mooi.html");
$htmlMail->setVar("SUBJECT", $this->name);
$htmlMail->setVar("CONTENT", $htmlContent);
$htmlContent = $htmlMail->toString();
$textMail = new Template(NEWSLETTER_DIR."newsletter_plain.txt");
$textMail->setVar("CONTENT", $textContent);
$textContent = $textMail->toString();
if (strtoupper(substr(PHP_OS,0,3) == 'WIN')){
$eol = "\r\n";
}
else if (strtoupper(substr(PHP_OS,0,3) == 'MAC')){
$eol = "\r";
}
else{
$eol = "\n";
}
$boundary = md5(time());
$headers = "From: Kulturhaus Pregarten Bruckmühle <kulturhaus@bruckmuehle.at>$eol";
$headers .= "Return-Path: Webmaster <mtd04041@fh-hagenberg.at>$eol";
$headers .= "Errors-To: Webmaster <mtd04041@fh-hagenberg.at>$eol";
$headers .= "Message-ID: <" . time() . "-kulturhaus@bruckmuehle.at>$eol";
$headers .= "Content-Type: multipart/alternative; charset=\"utf-8\"; boundary=\"$boundary\"$eol";
$headers .= "MIME-Version: 1.0$eol";
$subject = $this->name;
// fetch recipients & send newsletter
$recipients = $this->doFetchRecipients();
foreach($recipients as $recipient){
$encodedEmail = urlencode($recipient['email']);
$email = "$recipient[firstname] $recipient[surname] <$recipient[email]>";
$hash = $this->generateHash($recipient);
$htmlMail->setString($htmlContent);
$htmlMail->setVar("USER_EMAIL", $encodedEmail);
$htmlMail->setVar("USER_ID", $recipient['id']);
$htmlMail->setVar("USER_HASH", $hash);
$textMail->setString($textContent);
$textMail->setVar("USER_EMAIL", $encodedEmail);
$textMail->setVar("USER_ID", $recipient['id']);
$textMail->setVar("USER_HASH", $hash);
$message = "--$boundary$eol" .
"Content-Type: text/plain; charset=\"utf-8\"$eol" .
"Content-Transfer-Encoding: 7bit$eol$eol" .
$textMail->toString() .
"$eol$eol" .
"--$boundary$eol" .
"Content-Type: text/html; charset=\"utf-8\"$eol" .
"Content-Transfer-Encoding: 7bit$eol$eol" .
$htmlMail->toString() .
"$eol$eol" .
"--$boundary--";
@mail($email, $subject, $message, $to.$headers);
#$f = fopen("newsletter-".$recipient['id'].".html", "w");
#fwrite($f, $message);
#fclose($f);
$this->sent++;
}
// update status
$this->save();
}
/** sends a preview version to the bruckmuehle
*/
//-----------------------------------------------
function sendPreview($recipient=null){
//-----------------------------------------------
//send preview newsletter to the bruckmuehle
$htmlContent = "";
$textContent = "";
foreach($this->buildingBlocks as $i=>$block){
if(strtolower(get_class($block)) == "imageblock" || strtolower(get_class($block)) == "fileblock"){
$this->buildingBlocks[$i]->useAbsoluteUrl(true);
}
$htmlContent .= $this->buildingBlocks[$i]->publishForNewsletter();
$textContent .= $this->buildingBlocks[$i]->publishAsPlainText();
}
$htmlMail = new Template(NEWSLETTER_DIR."template-mooi.html");
$htmlMail->setVar("SUBJECT", $this->name);
$htmlMail->setVar("CONTENT", $htmlContent);
$htmlContent = $htmlMail->toString();
$textMail = new Template(NEWSLETTER_DIR."newsletter_plain.txt");
$textMail->setVar("CONTENT", $textContent);
$textContent = $textMail->toString();
if (strtoupper(substr(PHP_OS,0,3) == 'WIN')){
$eol = "\r\n";
}
else if (strtoupper(substr(PHP_OS,0,3) == 'MAC')){
$eol = "\r";
}
else{
$eol = "\n";
}
$boundary = md5(time());
$headers = "From: Kulturhaus Pregarten Bruckmühle <kulturhaus@bruckmuehle.at>$eol";
$headers .= "Return-Path: Webmaster <mtd04041@fh-hagenberg.at>$eol";
$headers .= "Errors-To: Webmaster <mtd04041@fh-hagenberg.at>$eol";
$headers .= "Message-ID: <" . time() . "-kulturhaus@bruckmuehle.at>$eol";
$headers .= "Content-Type: multipart/alternative; charset=\"utf-8\"; boundary=\"$boundary\"$eol";
$headers .= "MIME-Version: 1.0$eol";
$subject = $this->name;
$htmlMail->setString($htmlContent);
$htmlMail->setVar("USER_EMAIL", "");
$htmlMail->setVar("USER_ID", "");
$htmlMail->setVar("USER_HASH", "");
$textMail->setString($textContent);
$textMail->setVar("USER_EMAIL", "");
$textMail->setVar("USER_ID", "");
$textMail->setVar("USER_HASH", "");
$message = "--$boundary$eol" .
"Content-Type: text/plain; charset=\"utf-8\"$eol" .
"Content-Transfer-Encoding: 7bit$eol$eol" .
$textMail->toString() .
"$eol$eol" .
"--$boundary$eol" .
"Content-Type: text/html; charset=\"utf-8\"$eol" .
"Content-Transfer-Encoding: 7bit$eol$eol" .
$htmlMail->toString() .
"$eol$eol" .
"--$boundary--";
if(empty($recipient)) {
$recipient = "kulturhaus@bruckmuehle.at";
}
@mail($recipient, $subject, $message, $headers);
#mail("contact@mlenzelbauer.at", $subject, $message, $headers);
#$f = fopen("_newsletter.html", "w");
#fwrite($f, $message);
#fclose($f);
}
/** cancels the sending process
*/
//-----------------------------------------------
function cancel(){
//-----------------------------------------------
$this->sent = 0;
$this->total = 0;
$this->save();
}
/** fetches the list of the next recipients
* @return recipients
*/
//-----------------------------------------------
function doFetchRecipients(){
//-----------------------------------------------
$recipients = array();
$size = NEWSLETTER_MAX_COUNT;
if (sizeof($this->recipientList) < NEWSLETTER_MAX_COUNT) {
$size = sizeof($this->recipientList);
}
for ($i = 0; $i < $size; $i++) {
$query = sprintf("SELECT * FROM bruckm_ticketcustomer WHERE id = %d",
sqlnum($this->recipientList[$i]));
$result = dbQuery($query);
if ($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$recipients[] = $line;
}
}
array_splice($this->recipientList, 0, $size);
if (sizeof($recipients) == 0) {
$this->sent = $this->total;
}
return $recipients;
/*
$recipients = array();
if ($this->recipients == 0) {
$query = sprintf("SELECT id, firstname, surname, email FROM bruckm_ticketcustomer
WHERE id > %d AND newsletter = 'true' AND email != '' AND firstname != '' AND surname != ''
GROUP BY email
ORDER BY id ASC
LIMIT %d",
sqlnum($this->currentRecipient),
sqlnum(NEWSLETTER_MAX_COUNT));
$result = dbQuery($query);
while($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$recipients[] = $line;
}
}
else {
$query = sprintf("SELECT id, firstname, surname, email FROM bruckm_ticketcustomer
WHERE id > %d AND newsletter = 'true' AND email != '' AND firstname != '' AND surname != ''
GROUP BY email
ORDER BY id ASC",
sqlnum($this->currentRecipient));
$result = dbQuery($query);
$count = 0;
while(($line = mysqli_fetch_array($result, MYSQLI_ASSOC)) && $count < NEWSLETTER_MAX_COUNT){
$query = sprintf("SELECT customerId FROM bruckm_ticketcustomergroupmemberships WHERE groupId = %d AND customerId = %d LIMIT 1",
sqlnum($this->recipients),
sqlnum($line['id']));
$customer = dbQuery($query);
if(mysql_num_rows($customer) > 0) {
$recipients[] = $line;
$count++;
}
}
}
if (sizeof($recipients) == 0) {
$this->sent = $this->total;
}
return $recipients;
*/
}
/** sets the id
* @param id id
*/
//-----------------------------------------------
function setId($id){
//-----------------------------------------------
$this->id = $id;
}
/** sets the recipients
* @param recipient group id
*/
//-----------------------------------------------
function setRecipientGroup($recipients){
//-----------------------------------------------
$this->recipientGroup = $recipients;
}
/** returns the creation date
* @return date
*/
//-----------------------------------------------
function getCreationDate(){
//-----------------------------------------------
if(isset($this->creationDate)){
return $this->creationDate;
}
return "0000-00-00";
}
/** generates a hash code for personalised newsletter links
* @return hash
*/
//-----------------------------------------------
function generateHash($user){
//-----------------------------------------------
return substr(md5("#" . $user['email'] . "#"), 0, 8);
}
};
?>