<?php
/*
会员关注列表区块
参数1：关注条数，默认10，可用参数：大于0整数
参数2：类型，默认follow，可用参数：follow（关注会员列表），fans（粉丝列表），active（最新关注列表）
参数3：会员ID，默认0（参数2为active时可留空，否则必须填写），可用参数：0（最新关注），会员ID，uid（系统取$_REQUEST['uid']值），$uid（系统取已定义模版变量uid值）
*/
class blockfollowlist extends saxueblock {
		public $template = "block_followlist.html";
		public $exevars = array( 'listnum' => 10, 'type' => 'follow', 'uid' => 0 );
		public function blockfollowlist( &$_vars ) {
				$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 ( in_array( $_fieldarr[1], array( 'follow', 'fans', 'active' ) ) ) {
										$this -> exevars['type'] = $_fieldarr[1];
								} 
						} 
						if ( 2 < $_fieldcount ) {
								$_fieldarr[2] = trim( $_fieldarr[2] );
								if ( is_numeric( $_fieldarr[2] ) && 0 < $_fieldarr[2] ) {
										$this -> exevars['uid'] = intval( $_fieldarr[2] );
								} else if ( substr( $_fieldarr[2], 0, 1 ) == "\$" ) {
										$_uidvar = $saxueTpl -> get_assign( substr( $_fieldarr[2], 1 ) );
										if ( is_numeric( $_uidvar ) ) {
												$this -> exevars['uid'] = $_uidvar;
										} 
								} else if ( isset( $_REQUEST[$_fieldarr[2]] ) && is_numeric( $_REQUEST[$_fieldarr[2]] ) ) {
										$this -> exevars['uid'] = $_REQUEST[$_fieldarr[2]];
								} 
						}
				} 
				$this -> blockvars['cacheid'] = md5( serialize( $this -> exevars ) . "|" . $this -> blockvars['template'] );
		} 
		public function setcontent( $isreturn = false ) {
				global $saxueTpl;
				global $follow_handler;
				if ( !is_object( $follow_handler ) ) {
						include_once SAXUE_ROOT_PATH . "/model/member_follow.php";
						$follow_handler = &saxuememberfollowhandler :: getinstance( "saxuememberfollowhandler" );
				}
				include_once SAXUE_ROOT_PATH . "/common/function.php";
				switch ( $this -> exevars['type'] ) {
						case 'active':
								$sql = 'SELECT uid, toid, followtime FROM ' . saxue_dbprefix( "member_follow" );
								break;
						case 'fans':
								$sql = 'SELECT uid, toid, followtime FROM ' . saxue_dbprefix( "member_follow" ) . ' WHERE toid=' . $this -> exevars['uid'];
								break;
						case 'follow':
								$sql = 'SELECT uid, toid, followtime FROM ' . saxue_dbprefix( "member_follow" ) . ' WHERE uid=' . $this -> exevars['uid'];
								break;
				}
				$sql .= ' ORDER BY fid DESC LIMIT 0, ' . $this -> exevars['listnum'];
				$res = $follow_handler -> db -> query( $sql );
				$rows = $ids = array();
				$k = 0;
				while ( $v = $follow_handler -> getobject( $res ) ) {
						$rows[$k] = $v -> getvars( 'n' );
						$rows[$k]['followdate'] = saxue_getdate( $rows[$k]['followtime'] );
						$ids[] = $rows[$k]['uid'];
						$ids[] = $rows[$k]['toid'];
						++$k;
				} 
				if ( $k > 0 ) {
						$users = array();
						$ids = array_unique( $ids );
						$sql = 'SELECT * FROM ' . saxue_dbprefix( "member_users" ) . ' WHERE uid IN(' . implode( ',', $ids ) . ')';
						$res = $follow_handler -> db -> query( $sql );
						while ( $v = $follow_handler -> getobject( $res ) ) {
								$k = $v -> getvar( 'uid' );
								$users[$k] = $v -> getvars( 'n' );
								$users[$k]['uhome'] = saxue_geturl( "user_home", $users[$k]['uid'] );
								$users[$k]['uavatar'] = saxue_geturl( "user_avatar", $users[$k]['uid'], 's', $users[$k]['avatar'] );
						} 
						$sql = 'SELECT uid,intro,gender FROM ' . saxue_dbprefix( "member_userdata" ) . ' WHERE uid IN(' . implode( ',', $ids ) . ')';
						$res = $follow_handler -> db -> query( $sql );
						while ( $v = $follow_handler -> getobject( $res ) ) {
								$k = $v -> getvar( 'uid' );
								$users[$k] = $users[$k] + ( $v -> getvars( 'n' ) );
						} 
						foreach ( $rows as $k => $v ) {
								if ( isset( $users[$v['uid']] ) && isset( $users[$v['toid']] ) ) {
										switch ( $this -> exevars['type'] ) {
												case 'active':
														$rows[$k]['user'] = $users[$v['uid']];
														$rows[$k]['touser'] = $users[$v['toid']];
														break;
												case 'fans':
														$rows[$k] = $users[$v['uid']] + $v;
														break;
												case 'follow':
														$rows[$k] = $users[$v['toid']] + $v;
														break;
										}
								} else {
										unset( $rows[$k] );
								}
						}
				}
				if ( $isreturn ) {
						return $rows;
				}
				$saxueTpl -> assign_by_ref( "rows", $rows );
		} 
} 
