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