<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendCheckbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendTextarea.inc.php" );

	class MemberExtendedProperties extends Widget {
		protected $bGlobal = false;
		protected $oCtrls = array();
		protected $iLanguageId = 1;

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

		public function setGlobalProperties( $v ) { $this->bGlobal = $v; }
		public function setLanguageId( $language_id ) { $this->iLanguageId = $language_id; }

		public function modifyGetPost( &$g , &$p ) {
			$query = sprintf( "select
				ep.extended_property_id,
				ep.ep_name,
				ep.ep_type,
				ep.ep_multilang,
				epl.langdef_name as epl_name
					from
				extended_properties as ep,
				extended_property_langdefs as epl
					where
				ep.extended_property_id=epl.extended_property_id and
				epl.language_id=%d",
				$GLOBALS["langcode"] );
			$query.=($this->bGlobal) ?
				" and not ep.ep_multilang" :
				" and ep.ep_multilang";
			if ($this->bGlobal) {
				$fields = array();
				$fields[]="experience_5+";
				$fields[]="native_language";
				$fields[]="certificate";
				$fields[]="invoice";
				$query.=sprintf( " and ep_name in ('%s')",implode( "','" , $fields ) );
			}
				
			$ds = FNTH_db_get_resultset( $query );
			foreach ($ds as $ctrl) {
				$name = $this->getId()."_".$ctrl["ep_name"];
				$wdg = null;
				switch ($ctrl["ep_type"]) {
				case "checkbox":
					$wdg = new FrontendCheckbox( $name , $this );
					$wdg->setShowTitle( true );
					$wdg->setTitle( $ctrl["epl_name"] );
					break;
				case "text":
					$wdg = new FrontendTextarea( $name , $this );
					break;
				}
				$this->oCtrls[$name] = $wdg;
			}
		}

		public function setSubChecked( $name , $value ) {
			$this->oCtrls[$this->getId()."_".$name]->setChecked( $value );
		}
		public function isSubChecked( $name ) {
			return $this->oCtrls[$this->getId()."_".$name]->isChecked();
		}

		protected function getControls() {
			$retval = array();
			foreach ($this->oCtrls as $wdg) {
				$retval[]=$wdg->getId();
			}
			return $retval;
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->setParam( "controls" , $this->getControls() );
			return parent::render();
		}
	}

?>
