<?php
	require_once( $GLOBALS["site_root"] . "/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/models/Users.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/Member.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/User.inc.php" );

	class AdminUserNameTag extends Widget {
		protected $iUserId = 0;
		protected $sPrefix = "";
		protected $sPostfix = "";

		protected function initNameTagCache() {
			if (!isset( $GLOBALS["AdminUserNameTagCache"] )) $GLOBALS["AdminUserNameTagCache"]=array();
		}

		public function setPrefix( $v ) { $this->sPrefix = $v; }
		public function setPostfix( $v ) { $this->sPostfix = $v; }

		protected function loadCache() {
			if (isset( $GLOBALS["AdminUserNameTagCacheData"] )) return;
			$this->initNameTagCache();
			$usersmodel = new UsersModel();
			$ds = $usersmodel->getMembersFromUserIds( array_keys( $GLOBALS["AdminUserNameTagCache"] ) );
			$GLOBALS["AdminUserNameTagCacheData"]=array();
			$murl = new MemberURL();
			$uurl = new UserURL();
			foreach ($ds as $row) {
				if (isset( $row["member_id"] )) {
					$row["url"]=$murl->renderAdminURLFromMember( $row );
					$row["jsparams"]="AdminMembersGridJsParams";
				} else {
					$row["url"]=$uurl->renderAdminURLFromUser( $row );
					$row["jsparams"]="AdminUsersGridJsParams";
				}
				$GLOBALS["AdminUserNameTagCacheData"][$row["user_id"]]=$row;
			}
		}

		protected function setNameTagUserId( $user_id ) {
			$this->initNameTagCache();
			$GLOBALS["AdminUserNameTagCache"][$user_id]=true;
		}

		protected function unsetNameTagUserId( $user_id ) {
			$this->initNameTagCache();
			unset( $GLOBALS["AdminUserNameTagCache"][$user_id] );
		}

		public function setId( $user_id ) {
			if ($this->iUserId) $this->unsetNameTagUserId( $this->iUserId );
			$this->iUserId = $user_id;
			$this->setNameTagUserId( $this->iUserId );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->loadCache();
			$this->setParam( "datas" , $GLOBALS["AdminUserNameTagCacheData"][$this->iUserId] );
			$this->setParam( "sPrefix" , $this->sPrefix );
			$this->setParam( "sPostfix" , $this->sPostfix );
			return parent::render();
		}
		

	}

?>
