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

	class TranslatorBox extends Widget {
		protected $iMemberId = 0;
		protected $sMemberType = "";
		protected $sTitle = "";

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
		}

		public function setTitle( $v ) { $this->sTitle = $v; }

		public function setMemberId( $v ) { $this->iMemberId = $v; }
		public function setMemberType( $v ) { $this->sMemberType = $v; }

		protected function getPrevNextMember( $member_id , $member_type , $forward = true ) {
			$filters = array();
			// a tipusa
			$filters["type"]=sprintf( "member_type='%s'" , FNTH_db_addslashes( $member_type ) );
			// valahol a kozepebe seekelunk
			$filters["offset"]=sprintf( "member_id%s%d" , ($forward) ? ">" : "<" , $member_id );
			// nem torolt
			$filters["deleted"]="not is_deleted";
			// es meg aktiv elofizeto is
			$filters["expires"]="expires_tstamp>now()";
			// es raadasul nem parttimer
			$filters["noparttime"]="member_type not in ('parttime')";

			if ($GLOBALS["language"]!="hu") {
				$epm = new ExtendedPropertiesModel();
				$prop_id = $epm->getEPropIdFromName( "use_langsite" );
				$filters["language"]=sprintf( "member_id in (select member_id from member_extended_properties where language_id=%d and extended_property_id=%d and value='on')",
					$GLOBALS["langcode"],
					$prop_id );
			}

			$order=($forward) ? "member_id" : "member_id desc";
			$query = sprintf( "select member_id from members where %s order by %s limit 1",implode( " and " , $filters ),$order );

			$retval = FNTH_db_get_field( $query , "member_id" );
			if ($retval) return $retval;

			unset( $filters["offset"] );
			$query = sprintf( "select member_id from members where %s order by %s limit 1",implode( " and " , $filters ),$order );
			return FNTH_db_get_field( $query , "member_id" );
		}

		public function handleEvents() {
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxtarget"] )) return parent::handleEvents();
			if ($this->aP["ajaxtarget"]!=$this->getId()) return parent::handleEvents();
			switch ($this->aP["ajaxevent"]) {
			case "translatorbox_prev":
			case "translatorbox_next":
				if (!isset( $this->aP["member_id"] )) return parent::handleEvents();
				if (!isset( $this->aP["member_type"] )) return parent::handleEvents();
				$this->iMemberId = $this->getPrevNextMember( $this->aP["member_id"] , $this->aP["member_type"] , ($this->aP["ajaxevent"]=="translatorbox_next") ? true : false );

				// FNTH_da( $this->aP );
				FNTH_echo_json_response( $this->prepareJSON() );
				fo3_exit();
			}
			return parent::handleEvents();
		}
		
		protected function renderWorkingLanguages( $member_id , $limit = 4 ) {
			$this->lm = new LanguagesModel();
			$languages = $this->lm->getLanguageOptions();
			$lang_ids = $this->member_model->getMemberLanguages( $member_id );
			$retval="";
			$i=0;
			foreach ($lang_ids as $lang_id ) {
				$retval.=(empty( $retval )) ? "" : ", ";
				if ($i>=$limit) {
					$retval.="...";
					break;
				}
				$retval.=$languages[$lang_id];
				$i++;
			}
			return $retval;
		}

		protected function prepareJSON() {
			$this->member_model = new MemberModel();
			$this->member = $this->member_model->getMemberFromMemberId( $this->iMemberId , true );
			$this->user_model = new UserModel();
			$this->user = $this->user_model->getUserFromUserId( $this->member["user_id"] );

			$retval = array();

			$member = array();
			$mf = array( "member_id" , "user_id" , "thumburlwww" , "member_url" , "member_type" , "localized_name" );
			foreach ($mf as $f) if (isset( $this->member[$f] )) $member[$f]=$this->member[$f];
			$member["languages"]=$this->renderWorkingLanguages( $member["member_id"] );
			$retval["member"]=$member;

			$user = array();
			$uf = array( "user_id" , "user_name" );
			foreach ($uf as $f) $user[$f]=$this->user[$f];
			$retval["user"]=$user;
			return $retval;
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;

			FNTHPage::addUniquePageFullInit(
				sprintf( "InitTranslatorBox( '%s' , %s );",
				$this->getId(),
				json_encode( $this->prepareJSON() ) ) );
			$this->setParam( "member_type" , $this->sMemberType );
			$sup = array();
			$sup["smb_search"]=__("searchpage_Search","attr");
			$sup["utype"]=$this->sMemberType;
			switch ($this->sMemberType) {
			case "agency":
				$title_more_translators = __("more_agencies" );
				break;
			case "freelancer":
				$title_more_translators = __("more_freelancers" );
				break;
			}
			$url_more_translators = str_replace( "&", "&amp;" , MemberSearchURL::renderURL( $sup ) );
			$this->setParam( "url_more_translators" , $url_more_translators );
			$this->setParam( "title_more_translators" , $title_more_translators );
			$this->setParam( "title" , $this->sTitle );
			return parent::render();
		}
	}
?>
