| [ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/dao/model.class.php" ); 4 lt_include( PLOG_CLASS_PATH."class/dao/articlecommentstatus.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/dao/daocacheconstants.properties.php" ); 6 lt_include( PLOG_CLASS_PATH."class/dao/usercomment.class.php" ); 7 lt_include( PLOG_CLASS_PATH."class/dao/trackback.class.php" ); 8 9 /** 10 * different orders that comments can have 11 */ 12 define( "COMMENT_ORDER_OLDEST_FIRST", 1 ); 13 define( "COMMENT_ORDER_NEWEST_FIRST", 2 ); 14 15 /** 16 * whether we'd like to fetch a comment, trackback or anything 17 */ 18 define( "COMMENT_TYPE_COMMENT", 1 ); 19 define( "COMMENT_TYPE_TRACKBACK", 2 ); 20 define( "COMMENT_TYPE_ANY", -1 ); 21 22 /** 23 * \ingroup DAO 24 * 25 * Since comments and trackbacks are now in the same table, this class contains all the 26 * common code needed to deal with these items. Most of the methods are exactly the same in both 27 * ArticleComments and Trackbacks except that they take an additional parameter called 'status' 28 * which can either be 29 * 30 * - COMMENT_TYPE_COMMENT 31 * - COMMENT_TYPE_TRACKBACK 32 * - COMMENT_TYPE_ANY 33 * 34 * Depending on whether we'd like to retrieve a trackback or a comment. 35 */ 36 class CommentsCommon extends Model 37 { 38 39 var $_blogSettings; 40 var $blogSettings; 41 var $timeDiff; 42 43 function CommentsCommon() 44 { 45 $this->Model(); 46 $this->table = $this->getPrefix()."articles_comments"; 47 } 48 49 /** 50 * Adds a comment to an article 51 * 52 * @param comment the UserComment object that we're going to add. 53 * @return Returns true if successful or false if error. Also in case of success, it will modify the UserComment 54 * object passed by reference and include its new id. 55 */ 56 function addComment( &$comment ) 57 { 58 if(( $result = $this->add( $comment ))) { 59 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 60 $this->_cache->removeData( $comment->getArticleId(), CACHE_ARTICLE_COMMENTS_BYARTICLE_NEWEST_TO_OLDEST ); 61 $this->_cache->removeData( $comment->getArticleId(), CACHE_ARTICLE_COMMENTS_BYARTICLE_OLDEST_TO_NEWEST ); 62 // update the article comments 63 $article = $comment->getArticle(); 64 $blog = $article->getBlogInfo(); 65 if( $comment->getType() == COMMENT_TYPE_COMMENT ) { 66 $article->setNumComments( $this->getNumItems( $this->getPrefix().'articles_comments', 67 'article_id = '.$article->getId(). 68 ' AND status = '.COMMENT_STATUS_NONSPAM. 69 ' AND type = '.$comment->getType())); 70 $article->setTotalComments($this->getNumItems( $this->getPrefix().'articles_comments', 71 'article_id = '.$article->getId().' AND type = '.$comment->getType())); 72 $blog->setTotalComments($this->getNumItems( $this->getPrefix().'articles_comments', 73 'blog_id = '.$blog->getId().' AND type = '.$comment->getType())); 74 } 75 else { 76 $article->setNumTrackbacks( $this->getNumItems( $this->getPrefix().'articles_comments', 77 'article_id = '.$article->getId(). 78 ' AND status = '.COMMENT_STATUS_NONSPAM. 79 ' AND type = '.$comment->getType())); 80 $article->setTotalTrackbacks($this->getNumItems( $this->getPrefix().'articles_comments', 81 'article_id = '.$article->getId().' AND type = '.$comment->getType())); 82 $blog->setTotalTrackbacks($this->getNumItems( $this->getPrefix().'articles_comments', 83 'blog_id = '.$blog->getId().' AND type = '.$comment->getType())); 84 } 85 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 86 $articles = new Articles(); 87 $articles->updateArticle( $article ); 88 lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); 89 $blogs = new Blogs(); 90 $blogs->updateBlog( $blog ); 91 } 92 return( $result ); 93 } 94 95 /** 96 * Retrieves all the comments for a post 97 * 98 * @param artid The article identifier 99 * @param order The order in which comments should be retrieved 100 * @param status The status that the comment should have, use COMMENT_STATUS_ALL for 101 * all possible statuses 102 * @param page 103 * @param itemsPerPage 104 * @return False if error or an array of ArticleComments objects 105 */ 106 function getPostComments( $artid, 107 $order = COMMENT_ORDER_NEWEST_FIRST, 108 $status = COMMENT_STATUS_ALL, 109 $type = COMMENT_TYPE_ANY, 110 $page = -1, 111 $itemsPerPage = DEFAULT_ITEMS_PER_PAGE ) 112 { 113 $query = "SELECT * FROM ".$this->getPrefix()."articles_comments ". 114 "WHERE article_id = '".Db::qstr( $artid )."'"; 115 116 if( $status != COMMENT_STATUS_ALL ) 117 $query .= "AND status = '".Db::qstr( $status )."'"; 118 119 if( $type != COMMENT_TYPE_ANY ) 120 $query .= " AND type = '".Db::qstr( $type )."'"; 121 122 if( $order == COMMENT_ORDER_NEWEST_FIRST ) 123 $query .= " ORDER BY date DESC"; 124 else 125 $query .=" ORDER BY date ASC"; 126 127 $result = $this->Execute( $query, $page, $itemsPerPage ); 128 129 if( !$result ) 130 return( Array()); 131 132 $results = Array(); 133 134 while( $row = $result->FetchRow()) { 135 $results[] = $this->mapRow( $row ); 136 } 137 138 139 return( $results ); 140 } 141 142 /** 143 * Returns the total number of comments for a post 144 * 145 * @param artId the post id 146 * @param status 147 * @return The number of comments 148 */ 149 function getNumPostComments( $artId, $status = COMMENT_STATUS_ALL, $type = COMMENT_TYPE_ANY ) 150 { 151 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 152 $numComments = 0; 153 $articles = new Articles(); 154 $article = $articles->getArticle( $artId ); 155 156 if(!$article) 157 return 0; 158 159 if( $type == COMMENT_TYPE_COMMENT ) { 160 if( $status == COMMENT_STATUS_ALL ) { 161 $numComments = $article->getTotalComments(); 162 } 163 elseif( $status == COMMENT_STATUS_NONSPAM ) { 164 $numComments = $article->getNumComments(); 165 } 166 elseif( $status == COMMENT_STATUS_SPAM ) { 167 $numComments = $article->getTotalComments() - $article->getNumComments(); 168 } 169 else { 170 $numComments = 0; 171 } 172 } 173 else if($type == COMMENT_TYPE_TRACKBACK) { 174 if( $status == COMMENT_STATUS_ALL ) { 175 $numComments = $article->getTotalTrackBacks(); 176 } 177 elseif( $status == COMMENT_STATUS_NONSPAM ) { 178 $numComments = $article->getNumTrackBacks(); 179 } 180 elseif( $status == COMMENT_STATUS_SPAM ) { 181 $numComments = $article->getTotalTrackbacks() - $article->getNumTrackbacks(); 182 } 183 else { 184 $numComments = 0; 185 } 186 } 187 else{ 188 $numComments = $this->getNumPostComments($artId, $status, COMMENT_TYPE_COMMENT) + 189 $this->getNumPostComments($artId, $status, COMMENT_TYPE_TRACKBACK); 190 } 191 192 return( $numComments ); 193 } 194 195 /** 196 * returns the number of comments that a blog has 197 * 198 * @param blogId 199 * @param status 200 * @param type 201 * @param searchTerms 202 * @return The number of comments, or 0 if error or no comments 203 */ 204 function getNumBlogComments( $blogId, $status = COMMENT_STATUS_ALL, $type = COMMENT_TYPE_ANY, $searchTerms = "" ) 205 { 206 if( $status == COMMENT_STATUS_ALL && $type != COMMENT_TYPE_ANY && $searchTerms == "" ) { 207 // fast case, we can load the blog and query one of its intrinsic fields 208 lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); 209 $blogs = new Blogs(); 210 $blogInfo = $blogs->getBlogInfo( $blogId ); 211 if( !$blogInfo ) 212 $numComments = 0; 213 else { 214 if( $type == COMMENT_TYPE_COMMENT ) 215 $numComments = $blogInfo->getTotalComments(); 216 else 217 $numComments = $blogInfo->getTotalTrackbacks(); 218 } 219 } 220 else { 221 // create the table name 222 $prefix = $this->getPrefix(); 223 $table = "{$prefix}articles_comments c"; 224 // and the condition if any... 225 $cond = "c.blog_id = '".Db::qstr($blogId)."'"; 226 if( $status != COMMENT_STATUS_ALL ) 227 $cond .= " AND c.status = '".Db::qstr($status)."'"; 228 if( $type != COMMENT_TYPE_ANY ) 229 $cond .= " AND c.type = '".Db::qstr($type)."'"; 230 if( $searchTerms != "" ) 231 $cond .= " AND ".$this->getSearchConditions( $searchTerms ); 232 //print("type = ".$type." - cond = $cond"); 233 $numComments = $this->getNumItems( $table, $cond ); 234 } 235 236 return( $numComments ); 237 } 238 239 /** 240 * Removes a comment from a post. It also updates all the other posts that 241 * have this one as the parent and makes them look as if they were 'top level' 242 * comments with no parent. 243 * 244 * @param artid The article identifier. 245 * @param commentid The comment identifier. 246 */ 247 function deleteComment( $commentid ) 248 { 249 $comment = $this->getComment( $commentid ); 250 if( $comment ) { 251 $this->delete( "id", $commentid ); 252 // update all the other posts 253 $query = "UPDATE ".$this->getPrefix()."articles_comments SET parent_id = 0 WHERE parent_id = '". 254 Db::qstr($commentid)."' AND article_id = '". 255 Db::qstr( $comment->getArticleId())."'"; 256 $result = $this->Execute( $query ); 257 $this->_cache->removeData( $comment->getArticleId(), CACHE_ARTICLE_COMMENTS_BYARTICLE_NEWEST_TO_OLDEST ); 258 $this->_cache->removeData( $comment->getArticleId(), CACHE_ARTICLE_COMMENTS_BYARTICLE_OLDEST_TO_NEWEST ); 259 $this->_cache->removeData( $comment->getId(), CACHE_ARTICLE_COMMENTS ); 260 261 // update the blog and the article counters 262 lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" ); 263 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 264 265 $article = $comment->getArticle(); 266 $blog = $article->getBlogInfo(); 267 $type = $comment->getType(); 268 if( $type == COMMENT_TYPE_COMMENT ) { 269 $article->setNumComments( $this->getNumItems( $this->getPrefix().'articles_comments', 270 'article_id = '.$article->getId().' AND status = '.COMMENT_STATUS_NONSPAM. 271 ' AND type = '.$comment->getType())); 272 $article->setTotalComments( $this->getNumItems( $this->getPrefix().'articles_comments', 273 'article_id = '.$article->getId().' AND type = '.$comment->getType())); 274 $blog->setTotalComments($this->getNumItems( $this->getPrefix().'articles_comments', 275 'blog_id = '.$blog->getId().' AND type = '.$comment->getType())); 276 } 277 else { 278 $article->setNumTrackbacks( $this->getNumItems( $this->getPrefix().'articles_comments', 279 'article_id = '.$article->getId().' AND status = '.COMMENT_STATUS_NONSPAM. 280 ' AND type = '.$comment->getType())); 281 $article->setTotalTrackbacks($this->getNumItems( $this->getPrefix().'articles_comments', 282 'article_id = '.$article->getId().' AND type = '.$comment->getType())); 283 $blog->setTotalTrackbacks($this->getNumItems( $this->getPrefix().'articles_comments', 284 'blog_id = '.$blog->getId().' AND type = '.$comment->getType())); 285 } 286 $blogs = new Blogs(); 287 $blogs->updateBlog( $blog ); 288 $articles = new Articles(); 289 $articles->updateArticle( $article ); 290 } 291 else { 292 return false; 293 } 294 295 return( true ); 296 } 297 298 /** 299 * Updates a comment. It also takes into account status changes and updates counters in 300 * the blogs and articles table accordingly. 301 * 302 * @param comment An UserComment object 303 * @return true if update successful or false otherwise. 304 */ 305 function updateComment( $comment ) 306 { 307 // we need to undo the time offset before updating the comment, or else the comment will be saved 308 // with the time offset applied 309 $blogInfo = $comment->getBlogInfo(); 310 $blogOffset = $blogInfo->getValue( "time_offset" ); 311 if( $blogOffset != 0 ) { 312 // if there's an offset... 313 $newDate = Timestamp::getDateWithOffset( $comment->getDate(), -$blogOffset ); 314 $comment->setDate( $newDate ); 315 } 316 317 if(($result = $this->update( $comment ))) { 318 // reset the cache 319 $this->_cache->removeData( $comment->getArticleId(), CACHE_ARTICLE_COMMENTS_BYARTICLE_NEWEST_TO_OLDEST ); 320 $this->_cache->removeData( $comment->getArticleId(), CACHE_ARTICLE_COMMENTS_BYARTICLE_OLDEST_TO_NEWEST ); 321 $this->_cache->removeData( $comment->getId(), CACHE_ARTICLE_COMMENTS ); 322 // update counters in the articles table according to the status 323 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 324 $articles = new Articles(); 325 // load the article 326 $article = $comment->getArticle(); 327 if( $comment->getType() == COMMENT_TYPE_COMMENT ) { 328 $article->setNumComments( $this->getNumItems( $this->getPrefix().'articles_comments', 329 'article_id = '.$article->getId().' AND status = '.COMMENT_STATUS_NONSPAM. 330 ' AND type = '.$comment->getType())); 331 $article->setTotalComments( $this->getNumItems( $this->getPrefix().'articles_comments', 332 'article_id = '.$article->getId().' AND type = '.$comment->getType())); 333 } 334 elseif( $comment->getType() == COMMENT_TYPE_TRACKBACK ) { 335 $article->setNumTrackbacks( $this->getNumItems( $this->getPrefix().'articles_comments', 336 'article_id = '.$article->getId().' AND status = '.COMMENT_STATUS_NONSPAM. 337 ' AND type = '.$comment->getType())); 338 $article->setTotalTrackbacks( $this->getNumItems( $this->getPrefix().'articles_comments', 339 'article_id = '.$article->getId().' AND type = '.$comment->getType())); 340 } 341 $articles->updateArticle( $article ); 342 } 343 344 // this is not the cleanest solution but it will do the trick for now... 345 if( $blogOffset != 0 ) { 346 $newDate = Timestamp::getDateWithOffset( $comment->getDate(), $blogOffset ); 347 $comment->setDate( $newDate ); 348 } 349 350 return( $result ); 351 } 352 353 /** 354 * returns a single comment, identified by its identifier 355 * 356 * @param type 357 */ 358 function getComment( $id, $type = COMMENT_TYPE_ANY ) 359 { 360 $comment = $this->get( "id" , $id, CACHE_ARTICLE_COMMENTS ); 361 if( !$comment ) 362 return false; 363 if( $type != COMMENT_TYPE_ANY ) { 364 if( $comment->getType() != $type ) { 365 return false; 366 } 367 } 368 369 return( $comment ); 370 } 371 372 /** 373 * returns the comments received in the blog 374 * 375 * @param blogId 376 * @param order 377 * @param status 378 * @param type 379 * @param searchTerms 380 * @param page 381 * @param itemsPerPage 382 * @return An array of ArticleComment objects 383 */ 384 function getBlogComments( $blogId, 385 $order = COMMENT_ORDER_NEWEST_FIRST, 386 $status = COMMENT_STATUS_ALL, 387 $type = COMMENT_TYPE_ANY, 388 $searchTerms = "", 389 $page = -1, 390 $itemsPerPage = DEFAULT_ITEMS_PER_PAGE ) 391 { 392 /** 393 * :TODO: 394 * - implement this method in a better way! 395 */ 396 $prefix = $this->getPrefix(); 397 $query = "SELECT c.* 398 FROM {$prefix}articles_comments c 399 WHERE c.blog_id = '".Db::qstr( $blogId )."'"; 400 if( $status != COMMENT_STATUS_ALL ) 401 $query .= " AND status = $status"; 402 if( $type != COMMENT_TYPE_ANY ) 403 $query .= " AND type = '".Db::qstr($type)."'"; 404 if( $searchTerms != "" ) { 405 $query .= " AND ".$this->getSearchConditions( $searchTerms ); 406 } 407 408 // check in which order we should display those comments 409 if( $order == COMMENT_ORDER_NEWEST_FIRST ) 410 $query .= " ORDER BY date DESC"; 411 else 412 $query .= " ORDER BY date ASC"; 413 414 $result = $this->Execute( $query, $page, $itemsPerPage ); 415 416 if( !$result ) 417 return false; 418 419 if( $result->RowCount() == 0 ){ 420 $result->Close(); 421 return Array(); 422 } 423 424 $comments = Array(); 425 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 426 $articles = new Articles(); 427 while( $row = $result->FetchRow()) { 428 $comments[] = $this->mapRow( $row ); 429 } 430 $result->Close(); 431 432 /*if( $page > -1 && $amount > -1 ) 433 $comments = array_slice( $comments, ($page-1) * $amount, $amount );*/ 434 435 return( $comments ); 436 } 437 438 /** 439 * @private 440 * @see Model::mapRow() 441 */ 442 function mapRow( $row ) 443 { 444 lt_include( PLOG_CLASS_PATH."class/dao/usercomment.class.php" ); 445 lt_include( PLOG_CLASS_PATH."class/data/timestamp.class.php" ); 446 447 $prefix = $this->getPrefix(); 448 $date = $row["date"]; 449 $articleId = $row["article_id"]; 450 $blogId = $row["blog_id"]; 451 452 $blogs = new Blogs(); 453 $blogInfo = $blogs->getBlogInfo( $blogId ); 454 $blogSettings = $blogInfo->getSettings(); 455 $timeDiff = $blogSettings->getValue( "time_offset" ); 456 457 $date = Timestamp::getDateWithOffset( $date, $timeDiff ); 458 459 $comment = new UserComment( $row["article_id"], 460 $row['blog_id'], 461 $row["parent_id"], 462 $row["topic"], 463 $row["text"], 464 $date, 465 $row["user_name"], 466 $row["user_email"], 467 $row["user_url"], 468 $row["client_ip"], 469 $row["spam_rate"], 470 $row["status"], 471 unserialize($row["properties"]), 472 $row["id"] ); 473 474 // set the normalized text and topic 475 $comment->setNormalizedText( $row['normalized_text'] ); 476 $comment->setNormalizedTopic( $row['normalized_topic'] ); 477 $comment->setType( $row['type'] ); 478 $comment->setUserId( $row['user_id'] ); 479 480 return $comment; 481 } 482 483 /** 484 * @see Model::getSearchConditions 485 * 486 * @param searchTerms 487 */ 488 function getSearchConditions( $searchTerms ) 489 { 490 lt_include( PLOG_CLASS_PATH."class/dao/searchengine.class.php" ); 491 492 $query = SearchEngine::adaptSearchString($searchTerms); 493 494 // MARKWU: I also need to take care when there are multiplu search term 495 496 // Split the search term by space 497 $query_array = explode(' ',$query); 498 499 $db =& Db::getDb(); 500 if( $db->isFullTextSupported()) { 501 // fast path used when FULLTEXT searches are supported 502 $where_string = "(MATCH(c.normalized_text) AGAINST ('{$query}' IN BOOLEAN MODE))"; 503 } 504 else { 505 // old and slower path for those cases when they are not 506 $where_string = "("; 507 $where_string .= "((c.normalized_topic LIKE '%{$query_array[0]}%') OR (c.normalized_text LIKE '%{$query_array[0]}%'))"; 508 for ( $i = 1; $i < count($query_array); $i = $i + 1) { 509 $where_string .= " AND ((c.normalized_topic LIKE '%{$query_array[$i]}%') OR (c.normalized_text LIKE '%{$query_array[$i]}%'))"; 510 } 511 $where_string .= " OR ((c.normalized_topic LIKE '%{$query}%') OR (c.normalized_text LIKE '%{$query}%'))"; 512 $where_string .= ")"; 513 } 514 515 return( $where_string ); 516 } 517 518 /** 519 * Delete all the blog comments 520 */ 521 function deleteBlogComments( $blogId ) 522 { 523 // we really don't bother about caches or comment, as this method is only being called 524 // when deleting a blog, which means that everything is deleted anyway 525 return( $this->delete( "blog_id", $blogId )); 526 } 527 528 /** 529 * Delete all the article comments 530 * 531 * @param blogId 532 */ 533 function deleteArticleComments( $articleId ) 534 { 535 lt_include( PLOG_CLASS_PATH."class/dao/articles.class.php" ); 536 537 $articles = new Articles(); 538 $art = $articles->getArticle( $articleId ); 539 540 // there is no article 541 if( !$art ) 542 return( false ); 543 544 if(( $res = $this->delete( "article_id", $articleId ))) { 545 // comments deleted successfully, we can update all the counters out there... 546 $blog = $art->getBlogInfo(); 547 // update the counter to reflect the number of comments 548 $blog->setTotalComments( $this->getNumItems( $this->getPrefix()."articles_comments", 549 "blog_id = '".Db::qstr($blog->getId())."'". 550 " AND type = ".COMMENT_TYPE_COMMENT )); 551 // and the one to reflect the number of trackbacks 552 $blog->setTotalTrackbacks( $this->getNumItems( $this->getPrefix()."articles_comments", 553 "blog_id = '".Db::qstr($blog->getId())."'". 554 " AND type = ".COMMENT_TYPE_TRACKBACK )); 555 556 $blogs = new Blogs(); 557 $blogs->updateBlog( $blog ); 558 } 559 560 return( $res ); 561 } 562 } 563 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
|