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

Source for file BitNewsletterEdition.php

Documentation is available at BitNewsletterEdition.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  *
  5.  * @copyright (c) 2004-15 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.  *  Class that handles editions of newsletters
  12.  * @package newsletters
  13.  *
  14.  * @date created 2005/12/08
  15.  *
  16.  * @author spiderr <spider@steelsun.com>
  17.  *
  18.  * @version $Revision$
  19.  */
  20.  
  21. /**
  22.  * required setup
  23.  */
  24. require_once( NEWSLETTERS_PKG_PATH.'BitNewsletter.php' );
  25. require_once( LIBERTY_PKG_PATH.'LibertyMime.php' );
  26.  
  27. define( 'BITNEWSLETTEREDITION_CONTENT_TYPE_GUID', 'bitnewsletteredn' );
  28.  
  29. /**
  30.  * @package newsletters
  31.  */
  32. class BitNewsletterEdition extends LibertyMime {
  33.     function __construct( $pEditionId=NULL, $pContentId=NULL, $pNlId=NULL ) {
  34.         parent::__construct();
  35.             'content_type_guid' => BITNEWSLETTEREDITION_CONTENT_TYPE_GUID,
  36.             'content_name' => 'Edition',
  37.             'handler_class' => 'BitNewsletterEdition',
  38.             'handler_package' => 'newsletters',
  39.             'handler_file' => 'BitNewsletterEdition.php',
  40.             'maintainer_url' => 'http://www.bitweaver.org'
  41.         ) );
  42.         $this->mEditionId = $pEditionId;
  43.         $this->mContentId = $pContentId;
  44.         $this->mNewsletter = new BitNewsletter( $pNlId );
  45.  
  46.         // Permission setup
  47.         //$this->mViewContentPerm  = '';
  48.         $this->mUpdateContentPerm  = 'p_newsletters_create_editions';
  49.         $this->mAdminContentPerm = 'p_newsletters_admin';
  50.     }
  51.  
  52.     function verify( &$pParamHash ) {
  53.  
  54.         if( @$this->verifyId( $pParamHash['nl_content_id'] ) ) {
  55.             $pParamHash['edition_store']["nl_content_id"] = $pParamHash['nl_content_id'];
  56.         } else {
  57.             $this->mErrors['nl_content_id'] = tra( 'No newsletter was selected for this edition.' );
  58.         }
  59.         $pParamHash['edition_store']['is_draft'] = !empty( $pParamHash['is_draft'] ) ? 'y' : NULL;
  60.         $pParamHash['edition_store']['reply_to'] = !empty( $pParamHash['reply_to'] ) ? $pParamHash['reply_to'] : NULL;
  61.  
  62.         return( count( $this->mErrors ) == 0 );
  63.     }
  64.  
  65.     function store( &$pParamHash ) {
  66.         if( $this->verify( $pParamHash ) ) {
  67.             $this->mDb->StartTrans();
  68.             if( parent::store( $pParamHash ) ) {
  69.                 if( $this->mEditionId ) {
  70.                     $result = $this->mDb->associateUpdate( BIT_DB_PREFIX."newsletters_editions", $pParamHash['edition_store'], array ( "edition_id" => $this->mEditionId ) );
  71.                 } else {
  72.                     $pParamHash['edition_store']['content_id'] = $pParamHash['content_id'];
  73.                     $result = $this->mDb->associateInsert( BIT_DB_PREFIX."newsletters_editions", $pParamHash['edition_store'] );
  74.                 }
  75.                 $this->mDb->CompleteTrans();
  76.                 $this->load();
  77.             } else {
  78.                 $this->mDb->RollbackTrans();
  79.             }
  80.         }
  81.         return( count( $this->mErrors ) == 0 );
  82.     }
  83.  
  84.     function load( $pContentId = NULL, $pPluginParams = NULL ) {
  85.         if( $this->verifyId( $this->mEditionId ) || $this->verifyId( $this->mContentId ) ) {
  86.             global $gBitSystem;
  87.  
  88.             $bindVars = array(); $selectSql = ''; $joinSql = ''; $whereSql = '';
  89.  
  90.             $lookupColumn = $this->verifyId( $this->mEditionId )? 'edition_id' : 'content_id';
  91.             $lookupId = $this->verifyId( $this->mEditionId )? $this->mEditionId : $this->mContentId;
  92.             array_push( $bindVars, $lookupId );
  93.  
  94.             $this->getServicesSql( 'content_load_function', $selectSql, $joinSql, $whereSql, $bindVars );
  95.  
  96.             $query = "SELECT ne.*, lc.*
  97.                       FROM `".BIT_DB_PREFIX."newsletters_editions` ne
  98.                         INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( ne.`content_id`=lc.`content_id` )
  99.                       WHERE ne.`$lookupColumn`=? $whereSql";
  100.             if ( $result = $this->mDb->query($query,$bindVars) ) {
  101.                 $this->mInfo = $result->fetchRow();
  102.                 $this->mEditionId = $this->mInfo['edition_id'];
  103.                 $this->mContentId = $this->mInfo['content_id'];
  104.                 LibertyMime::load();
  105.                 $this->mNewsletter = new BitNewsletter( NULL, $this->mInfo['nl_content_id'] );
  106.                 $this->mNewsletter->load();
  107.             } else {
  108.                 unset( $this->mEditionId );
  109.             }
  110.         }
  111.         return( count( $this->mInfo ) );
  112.     }
  113.  
  114.     function isValid() {
  115.         return( $this->verifyId( $this->mEditionId ) );
  116.     }
  117.  
  118.     /**
  119.      * Generate a valid url for the Newsletter Edition
  120.      *
  121.      * @param    object    PostId of the item to use
  122.      * @return    object    Url String
  123.      */
  124.     public static function getDisplayUrlFromHash( &$pParamHash ) {
  125.         $ret = NULL;
  126.         global $gBitSystem;
  127.         if( BitBase::verifyId( $pParamHash['edition_id'] ) ) {
  128.             if( $gBitSystem->isFeatureActive( 'pretty_urls' ) ) {
  129.                 $ret = NEWSLETTERS_PKG_URL.'edition/'.$pParamHash['edition_id'];
  130.             } else {
  131.                 $ret = NEWSLETTERS_PKG_URL.'edition.php?edition_id='.$pParamHash['edition_id'];
  132.             }
  133.         } else {
  134.             $ret = NEWSLETTERS_PKG_URL.'edition.php';
  135.         }
  136.         return $ret;
  137.     }
  138.  
  139.  
  140.     function getList( &$pListHash ) {
  141.         global $gBitDb;
  142.  
  143.         $bindVars = array();
  144.         parent::prepGetList( $pListHash );
  145.         $mid = '';
  146.  
  147.         if( @BitBase::verifyId( $pListHash['nl_id'] ) ) {
  148.             $mid .= (empty( $mid ) ? 'WHERE' : 'AND').' n.nl_id=? ';
  149.             $bindVars[] = $pListHash['nl_id'];
  150.         }
  151.  
  152.         if( $pListHash['find'] ) {
  153.             $findesc = '%' . $pListHash['find'] . '%';
  154.             $mid .= (empty( $mid ) ? 'WHERE' : 'AND').' (lc.`title` like ? or lc.`data` like ?)';
  155.             $bindVars[] = $findesc;
  156.             $bindVars[] = $findesc;
  157.         }
  158.  
  159.         $query = "SELECT `edition_id` AS `hash_key`, ne.*, lc.*, lc2.`title` AS `newsletter_title`
  160.                   FROM `".BIT_DB_PREFIX."newsletters_editions` ne
  161.                       INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`content_id`=ne.`content_id` )
  162.                       INNER JOIN `".BIT_DB_PREFIX."newsletters` n ON( ne.`nl_content_id`=n.`content_id` )
  163.                       LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lc2 ON( n.`content_id`=lc2.`content_id` )
  164.                   $mid ORDER BY ".$gBitDb->convertSortmode( $pListHash['sort_mode'] );
  165.         $query_cant = "select count(*) from `".BIT_DB_PREFIX."newsletters` n INNER JOIN `".BIT_DB_PREFIX."newsletters_editions` ne ON(n.`content_id`=ne.`nl_content_id`) $mid";
  166.         $ret = $gBitDb->getAssoc( $query, $bindVars, $pListHash['max_records'], $pListHash['offset'] );
  167.         foreach( array_keys( $ret ) as $k ) {
  168.             $ret[$k]['display_url'] = BitNewsletterEdition::getDisplayUrlFromHash( $ret[$k] );
  169.             // remove formating tags
  170.             $data = preg_replace( '/{[^{}]*}/', '', $ret[$k]['data'] );
  171. //            $ret[$k]['parsed'] = BitNewsletterEdition::parseData( $data, $ret[$k]['format_guid'] );
  172.         }
  173.         $pListHash['total_records'] = $gBitDb->getOne( $query_cant, $bindVars );
  174.         $pListHash['block_pages'] = 5;
  175.         $pListHash['total_pages'] = ceil( $pListHash['total_records'] / $pListHash['max_records'] );
  176.         $pListHash['current_page'] = (!empty( $pListHash['offset'] ) ? floor( $pListHash['offset'] / $pListHash['max_records'] ) + 1 : 1 );
  177.  
  178.         return $ret;
  179.     }
  180.  
  181.     function expunge() {
  182.         $ret = FALSE;
  183.         if( $this->isValid() ) {
  184.             $this->mDb->StartTrans();
  185.             $query = "DELETE FROM `".BIT_DB_PREFIX."newsletters_editions` WHERE `content_id`=?";
  186.             $result = $this->mDb->query( $query, array( $this->mContentId ) );
  187.             if( LibertyMime::expunge() ) {
  188.                 $ret = TRUE;
  189.                 $this->mDb->CompleteTrans();
  190.             } else {
  191.                 $this->mDb->RollbackTrans();
  192.             }
  193.         }
  194.         return $ret;
  195.     }
  196.  
  197.     function isDraft() {
  198.         return( $this->getField( 'is_draft' ) );
  199.     }
  200.  
  201.     function getRecipients( $pGroupArray, $validated = TRUE, $pRequeue = FALSE ) {
  202.         global $gBitUser;
  203.         $ret = array();
  204.         if( is_array( $pGroupArray ) ) {
  205.             foreach( $pGroupArray as $groupId ) {
  206.                 $ret = array_merge( $ret, $gBitUser->getGroupUserData( $groupId, array( 'email', 'uu.user_id', 'login', 'real_name' ) ) );
  207.             }
  208.  
  209.             if ( array_search( 'send_subs', $pGroupArray ) !== false ) {
  210.                 $valid = "";
  211.                 $bindvars = array( $this->mNewsletter->mNewsletterId );
  212.                 if ($validated) {
  213.                     $valid = " AND `is_valid`=?";
  214.                     $bindvars[] = 'y';
  215.                 }
  216.                 $query = "SELECT * FROM `".BIT_DB_PREFIX."mail_subscriptions`
  217.                       WHERE `content_id`=? AND `unsubscribe_date` IS NULL AND `unsubscribe_all` IS NULL".$valid;
  218.                 $subs = $this->mDb->getArray( $query, $bindvars );
  219.                 foreach( $subs as $sub) {
  220.                     if (!isset($ret[$sub['email']]))
  221.                         $ret[$sub['email']] = $sub;
  222.                 }
  223.             }
  224.             if( !$pRequeue ) {
  225.                 $query = "SELECT `email`, `user_id` FROM `".BIT_DB_PREFIX."mail_queue` WHERE `content_id`=?";
  226.                 if( $dupes = $this->mDb->getAssoc( $query, array( $this->mContentId ) ) ) {
  227.                     $ret = array_diff_keys( $ret, $dupes );
  228.                 }
  229.             }
  230.         }
  231.         return $ret;
  232.     }
  233.  
  234.     function render() {
  235.         global $gBitSmarty;
  236.         $ret = NULL;
  237.         if( $this->isValid() ) {
  238.             $gBitSmarty->assign_by_ref( 'gContent', $this );
  239.             $ret = $gBitSmarty->fetch( 'bitpackage:newsletters/view_edition.tpl' );
  240.         }
  241.         return $ret;
  242.     }
  243. }
  244.  
  245. ?>

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