newsletters
[ class tree: newsletters ] [ index: newsletters ] [ all elements ]

Source for file BitNewsletter.php

Documentation is available at BitNewsletter.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  *
  5.  * @copyright (c) 2004 bitweaver.org
  6.  *  All Rights Reserved. See below for details and a complete list of authors.
  7.  *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  8.  *
  9.  *  $Id$
  10.  *
  11.  *  Virtual base class (as much as one can have such things in PHP) for all
  12.  *  derived tikiwiki classes that require database access.
  13.  * @package newsletters
  14.  *
  15.  * @date created 2004/10/20
  16.  *
  17.  * @author drewslater <andrew@andrewslater.com>, spiderr <spider@steelsun.com>
  18.  *
  19.  * @version $Revision$
  20.  */
  21.  
  22. /**
  23.  * required setup
  24.  */
  25. require_onceLIBERTY_PKG_PATH.'LibertyContent.php' );
  26. require_onceNEWSLETTERS_PKG_PATH.'BitNewsletterEdition.php' );
  27.  
  28. define'BITNEWSLETTER_CONTENT_TYPE_GUID''bitnewsletter' );
  29.  
  30. /**
  31.  * @package newsletters
  32.  */
  33. class BitNewsletter extends LibertyContent {
  34.     function __construct$pNlId=NULL$pContentId=NULL {
  35.         parent::__construct();
  36.         $this->registerContentTypeBITNEWSLETTER_CONTENT_TYPE_GUIDarray(
  37.             'content_type_guid' => BITNEWSLETTER_CONTENT_TYPE_GUID,
  38.             'content_name' => 'Newsletter',
  39.             'handler_class' => 'BitNewsletter',
  40.             'handler_package' => 'newsletters',
  41.             'handler_file' => 'BitNewsletter.php',
  42.             'maintainer_url' => 'http://www.bitweaver.org'
  43.         ) );
  44.         $this->mNewsletterId $this->verifyId$pNlId $pNlId NULL;
  45.         $this->mContentId = $pContentId;
  46.  
  47.         // Permission setup
  48.         //$this->mViewContentPerm  = '';
  49.         $this->mUpdateContentPerm  = 'p_newsletters_create';
  50.         $this->mAdminContentPerm = 'p_newsletters_admin';
  51.     }
  52.  
  53.     function load$pContentId NULL$pPluginParams NULL {
  54.         if$this->verifyId$this->mNewsletterId || $this->verifyId$this->mContentId ) ) {
  55.             global $gBitSystem;
  56.  
  57.             $bindVars array()$selectSql ''$joinSql ''$whereSql '';
  58.  
  59.             $lookupColumn $this->verifyId$this->mNewsletterId 'nl_id' 'content_id';
  60.             $bindVars[$this->verifyId$this->mNewsletterId )$this->mNewsletterId $this->mContentId;
  61.  
  62.             $this->getServicesSql'content_load_function'$selectSql$joinSql$whereSql$bindVars );
  63.  
  64. /*            if( $pUserId ) {
  65.                 error_log( 'BitNewsleters: user id loading not implemented yet' );
  66.                 $whereSql = "";
  67.                 $joinSql = "";
  68.             }
  69. */
  70.             $query "SELECT * $selectSql
  71.                       FROM `".BIT_DB_PREFIX."newsletters` n
  72.                           INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( n.`content_id`=lc.`content_id` )
  73.                           $joinSql
  74.                       WHERE n.`$lookupColumn`=? $whereSql";
  75.             $result $this->mDb->query($query,$bindVars);
  76.             if ($result->numRows()) {
  77.                 $this->mInfo = $result->fetchRow();
  78.                 $this->mNewsletterId $this->mInfo['nl_id'];
  79.                 $this->mContentId = $this->mInfo['content_id'];
  80.             }
  81.         }
  82.         returncount$this->mInfo ) );
  83.     }
  84.  
  85.     function loadEditions({
  86.         if$this->isValid() ) {
  87.             $this->mEditions $this->getEditions();
  88.         }
  89.     }
  90.  
  91.     function store&$pParamHash //$nl_id, $name, $description, $allow_user_sub, $allow_any_sub, $unsub_msg, $validate_addr) {
  92.         if$this->verify$pParamHash ) ) {
  93.             $this->mDb->StartTrans();
  94.             ifparent::store$pParamHash ) ) {
  95.                 if$this->mNewsletterId {
  96.                     $result $this->mDb->associateUpdateBIT_DB_PREFIX."newsletters"$pParamHash['newsletter_store']array "nl_id" => $this->mNewsletterId ) );
  97.                 else {
  98.                     $pParamHash['newsletter_store']['content_id'$pParamHash['content_id'];
  99.                     $result $this->mDb->associateInsertBIT_DB_PREFIX."newsletters"$pParamHash['newsletter_store');
  100.                 }
  101.                 $this->mDb->CompleteTrans();
  102.             else {
  103.                 $this->mDb->RollbackTrans();
  104.             }
  105.         }
  106.         returncount$this->mErrors == );
  107.     }
  108.  
  109.     function verify&$pParamHash {
  110.         // It is possible a derived class set this to something different
  111.         ifempty$pParamHash['content_type_guid') ) {
  112.             $pParamHash['content_type_guid'$this->mContentTypeGuid;
  113.         }
  114.         $pParamHash['newsletter_store']["allow_user_sub"(isset($pParamHash["allow_user_sub"]&& $pParamHash["allow_user_sub"== 'on''y' 'n';
  115.         $pParamHash['newsletter_store']["allow_any_sub"(isset($pParamHash["allow_any_sub"]&& $pParamHash["allow_any_sub"== 'on''y''n';
  116.         $pParamHash['newsletter_store']["unsub_msg"(isset($pParamHash["unsub_msg"]&& $pParamHash["unsub_msg"== 'on''y' 'n';
  117.         $pParamHash['newsletter_store']["validate_addr"(isset($pParamHash["validate_addr"]&& $pParamHash["validate_addr"== 'on''y' 'n';
  118.         returncount$this->mErrors == );
  119.     }
  120.  
  121.     function getSubscriberInfo$pLookup {
  122.         $ret array();
  123.         if$this->isValid() ) {
  124.             $bindVars array();
  125.             $whereSql '';
  126.             if!empty$pLookup['email') ) {
  127.                 $whereSql .= ' AND `email`=? ` ';
  128.                 $bindVars[$pLookup['email'];
  129.             }
  130.             if!empty$pLookup['user_id') ) {
  131.                 $whereSql .= ' AND `user_id`=? ` ';
  132.                 $bindVars[$pLookup['user_id'];
  133.             }
  134.             $whereSql preg_replace'/^[\s]AND/'''$whereSql );
  135.             $query "SELECT `content_id` AS `hash_key`, ms.* from `".BIT_DB_PREFIX."mail_subscriptions` ms WHERE $whereSql ";
  136.             if$res $this->mDb->query$query$bindVars ) ) {
  137.                 $ret $res->GetAssoc();
  138.             }
  139.         }
  140.         return $ret;
  141.     }
  142.  
  143.     function getSubscribers$pAll=FALSE{
  144.         $ret array();
  145.         if$this->isValid() ) {
  146.             $whereSql $pAll '' '  `unsubscribe_date` is NULL AND ';
  147.             $query "select * from `".BIT_DB_PREFIX."mail_subscriptions` WHERE $whereSql `content_id`=?";
  148.             if$res $this->mDb->query$queryarray$this->mContentId ) ) ) {
  149.                 $ret $res->GetRows();
  150.             }
  151.         }
  152.         return $ret;
  153.     }
  154.  
  155.     function removeSubscription$email$notify FALSE$del_record FALSE {
  156.         if ($del_record{
  157.             $this->mDb->query("DELETE FROM `".BIT_DB_PREFIX."mail_subscriptions` WHERE `content_id`=? AND `email`=?"array($this->mContentId$email));
  158.         else {
  159.             $urlCode $this->mDb->getOne("select `sub_code` from `".BIT_DB_PREFIX."mail_subscriptions` where `content_id`=? and `email`=?"array($this->mContentId$email));
  160.             $this->unsubscribe($urlCode$notify);
  161.         }
  162.     }
  163.  
  164.     function subscribe$pSubscribeHash // $notify = FALSE, $remind = FALSE ) {
  165.         $ret FALSE;
  166.         if$this->isValid() ) {
  167.             global $gBitSystem;
  168.             global $gBitSmarty;
  169.             global $gBitUser;
  170.  
  171.             // Check for duplicates
  172.             $all_subs $this->getSubscribersTRUE );
  173.             $duplicate FALSE;
  174.             foreach($all_subs as $sub{
  175.                 if$sub['email'== $pSubscribeHash['email'{
  176.                     $duplicate TRUE;
  177.                     $urlCode $sub['sub_code'];
  178.                 elseif!empty$pSubscribeHash['user_id'&& $sub['user_id'== $pSubscribeHash['user_id'{
  179.                 }
  180.             }
  181.  
  182.             $urlCode (!$duplicatemd5BitUser::genPass() ) $urlCode;
  183.             $now date("U");
  184.             // Generate a code and store it and send an email  with the
  185.             // URL to confirm the subscription put valid as 'n'
  186.             if (!$duplicate{
  187.                 if@BitBase::verifyId$pSubscribeHash['user_id') ) {
  188.                     // we have user_id subscribing, use the id, NULL the email
  189.                     $subUserId $pSubscribeHash['user_id'];
  190.                     $subEmail NULL;
  191.                 else {
  192.                     // we have user_id subscribing, use the id, NULL the email
  193.                     $subUserId NULL;
  194.                     $subEmail $pSubscribeHash['email'];
  195.                 }
  196.                 $query "insert into `".BIT_DB_PREFIX."mail_subscriptions` (`content_id`, `user_id`, `email`,`sub_code`,`is_valid`,`subscribed_date`) VALUES (?,?,?,?,?,?)";
  197.                 $result $this->mDb->query$queryarray$this->mContentId$subUserId$subEmail$urlCode'n'(int)$now ) );
  198.             }
  199.             if( ( !empty$pSubscribeHash['notify'&& $this->getField'validate_addr' == 'y'|| !empty$pSubscribeHash['remind') ) {
  200.                 // Generate a code and store it and send an email  with the
  201.                 $gBitSmarty->assign'sub_code'$urlCode );
  202.                 $mail_data $gBitSmarty->fetch('bitpackage:newsletters/confirm_newsletter_subscription.tpl');
  203.                 @mail($emailtra('Newsletter subscription information at'' ' $gBitSystem->getConfig"bitmailer_from" )$mail_data,
  204.                     "From: " $gBitSystem->getConfig"sender_email" "\r\nContent-type: text/plain;charset=utf-8\r\n");
  205.             }
  206.             $ret TRUE;
  207.         }
  208.         return $ret;
  209.     }
  210.  
  211.     function unsubscribe$pMixed$notify TRUE {
  212.         global $gBitSystem;
  213.         global $gBitSmarty;
  214.         global $gBitUser;
  215.  
  216.         $ret FALSE;
  217.         $now date("U");
  218.  
  219.         ifis_numeric$pMixed ) ) {
  220.             $query "SELECT `content_id` FROM `".BIT_DB_PREFIX."newsletters` WHERE `nl_id`=?";
  221.             if$subRow['content_id'$this->mDb->getOne$queryarray$pMixed ) ) ) {
  222.                 $subRow['col_name''user_id';
  223.                 $subRow['col_val'$gBitUser->mUserId;
  224.             }
  225.         elseifis_string$pMixed ) ) {
  226.             $query "SELECT * FROM `".BIT_DB_PREFIX."mail_queue` WHERE `url_code`=?";
  227.             if$subRow $this->mDb->getRow$queryarray$pMixed ) ) ) {
  228.                 $subRow['col_name'!empty$subRow['user_id''user_id' 'email';
  229.                 $subRow['col_val'!empty$subRow['user_id'$subRow['user_id'$subRow['email'];
  230.             }
  231.         }
  232.  
  233.         if!empty$subRow ) ) {
  234.             $this->mContentId = $subRow['content_id'];
  235.             $this->load();
  236.             if$this->mDb->getRow"SELECT * FROM `".BIT_DB_PREFIX."mail_subscriptions` WHERE `$subRow[col_name]`=?"array$subRow['col_val') ) ) {
  237.                 $query "UPDATE `".BIT_DB_PREFIX."mail_subscriptions` SET `unsubscribe_date`=?, `content_id`=? WHERE `$subRow[col_name]`=? AND `unsubscribe_date` IS NULL";
  238.             else {
  239.                 $query "INSERT INTO `".BIT_DB_PREFIX."mail_subscriptions` (`unsubscribe_date`,`content_id`,`$subRow[col_name]`) VALUES(?,?,?)";
  240.             }
  241.             $result $this->mDb->query$queryarray$now$subRow['content_id']$subRow['col_val') );
  242.             if$notify {
  243.                 // Now send a bye bye email
  244.                 $gBitSmarty->assign('sub_code'$res["sub_code"]);
  245.                 $mail_data $gBitSmarty->fetch('bitpackage:newsletters/newsletter_byebye.tpl');
  246.                 @mail($res["email"]tra('Thank you from'' ' $gBitSystem->getConfig"bitmailer_from" )$mail_data,
  247.                     "From: " $gBitSystem->getConfig"sender_email" "\r\nContent-type: text/plain;charset=utf-8\r\n");
  248.             }
  249.             $ret TRUE;
  250.         }
  251.         return $ret;
  252.     }
  253.  
  254. /*
  255.     function add_all_users($nl_id) {
  256.         $query = "select `email` from `".BIT_DB_PREFIX."users_users`";
  257.         $result = $this->mDb->query($query,array());
  258.         while ($res = $result->fetchRow()) {
  259.             $email = $res["email"];
  260.             if (!empty($email)) {
  261.                 $this->newsletter_subscribe($nl_id, $email);
  262.             }
  263.         }
  264.     }
  265.  
  266.     function updateUsers() {
  267.         if( $this->isValid() ) {
  268.             $users = $this->mDb->getOne( "select count(*) from `".BIT_DB_PREFIX."mail_subscriptions` where `nl_id`=?", array( $this->mNewsletterId ) );
  269.             $query = "update `".BIT_DB_PREFIX."newsletters` set `users`=? where `nl_id`=?";
  270.             $result = $this->mDb->query( $query, array( $users, $this->mNewsletterId ) );
  271.         }
  272.     }
  273. */
  274.  
  275.     public static function getList&$pListHash {
  276.         global $gBitDb;
  277.         if empty$pParamHash["sort_mode") ) {
  278.             $pListHash['sort_mode''created_desc';
  279.         }
  280.         BitBase::prepGetList$pListHash );
  281.         $bindVars array();
  282.         $joinSql '';
  283.         $mid '';
  284.  
  285.         if@BitBase::verifyId$pListHash['nl_id') ) {
  286.             $mid .= ' AND n.nl_id=? ';
  287.             $bindVars[$pListHash['nl_id'];
  288.         }
  289.  
  290.         if!empty$pListHash['find') ) {
  291.             $findesc '%' $pListHash['find''%';
  292.             $mid .= " AND (`name` like ? or `description` like ?)";
  293.             $bindVars[$findesc;
  294.             $bindVars[$findesc;
  295.         }
  296.  
  297.         if!empty$pListHash['registration_optin') ) {
  298.             $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') ";
  299.         }
  300.  
  301.         $query "SELECT *
  302.                   FROM `".BIT_DB_PREFIX."newsletters` n INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( n.`content_id`=lc.`content_id`)
  303.                     $joinSql
  304.                   WHERE n.`content_id`=lc.`content_id` $mid
  305.                   ORDER BY ".$gBitDb->convertSortmode$pListHash['sort_mode');
  306.         $result $gBitDb->query$query$bindVars$pListHash['max_records']$pListHash['offset');
  307.  
  308.         $query_cant "select count(*) from `".BIT_DB_PREFIX."newsletters` $mid";
  309.  
  310.         $ret array();
  311.         while$res $result->fetchRow() ) {
  312.             $res['display_url'BitNewsletter::getDisplayUrlFromHash$res );
  313.             $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') );
  314.             $res["unsub_count"$gBitDb->getOne"SELECT COUNT(*) FROM `".BIT_DB_PREFIX."mail_subscriptions` WHERE `content_id`=?",array(int)$res['content_id') );
  315.             $ret[$res['content_id']] $res;
  316.         }
  317.  
  318.         return $ret;
  319.     }
  320.  
  321. /*    function list_newsletter_subscriptions($nl_id, $offset, $maxRecords, $sort_mode, $find) {
  322.         $bindVars = array((int)$nl_id);
  323.         if ($find) {
  324.             $findesc = '%' . $find . '%';
  325.             $mid = " where `nl_id`=? and (`name` like ? or `description` like ?)";
  326.             $bindVars[] = $findesc;
  327.             $bindVars[] = $findesc;
  328.         } else {
  329.             $mid = " where `nl_id`=? ";
  330.         }
  331.  
  332.         $query = "select * from `".BIT_DB_PREFIX."mail_subscriptions` $mid order by ".$this->mDb->convertSortmode("$sort_mode");
  333.         $query_cant = "select count(*) from mail_subscriptions $mid";
  334.         $result = $this->mDb->query($query,$bindVars,$maxRecords,$offset);
  335.         $cant = $this->mDb->getOne($query_cant,$bindVars);
  336.         $ret = array();
  337.  
  338.         while ($res = $result->fetchRow()) {
  339.             $ret[] = $res;
  340.         }
  341.         $retval = array();
  342.         $retval["data"] = $ret;
  343.         $retval["cant"] = $cant;
  344.         return $retval;
  345.     }
  346.  
  347. */
  348.  
  349.     function expunge({
  350.         $ret FALSE;
  351.         if$this->isValid() ) {
  352.             $this->mDb->StartTrans();
  353.             $query "DELETE FROM `".BIT_DB_PREFIX."newsletters` where `nl_id`=?";
  354.             $result $this->mDb->query$queryarray$this->mNewsletterId ) );
  355.             // Clear out all individual subscriptions/unsubscriptions, but preserve the unsubscribe_all's
  356.             $query "DELETE FROM `".BIT_DB_PREFIX."mail_subscriptions` WHERE `content_id`=? AND `unsubscribe_all` IS NOT NULL";
  357.             $result $this->mDb->query$queryarray$this->mContentId ) );
  358.             $query "UPDATE `".BIT_DB_PREFIX."mail_subscriptions` SET `content_id`=NULL WHERE `content_id`=? AND `unsubscribe_all` IS NOT NULL";
  359.             $result $this->mDb->query$queryarray$this->mContentId ) );
  360.             ifparent::expunge() ) {
  361.                 $ret TRUE;
  362.                 $this->mDb->CompleteTrans();
  363.             else {
  364.                 $this->mDb->RollbackTrans();
  365.             }
  366.         }
  367.         return $ret;
  368.     }
  369.  
  370.     function isValid({
  371.         return$this->verifyId$this->mNewsletterId ) );
  372.     }
  373.  
  374.  
  375.     /**
  376.      * Generate a valid url for the Newsletter
  377.      *
  378.      * @param    object    $pNewsletterId of the item to use
  379.      * @return    object    Url String
  380.      */
  381.     public static function getDisplayUrlFromHash&$pParamHash {
  382.         global $gBitSystem;
  383.         $ret NULL;
  384.         ifBitBase::verifyId$pParamHash['nl_id') ) {
  385.             if$gBitSystem->isFeatureActive'pretty_urls' ) ) {
  386.                 $ret NEWSLETTERS_PKG_URL.$pParamHash['nl_id'];
  387.             else {
  388.                 $ret NEWSLETTERS_PKG_URL.'index.php?nl_id='.$pParamHash['nl_id'];
  389.             }
  390.         else {
  391.             $ret NEWSLETTERS_PKG_URL.'index.php';
  392.         }
  393.         return $ret;
  394.     }
  395.  
  396.  
  397.     function getEditions$pNewsletterId NULL {
  398.         $ret array();
  399.         ifempty$pNewsletterId ) ) {
  400.             $nlId $this->mNewsletterId;
  401.         elseifBitBase::verifyId$pNewsletterId ) ) {
  402.             $nlId $pNewsletterId;
  403.         }
  404.         if!empty$nlId ) ) {
  405.             $listHash array'nl_id' => $nlId  );
  406.             $ret BitNewsletterEdition::getList$listHash );
  407.         }
  408.         return $ret;
  409.     }
  410.     
  411.     function getUserSubscriptions$pUserId$pEmail {
  412.         global $gBitDb;
  413.         $query "SELECT `content_id` AS hash_key, ms.* FROM `".BIT_DB_PREFIX."mail_subscriptions` ms WHERE `user_id`=? OR `email`=?";
  414.         $ret $gBitDb->getAssoc$queryarray$pUserId$pEmail ) );
  415.         return $ret;
  416.     }
  417.  
  418.  
  419. }

Documentation generated on Wed, 29 Jul 2015 13:55:54 +0000 by phpDocumentor 1.5.0-lsces