<?php
/*
专题列表区块
参数1：专题数，默认10，可用参数：大于0整数
参数2：分类ID，默认0，可用参数：0（全部专题），分类ID，cid（系统取$_REQUEST['cid']值），$cid（系统取已定义模版变量cid值）
参数3：是否推荐，默认0，可用参数：0（全部专题），1（推荐专题）
参数4：是否有标题图片，默认0，可用参数：0（全部专题），1（有标题图片专题）
*/
class blocktopiclist extends saxueblock {
		public $template = "block_topiclist.html";
		public $exevars = array( 'listnum' => 10, 'cid' => '0', 'isgood' => '0', 'isthumb' => '0' );
		public function blocktopiclist( &$_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 ) ) ) {
										$this -> exevars['isgood'] = intval( $_fieldarr[2] );
								} 
						} 
						if ( 3 < $_fieldcount ) {
								$_fieldarr[3] = trim( $_fieldarr[3] );
								if ( is_numeric( $_fieldarr[3] ) && in_array( $_fieldarr[3], array( 0, 1 ) ) ) {
										$this -> exevars['isthumb'] = intval( $_fieldarr[3] );
								} 
						} 
				} 
				$this -> blockvars['cacheid'] = md5( serialize( $this -> exevars ) . "|" . $this -> blockvars['template'] );
		} 
		public function setcontent( $isreturn = false ) {
				global $saxueTpl;
				global $saxueCate;
				global $topic_handler;
				if ( !isset( $saxueCate['topic'] ) ) {
						saxue_getconfigs( "cate", 'topic' );
				}
				if ( !is_object( $topic_handler ) ) {
						include_once SAXUE_ROOT_PATH . "/model/topic.php";
						$topic_handler = &saxuetopichandler :: getinstance( "saxuetopichandler" );
				}
				$where = array();
				if ( !empty( $this -> exevars['cid'] ) ) {
						$sql = 'SELECT * FROM ' . saxue_dbprefix( "topic" ) . ' WHERE cid=' . $this -> exevars['cid'] . ' AND status=1';
				}
				if ( !empty( $this -> exevars['isgood'] ) ) {
						$where[] = "isgood=1";
				}
				if ( !empty( $this -> exevars['cid'] ) ) {
						$where[] = 'cid=' . $this -> exevars['cid'];
				} 
				if ( !empty( $this -> exevars['isthumb'] ) ) {
						$where[] = "isthumb=1";
				}
				$where[] = "status=1";
				$sql = 'SELECT * FROM ' . saxue_dbprefix( "topic" ) . ' WHERE ' . implode( ' AND ', $where ) . ' ORDER BY topicid DESC LIMIT 0, ' . $this -> exevars['listnum'];
				$res = $topic_handler -> db -> query( $sql );
				$rows = $ids = array();
				while ( $v = $topic_handler -> getobject( $res ) ) {
						$k = $v -> getvar( 'topicid' );
						$ids[] = $k;
						$rows[$k] = $v -> getvars( 'n' );
						$rows[$k]['cname'] = $saxueCate['topic'][$rows[$k]['cid']]['cname'];
						$rows[$k]['curl'] = $saxueCate['topic'][$rows[$k]['cid']]['curl'];
				} 
				if ( $ids ) {
						$_res = $topic_handler -> db -> query( "SELECT * FROM " . saxue_dbprefix( 'topic_data' ) . " WHERE topicid IN (" . implode( ',', $ids ) . ")" );
						while ( $v = $topic_handler -> getobject( $_res ) ) {
								$k = $v -> getvar( 'topicid' );
								$rows[$k] = $rows[$k] + ( $v -> getvars( 'n' ) );
								$rows[$k]['intro'] = nl2br( $rows[$k]['intro'] );
						} 
				}
				if ( $isreturn ) {
						return $rows;
				}
				$saxueTpl -> assign_by_ref( "rows", $rows );
		} 
} 
