<?php   if(!defined('DEDEINC')) exit('Request Error!');
/**
 * 連載圖書調用
 *
 * @version        $Id: booklist.lib.php 1 9:29 2010年7月6日Z tianya $
 * @package        DedeCMS.Taglib
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */

/**
 *  圖書列表調用
 *
 * @access    public
 * @param     object  $ctag  解析標籤
 * @param     object  $refObj  引用對像
 * @param     int  $getcontent  是否調用內容
 * @return    string
 */
 
/*>>dede>>
<name>連載圖書</name>
<type>全局標記</type>
<for>V55,V56,V57</for>
<description>連載圖書調用</description>
<demo>
{dede:booklist row='12' booktype='-1' orderby='lastpost' author='' keyword=''}
<a href='[field:bookurl /]'>[field:bookname /]</a><br />
{/dede:booklist}
</demo>
<attributes>
    <iterm>row:調用記錄條數</iterm> 
    <iterm>booktype:圖書類型，0 圖書、1 漫畫，默認全部</iterm>
    <iterm>orderby:排序類型，當按排序類型為 commend 表示推薦圖書</iterm>
    <iterm>author:作者</iterm>
    <iterm>keyword:關鍵字</iterm>
</attributes> 
>>dede>>*/

function lib_booklist(&$ctag, &$refObj, $getcontent=0)
{
    global $dsql, $envs, $cfg_dbprefix, $cfg_cmsurl;
    
    //屬性處理
    $attlist="row|12,booktype|-1,titlelen|30,orderby|lastpost,author|,keyword|";
    FillAttsDefault($ctag->CAttribute->Items,$attlist);
    extract($ctag->CAttribute->Items, EXTR_SKIP);

    if( !$dsql->IsTable("{$cfg_dbprefix}story_books") ) return '沒安裝連載模塊';
    $addquery = '';
    
    if(empty($innertext))
    {
        if($getcontent==0) $innertext = GetSysTemplets('booklist.htm');
        else $innertext = GetSysTemplets('bookcontentlist.htm');
    }
    
    //圖書類型
    if($booktype!=-1) {
        $addquery .= " AND b.booktype='{$booktype}' ";
    }
    
    //推薦
    if($orderby=='commend')
    {
        $addquery .= " AND b.iscommend=1 ";
        $orderby = 'lastpost';
    }

    //作者條件
    if(!empty($author))
    {
        $addquery .= " AND b.author LIKE '$author' ";
    }
    
    //關鍵字條件
    if(!empty($keyword))
    {
        $keywords = explode(',', $keyword);
        $keywords = array_unique($keywords);
        if(count($keywords) > 0) {
            $addquery .= " AND (";
        }
        foreach($keywords as $v) {
            $addquery .= " CONCAT(b.author,b.bookname,b.keywords) LIKE '%$v%' OR";
        }
        $addquery = preg_replace("# OR$#", "", $addquery);
        $addquery .= ")";
    }
    
    $clist = '';
    $query = "SELECT b.id,b.catid,b.ischeck,b.booktype,b.iscommend,b.click,b.bookname,b.lastpost,
         b.author,b.mid,b.litpic,b.pubdate,b.weekcc,b.monthcc,b.description,c.classname,c.classname as typename,c.booktype as catalogtype
         FROM `#@__story_books` b LEFT JOIN `#@__story_catalog` c ON c.id = b.catid
         WHERE b.postnum>0 AND b.ischeck>0 $addquery ORDER BY b.{$orderby} DESC LIMIT 0, $row";
    $dsql->SetQuery($query);
    $dsql->Execute();

    $ndtp = new DedeTagParse();
    $ndtp->SetNameSpace('field', '[', ']');
    $GLOBALS['autoindex'] = 0;
    while($row = $dsql->GetArray())
    {
        $GLOBALS['autoindex']++;
        $row['title'] = $row['bookname'];
        $ndtp->LoadString($innertext);

        //獲得圖書最新的一個更新章節
        $row['contenttitle'] = '';
        $row['contentid'] = '';
        if($getcontent==1) {
            $nrow = $dsql->GetOne("SELECT id,title,chapterid FROM `#@__story_content` WHERE bookid='{$row['id']}' order by id desc ");
            $row['contenttitle'] = $nrow['title'];
            $row['contentid'] = $nrow['id'];
        }
        if($row['booktype']==1) {
            $row['contenturl'] = $cfg_cmspath.'/book/show-photo.php?id='.$row['contentid'];
        }
        else {
            $row['contenturl'] = $cfg_cmspath.'/book/story.php?id='.$row['contentid'];
        }

        //動態網址
        $row['dmbookurl'] = $cfg_cmspath.'/book/book.php?id='.$row['id'];

        //靜態網址
        $row['bookurl'] = $row['url'] = GetBookUrl($row['id'],$row['bookname']);
        $row['catalogurl'] = $cfg_cmspath.'/book/list.php?id='.$row['catid'];
        $row['cataloglink'] = "<a href='{$row['catalogurl']}'>{$row['classname']}</a>";
        $row['booklink'] = "<a href='{$row['bookurl']}'>{$row['bookname']}</a>";
        $row['contentlink'] = "<a href='{$row['contenturl']}'>{$row['contenttitle']}</a>";
        $row['imglink'] = "<a href='{$row['bookurl']}'><img src='{$row['litpic']}' width='$imgwidth' height='$imgheight' border='0' /></a>";
        
        if($row['ischeck']==2) $row['ischeck']='已完成連載';
        else $row['ischeck']='連載中...';

        if($row['booktype']==0) $row['booktypename']='小說';
        else $row['booktypename']='漫畫';

        if(is_array($ndtp->CTags))
        {
            foreach($ndtp->CTags as $tagid=>$ctag)
            {
                $tagname = $ctag->GetTagName();
                if(isset($row[$tagname])) $ndtp->Assign($tagid,$row[$tagname]);
                else $ndtp->Assign($tagid,'');
            }
        }
        $clist .= $ndtp->GetResult();
    }
    
    return $clist;
}