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

	
	class SelectedTranslators extends Widget {
		protected $aSelectedTranslators = array();
		protected $bShowTemplatePanel = false;

		protected function generateLangDefs() {
			fo3_add_js_langdef( "No_group_name_given" );
			fo3_add_js_langdef( "Translator_group_name" );
			fo3_add_js_langdef( "confirm_Delete_translator_group" );
		}

		protected function scrollToThisOnRefresh() {
			$_SESSION["scrolltothis"]=$this->getId();
		}

		public function handleEvents() {
			$this->mmm = new MyMemberModel();
			$this->bActiveAgency = ($this->mmm->isLoggedinMemberActiveSubscriber() && $this->mmm->getLoggedInUserType()=="agency");
			$this->bShowTemplatePanel = $this->bActiveAgency;

			if (!isset( $this->aP["inajax"] ) && !isset( $this->aP["task"] )) return parent::handleEvents();
			if (isset( $this->aP["inajax"] )) {
				if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
				$ae = $this->aP["ajaxevent"];
				switch ($ae) {
				case "refresh_selected_translators":
					FNTH_echo_json_response( $this->getSelectedUsers() , 0 , "" , false );
					break;
				case "load_translators":
					$mym = new MyUserModel();
					if (!$mym->isUserLoggedIn()) {
						printf( "alert( '%s' );" , __("globalerror_User_has_to_login_to_reach_functionality") );
						fo3_exit();
					}
					$selusers = new SelectedUsersModel();
					$template_id = $this->aP["template_id"];
					$template = $mym->getUserTemplate( $template_id );
					if ($template) {
						$users = $mym->getUserTemplateUsers( $template_id );
						$selusers->setAllSelectedUsers( $users );
						$this->scrollToThisOnRefresh();
						printf( "document.location.href=document.location.href;" );
					} else {
						printf( "alert( '%s' );" , __("selectedtranslators_Not_your_template") );
					}
					fo3_exit();
					break;
				case "save_translators":
					$mym = new MyUserModel();
					if (!$mym->isUserLoggedIn()) {
						printf( "alert( '%s' );" , __("globalerror_User_has_to_login_to_reach_functionality") );
						fo3_exit();
					}
					// save template
					$selusers = new SelectedUsersModel();
					$mym->addUserTemplate( $this->aP["template_name"] , $selusers->getAllSelectedUsers() );
					$this->scrollToThisOnRefresh();
					printf( "document.location.href=document.location.href;" );
					fo3_exit();
					break;
				case "del_translators":
					$mym = new MyUserModel();
					if (!$mym->isUserLoggedIn()) {
						printf( "alert( '%s' );" , __("globalerror_User_has_to_login_to_reach_functionality") );
						fo3_exit();
					}
					$mym->deleteUserTemplate( $this->aP["template_id"] );
					$this->scrollToThisOnRefresh();
					printf( "document.location.href=document.location.href;" );
					fo3_exit();
					break;
				case "add_selected_user":
				case "del_selected_user":
					$selusers = new SelectedUsersModel();
					switch ($ae) {
					case "add_selected_user":
						if (isset( $this->aP["user_id"] ))
							$selusers->addSelectedUser( $this->aP["user_id"] );
						break;
					case "del_selected_user":
						if (isset( $this->aP["user_id"] ))
							$selusers->deleteSelectedUser( $this->aP["user_id"] );
						break;
					}
					printf( "\$(\"#%s\").html( %s );",
						$this->getId(),
						json_encode( $this->renderUserTable( $this->getSelectedUsers() ) ) );
					fo3_exit();
				}
			} else if (isset( $this->aP["task"] )) {

			}
			return parent::handleEvents();
		}

		protected function getSelectedUsers() {
			$selusers = new SelectedUsersModel();
			$user_ids = $selusers->getAllSelectedUsers();
			$users = new UsersModel();
			return $users->getMembersFromUserIds( $user_ids );
		}

		protected function getLanguageDefinitions( ) {
			$retval = array();
			$retval[]="No_selected_translators";
			return $retval;
		}

		protected function generateXMLFromData( $data ) {
			$retval = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
			$retval.= "<members>\n";
			$retval.= "\t<memberdata>\n";
			foreach ($data as $member) {
				$retval.="\t\t<member>\n";
					$retval.=sprintf( "\t\t\t<user_id>%d</user_id>\n",$member["user_id"] );
					$retval.=sprintf( "\t\t\t<member_name>%s</member_name>\n",$member["user_name"] );
				$retval.="\t\t</member>\n";
			}
			$retval.= "\t</memberdata>\n";
			$langdefs = $this->getLanguageDefinitions();
			if (!empty( $langdefs )) {
				$retval.= "\t<langdefs>\n";
				foreach ($langdefs as $langdef)
					$retval.= sprintf( "\t\t<%s>%s</%s>\n" , $langdef , __($langdef) , $langdef );
				$retval.= "\t</langdefs>\n";
			}
			$retval.= "</members>\n";
			// FNTH_da( htmlspecialchars( $retval ) );
			return $retval;
		}

		protected function renderUserTable( $data ) {
			$this->setParam( "usertable" , $data );
			$filename = $this->getTemplateFilename( ".usertable" );
			return $this->renderFromTemplate( $filename );
		}

		public function renderJSONHelper() {
			$data = $this->getSelectedUsers();
			FNTH_echo_json_response( $data );
		}

		public function renderXMLHelper() {
			header( "Content-type: text/xml" );
			$data = $this->getSelectedUsers();
			$xml = $this->generateXMLFromData( $data );
			echo $xml;
			fo_exit();
		}

		protected function getUserTemplates() {
			$mym = new MyUserModel();
			return $mym->getUserTemplates();
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			/*
			// obsolete
			FNTH_include_3rdparty( "xslt" );
			FNTH_include_3rdparty( "jquery_transform" );
			*/
			$usertable = $this->renderUserTable( $this->getSelectedUsers() );
			$this->setParam( "usertable" , $usertable );
			$this->setParam( "translator_groups" , $this->getUserTemplates() );
			$this->setParam( "bShowTemplatePanel" , $this->bShowTemplatePanel );
			$url_article = ArticleURL::renderURLFromArticleName( "sys_translator_group_management" );
			$this->setParam( "url_article_selected_translators" , $url_article );
			$this->generateLangDefs();
			return parent::render();
		}
	}
?>
