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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
<?php
/**
* @version $Header$
* @package articles
*
* @copyright Copyright (c) 2004-2006, 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.
*/
/**
* Required setup
*/
global $gBitSystem;
require_once( KERNEL_PKG_PATH."BitBase.php" );
require_once( ARTICLES_PKG_CLASS_PATH.'BitArticle.php' );
/**
* @package articles
*/
class BitArticleTopic extends BitBase {
var $mTopicId;
function __construct($iTopicId = NULL, $iTopicName = NULL) {
$this->mTopicId = NULL;
parent::__construct();
if ($iTopicId || $iTopicName) {
$this->loadTopic(array('topic_id'=>$iTopicId, 'topic_name'=>$iTopicName));
}
}
function isValid() {
return ($this->verifyId($this->mTopicId));
}
function loadTopic($iParamHash = NULL) {
$whereSQL = ' WHERE artt.';
$ret = NULL;
if (@$this->verifyId($iParamHash['topic_id']) || !empty($iParamHash['topic_name'])) {
$whereSQL .= "`".((@$this->verifyId($iParamHash['topic_id']) || $this->mTopicId) ? 'topic_id' : 'topic_name')."` = ?";
$bindVars = array((@$this->verifyId($iParamHash['topic_id']) ? (int)$iParamHash['topic_id'] : ($this->mTopicId ? $this->mTopicId : $iParamHash['topic_name'])) );
$sql = "SELECT artt.*".
"FROM `".BIT_DB_PREFIX."article_topics` artt ".
$whereSQL;
$this->mInfo = $this->mDb->getRow($sql, $bindVars);
if( !empty( $this->mInfo['topic_id'] ) ) {
$this->mTopicId = $this->mInfo['topic_id'];
if ($this->mInfo['has_topic_image']) {
$this->mInfo['topic_image_url'] = static::getTopicImageStorageUrl($this->mTopicId, FALSE, TRUE);
} else {
$this->mInfo['topic_image_url'] = NULL;
}
}
}
return $ret;
}
function verify(&$iParamHash) {
// Validate the (optional) topic_id parameter
if (@$this->verifyId($iParamHash['topic_id'])) {
$cleanHash['topic_id'] = (int)$iParamHash['topic_id'];
} else {
$cleanHash['topic_id'] = NULL;
}
// Was an acceptable name given?
if (empty($iParamHash['topic_name']) || ($iParamHash['topic_name'] == '')) {
$this->mErrors['topic_name'] = tra("Invalid or blank topic name supplied");
} else if (empty($iParamHash['topic_id'])) {
$ret = $this->getTopicList( array( 'topic_name' => $iParamHash['topic_name'] ) );
if ( sizeof( $ret ) ) {
$this->mErrors['topic_name'] = 'Topic "'.$iParamHash['topic_name'].'" already exists. Please choose a different name.';
} else {
$cleanHash['topic_name'] = $iParamHash['topic_name'];
}
}
else {
$cleanHash['topic_name'] = $iParamHash['topic_name'];
}
// Whether the topic is active or not
if ( empty($iParamHash['active_topic']) || (strtoupper($iParamHash['active_topic']) != 'CHECKED' && strtoupper($iParamHash['active_topic']) != 'ON' && strtoupper($iParamHash['active_topic']) != 'Y')) {
if (@$this->verifyId($cleanHash['topic_id'])) {
$cleanHash['active_topic'] = 'n';
} else {
// Probably a new topic so lets go ahead and enable it
$cleanHash['active_topic'] = 'y';
}
} else {
$cleanHash['active_topic'] = 'y';
}
if (empty($iParamHash['created'])) {
global $gBitSystem;
$cleanHash['created'] = $gBitSystem->getUTCTime();
}
$iParamHash = $cleanHash;
return(count($this->mErrors) == 0);
}
function storeTopic($iParamHash = NULL) {
global $gLibertySystem;
global $gBitUser;
if ($this->verify($iParamHash)) {
if (!$iParamHash['topic_id']) {
$topicId = $this->mDb->GenID('article_topics_id_seq');
} else {
$topicId = $this->mTopicId;
}
if( !empty( $_FILES['upload'] ) && $_FILES['upload']['tmp_name'] ) {
$checkFunc = liberty_get_function( 'can_thumbnail' );
if( $checkFunc( $_FILES['upload']['type'] )) {
$fileHash = $_FILES['upload'];
$fileHash['dest_branch'] = $this->getTopicImageBaseUrl( $topicId );
$fileHash['source_file'] = $fileHash['tmp_name'];
liberty_clear_thumbnails( $fileHash );
liberty_generate_thumbnails( $fileHash );
$iParamHash['has_topic_image'] = 'y';
} else {
$this->mErrors = tra( "The file you uploaded doesn't appear to be a valid image. The reported mime type is" ).": ".$_FILES['upload']['type'];
}
}
if( $iParamHash['topic_id'] ) {
$this->mDb->associateUpdate( BIT_DB_PREFIX."article_topics", $iParamHash, array( 'topic_id' => $iParamHash['topic_id'] ) );
} else {
$iParamHash['topic_id'] = $topicId;
$this->mDb->associateInsert( BIT_DB_PREFIX."article_topics", $iParamHash );
}
}
$this->mTopicId = $iParamHash['topic_id'];
}
/**
* Work out the path to the image for this article
* @param $pTopicId id of the article we need the image path for
* @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
* @return path on success, FALSE on failure
* @access public
**/
function getTopicImageBaseUrl( $pTopicId = NULL ) {
$ret = FALSE;
if( !@BitBase::verifyId( $pTopicId ) && $this->isValid() ) {
$pTopicId = $this->mTopicId;
}
if( @BitBase::verifyId( $pTopicId )) {
$ret = LibertyMime::getStorageUrl( 'topics/'.$pTopicId );
}
return $ret;
}
/**
* Get the full URL to the needed thumbnail
*
* @param numeric $pTopicId Topic ID of topic in question
* @access public
* @return Path to thumbnail, FALSE on failure
*/
public static function getTopicImageThumbUrl( $pTopicId ) {
$ret = FALSE;
if( @BitBase::verifyId( $pTopicId )) {
$ret = STORAGE_PKG_URL.ARTICLES_PKG_NAME.'/topic_'.$pTopicId.'.jpg';
}
return $ret;
}
public static function getTopicList( $pOptionHash=NULL ) {
global $gBitSystem;
$where = '';
$bindVars = array();
if( !empty( $pOptionHash['active_topic'] ) ) {
$where = " WHERE artt.`active_topic` = 'y' ";
}
if ( !empty( $pOptionHash['topic_name'] ) ) {
$where = " WHERE artt.`topic_name` = ? ";
$bindVars[] = $pOptionHash['topic_name'];
}
$query = "SELECT artt.*
FROM `".BIT_DB_PREFIX."article_topics` artt
$where ORDER BY artt.`topic_name`";
$result = $gBitSystem->mDb->query( $query, $bindVars );
$ret = array();
while( $res = $result->fetchRow() ) {
$res["num_articles"] = $gBitSystem->mDb->getOne( "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."articles` WHERE `topic_id`= ?", array( $res["topic_id"] ) );
if( empty( $res['topic_image_url'] ) && $res['has_topic_image'] == 'y' ) {
$res['topic_image_url'] = static::getTopicImageStorageUrl( $res['topic_id'] );
}
$ret[] = $res;
}
return $ret;
}
function removeTopicImage() {
if( $this->mTopicId ) {
if( file_exists($this->getTopicImageStoragePath() ) ) {
@unlink( $this->getTopicImageStoragePath() );
}
$sql = "UPDATE `".BIT_DB_PREFIX."article_topics` SET `has_topic_image` = 'n' WHERE `topic_id` = ?";
$rs = $this->mDb->query($sql, array($this->mTopicId));
$this->mInfo['has_topic_image'] = 'n';
}
}
function activateTopic() {
$this->setActivation(TRUE);
}
function deactivateTopic() {
$this->setActivation(FALSE);
}
function setActivation($iIsActive = FALSE) {
$sql = "UPDATE `".BIT_DB_PREFIX."article_topics` SET `active_topic` = '".($iIsActive ? 'y' : 'n')."' WHERE `topic_id` = ?";
$rs = $this->mDb->query($sql, array($this->mTopicId));
$this->mInfo['active_topic'] = ($iIsActive ? 'y' : 'n');
}
function getTopicArticles() {
if (!$this->mTopicId) {
return NULL;
}
$sql = "SELECT `article_id` FROM `".BIT_DB_PREFIX."articles` WHERE `topic_id` = ?";
$rs = $this->mDb->query($sql, array($this->mTopicId));
$ret = array();
while ($row = $rs->fetchRow()) {
$tmpArticle = new BitArticle($row['article_id']);
$tmpArticle->load();
$ret[] = $tmpArticle;
}
}
function removeTopic($iRemoveArticles = FALSE) {
if (!$this->mTopicId) {
return NULL;
}
$this->removeTopicImage();
if ($iRemoveArticles == TRUE) {
$topicArticles = $this->getTopicArticles();
for ($articleCount = 0; $articleCount < count($topicArticles); $articleCount++) {
$topicArticles[$articleCount]->expunge();
}
} else {
$sql = "UPDATE `".BIT_DB_PREFIX."articles` SET `topic_id` = ? WHERE `topic_id` = ?";
$rs = $this->mDb->query($sql, array(NULL, $this->mTopicId));
}
$sql = "DELETE FROM `".BIT_DB_PREFIX."article_topics` WHERE `topic_id` = ?";
$rs = $this->mDb->query($sql, array($this->mTopicId));
}
/*****************************************************************************
* Image functions needed for backward compatability - these are needed to *
* handle old article image style images that are not attachments. generally *
* these functions are deprecated but needed for legacy code *
****************************************************************************/
/**
* Get the name of the article image file
*
* @param array $pTopicId article id
* @access public
* @return TRUE on success, FALSE on failure
*/
function getTopicImageStorageName( $pTopicId = NULL ) {
if( !@BitBase::verifyId( $pTopicId ) ) {
if( $this->isValid() ) {
$pTopicId = $this->mTopicId;
} else {
return NULL;
}
}
global $gBitSystem;
return "topic_$pTopicId.".$gBitSystem->getConfig( 'liberty_thumbnail_format', 'jpg' );
}
/**
* Work out the path to the image for this article
* @param $pTopicId id of the article we need the image path for
* @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
* @return path on success, FALSE on failure
* @access public
**/
function getTopicImageStoragePath( $pTopicId = NULL, $pBasePathOnly = FALSE ) {
$path = static::getArticleImageStoragePath( NULL, TRUE );
if( $pBasePathOnly ) {
return $path;
}
if( !@BitBase::verifyId( $pTopicId ) ) {
if( $this->isValid() ) {
$pTopicId = $this->mTopicId;
} else {
return NULL;
}
}
if( !empty( $pTopicId ) ) {
return $path.static::getTopicImageStorageName( $pTopicId );
} else {
return FALSE;
}
}
/**
* Work out the URL to the image for this article
* @param $pTopicId id of the article we need the image path for
* @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
* @return URL on success, FALSE on failure
* @access public
**/
public static function getTopicImageStorageUrl( $pTopicId, $pBasePathOnly = FALSE, $pForceRefresh = FALSE ) {
global $gBitSystem;
$ret = FALSE;
// first we check to see if this is a new type thumbnail. if that fails we'll use the old method
if( !( $ret = static::getTopicImageThumbUrl( $pTopicId ))) {
$url = static::getArticleImageStorageUrl( $this->mArticleId, NULL, TRUE );
if( $pBasePathOnly ) {
return $url;
}
if( is_file( static::getTopicImageStoragePath( NULL, TRUE ).static::getTopicImageStorageName( $pTopicId ))) {
$ret = $url.static::getTopicImageStorageName( $pTopicId ).( $pForceRefresh ? "?".$gBitSystem->getUTCTime() : '' );
}
}
return str_replace( "//", "/", $ret );
}
/*****************************************************************************
* Image functions needed for backward compatability - these are needed to *
* handle old article image style images that are not attachments. generally *
* these functions are deprecated but needed for legacy code *
* *
* the legacy code below here should go at some point. this code is old and *
* fugly. In fact, a lot of the code in here is fugly. we should use *
* pigoenholes to do this topic thing, now that pigoenholes can have primary *
* attachments. *
****************************************************************************/
/**
* Get the name of the article image file
*
* @param array $pArticleId article id
* @access public
* @return TRUE on success, FALSE on failure
*/
public function getArticleImageStorageName( $pArticleId ) {
$ret = FALSE;
if( BitBase::verifyId( $pArticleId ) ) {
$ret = "article_$pArticleId.jpg";
}
return $ret;
}
/**
* Work out the path to the image for this article
* @param $pArticleId id of the article we need the image path for
* @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
* @return path on success, FALSE on failure
* @access public
**/
function getArticleImageStoragePath( $pArticleId = NULL, $pBasePathOnly = FALSE ) {
$path = STORAGE_PKG_PATH.ARTICLES_PKG_NAME.'/';
if( !is_dir( $path ) ) {
mkdir_p( $path );
}
if( $pBasePathOnly ) {
return $path;
}
if( !@BitBase::verifyId( $pArticleId ) ) {
if( $this->isValid() ) {
$pArticleId = $this->mArticleId;
} else {
return NULL;
}
}
if( !empty( $pArticleId ) ) {
return $path.static::getArticleImageStorageName( $pArticleId );
} else {
return FALSE;
}
}
/**
* Work out the URL to the image for this article
* @param $pArticleId id of the article we need the image path for
* @param $pBasePathOnly bool TRUE / FALSE - specify whether you want full path or just base path
* @return URL on success, FALSE on failure
* @access public
**/
public static function getArticleImageStorageUrl( $pArticleId = NULL, $pBasePathOnly = FALSE, $pForceRefresh = FALSE ) {
global $gBitSystem;
$ret = FALSE;
$url = STORAGE_PKG_URL.ARTICLES_PKG_NAME.'/';
if( $pBasePathOnly ) {
return $url;
}
if( BitBase::verifyId( $pArticleId ) ) {
if( is_file( static::getArticleImageStoragePath( NULL, TRUE ).static::getArticleImageStorageName( $pArticleId ) ) ) {
$ret = $url.static::getArticleImageStorageName( $pArticleId ).( $pForceRefresh ? "?".$gBitSystem->getUTCTime() : '' );
}
}
return $ret;
}
}
|