<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/MemberWidget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendGrid.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendCheckbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Cat.inc.php" );

	FNTH_class( "RadioSegment" );

	class MyCatPane extends MemberWidget {
		protected $oCustomCat;

		protected $oChecks;
		protected $oRadios;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			if (!$this->bLoggedIn) return;

			$this->sPageName = "cat";
			$this->oMyProfileMenu->setCurrentMenuItem( $this->sPageName );
			$this->oCustomCat = new FrontendTextbox( "custom_cat" , $this );
			
			$this->generateMemberWidgetNormalSubmit( $this->sPageName );
		}

		protected function createCatGrid() {

			$catgrid = new FrontendGrid( "CATGrid" , $this );
			$catgrid->setAttribute( "class" , "CATTable" );

			// create header
			$hrow = new FNTHGridRow( "ttlrow" , $catgrid , "header" );
			$field = new FNTHGridField( "catname" , $hrow );
			$field->setValue( __("mycat_Name_of_CAT") );
			$field->setAttribute( "class" , "ttl" );

			for ($level=1;$level<=3;$level++) {
				$field = new FNTHGridField( sprintf( "level%d" , $level ) , $hrow );
				$field->setAttribute( "class" , "lvl" );
				$field->setValue( __( sprintf( "mycat_level%d" , $level ) ) );
			}

			// create body
			$mmm =new CatModel();
			$cats = $mmm->getCatOptions( false );
			foreach ($cats as $cat_id => $cat_name ) {
				$rowname = sprintf( "CATGrid_%d_row" , $cat_id );

				$row = new FNTHGridRow( $rowname , $catgrid );
				$field = new FNTHGridField( "chk_field" , $row );
				$field->setAttribute( "class" , "ttl" );

				$this->oChecks[$cat_id] = $chk = new FrontendCheckbox( $rowname . "_chk" , $field );
				$chk->setShowTitle( true );
				$chk->setTitle( $cat_name );
				$chk->setParam( "cat_id" , $cat_id );

				$this->oRadios[$cat_id] = array();

				for ($level=1;$level<=3;$level++) {
					$field = new FNTHGridField( "level".$level."_field" , $row );
					$field->setAttribute( "class" , "lvl" );
					$this->oRadios[$cat_id][$level] = $radio = new FNTHRadioSegment( $rowname . "_radio" , $field );
					$radio->setValue( $level );
					
					$radio->setParam( "cat_id" , $cat_id );
					$radio->setParam( "level" , $level );
				}
			}
		}

		protected function loadDatas() {
			$cm = new CatModel();
			$cats = $cm->getCatOptions( false );
			$sel_cats = $this->member_model->getMemberCats( $this->iMemberId );
			$dsel_cats = $sel_cats["cats"];

			foreach ($cats as $cat_id => $val) {
				if (isset( $dsel_cats[$cat_id] )) {
					$this->oChecks[$cat_id]->setChecked( true );
					$level = $dsel_cats[$cat_id];
					$this->oRadios[$cat_id][$level]->setChecked( true );
				} else {
					$this->oRadios[$cat_id][1]->setChecked( true );
				}
			}
			$member = $this->member_model->getMemberFromMemberId( $this->iMemberId );
			$this->oCustomCat->setValue( $member["custom_cat"] );
		}

		protected function saveData() {
			$this->refreshChildValuesFromArray();

			$d = array();
			$cm = new CatModel();
			$cats = $cm->getCatOptions( false );
			foreach ($cats as $cat_id => $val) {
				if (!$this->oChecks[$cat_id]->isChecked()) continue;
				$d[$cat_id]=0;
				for ($level=1;$level<=3;$level++)
					if ($this->oRadios[$cat_id][$level]->isChecked()) $d[$cat_id]=$level;
			}

			$datas = array();
			$datas["cats"]=$d;
			$datas["custom_cat"]=$this->oCustomCat->getValue();
			$this->member_model->setMemberCats( $this->iMemberId , $datas );
		}

		public function modifyGetPost( &$g , &$p ) {
			$retval = parent::modifyGetPost( $g , $p );
			$this->createCatGrid();
			return $retval;
		}

		public function handleEvents() {
			if (!$this->bLoggedIn) return parent::handleEvents();
			$this->oSubmit->refreshValueFromArray();
			if (!$this->oSubmit->isSubmitted()) return parent::handleEvents();
			$this->saveData();

			// clear admin ack
			// $this->user_model->adminAcknowledgeUser( $this->iUserId , false );		// mar a dirty csinalja

			// set dirty
			$this->member_model->setMemberDirty( $this->iMemberId , true );

			// clear todo
			$tlm = new TodoListModel();
			$tlm->clearTodoListItem( $this->iUserId , "cat" );
		
			// log	
			$ulm = new UserLogModel();
			$ulm->logMemberActivity( USER_LOG_GENERAL | USER_GENERAL_MODIFIED_PROFILE , "változtatott fordítástámogató szoftverein" );	// LANGFIX

			FNTH_refresh();
			return parent::handleEvents();
		}

	
		public function render() {
			if ($this->sRendered) return $this->sRendered;
			if (!$this->bLoggedIn) return parent::render();
			$this->loadDatas();
			fo3_form_change_warning( "document.frm_my_cat" );
			return parent::render();
		}
	}

?>
