Source for file BitNewsletter.php
Documentation is available at BitNewsletter.php
* @copyright (c) 2004 bitweaver.org
* All Rights Reserved. See below for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
* Virtual base class (as much as one can have such things in PHP) for all
* derived tikiwiki classes that require database access.
* @date created 2004/10/20
* @author drewslater <andrew@andrewslater.com>, spiderr <spider@steelsun.com>
require_once( LIBERTY_PKG_PATH. 'LibertyContent.php' );
require_once( NEWSLETTERS_PKG_PATH. 'BitNewsletterEdition.php' );
define( 'BITNEWSLETTER_CONTENT_TYPE_GUID', 'bitnewsletter' );
function __construct( $pNlId= NULL, $pContentId= NULL ) {
'content_name' => 'Newsletter',
'handler_class' => 'BitNewsletter',
'handler_package' => 'newsletters',
'handler_file' => 'BitNewsletter.php',
'maintainer_url' => 'http://www.bitweaver.org'
$this->mNewsletterId = $this->verifyId( $pNlId ) ? $pNlId : NULL;
//$this->mViewContentPerm = '';
function load( $pContentId = NULL, $pPluginParams = NULL ) {
$bindVars = array(); $selectSql = ''; $joinSql = ''; $whereSql = '';
$lookupColumn = $this->verifyId( $this->mNewsletterId ) ? 'nl_id' : 'content_id';
$bindVars[] = $this->verifyId( $this->mNewsletterId )? $this->mNewsletterId : $this->mContentId;
$this->getServicesSql( 'content_load_function', $selectSql, $joinSql, $whereSql, $bindVars );
error_log( 'BitNewsleters: user id loading not implemented yet' );
$query = "SELECT * $selectSql
INNER JOIN `". BIT_DB_PREFIX. "liberty_content` lc ON( n.`content_id`=lc.`content_id` )
WHERE n.`$lookupColumn`=? $whereSql";
$result = $this->mDb->query($query,$bindVars);
if ($result->numRows()) {
$this->mInfo = $result->fetchRow();
$this->mNewsletterId = $this->mInfo['nl_id'];
function store( &$pParamHash ) { //$nl_id, $name, $description, $allow_user_sub, $allow_any_sub, $unsub_msg, $validate_addr) {
if( $this->verify( $pParamHash ) ) {
$this->mDb->StartTrans();
if( parent::store( $pParamHash ) ) {
if( $this->mNewsletterId ) {
$result = $this->mDb->associateUpdate( BIT_DB_PREFIX. "newsletters", $pParamHash['newsletter_store'], array ( "nl_id" => $this->mNewsletterId ) );
$pParamHash['newsletter_store']['content_id'] = $pParamHash['content_id'];
$result = $this->mDb->associateInsert( BIT_DB_PREFIX. "newsletters", $pParamHash['newsletter_store'] );
$this->mDb->CompleteTrans();
$this->mDb->RollbackTrans();
function verify( &$pParamHash ) {
// It is possible a derived class set this to something different
if( empty( $pParamHash['content_type_guid'] ) ) {
$pParamHash['newsletter_store']["allow_user_sub"] = (isset ($pParamHash["allow_user_sub"]) && $pParamHash["allow_user_sub"] == 'on') ? 'y' : 'n';
$pParamHash['newsletter_store']["allow_any_sub"] = (isset ($pParamHash["allow_any_sub"]) && $pParamHash["allow_any_sub"] == 'on') ? 'y': 'n';
$pParamHash['newsletter_store']["unsub_msg"] = (isset ($pParamHash["unsub_msg"]) && $pParamHash["unsub_msg"] == 'on') ? 'y' : 'n';
$pParamHash['newsletter_store']["validate_addr"] = (isset ($pParamHash["validate_addr"]) && $pParamHash["validate_addr"] == 'on') ? 'y' : 'n';
if( !empty( $pLookup['email'] ) ) {
$whereSql .= ' AND `email`=? ` ';
$bindVars[] = $pLookup['email'];
if( !empty( $pLookup['user_id'] ) ) {
$whereSql .= ' AND `user_id`=? ` ';
$bindVars[] = $pLookup['user_id'];
$query = "SELECT `content_id` AS `hash_key`, ms.* from `". BIT_DB_PREFIX. "mail_subscriptions` ms WHERE $whereSql ";
if( $res = $this->mDb->query( $query, $bindVars ) ) {
$whereSql = $pAll ? '' : ' `unsubscribe_date` is NULL AND ';
$query = "select * from `". BIT_DB_PREFIX. "mail_subscriptions` WHERE $whereSql `content_id`=?";
if( $res = $this->mDb->query( $query, array( $this->mContentId ) ) ) {
$this->mDb->query("DELETE FROM `". BIT_DB_PREFIX. "mail_subscriptions` WHERE `content_id`=? AND `email`=?", array($this->mContentId, $email));
$urlCode = $this->mDb->getOne("select `sub_code` from `". BIT_DB_PREFIX. "mail_subscriptions` where `content_id`=? and `email`=?", array($this->mContentId, $email));
function subscribe( $pSubscribeHash ) { // $notify = FALSE, $remind = FALSE ) {
foreach($all_subs as $sub) {
if( $sub['email'] == $pSubscribeHash['email'] ) {
$urlCode = $sub['sub_code'];
} elseif( !empty( $pSubscribeHash['user_id'] ) && $sub['user_id'] == $pSubscribeHash['user_id'] ) {
// Generate a code and store it and send an email with the
// URL to confirm the subscription put valid as 'n'
if( @BitBase::verifyId( $pSubscribeHash['user_id'] ) ) {
// we have user_id subscribing, use the id, NULL the email
$subUserId = $pSubscribeHash['user_id'];
// we have user_id subscribing, use the id, NULL the email
$subEmail = $pSubscribeHash['email'];
$query = "insert into `". BIT_DB_PREFIX. "mail_subscriptions` (`content_id`, `user_id`, `email`,`sub_code`,`is_valid`,`subscribed_date`) VALUES (?,?,?,?,?,?)";
$result = $this->mDb->query( $query, array( $this->mContentId, $subUserId, $subEmail, $urlCode, 'n', (int) $now ) );
if( ( !empty( $pSubscribeHash['notify'] ) && $this->getField( 'validate_addr' ) == 'y') || !empty( $pSubscribeHash['remind'] ) ) {
// Generate a code and store it and send an email with the
$gBitSmarty->assign( 'sub_code', $urlCode );
$mail_data = $gBitSmarty->fetch('bitpackage:newsletters/confirm_newsletter_subscription.tpl');
@mail($email, tra('Newsletter subscription information at') . ' ' . $gBitSystem->getConfig( "bitmailer_from" ), $mail_data,
"From: " . $gBitSystem->getConfig( "sender_email" ) . "\r\nContent-type: text/plain;charset=utf-8\r\n");
$query = "SELECT `content_id` FROM `". BIT_DB_PREFIX. "newsletters` WHERE `nl_id`=?";
if( $subRow['content_id'] = $this->mDb->getOne( $query, array( $pMixed ) ) ) {
$subRow['col_name'] = 'user_id';
$subRow['col_val'] = $gBitUser->mUserId;
$query = "SELECT * FROM `". BIT_DB_PREFIX. "mail_queue` WHERE `url_code`=?";
if( $subRow = $this->mDb->getRow( $query, array( $pMixed ) ) ) {
$subRow['col_name'] = !empty( $subRow['user_id'] ) ? 'user_id' : 'email';
$subRow['col_val'] = !empty( $subRow['user_id'] ) ? $subRow['user_id'] : $subRow['email'];
if( !empty( $subRow ) ) {
if( $this->mDb->getRow( "SELECT * FROM `". BIT_DB_PREFIX. "mail_subscriptions` WHERE `$subRow[col_name]`=?", array( $subRow['col_val'] ) ) ) {
$query = "UPDATE `". BIT_DB_PREFIX. "mail_subscriptions` SET `unsubscribe_date`=?, `content_id`=? WHERE `$subRow[col_name]`=? AND `unsubscribe_date` IS NULL";
$query = "INSERT INTO `". BIT_DB_PREFIX. "mail_subscriptions` (`unsubscribe_date`,`content_id`,`$subRow[col_name]`) VALUES(?,?,?)";
$result = $this->mDb->query( $query, array( $now, $subRow['content_id'], $subRow['col_val'] ) );
// Now send a bye bye email
$gBitSmarty->assign('sub_code', $res["sub_code"]);
$mail_data = $gBitSmarty->fetch('bitpackage:newsletters/newsletter_byebye.tpl');
@mail($res["email"], tra('Thank you from') . ' ' . $gBitSystem->getConfig( "bitmailer_from" ), $mail_data,
"From: " . $gBitSystem->getConfig( "sender_email" ) . "\r\nContent-type: text/plain;charset=utf-8\r\n");
function add_all_users($nl_id) {
$query = "select `email` from `".BIT_DB_PREFIX."users_users`";
$result = $this->mDb->query($query,array());
while ($res = $result->fetchRow()) {
$this->newsletter_subscribe($nl_id, $email);
$users = $this->mDb->getOne( "select count(*) from `".BIT_DB_PREFIX."mail_subscriptions` where `nl_id`=?", array( $this->mNewsletterId ) );
$query = "update `".BIT_DB_PREFIX."newsletters` set `users`=? where `nl_id`=?";
$result = $this->mDb->query( $query, array( $users, $this->mNewsletterId ) );
public static function getList( &$pListHash ) {
if ( empty( $pParamHash["sort_mode"] ) ) {
$pListHash['sort_mode'] = 'created_desc';
BitBase::prepGetList( $pListHash );
if( @BitBase::verifyId( $pListHash['nl_id'] ) ) {
$mid .= ' AND n.nl_id=? ';
$bindVars[] = $pListHash['nl_id'];
if( !empty( $pListHash['find'] ) ) {
$findesc = '%' . $pListHash['find'] . '%';
$mid .= " AND (`name` like ? or `description` like ?)";
if( !empty( $pListHash['registration_optin'] ) ) {
$joinSql = " INNER JOIN `". BIT_DB_PREFIX. "liberty_content_prefs` lcp ON (lcp.`content_id`=n.`content_id` AND lcp.`pref_name`='registration_optin' AND lcp.`pref_value`='y') ";
WHERE n.`content_id`=lc.`content_id` $mid
ORDER BY ". $gBitDb->convertSortmode( $pListHash['sort_mode'] );
$result = $gBitDb->query( $query, $bindVars, $pListHash['max_records'], $pListHash['offset'] );
$query_cant = "select count(*) from `". BIT_DB_PREFIX. "newsletters` $mid";
while( $res = $result->fetchRow() ) {
$res["confirmed"] = $gBitDb->getOne( "SELECT COUNT(*) FROM `". BIT_DB_PREFIX. "mail_subscriptions` WHERE `unsubscribe_date` IS NULL and `content_id`=?",array( (int) $res['content_id'] ) );
$res["unsub_count"] = $gBitDb->getOne( "SELECT COUNT(*) FROM `". BIT_DB_PREFIX. "mail_subscriptions` WHERE `content_id`=?",array( (int) $res['content_id'] ) );
$ret[$res['content_id']] = $res;
/* function list_newsletter_subscriptions($nl_id, $offset, $maxRecords, $sort_mode, $find) {
$bindVars = array((int)$nl_id);
$findesc = '%' . $find . '%';
$mid = " where `nl_id`=? and (`name` like ? or `description` like ?)";
$mid = " where `nl_id`=? ";
$query = "select * from `".BIT_DB_PREFIX."mail_subscriptions` $mid order by ".$this->mDb->convertSortmode("$sort_mode");
$query_cant = "select count(*) from mail_subscriptions $mid";
$result = $this->mDb->query($query,$bindVars,$maxRecords,$offset);
$cant = $this->mDb->getOne($query_cant,$bindVars);
while ($res = $result->fetchRow()) {
$this->mDb->StartTrans();
$query = "DELETE FROM `". BIT_DB_PREFIX. "newsletters` where `nl_id`=?";
$result = $this->mDb->query( $query, array( $this->mNewsletterId ) );
// Clear out all individual subscriptions/unsubscriptions, but preserve the unsubscribe_all's
$query = "DELETE FROM `". BIT_DB_PREFIX. "mail_subscriptions` WHERE `content_id`=? AND `unsubscribe_all` IS NOT NULL";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
$query = "UPDATE `". BIT_DB_PREFIX. "mail_subscriptions` SET `content_id`=NULL WHERE `content_id`=? AND `unsubscribe_all` IS NOT NULL";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
$this->mDb->CompleteTrans();
$this->mDb->RollbackTrans();
return( $this->verifyId( $this->mNewsletterId ) );
* Generate a valid url for the Newsletter
* @param object $pNewsletterId of the item to use
* @return object Url String
if( BitBase::verifyId( $pParamHash['nl_id'] ) ) {
if( $gBitSystem->isFeatureActive( 'pretty_urls' ) ) {
$ret = NEWSLETTERS_PKG_URL. $pParamHash['nl_id'];
$ret = NEWSLETTERS_PKG_URL. 'index.php?nl_id='. $pParamHash['nl_id'];
$ret = NEWSLETTERS_PKG_URL. 'index.php';
if( empty( $pNewsletterId ) ) {
$nlId = $this->mNewsletterId;
} elseif( BitBase::verifyId( $pNewsletterId ) ) {
$listHash = array( 'nl_id' => $nlId );
$query = "SELECT `content_id` AS hash_key, ms.* FROM `". BIT_DB_PREFIX. "mail_subscriptions` ms WHERE `user_id`=? OR `email`=?";
$ret = $gBitDb->getAssoc( $query, array( $pUserId, $pEmail ) );
|