<?php
/*
TAG列表区块
参数1：TAG数，默认10，可用参数：大于0整数
参数2：分类ID，默认0，可用参数：0（全部TAG），分类ID，cid（系统取$_REQUEST['cid']值），$cid（系统取已定义模版变量cid值）
参数3：筛选方式，默认0，可用参数：0（全部TAG），1（推荐TAG），2（随机TAG）
参数4：随机颜色数，默认0，可用参数：大于1整数（生成随机颜色变量color，需配个CSS展示）
参数5：随机字体大小数，默认0，可用参数：大于1整数（生成随机字体大小变量size，需配个CSS展示）
*/
class blocktaglist extends saxueblock {
		public $template = "block_taglist.html";
		public $exevars = array( 'listnum' => 10, 'cid' => '0', 'filter' => '0', 'colors' => '0', 'sizes' => '0' );
		public function blocktaglist( &$_vars ) {
				global $saxueTpl;
				$this -> saxueblock( $_vars );
				if ( !empty( $this -> blockvars['vars'] ) ) {
						$_fieldarr = explode( ",", trim( $this -> blockvars['vars'] ) );
						$_fieldcount = count( $_fieldarr );
						if ( 0 < $_fieldcount ) {
								$_fieldarr[0] = trim( $_fieldarr[0] );
								if ( is_numeric( $_fieldarr[0] ) && 0 < $_fieldarr[0] ) {
										$this -> exevars['listnum'] = intval( $_fieldarr[0] );
								} 
						}
						if ( 1 < $_fieldcount ) {
								$_fieldarr[1] = trim( $_fieldarr[1] );
								if ( is_numeric( $_fieldarr[1] ) ) {
										$this -> exevars['cid'] = $_fieldarr[1];
								} else if ( substr( $_fieldarr[1], 0, 1 ) == "\$" ) {
										$_cidvar = $saxueTpl -> get_assign( substr( $_fieldarr[1], 1 ) );
										if ( is_numeric( $_cidvar ) ) {
												$this -> exevars['cid'] = $_cidvar;
										} 
								} else if ( isset( $_REQUEST[$_fieldarr[1]] ) && is_numeric( $_REQUEST[$_fieldarr[1]] ) ) {
										$this -> exevars['cid'] = $_REQUEST[$_fieldarr[1]];
								}
						} 
						if ( 2 < $_fieldcount ) {
								$_fieldarr[2] = trim( $_fieldarr[2] );
								if ( is_numeric( $_fieldarr[2] ) && in_array( $_fieldarr[2], array( 0, 1, 2 ) ) ) {
										$this -> exevars['filter'] = intval( $_fieldarr[2] );
								} 
						} 
						if ( 3 < $_fieldcount ) {
								$_fieldarr[3] = trim( $_fieldarr[3] );
								if ( is_numeric( $_fieldarr[3] ) && 1 < $_fieldarr[3] ) {
										$this -> exevars['colors'] = intval( $_fieldarr[3] );
								} 
						}
						if ( 4 < $_fieldcount ) {
								$_fieldarr[4] = trim( $_fieldarr[4] );
								if ( is_numeric( $_fieldarr[4] ) && 1 < $_fieldarr[4] ) {
										$this -> exevars['sizes'] = intval( $_fieldarr[4] );
								} 
						}
				} 
				$this -> blockvars['cacheid'] = md5( serialize( $this -> exevars ) . "|" . $this -> blockvars['template'] );
		} 
		public function setcontent( $isreturn = false ) {
				global $saxueTpl;
				global $tag_handler;
				global $saxueCate;
				if ( !isset( $saxueCate['article'] ) ) {
						saxue_getconfigs( "cate", 'article' );
				}
				if ( !is_object( $tag_handler ) ) {
						include_once SAXUE_ROOT_PATH . "/model/tag.php";
						$tag_handler = &saxuetaghandler :: getinstance( "saxuetaghandler" );
				}
				$where = array();
				if ( $this -> exevars['filter'] == 1 ) {
						$where[] = "isgood=1";
				}
				if ( !empty( $this -> exevars['cid'] ) ) {
						$cid = $this -> exevars['cid'];
						if ( $saxueCate['article'][$cid]['child'] ) {
								$where[] = 'cid IN(' . $saxueCate['article'][$cid]['arrchild'] . ')';
						} else {
								$where[] = 'cid=' . $cid;
						} 
				} 
				if ( $this -> exevars['filter'] == 2 ) {
						$sql = 'SELECT * FROM ' . saxue_dbprefix( "tag" ) . ' WHERE ';
						if ( count( $where ) > 0 ) {
								$sql .= implode( ' AND ', $where ) . ' AND ';
						}
						$sql .= 'tagid >= ((SELECT MAX(tagid) FROM ' . saxue_dbprefix( "tag" ) . ')-(SELECT MIN(tagid) FROM ' . saxue_dbprefix( "tag" ) . ')) * RAND() + (SELECT MIN(tagid) FROM ' . saxue_dbprefix( "tag" ) . ') LIMIT 0, ' . $this -> exevars['listnum'];
				} else {
						$sql = 'SELECT * FROM ' . saxue_dbprefix( "tag" );
						if ( count( $where ) > 0 ) {
								$sql .= ' WHERE ' . implode( ' AND ', $where );
						}
						$sql .= ' ORDER BY tagid DESC LIMIT 0, ' . $this -> exevars['listnum'];
				}
				$res = $tag_handler -> db -> query( $sql );
				$rows = array();
				$k = 0;
				while ( $v = $tag_handler -> getobject( $res ) ) {
						$rows[$k] = $v -> getvars( 'n' );
						$rows[$k]['url'] = saxue_geturl( "tag_show", $rows[$k]['tagid'] );
						if ( $this -> exevars['colors'] ) $rows[$k]['color'] = mt_rand( 1, $this -> exevars['colors'] );
						if ( $this -> exevars['sizes'] ) $rows[$k]['size'] = mt_rand( 1, $this -> exevars['sizes'] );
						++$k;
				} 
				if ( $isreturn ) {
						return $rows;
				}
				$saxueTpl -> assign_by_ref( "rows", $rows );
		} 
} 
