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

	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendTextbox.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/MemberExtendedProperties.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/MemberAttachments.inc.php" );

	
	require_once( $GLOBALS["site_root"]."/includes/widgets/Zipcode.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/County.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/Country.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/models/TodoList.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Youtube.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/ExtendedProperties.inc.php" );

	class MyProfileParametersPane extends MemberWidget {
		protected $oIsAddressPublic = null;
		protected $oZipcode = null;
		protected $oCity = null;
		protected $oAddress = null;
		protected $oCounty = null;
		protected $oCountry = null;

		protected $oPhone = null;
		protected $oFax = null;
		protected $oGlobalProperties = array();
		protected $oLocalProperties = array();
		protected $aGlobalProps = array( "experience_5+", "native_language" , "certificate" , "invoice" );

		
		protected function generateContactWidgets() {
			if (!$this->bLoggedIn) return;

			$this->oIsAddressPublic = new FrontendCheckbox( "is_address_public" , $this );

			$this->oZipcode = new Zipcode( "zip" , $this );
			$this->oZipcode->setAttribute( "maxlength" , 16 );
			$this->oZipcode->setMandatory( true );
			$this->oZipcode->setWidth( 80 );
			$this->oCity = new FrontendTextbox( "city" , $this );
			$this->oCity->setAttribute( "maxlength" , 64 );	
			$this->oCity->setMandatory( true );
			$this->oCity->setWidth( 225 );
			$this->oAddress = new FrontendTextbox( "address" , $this );
			$this->oAddress->setAttribute( "maxlength" , 96 );
			$this->oAddress->setMandatory( true );
			$this->oAddress->setWidth( 370 );

			$this->oCounty = new County( "county" , $this );
			$this->oCounty->setMandatory( true );
			$this->oCountry = new Country( "country" , $this );
			$this->oCountry->setMandatory( true );
			$this->oCountry->setValue( "hu" );
			$this->oCountry->setWidth( 370 );

			$this->oPhone = new FrontendTextbox( "phone" , $this );

			$this->oIsPhoneVisible = new FrontendCheckbox( "is_phone_visible" , $this );
			$this->oIsFaxVisible = new FrontendCheckbox( "is_fax_visible" , $this );
			$this->oFax = new FrontendTextbox( "fax" , $this );
			$this->oFax->setAttribute( "maxlength" , 64 );

			$this->oUrl = new FrontendTextbox( "url" , $this );
			$this->oUrl->setWidth( 370 );
			$this->oUrl->setAttribute( "maxlength" , 96 );
			
			$this->oMSN = new FrontendTextbox( "msn" , $this );
			$this->oMSN->setWidth( 370 );
			$this->oMSN->setAttribute( "maxlength" , 96 );
			
			$this->oSkype = new FrontendTextbox( "skype" , $this );
			$this->oSkype->setWidth( 370 );
			$this->oSkype->setAttribute( "maxlength" , 64 );
			
			$this->oFacebook = new FrontendTextbox( "facebook" , $this );
			$this->oFacebook->setWidth( 370 );
			$this->oFacebook->setAttribute( "maxlength" , 255 );
			
			$this->oYoutube = new FrontendTextbox( "youtube" , $this );
			$this->oYoutube->setWidth( 370 );
			$this->oYoutube->setAttribute( "maxlength" , 255 );

		}

		protected function generateGlobalCustomProperties() {
			if (!$this->bLoggedIn) return;
			$this->oGlobalProperties = new MemberExtendedProperties( "global_extended_properties" , $this );
			$this->oGlobalProperties->setGlobalProperties( true );
		}

		protected function addLocalCheckbox( $langiso , $name , $title = "" ) {
			$fname = sprintf( "%s_%s" , $langiso , $name );	
			$wdg = new FrontendCheckbox( $fname , $this );
			$wdg->setShowTitle( true );
			$wdg->setTitle( $title );
			$this->oLocalProperties[$fname]=$wdg;
			return $wdg;
		}
		
		protected function addLocalTextbox( $langiso , $name , $title = "" ) {
			$fname = sprintf( "%s_%s" , $langiso , $name );	
			$wdg = new FrontendTextBox( $fname , $this );
			$wdg->setShowTitle( true );
			$wdg->setTitle( $title );
			$wdg->setWidth( 500 );
			$this->oLocalProperties[$fname]=$wdg;
			return $wdg;
		}

		protected function addLocalTextarea( $langiso , $name , $title = "" ) {
			$fname = sprintf( "%s_%s" , $langiso , $name );	
			$wdg = new FrontendTextarea( $fname , $this );
			$wdg->setShowTitle( true );
			$wdg->setTitle( $title );
			$wdg->setColumns( 79 );
			$this->oLocalProperties[$fname]=$wdg;
			return $wdg;
		}
		
		protected function generateLocalizedCustomProperties() {
			if (!$this->bLoggedIn) return;
			$epm = new ExtendedPropertiesModel();

			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				switch ($langiso) {
				default:
					$this->addLocalCheckbox( $langiso , "use_langsite",
						"Jelenjen meg ezen az oldalon is az adatlapom" );
					$this->addLocalTextbox( $langiso , "langsite_name",
						$epm->getEPropLangdefFromName( $GLOBALS["langcode"] , "langsite_name" ) );
				case "hu":
					$this->addLocalTextarea( $langiso , "references",
						$epm->getEPropLangdefFromName( $GLOBALS["langcode"] , "references" ) );
					$this->addLocalTextarea( $langiso , "services",
						$epm->getEPropLangdefFromName( $GLOBALS["langcode"] , "services" ) );
					break;
				}
			}
		}

		protected function checkPublicAddress() {
			if (!$this->oIsAddressPublic->isChecked()) return true;
			$retval = true;
			if (!strlen( $this->oZipcode->getValue() )) {
				$retval = false;
				GlobalWarningMessage( "empty_zipcode" , __("Empty_zipcode" ) );
			}
			if (!strlen( $this->oCity->getValue())) {
				$retval = false;
				GlobalWarningMessage( "empty_city" , __("Empty_city" ) );
			}
			/*
			if (!strlen( $this->oAddress->getValue())) {
				$retval = false;
				GlobalWarningMessage( "empty_address" , __("Empty_address" ) );
			}
			*/
			if (!strlen( $this->oCounty->getValue())) {
				$retval = false;
				GlobalWarningMessage( "empty_county" , __("Empty_county" ) );
			}
			if (!strlen( $this->oCountry->getValue())) {
				$retval = false;
				GlobalWarningMessage( "empty_country" , __("Empty_country" ) );
			}
			return $retval;
		}

		protected function checkLangsiteNames() {
			$retval = true;
			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				if ($langiso=="hu") continue;
				$checked = $this->oLocalProperties[$langiso."_use_langsite"]->isChecked();
				if (!$checked) continue;
				$name = $this->oLocalProperties[$langiso."_langsite_name"]->getValue();
				if (strlen( $name )) continue;
				GlobalWarningMessage( $langiso."_no_langsite_name" , __("No_langsite_name") );
				$retval = false;
			}
			return $retval;
		}
		
		protected function saveDatas( $admin = false ) {
			// build update
			$old_datas = $this->member_model->getMemberFromMemberId( $this->iMemberId );

			$dirty = false;
			$admin_dirty = false;

			$p = array();
			$p["is_address_visible"]=$this->oIsAddressPublic->isChecked();
			if ($old_datas["is_address_visible"]!=$p["is_address_visible"]) {
				$dirty = true;
			}
			$p["zip"]=$this->oZipcode->getValue();
			if ($old_datas["zip"]!=$p["zip"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "zip" );
			}
			$p["city"]=$this->oCity->getValue();
			if ($old_datas["city"]!=$p["city"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "city" );
			}
			$p["address"]=$this->oAddress->getValue();
			if ($old_datas["address"]!=$p["address"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "address" );
			}
			$p["country_id"]=$this->oCountry->getValue();
			if ($old_datas["country_id"]!=$p["country_id"]) $dirty = true;
			$p["county_id"]=$this->oCounty->getValue();
			if ($old_datas["county_id"]!=$p["county_id"]) $dirty = true;

			$p["phone"]=$this->oPhone->getValue();
			if ($old_datas["phone"]!=$p["phone"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "phone" );
			}
			$p["is_phone_visible"]=$this->oIsPhoneVisible->isChecked();
			if ($old_datas["is_phone_visible"]!=$p["is_phone_visible"]) $dirty = true;

			$p["fax"]=$this->oFax->getValue();
			if ($old_datas["fax"]!=$p["fax"]) {
				$dirty = true;
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "fax" );
			}
			$p["is_fax_visible"]=$this->oIsFaxVisible->isChecked();
			if ($old_datas["is_fax_visible"]!=$p["is_fax_visible"]) $dirty = true;
			
			$p["url"]=FNTH_fixup_url( $this->oUrl->getValue() );
			if ($old_datas["url"]!=$p["url"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "url" );
			}
			
			$p["msn"]=$this->oMSN->getValue();
			if ($old_datas["msn"]!=$p["msn"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "msn" );
			}
			
			$p["skype"]=$this->oSkype->getValue();
			if ($old_datas["skype"]!=$p["skype"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "skype" );
			}
			
			$p["facebook"]=$this->oFacebook->getValue();
			if ($old_datas["facebook"]!=$p["facebook"]) {
				$dirty = $admin_dirty = true;
				$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "facebook" );
			}
	
			if (YoutubeModel::isValidYoutubeLink( $this->oYoutube->getValue() )) {
				$p["youtube"]=$this->oYoutube->getValue();
				if ($old_datas["youtube"]!=$p["youtube"]) {
					$dirty = $admin_dirty = true;
					$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , "youtube" );
				}
			}

			$this->member_model->updateMember( $this->iMemberId , $p );

			// update custom properties
			foreach ($this->aGlobalProps as $prop) {
				$prop_id = $this->member_model->getExtendedPropertyIdFromName( $prop );
				$checked = $this->oGlobalProperties->isSubChecked( $prop );
				if ($checked) {
					$this->member_model->setMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , "on" );
				} else {
					$this->member_model->unsetMemberExtendedPropertyFromId( $this->iMemberId , $prop_id );
				}
			}

			// use_langsite
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "use_langsite" );
			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				if ($langiso=="hu") continue;
				$checked = $this->oLocalProperties[$langiso."_use_langsite"]->isChecked();
				if ($checked) {
					$this->member_model->setMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , "on" , $langcode );
				} else {
					$this->member_model->unsetMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , $langcode );
				}
			}
			
			// services & references & langsite_name
			$props = array( "services" , "references" , "langsite_name" );
			foreach ($props as $propname) {
				$prop_id = $this->member_model->getExtendedPropertyIdFromName( $propname );
				foreach ($GLOBALS["languages"] as $langiso => $langcode) {
					$name = $langiso."_".$propname;
					if (!isset( $this->oLocalProperties[$name] )) continue;
					$value = $this->oLocalProperties[$name]->getValue();
					$old_value = $this->member_model->getMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , $langcode );
					if ($old_value!=$value)
						$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , $name );

					if (strlen( $value )) {
						$this->member_model->setMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , $value , $langcode );
					} else {
						$this->member_model->unsetMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , $langcode );
					}
				}
			}
			
			$this->member_model->updateMemberAliasname( $this->iMemberId );

			// attachments
			$this->oMemberAttachments->handleEvents();
			$this->oMemberAttachments->deleteAttachments();
			$this->oMemberAttachments->saveAttachments();

			// picture
			$this->handlePictureEvents();
			
			if ($dirty) {
				// $this->user_model->adminAcknowledgeUser( $this->iUserId , false );	// mar a dirty csinalja
				$this->member_model->setMemberDirty( $this->iMemberId , true );
			}
			if ($admin) {
				$this->clearDirtyObjectAdmin( $this->sPageName );
			}

			// clear todo
			$tlm = new TodoListModel();
			$tlm->clearTodoListItem( $this->iUserId , "profile_datas" );
		}


		public function handleEvents() {
			if (!$this->bLoggedIn) return parent::handleEvents();	// not logged in
			$this->refreshChildValuesFromArray();

			$submitted = false;
			if (isset( $this->oSubmit) && $this->oSubmit->isSubmitted()) $submitted = true;
			if (isset( $this->oAdminSubmit) && $this->oAdminSubmit->isSubmitted()) $submitted = true;
			if (!$submitted) return parent::handleEvents(); 

			
			// check validity
			$valid = true;

			if (!$this->checkPublicAddress()) $valid = false;
			if (!$this->checkLangsiteNames()) $valid = false;

			if (!$valid) {
				return parent::handleEvents();
			} 
			
			$this->saveDatas( isset( $this->oAdminSubmit ) && $this->oAdminSubmit->isSubmitted() );
		
			// log	
			$ulm = new UserLogModel();
			$ulm->logMemberActivity( USER_LOG_GENERAL | USER_GENERAL_MODIFIED_PROFILE , "változtatott profil adatain" );	// LANGFIX
			FNTH_refresh();
			return parent::handleEvents();
		}

		protected function handlePictureEvents() {
			if (isset( $this->aP["del_member_pic"] )) {
				// delete picture
				$member = $this->member_model->getMemberFromMemberId( $this->iMemberId );

				$p = array( "haspicture" => false );
				$this->member_model->updateMember( $this->iMemberId , $p );

				if (file_exists( $member["picurl"] )) unlink( $member["picurl"] );
				if (file_exists( $member["thumburl"] )) unlink( $member["thumburl"] );
			}

			if (isset( $_FILES["member_pic"]) && !($_FILES["member_pic"]["error"])) {
				$file = $_FILES["member_pic"];
				// update database	
				$p = array( "haspicture" => true );
				$this->member_model->updateMember( $this->iMemberId , $p );

				$member = $this->member_model->getMemberFromMemberId( $this->iMemberId );

				$this->member_model->createAttachmentPath( $this->iMemberId );
				// generate thumbnails
				$cmd = sprintf( "convert %s -geometry 600x600 %s" , 
					$file["tmp_name"],$member["picurl"] );
				shell_exec( $cmd );
				$cmd = sprintf( "convert %s -geometry 100x100 %s" , 
					$file["tmp_name"],$member["thumburl"] );
				shell_exec( $cmd );
			}
		}

		protected function loadDatas() {
			if (!$this->bLoggedIn) return;

			// contact information
			$member = $this->member_model->getMemberFromMemberid( $this->iMemberId );
			$this->setParam( "member" , $member );

			$this->oIsAddressPublic->setChecked( $member["is_address_visible"] );
			$this->oIsAddressPublic->setShowTitle( true );
			$this->oIsAddressPublic->setTitle( __("Is_address_public") );
			$this->oCountry->setValue( $member["country_id"] );
			$this->oCounty->setValue( $member["county_id"] );
			$this->oZipcode->setValue( $member["zip"] );
			$this->oCity->setValue( $member["city"] );
			$this->oAddress->setValue( $member["address"] );

			$this->oPhone->setValue( $member["phone"] );

			$this->oIsPhoneVisible->setChecked( $member["is_phone_visible"] );
			$this->oIsPhoneVisible->setShowTitle( true );
			$this->oIsPhoneVisible->setTitle( __("Is_phone_public") );

			$this->oFax->setValue( $member["fax"] );

			$this->oIsFaxVisible->setChecked( $member["is_fax_visible"] );
			$this->oIsFaxVisible->setShowTitle( true );
			$this->oIsFaxVisible->setTitle( __("Is_fax_public" ) );

			$this->oUrl->setValue( $member["url"] );
			if (isset( $member["msn"] )) $this->oMSN->setValue( $member["msn"] );
			if (isset( $member["skype"] )) $this->oSkype->setValue( $member["skype"] );
			if (isset( $member["facebook"] )) $this->oFacebook->setValue( $member["facebook"] );
			if (isset( $member["youtube"] )) $this->oYoutube->setValue( $member["youtube"] );

			// global checkboxes
			foreach ($this->aGlobalProps as $prop) {
				$prop_id = $this->member_model->getExtendedPropertyIdFromName( $prop );
				$val = $this->member_model->getMemberExtendedPropertyFromId( $this->iMemberId , $prop_id );
				$this->oGlobalProperties->setSubChecked( $prop , ($val=="on") );
			}
			
			// use_langsite
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "use_langsite" );
			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				if ($langiso=="hu") continue;
				$this->oLocalProperties[$langiso."_use_langsite"]->setChecked(
					($this->member_model->getMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , $langcode )=="on") );
			}
			
			// langsite_name
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "langsite_name" );
			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				if ($langiso=="hu") continue;
				$val=$this->member_model->getMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , $langcode );
				$this->oLocalProperties[$langiso."_langsite_name"]->setValue( $val );
				$maxlength = strlen( $val );
				if ($maxlength<35) $maxlength=35;
				$this->oLocalProperties[$langiso."_langsite_name"]->setAttribute( "maxlength" , $maxlength );
			}

			// services
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "services" );
			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				$this->oLocalProperties[$langiso."_services"]->setValue(
					$this->member_model->getMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , $langcode ) );
			}

			
			// references
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "references" );
			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				$this->oLocalProperties[$langiso."_references"]->setValue(
					$this->member_model->getMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , $langcode ) );
			}

			$this->oMemberAttachments->loadAttachments();
			
			if (AdminUserModel::isAdminLoggedIn()) {
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "zip" ))
					$this->oZipcode->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "city" ))
					$this->oCity->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "address" ))
					$this->oAddress->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "phone" ))
					$this->oPhone->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "fax" ))
					$this->oFax->addClass( "adminblue" );
				
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "url" ))
					$this->oUrl->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "msn" ))
					$this->oMSN->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "skype" ))
					$this->oSkype->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "facebook" ))
					$this->oFacebook->addClass( "adminblue" );
				if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , "youtube" ))
					$this->oYoutube->addClass( "adminblue" );


				// langsite_name
				$prop_id = $this->member_model->getExtendedPropertyIdFromName( "langsite_name" );
				foreach ($GLOBALS["languages"] as $langiso => $langcode) {
					if ($langiso=="hu") continue;
					$name = $langiso."_langsite_name";
					if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , $name ))
						$this->oLocalProperties[$name]->addClass( "adminblue" );
				}

				
				// services
				$prop_id = $this->member_model->getExtendedPropertyIdFromName( "services" );
				foreach ($GLOBALS["languages"] as $langiso => $langcode) {
					$name = $langiso."_services";
					if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , $name ))
						$this->oLocalProperties[$name]->addClass( "adminblue" );
				}

				// references
				$prop_id = $this->member_model->getExtendedPropertyIdFromName( "references" );
				foreach ($GLOBALS["languages"] as $langiso => $langcode) {
					$name = $langiso."_references";
					if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , $name ))
						$this->oLocalProperties[$name]->addClass( "adminblue" );
				}
			

			}
		}

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			if (!$this->bLoggedIn) return;
			$this->sPageName = "profile_datas";
			$this->oMyProfileMenu->setCurrentMenuItem( $this->sPageName );
		
			$this->generateContactWidgets();
			$this->generateGlobalCustomProperties();
			$this->generateLocalizedCustomProperties();
			$this->oMemberAttachments = new MemberAttachments( "member_attachments" , $this );

			$this->generateMemberWidgetNormalSubmit( $this->sPageName );
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "Empty_zipcode" );
			fo3_add_js_langdef( "Empty_city" );
			fo3_add_js_langdef( "Empty_address" );
			fo3_add_js_langdef( "Empty_county" );
			fo3_add_js_langdef( "Empty_country" );
			fo3_add_js_langdef( "No_langsite_name" );
		}


		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->loadDatas();
			$this->setParam( "tstamp" , fo3_time() );
			$this->generateLangDefs();
			fo3_form_change_warning( "document.frm_profile_parameters" );
			return parent::render();
		}
	}

?>
