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

	class LanguagePairs extends Widget {
		protected $aLangMatrix = array();
		protected $bReadOnly = false;
		protected $bShowIntro = false;
		protected $aSelectedLanguages = array();

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

		public function setShowIntro( $v ) { $this->bShowIntro = $v; }
		public function setReadOnly( $v ) { $this->bReadOnly = $v; }

		public function refreshValueFromArray( $arr = null ) {
			if (!isset( $arr )) $arr = $this->aP;

			$langmatrix = array();
			$prefix = $this->getId() . "_";
			foreach ($arr as $k => $v) {
				if (strrpos($k,$prefix)!==0) continue;
				$k = str_replace( $prefix , "" , $k );
				$exp = explode( "_" , $k );
				if (count($exp)!=3) continue;
				$stid = $exp[0];
				$sl = $exp[1];
				$tl = $exp[2];
				if (!isset( $langmatrix[$stid] )) $langmatrix[$stid]=array();
				if (!isset( $langmatrix[$stid][$sl] )) $langmatrix[$stid][$sl]=array();
				if (!isset( $langmatrix[$stid][$sl][$tl] )) $langmatrix[$stid][$sl][$tl]=true;
			}
			$this->aLangMatrix = $langmatrix;
		}
		public function getValue() {
			return $this->aLangMatrix;
		}

		public function setSelectedLanguages( $v ) { $this->aSelectedLanguages = $v; }
		public function setValue( $v ) { $this->aLangMatrix = $v; }
		
		protected function generateLangDefs() {
			fo3_add_js_langdef( "myprofilepage_Expand_all_languages" );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			
			$datas = array();
			$stm = new ServiceTypesModel();
			$this->setParam( "bShowIntro" , $this->bShowIntro );
			$this->setParam( "bReadOnly" , $this->bReadOnly );
			$datas["readonly"]=$this->bReadOnly;
			$datas["servicetypes"]=$stm->getServiceTypeOptions( false );
			$lm = new LanguagesModel();
			$datas["languages"]=$lm->getLanguageOptions( false );
			$datas["selected_languages"] = $this->aSelectedLanguages;
			$datas["language_matrix"]=$this->aLangMatrix;
			FNTHPage::addUniquePageFullInit( sprintf( "InitLanguagePairs( '%s' , %s );\n",
				$this->getId(),
				json_encode( $datas ) ) );
			$this->generateLangDefs();
			return parent::render();
		}

	}

?>
