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


	class MultiLangDataProvider extends Widget {
		protected $aValues = array();
		protected $aSiteLanguages = array();
		protected $aSubWidgets = array();

		public function addNewDataProvider( $site_language_id ) {
			return new AdminTextbox( $this->getId() . "_" . $site_language_id , $this );
		}

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

			$lang = new LanguagesModel();
			$this->aSiteLanguages = $lang->getSiteLanguages( false , true );
			foreach ($this->aSiteLanguages as $site_language_id => $name) {
				$this->aSubWidgets[$site_language_id] = $this->addNewDataProvider( $site_language_id );
			}
		}

		public function setValue( $language_id , $val ) { $this->aValues[$language_id] = $val; }
		public function setValues( $kvarray ) { $this->aValues = $kvarray; }
		public function addValues( $kvarray ) { foreach ($kvarray as $k => $v) $this->aValues[$k]=$v; }
		public function clearValues() { $this->aValues = array(); }
		public function unsetValue( $language_id ) { if (isset( $this->aValues[$language_id] )) unset( $this->aValues[$language_id] ); }

		public function getSubWidget( $site_language_id ) {
			if (!isset( $this->aSubWidgets[$site_language_id] )) return null;
			return $this->aSubWidgets[$site_language_id];
		}

		// !!! FIXME !!! miert nincs itt refreshValueFromArray
		public function getValuesFromArr( $arr = null ) {
			$retval = array();
			if (!isset( $arr )) $arr = $this->aP;
			$sid = $this->getId()."_";
			foreach ($arr as $k => $v) {
				if (strrpos( $k , $sid )!==0) continue;	// hatulrol keresunk, igy nem lehet egyezes
				$id = (int)str_replace( $sid , "" , $k );
				$retval[$id]=$v;
			}
			return $retval;
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->setParam( "site_languages" , $this->aSiteLanguages );
			// generate widgetnames
			$ids = array();
			foreach ($this->aSubWidgets as $site_language_id => $wdg)
				$ids[$site_language_id]=$wdg->getId();$this->setParam( "widgets" , $ids );
			// fill up values
			foreach ($this->aValues as $site_language_id => $value)
				$this->aSubWidgets[$site_language_id]->setValue( $value );
			return parent::render();
		}
	}

?>
