typelink.class.php 15.4 KB
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 443 444 445 446 447 448 449 450
<?php   if(!defined('DEDEINC')) exit("Request Error!");
/**
 * 栏目连接
 *
 * @version        $Id: typelink.class.php 1 15:21 2010年7月5日Z tianya $
 * @package        DedeCMS.Libraries
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(DEDEINC."/channelunit.func.php");

/**
 * 栏目连接类
 *
 * @package          TypeLink
 * @subpackage       DedeCMS.Libraries
 * @link             http://www.dedecms.com
 */
class TypeLink
{
    var $typeDir;
    var $dsql;
    var $TypeID;
    var $baseDir;
    var $modDir;
    var $indexUrl;
    var $indexName;
    var $TypeInfos;
    var $SplitSymbol;
    var $valuePosition;
    var $valuePositionName;
    var $OptionArrayList;

    //构造函数///////
    //php5构造函数
    function __construct($typeid)
    {
        $this->indexUrl = $GLOBALS['cfg_basehost'].$GLOBALS['cfg_indexurl'];
        $this->indexName = $GLOBALS['cfg_indexname'];
        $this->baseDir = $GLOBALS['cfg_basedir'];
        $this->modDir = $GLOBALS['cfg_templets_dir'];
        $this->SplitSymbol = $GLOBALS['cfg_list_symbol'];
        $this->dsql = $GLOBALS['dsql'];
        $this->TypeID = $typeid;
        $this->valuePosition = '';
        $this->valuePositionName = '';
        $this->typeDir = '';
        $this->OptionArrayList = '';

        //载入类目信息
        $query = "SELECT tp.*,ch.typename as ctypename,ch.addtable,ch.issystem FROM `#@__arctype` tp left join `#@__channeltype` ch
        on ch.id=tp.channeltype  WHERE tp.id='$typeid' ";
        if($typeid > 0)
        {
            $this->TypeInfos = $this->dsql->GetOne($query);
            if(is_array($this->TypeInfos))
            {
                $this->TypeInfos['tempindex'] = MfTemplet($this->TypeInfos['tempindex']);
                $this->TypeInfos['templist'] = MfTemplet($this->TypeInfos['templist']);
                $this->TypeInfos['temparticle'] = MfTemplet($this->TypeInfos['temparticle']);
            }
        }
    }

    //对于使用默认构造函数的情况
    //GetPositionLink()将不可用
    function TypeLink($typeid)
    {
        $this->__construct($typeid);
    }

    //关闭数据库连接,析放资源
    function Close()
    {
    }

    //重设类目ID
    function SetTypeID($typeid)
    {
        $this->TypeID = $typeid;
        $this->valuePosition = "";
        $this->valuePositionName = "";
        $this->typeDir = "";
        $this->OptionArrayList = "";

        //载入类目信息
        $query = "
        SELECT #@__arctype.*,#@__channeltype.typename as ctypename
        FROM #@__arctype left join #@__channeltype
        on #@__channeltype.id=#@__arctype.channeltype WHERE #@__arctype.id='$typeid' ";
        $this->dsql->SetQuery($query);
        $this->TypeInfos = $this->dsql->GetOne();
    }

    //获得这个类目的路径
    function GetTypeDir()
    {
        if(empty($this->TypeInfos['typedir']))
        {
            return $GLOBALS['cfg_cmspath'].$GLOBALS['cfg_arcdir'];
        }
        else
        {
            return $this->TypeInfos['typedir'];
        }
    }

