<?php
/*
会员动态区块
参数1：动态数，默认10，可用参数：大于0整数
参数2：会员ID，默认0，可用参数：0（全部会员动态），会员ID，uid（系统取$_REQUEST['uid']值），$uid（系统取已定义模版变量uid值）
参数3：动态类型，默认0，可用参数：0（全部动态），1（发布），2（收藏），3（推荐、顶赞），4（下载）
参数4：文章ID，默认0，可用参数：0（全部文章动态），文章ID，aid（系统取$_REQUEST['aid']值），$aid（系统取已定义模版变量aid值）
*/
class blockdoinglist extends saxueblock {
		public $template = "block_doinglist.html";
		public $exevars = array( 'listnum' => 10, 'uid' => 0, 'doing' => 0, 'aid' => 0 );
		public function blockdoinglist( &$_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 ( is_numeric( $_fieldarr[1] ) && 0 < $_fieldarr[1] ) {
										$this -> exevars['uid'] = intval( $_fieldarr[1] );
								} else if ( substr( $_fieldarr[1], 0, 1 ) == "\$" ) {
										$_uidvar = $saxueTpl -> get_assign( substr( $_fieldarr[1], 1 ) );
										if ( is_numeric( $_uidvar ) ) {
												$this -> exevars['uid'] = $_uidvar;
										} 
								} else if ( isset( $_REQUEST[$_fieldarr[1]] ) && is_numeric( $_REQUEST[$_fieldarr[1]] ) ) {
										$this -> exevars['uid'] = $_REQUEST[$_fieldarr[1]];
								} 
						}
						if ( 2 < $_fieldcount ) {
								$_fieldarr[2] = trim( $_fieldarr[2] );
								if ( is_numeric( $_fieldarr[2] ) && 0 < $_fieldarr[2] ) {
										$this -> exevars['doing'] = intval( $_fieldarr[2] );
								} 
						}
						if ( 3 < $_fieldcount ) {
								$_fieldarr[3] = trim( $_fieldarr[3] );
								if ( is_numeric( $_fieldarr[3] ) && 0 < $_fieldarr[3] ) {
										$this -> exevars['aid'] = intval( $_fieldarr[3] );
								} else if ( substr( $_fieldarr[3], 0, 1 ) == "\$" ) {
										$_aidvar = $saxueTpl -> get_assign( substr( $_fieldarr[3], 1 ) );
										if ( is_numeric( $_aidvar ) ) {
												$this -> exevars['aid'] = $_aidvar;
										} 
								} else if ( isset( $_REQUEST[$_fieldarr[3]] ) && is_numeric( $_REQUEST[$_fieldarr[3]] ) ) {
										$this -> exevars['aid'] = $_REQUEST[$_fieldarr[3]];
								} 
						} 
				} 
				$this -> blockvars['cacheid'] = md5( serialize( $this -> exevars ) . "|" . $this -> blockvars['template'] );
		} 
		public function setcontent( $isreturn = false ) {
				global $saxueTpl;
				global $doing_handler;
				global $saxueDoingtype;
				if ( !isset( $saxueDoingtype ) ) {
						saxue_getconfigs( "doingtype", 'member' );
				}
				if ( !is_object( $doing_handler ) ) {
						include_once SAXUE_ROOT_PATH . "/model/member_doing.php";
						$doing_handler = &saxuememberdoinghandler :: getinstance( "saxuememberdoinghandler" );
				}
				include_once SAXUE_ROOT_PATH . "/common/function.php";
				$where = array();
				if ( !empty( $this -> exevars['uid'] ) ) {
						$where[] = "uid=" . $this -> exevars['uid'];
				}
				if ( !empty( $this -> exevars['doing'] ) ) {
						$where[] = "doing=" . $this -> exevars['doing'];
				}
				if ( !empty( $this -> exevars['aid'] ) ) {
						$where[] = "aid=" . $this -> exevars['aid'];
				}
				$sql = 'SELECT * FROM ' . saxue_dbprefix( "member_doing" );
				if ( count( $where ) > 0 ) {
						$sql .= ' WHERE ' . implode( ' AND ', $where );
				}
				$sql .= ' ORDER BY doid DESC LIMIT 0, ' . $this -> exevars['listnum'];
				$res = $doing_handler -> db -> query( $sql );
				$rows = $aids = array();
				$k = 0;
				while ( $v = $doing_handler -> getobject( $res ) ) {
						$rows[$k] = $v -> getvars( 'n' );
						$rows[$k]['doname'] = $saxueDoingtype[$rows[$k]['doing']];
						$rows[$k]['dodate'] = saxue_getdate( $rows[$k]['dotime'] );
						$aids[] = $rows[$k]['aid'];
						++$k;
				} 
				if ( $k > 0 ) {
						global $saxueCate;
						if ( !isset( $saxueCate['article'] ) ) {
								saxue_getconfigs( "cate", 'article' );
						}
						$aids = array_unique( $aids );
						$sql = 'SELECT * FROM ' . saxue_dbprefix( "article" ) . ' WHERE aid IN(' . implode( ',', $aids ) . ')';
						$_res = $doing_handler -> db -> query( $sql );
						$articles = $tables = array();
						while ( $v = $doing_handler -> getobject( $_res ) ) {
								$k = $v -> getvar( 'aid' );
								$articles[$k] = $v -> getvars( 'n' );
								$articles[$k]['cname'] = $saxueCate['article'][$articles[$k]['cid']]['cname'];
								$articles[$k]['curl'] = $saxueCate['article'][$articles[$k]['cid']]['curl'];
								if ( $articles[$k]['uid'] > 0 ) {
										$articles[$k]['uhome'] = saxue_geturl( "user_home", $articles[$k]['uid'] );
										$articles[$k]['uavatar'] = saxue_geturl( "user_avatar", $articles[$k]['uid'] );
								}
								$tables[$articles[$k]['tbnum']][] = $k;
						}
						foreach ( $tables as $tbnum => $ids  ) {
								$_res = $doing_handler -> db -> query( "SELECT * FROM " . saxue_dbprefix( 'article_data_' . $tbnum ) . " WHERE aid IN (" . implode( ',', $ids ) . ")" );
								while ( $v = $doing_handler -> getobject( $_res ) ) {
										$k = $v -> getvar( 'aid' );
										$articles[$k] = $articles[$k] + ( $v -> getvars( 'n' ) );
										$articles[$k]['intro'] = nl2br( $articles[$k]['intro'] );
										if ( $articles[$k]['isslide'] ) {
												$articles[$k]['album'] = explode( '|', $articles[$k]['album'] );
										}
										// 读取TAG
										$_tags = array();
										$_tagnum = 0;
										if ( strlen( $articles[$k]['tags'] ) > 0 ) {
												$_arr = explode( ',', $articles[$k]['tags'] );
												$_arrid = explode( ',', $articles[$k]['tagids'] );
												foreach ( $_arr as $_i => $_s ) {
														$_tags[$_tagnum]['name'] = $_s;
														$_tags[$_tagnum]['url'] = saxue_geturl( "tag_show", $_arrid[$_i] );
														++$_tagnum;
												} 
										}
										$articles[$k]['taglist'] = $_tags;
								} 
						} 
						foreach ( $rows as $k => $v ) {
								if ( isset( $articles[$v['aid']] ) ) {
										$rows[$k]['article'] = $articles[$v['aid']];
								} else {
										unset( $rows[$k] );
								}
						}
				}
				if ( $isreturn ) {
						return $rows;
				}
				$saxueTpl -> assign_by_ref( "rows", $rows );
		} 
} 
