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

	class MySpecialties extends Widget {
		protected $aSelSpecialties = array();
		protected $aCustomSpecialties = array();
		protected $iMemberId = 0;

		protected $oCustomSpecialties = null;

		public function setMemberId( $v ) {
			$this->iMemberId = $v;
			if (isset( $this->oCustomSpecialties)) $this->oCustomSpecialties->setMemberId( $this->iMemberId );
		}
		
		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->oCustomSpecialties = new MemberFrontendMultiLangDataProvider( "custom_specialties" , $this );
			$this->oCustomSpecialties->setTitle( __("Custom_specialty" ) );
		}

		public function setSpecialty( $id ) { $this->aSelSpecialties[$id]=true; }
		public function unsetSpecialty( $id ) { unset( $this->aSelSpecialties[$id] ); }

		public function getCustomSpecialtiesWidget() {
			return $this->oCustomSpecialties;
		}

		public function setCustomSpecialty( $lang_id , $s ) {
			$this->aCustomSpecialties[$lang_id] = $s;
		}

		public function getCustomSpecialty( $lang_id = 0 ) {
			if (!$lang_id) return "";
			if (!isset( $this->aCustomSpecialties[$lang_id] )) return "";
			return $this->aCustomSpecialties[$lang_id];
		}

		public function setCustomSpecialties( $arr ) {
			$this->aCustomSpecialties = $arr;
			$this->oCustomSpecialties->setValues( $this->aCustomSpecialties );
		}

		public function getCustomSpecialties( ) {
			return $this->aCustomSpecialties;
		}
		
		public function getSpecialties() { return array_keys( $this->aSelSpecialties ); }

		public function setSpecialties($specialty_ids) {
			$this->aSelSpecialties=array();
			foreach ($specialty_ids as $id) $this->aSelSpecialties[$id]=true;
		}
		
		public function refreshValueFromArray( $arr = null ) {
			if (!isset($arr)) $arr = $this->aP;
			$preid = $this->getId() . "_" ;

			// if (isset( $this->oCustomSpecialties )) $this->oCustomSpecialties->refreshValueFromArray( $arr );
			$this->aCustomSpecialties = $this->oCustomSpecialties->getValuesFromArr( $arr );

			$this->aSelSpecialties = array();
			foreach ($arr as $k => $v) {
				if (strpos( $k , $preid )!==0) continue;
				$id = str_replace( $preid , "" , $k );
				$this->setSpecialty( $id );
			}
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$specs = SpecialtiesModel::getSpecialtyOptions( false );
			$specs = fo3_xlat_horvert( $specs , 4 );
			$this->setParam( "specs" , $specs );
			$this->setParam( "sels" , $this->aSelSpecialties );
			// $this->setParam( "custom_specialties" , $this->aCustomSpecialties );
			// FNTH_da( $this->aCustomSpecialties );
			return parent::render();
		}

	}
?>