    //获得某类目的链接列表 如:类目一>>类目二>> 这样的形式
    //islink 表示返回的列表是否带连接
    function GetPositionLink($islink=true)
    {
        $indexpage = "<a href='".$this->indexUrl."'>".$this->indexName."</a>";
        if($this->valuePosition!="" && $islink)
        {
            return $this->valuePosition;
        }
        else if($this->valuePositionName!="" && !$islink)
        {
            return $this->valuePositionName;
        }
        else if($this->TypeID==0)
        {
            if($islink)
            {
                return $indexpage;
            }
            else
            {
                return "没指定分类!";
            }
        }
        else
        {
            if($islink)
            {
                $this->valuePosition = $this->GetOneTypeLink($this->TypeInfos);
                if($this->TypeInfos['reid']!=0)
                {
                    //调用递归逻辑
                    $this->LogicGetPosition($this->TypeInfos['reid'],true);
                }
                $this->valuePosition = $indexpage.$this->SplitSymbol.$this->valuePosition;
                return $this->valuePosition.$this->SplitSymbol;
            }
            else
            {
                $this->valuePositionName = $this->TypeInfos['typename'];
                if($this->TypeInfos['reid']!=0)
                {
                    //调用递归逻辑
                    $this->LogicGetPosition($this->TypeInfos['reid'],false);
                }
                return $this->valuePositionName;
            }
        }
    }

    //获得名字列表
    function GetPositionName()
    {
        return $this->GetPositionLink(false);
    }

    //获得某类目的链接列表,递归逻辑部分
    function LogicGetPosition($id,$islink)
    {
        $this->dsql->SetQuery("SELECT id,reid,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl,sitepath FROM #@__arctype WHERE id='".$id."'");
        $tinfos = $this->dsql->GetOne();
        if($islink)
        {
            $this->valuePosition = $this->GetOneTypeLink($tinfos).$this->SplitSymbol.$this->valuePosition;
        }
        else
        {
            $this->valuePositionName = $tinfos['typename'].$this->SplitSymbol.$this->valuePositionName;
        }
        if($tinfos['reid']>0)
        {
            $this->LogicGetPosition($tinfos['reid'],$islink);
        }
        else
        {
            return 0;
        }

    }

    //获得某个类目的超链接信息
    function GetOneTypeLink($typeinfos)
    {
        $typepage = $this->GetOneTypeUrl($typeinfos);
        $typelink = "<a href='".$typepage."'>".$typeinfos['typename']."</a>";
        return $typelink;
    }

    //获得某分类连接的URL
    function GetOneTypeUrl($typeinfos)
    {
        return GetTypeUrl($typeinfos['id'],MfTypedir($typeinfos['typedir']),$typeinfos['isdefault'],$typeinfos['defaultname'],
        $typeinfos['ispart'],$typeinfos['namerule2'],$typeinfos['moresite'],$typeinfos['siteurl'],$typeinfos['sitepath']);
    }

    //获得类别列表
    //hid 是指默认选中类目,0 表示“请选择类目”或“不限类目”
    //oper 是用户允许管理的类目,0 表示所有类目
    //channeltype 是指类目的内容类型,0 表示不限频道
    function GetOptionArray($hid=0,$oper=0,$channeltype=0,$usersg=0)
    {
        return $this->GetOptionList($hid,$oper,$channeltype,$usersg);
    }

