<?php   if(!defined('DEDEINC')) exit('Request Error!');
/**
 * 圈子調用標籤
 *
 * @version        $Id: group.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
 */
 
/*>>dede>>
<name>圈子標籤</name>
<type>全局標記</type>
<for>V55,V56,V57</for>
<description>圈子調用標籤</description>
<demo>
{dede:group row='6' orderby='threads' titlelen='30'}
 <li>
  <span><img style="visibility: inherit;" title="[field:groupname/]" src="[field:icon/]" /></span>
  <span><a href="[field:url/]" title="[field:groupname/]" target="_blank">[field:groupname/]</a></span>
 </li>
{/dede:group} 
</demo>
<attributes>
    <iterm>row:調用條數</iterm> 
    <iterm>orderby:排列順序（默認是主題數）</iterm>
    <iterm>titlelen:圈子名稱最大長度</iterm>
</attributes> 
>>dede>>*/
 
function lib_group(&$ctag,&$refObj)
{
    global $dsql, $envs, $cfg_dbprefix, $cfg_cmsurl;
    //屬性處理
    $attlist="row|6,orderby|threads,titlelen|30";
    FillAttsDefault($ctag->CAttribute->Items,$attlist);
    extract($ctag->CAttribute->Items, EXTR_SKIP);

    if( !$dsql->IsTable("{$cfg_dbprefix}groups") ) return '沒安裝圈子模塊';
    
    if(!preg("#\/$#", $cfg_cmsurl)) $cfg_group_url = $cfg_cmsurl.'/group';
    else $cfg_group_url = $cfg_cmsurl.'group';
    
    $innertext = $ctag->GetInnerText();
    if(trim($innertext)=='') $innertext = GetSysTemplets("groups.htm");
    
    $list = '';
    $dsql->SetQuery("SELECT groupimg,groupid,groupname FROM `#@__groups` WHERE ishidden=0 ORDER BY $orderby DESC LIMIT 0,{$row}");
    $dsql->Execute();
    $ctp = new DedeTagParse();
    $ctp->SetNameSpace('field', '[', ']');
    while($rs = $dsql->GetArray())
    {
        $ctp->LoadSource($innertext);
        $rs['groupname'] = cn_substr($rs['groupname'], $titlelen);
        $rs['url'] = $cfg_group_url."/group.php?id={$rs['groupid']}";
        $rs['icon']  = $rs['groupimg'];
        foreach($ctp->CTags as $tagid=>$ctag)
        {
            if( !empty($rs[strtolower($ctag->GetName())]) ) {
                $ctp->Assign($tagid,$rs[$ctag->GetName()]);
            }
          }
          $list .= $ctp->GetResult();
    }
    return $list;
}