成功了!
要達到這個功能,依照我的設計需要修改三個檔案,分別是:
class/summary/dao/summarystats.class.php
class/summary/action/summarydefaultaction.class.php
templates/summary/index.template
首先要修改summarystats.class.php 建立一個新的成員函式getRecentComment()
function getRecentComments( $maxComments = 10){
$prefix = $this->getPrefix();
$query = "SELECT * FROM {$prefix}articles_comments WHERE status = 0 ORDER BY date DESC LIMIT 0,".$maxComments;
$result = $this->Execute($query);
if(!$result)
return false;
$recentComments = Array();
while( $row = $result->FetchRow()){
$comment = new UserComment($row['article_id'],$row['parent_id'],$row['topic'],$row['text'],new Timestamp($row['date']),$row['user_name'],$row['user_email'],$row['user_url'],$row['client_ip'],$row['spam_rate'],0 ,$row['id']);
$query2 =
"SELECT a.id as id, a.id,a.date,
a.user_id,a.blog_id, a.status, a.properties,
a.num_reads, a.slug
FROM {$prefix}articles a, {$prefix}blogs b
WHERE a.blog_id = b.id
AND b.status = ".BLOG_STATUS_ACTIVE."
AND a.status = ".POST_STATUS_PUBLISHED."
AND a.id=".$row['article_id'];
$result2=$this->Execute($query2);
if($row2 = $result2->FetchRow()){
$comment->setArticle($this->articles->_fillArticleInformation($row2));
}
$result2->Close;
array_push($recentComments,$comment);
}
$result->Close();
return $recentComments;
}
再來是summarydefaultaction.class.php,需要加入兩行(紅色的部份)
$activeBlogs = $stats->getMostActiveBlogs( $this->_numPosts);
$commentedPosts = $stats->getMostCommentedArticles( $this->_numPosts, $registerTopic, $registerText );
$readestBlogs = $stats->getMostReadArticles( $this->_numPosts, $registerTopic, $registerText );
$recentComments = $stats->getRecentComments();
$this->_view->setValue( "posts", $recentPosts );
$this->_view->setValue( "recentBlogs", $recentBlogs );
$this->_view->setValue( "activeBlogs", $activeBlogs );
$this->_view->setValue( "commentedPosts", $commentedPosts );
$this->_view->setValue( "readestBlogs", $readestBlogs );
$this->_view->setValue( "blogs", $siteBlogs );
$this->_view->setValue("recentComments",$recentComments);
然後你就可以在前端的網頁選擇加入的地方,已我們實驗室為例,我們是加在左邊的功能列,也就是index.template這個頁面。
<ul class="itemList">
{foreach from=$recentComments item = comment}
{assign var="commentArticle" value=$comment->getArticle()}
{assign var="blog" value=$commentArticle->getBlogInfo()}
{assign var="url" value=$blog->getBlogRequestGenerator()}
<li class="item">
{if $comment->getUserUrl()}<a href="{$comment->getUserUrl()}">{/if}{$comment->getUserName()}{if $comment->getUserUrl()}</a>{/if} on<a href="{$url->postPermalink($commentArticle)}#comments">{$commentArticle->getTopic()|strip_tags}</a>
<!--{$comment->getText()|strip_tags|truncate:50:"..."}-->
</li>
{/foreach}
</ul>
----------------
備註:我知道取出getRecentComment()裡應該可以換成運行一次sql就把資料撈出來,可是剛剛試了一下,底下的_fillArticleInformation似乎無法正常運作,我推測大概是跟select出來的name有關。已經可以動了,所以我就不改了:P
之前資料拿不出來的原因很奇怪,我原本的程式是:
while( $row = $result->FetchRow() &&
($i<$maxComments)){
}
可是這樣$row['topic'].之類的資料會拿不出來,改成
while( $row = $result->FetchRow()){
這樣就好了,..我知道原本這樣寫有點無謂 XD
只是為什麼這會影響到資料的取出呢?真奇怪?