    function GetOptionList($hid=0,$oper=0,$channeltype=0,$usersg=0)
    {
        global $cfg_admin_channel;
        if(empty($cfg_admin_channel)) $cfg_admin_channel = 'all';
        
        if(!$this->dsql) $this->dsql = $GLOBALS['dsql'];
        $this->OptionArrayList = '';
        
        if($hid>0)
        {
            $row = $this->dsql->GetOne("SELECT id,typename,ispart,channeltype FROM #@__arctype WHERE id='$hid'");
            $channeltype = $row['channeltype'];
            if($row['ispart']==1) {
                $this->OptionArrayList .= "<option value='".$row['id']."' style='background-color:#DFDFDB;color:#888888' selected>".$row['typename']."</option>\r\n";
            }
            else {
                $this->OptionArrayList .= "<option value='".$row['id']."' selected>".$row['typename']."</option>\r\n";
            }
        }
        
        if($channeltype==0) $ctsql = '';
        else $ctsql=" AND channeltype='$channeltype' ";
        
        
        if(is_array($oper) && $cfg_admin_channel != 'all')
        {
            if( count($oper) == 0 )
            {
                $query = "SELECT id,typename,ispart FROM `#@__arctype` WHERE 1=2 ";
            }
            else
            {
                $admin_catalog_tmp = $admin_catalog = join(',', $oper);
                $this->dsql->SetQuery("SELECT reid FROM `#@__arctype` WHERE id in($admin_catalog) GROUP BY reid ");
                $this->dsql->Execute();
                $topidstr = '';
                while($row = $this->dsql->GetObject())
                {
                    if($row->reid==0) continue;
                    $topidstr .= ($topidstr=='' ? $row->reid : ','.$row->reid);
                }
                $admin_catalog .= ','.$topidstr;
                $admin_catalogs = explode(',', $admin_catalog);
                $admin_catalogs = array_unique($admin_catalogs);
                $admin_catalog = join(',', $admin_catalogs);
                $admin_catalog = preg_replace("/,$/", '', $admin_catalog);
                $query = "SELECT id,typename,ispart FROM `#@__arctype` WHERE ispart<>2 AND id in({$admin_catalog}) AND reid=0 $ctsql";
            }
        }
        else
        {
            $query = "SELECT id,typename,ispart FROM `#@__arctype` WHERE ispart<>2 AND reid=0 $ctsql ORDER BY sortrank ASC";
        }

        $this->dsql->SetQuery($query);
        $this->dsql->Execute();
        while($row=$this->dsql->GetObject())
        {
            if($row->id!=$hid)
            {
                if($row->ispart==1) {
                    $this->OptionArrayList .= "<option value='".$row->id."' style='background-color:#EFEFEF;color:#666666'>".$row->typename."</option>\r\n";
                }
                else {
                    $this->OptionArrayList .= "<option value='".$row->id."'>".$row->typename."</option>\r\n";
                }
            }
            $this->LogicGetOptionArray($row->id, "─", $oper);
        }
        return $this->OptionArrayList;
    }

    /**
     *  逻辑递归
     *
     * @access    public
     * @param     int   $id   栏目ID
     * @param     int   $step   步进标志
     * @param     int   $oper   操作权限
     * @return    string
     */
    function LogicGetOptionArray($id, $step, $oper=0)
    {
        global $cfg_admin_channel;
        if(empty($cfg_admin_channel)) $cfg_admin_channel = 'all';
        
        $this->dsql->SetQuery("SELECT id,typename,ispart FROM #@__arctype WHERE reid='".$id."' AND ispart<>2 ORDER BY sortrank ASC");
        $this->dsql->Execute($id);
        while($row=$this->dsql->GetObject($id))
        {
            if(is_array($oper) && $cfg_admin_channel != 'all')
            {
                if(!in_array($row->id, $oper)) continue;
            }
            if($row->ispart==1) {
                $this->OptionArrayList .= "<option value='".$row->id."' style='background-color:#EFEFEF;color:#666666'>$step".$row->typename."</option>\r\n";
            }
            else {
                $this->OptionArrayList .= "<option value='".$row->id."'>$step".$row->typename."</option>\r\n";
            }
            $this->LogicGetOptionArray($row->id, $step."─", $oper);
        }
    }

