<?php
/*
TAG相关文章区块
参数1：文章数，默认10，可用参数：大于0整数
参数2：Tagid，为空时返回空数组，多个Tagid用“|”分割
*/
class blocktagarticle extends saxueblock {
		public $template = "block_tagarticle.html";
		public $exevars = array( 'listnum' => 10, 'tagid' => 0 );
		public function blocktagarticle( &$_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] );
								$_tagid = str_replace( "|", "", $_fieldarr[1] );
								if ( is_numeric( $_tagid ) ) {
										$this -> exevars['tagid'] = $_fieldarr[1];
								} else if ( substr( $_fieldarr[1], 0, 1 ) == "\$" ) {
										$_tagidvar = $saxueTpl -> get_assign( substr( $_fieldarr[1], 1 ) );
										if ( is_numeric( str_replace( "|", "", $_tagidvar ) ) ) {
												$this -> exevars['tagid'] = $_tagidvar;
										} 
								} else if ( isset( $_REQUEST[$_tagid] ) && is_numeric( $_REQUEST[$_tagid] ) ) {
										$this -> exevars['tagid'] = $_REQUEST[$_tagid];
								}
						} 
				} 
				$this -> blockvars['cacheid'] = md5( serialize( $this -> exevars ) . "|" . $this -> blockvars['template'] );
		} 
		public function setcontent( $isreturn = false ) {
				$tagid = str_replace( '|', ',', trim( $this -> exevars['tagid'], '|' ) );
				$rows = array();
				if ( !empty( $tagid ) ) {
						global $saxueTpl;
						global $saxueCate;
						global $article_handler;
						if ( !isset( $saxueCate['article'] ) ) {
								saxue_getconfigs( "cate", 'article' );
						}
						if ( !is_object( $article_handler ) ) {
								include_once SAXUE_ROOT_PATH . "/model/article.php";
								$article_handler = &saxuearticlehandler :: getinstance( "saxuearticlehandler" );
						}
						$sql = 'SELECT DISTINCT aid FROM ' . saxue_dbprefix( "tag_article" ) . ' WHERE tagid IN(' . $tagid . ')';
						$res = $article_handler -> db -> query( $sql );
						$aids = array();
						while ( $v = $article_handler -> getobject( $res ) ) {
								$aids[] = $v -> getvar( 'aid' );
						} 
						if ( count( $aids ) > 0 ) {
								$sql = 'SELECT * FROM ' . saxue_dbprefix( "article" ) . ' WHERE aid IN(' . implode( ',', $aids ) . ') AND status=1 ORDER BY aid DESC LIMIT 0, ' . $this -> exevars['listnum'];
								$res = $article_handler -> db -> query( $sql );
								$tables = array();
								while ( $v = $article_handler -> getobject( $res ) ) {
										$k = $v -> getvar( 'aid' );
										$rows[$k] = $v -> getvars( 'n' );
										$rows[$k]['cname'] = $saxueCate['article'][$rows[$k]['cid']]['cname'];
										$rows[$k]['curl'] = $saxueCate['article'][$rows[$k]['cid']]['curl'];
										$rows[$k]['scurl'] = $saxueCate['article'][$rows[$k]['cid']]['scurl'];
										if ( $rows[$k]['uid'] > 0 ) {
												$rows[$k]['uhome'] = saxue_geturl( "user_home", $rows[$k]['uid'] );
												$rows[$k]['uavatar'] = saxue_geturl( "user_avatar", $rows[$k]['uid'] );
										}
										$tables[$rows[$k]['tbnum']][] = $k;
								} 
								if ( count( $tables ) > 0 ) {
										foreach ( $tables as $tbnum => $ids  ) {
												$_res = $article_handler -> db -> query( "SELECT * FROM " . saxue_dbprefix( 'article_data_' . $tbnum ) . " WHERE aid IN (" . implode( ',', $ids ) . ")" );
												while ( $v = $article_handler -> getobject( $_res ) ) {
														$k = $v -> getvar( 'aid' );
														$rows[$k] = $rows[$k] + ( $v -> getvars( 'n' ) );
														$rows[$k]['intro'] = nl2br( $rows[$k]['intro'] );
														if ( $rows[$k]['isslide'] ) {
																$rows[$k]['album'] = explode( '|', $rows[$k]['album'] );
														}
														// 读取TAG
														$_tags = array();
														$_tagnum = 0;
														if ( strlen( $rows[$k]['tags'] ) > 0 ) {
																$_arr = explode( ',', $rows[$k]['tags'] );
																$_arrid = explode( ',', $rows[$k]['tagids'] );
																foreach ( $_arr as $_i => $_s ) {
																		$_tags[$_tagnum]['name'] = $_s;
																		$_tags[$_tagnum]['url'] = saxue_geturl( "tag_show", $_arrid[$_i] );
																		++$_tagnum;
																} 
														}
														$rows[$k]['taglist'] = $_tags;
												} 
										} 
								}
						}
				}
				if ( $isreturn ) {
						return $rows;
				}
				$saxueTpl -> assign_by_ref( "rows", $rows );
		} 
} 