    /**
     *  获得与该类相关的类目,本函数应用于模板标记{dede:channel}{/dede:channel}中
     *  $typetype 的值为: sun 下级分类 self 同级分类 top 顶级分类
     *
     * @access    public
     * @param     int   $typeid   栏目ID
     * @param     int   $reid   所属ID
     * @param     int   $row   栏目行数
     * @param     string   $typetype   栏目类型
     * @param     string   $innertext   底层模板
     * @param     int   $col   显示列数
     * @param     int   $tablewidth   表格宽度
     * @param     int   $myinnertext   自定义底层模板
     * @return    string
     */
    function GetChannelList($typeid=0, $reid=0, $row=8, $typetype='sun', $innertext='',
    $col=1, $tablewidth=100, $myinnertext='')
    {
        if($typeid==0) $typeid = $this->TypeID;
        if($row=="") $row = 8;
        if($reid=="") $reid = 0;
        if($col=="") $col = 1;

        $tablewidth = str_replace("%","",$tablewidth);
        if($tablewidth=="") $tablewidth=100;
        if($col=="") $col = 1;

        $colWidth = ceil(100/$col);
        $tablewidth = $tablewidth."%";
        $colWidth = $colWidth."%";
        if($typetype=="") $typetype="sun";

        if($innertext=="") $innertext = GetSysTemplets("channel_list.htm");

        if($reid==0 && $typeid>0)
        {
            $dbrow = $this->dsql->GetOne("SELECT reid FROM #@__arctype WHERE id='$typeid' ");
            if(is_array($dbrow))
            {
                $reid = $dbrow['reid'];
            }
        }
        $likeType = "";
        if($typetype=="top")
        {
            $sql = "SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl
          FROM #@__arctype WHERE reid=0 AND ishidden<>1 ORDER BY sortrank ASC limit 0,$row";
        }
        else if($typetype=="sun"||$typetype=="son")
        {
            $sql = "SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl
          FROM #@__arctype WHERE reid='$typeid' AND ishidden<>1 ORDER BY sortrank ASC limit 0,$row";
        }
        else if($typetype=="self")
        {
            $sql = "SELECT id,typename,typedir,isdefault,ispart,defaultname,namerule2,moresite,siteurl
            FROM #@__arctype WHERE reid='$reid' AND ishidden<>1 ORDER BY sortrank ASC limit 0,$row";
        }

        //AND ID<>'$typeid'
        $dtp2 = new DedeTagParse();
        $dtp2->SetNameSpace("field","[","]");
        $dtp2->LoadSource($innertext);
        $this->dsql->SetQuery($sql);
        $this->dsql->Execute();
        $line = $row;
        $GLOBALS['autoindex'] = 0;
        if($col>1)
        {
            $likeType = "<table width='$tablewidth' border='0' cellspacing='0' cellpadding='0'>\r\n";
        }
        for($i=0;$i<$line;$i++)
        {
            if($col>1)
            {
                $likeType .= "<tr>\r\n";
            }
            for($j=0;$j<$col;$j++)
            {
                if($col>1) $likeType .= "    <td width='$colWidth'>\r\n";
                if($row=$this->dsql->GetArray())
                {
                    //处理当前栏目的样式
                    if($row['id']=="$typeid" && $myinnertext != '')
                    {
                        $linkOkstr = $myinnertext;
                        $row['typelink'] = $this->GetOneTypeUrl($row);
                        $linkOkstr = str_replace("~typelink~", $row['typelink'], $linkOkstr);
                        $linkOkstr = str_replace("~typename~", $row['typename'], $linkOkstr);
                        $likeType .= $linkOkstr;
                    }
                    else
                    {
                        //非当前栏目
                        $row['typelink'] = $this->GetOneTypeUrl($row);
                        if(is_array($dtp2->CTags))
                        {
                            foreach($dtp2->CTags as $tagid=>$ctag)
                            {
                                if(isset($row[$ctag->GetName()]))
                                {
                                    $dtp2->Assign($tagid, $row[$ctag->GetName()]);
                                }
                            }
                        }
                        $likeType .= $dtp2->GetResult();
                    }
                }
                if($col>1)
                {
                    $likeType .= "    </td>\r\n";
                }
                $GLOBALS['autoindex']++;
            }//Loop Col

            if($col>1)
            {
                $i += $col - 1;
            }
            if($col>1)
            {
                $likeType .= "    </tr>\r\n";
            }
        }//Loop for $i

        if($col>1)
        {
            $likeType .= "    </table>\r\n";
        }
        $this->dsql->FreeResult();
        return $likeType;
    }//GetChannel

}//End Class