<?php
	require_once( $GLOBALS["site_root"]."/includes/pages/BackendPopup.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextarea.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminButton.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminDatetime.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSelect.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminUserLogTable.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminRadio.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminCheckbox.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/User.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/DirtyObject.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Utype.inc.php" );

	class AdminMemberEditPage extends BackendPopup {
		protected $reg_tstamp = null;
		protected $status_senttstamp = null;
		protected $paid_tstamp = null;
		protected $expires_tstamp = null;
		protected $npublic_comment = null;
		protected $member_username = null;
		protected $member_aliasname = null;
		protected $password = null;
		protected $utype = null;
		protected $use_email = null;
		protected $is_forditmail = null;
		protected $is_acknowledged = null;
		protected $is_admin_acknowledged = null;

		protected $user_submit;
		public function __construct( $id = "AdminMemberEditPage" , $parent = null ) {
			parent::__construct( $id , $parent );
			if (!AdminUserModel::isAdminLoggedIn()) return;

			$this->reg_tstamp = new AdminDatetime( "reg_tstamp" , $this );
			$this->status_senttstamp = new AdminDatetime( "status_senttstamp" , $this );
			$this->paid_now = new AdminButton( "paid_now" , $this );
			$this->paid_tstamp = new AdminDatetime( "paid_tstamp" , $this );
			$this->paid_method = new AdminSelect( "paid_method" , $this );
			$this->expires_tstamp = new AdminDatetime( "expires_tstamp" , $this );
			$this->npublic_comment = new AdminTextarea( "npublic_comment" , $this );
			$this->npublic_comment->setRows( 14 );
			$this->npublic_comment->setWidth( 600 );

			$this->member_id_text = new AdminTextbox( "member_id_text" , $this );
			$this->member_username = new AdminTextbox( "member_username" , $this );
			$this->member_aliasname = new AdminTextbox( "member_aliasname" , $this );
			// $this->email = new AdminTextbox( "email" , $this );
			$this->password = new AdminTextbox( "pwd" , $this );
			$this->utype = new AdminRadio( "utype" , $this );


			$this->is_forditmail = new AdminCheckbox( "is_forditmail" , $this );
			$this->is_acknowledged = new AdminCheckbox( "is_acknowledged" , $this );
			$this->is_admin_acknowledged = new AdminCheckbox( "is_admin_acknowledged" , $this );
			$this->is_instant_sender = new AdminCheckbox( "is_instant_sender" , $this );
			$this->is_forum_poster = new AdminCheckbox( "is_forum_poster" , $this );
			$this->is_feedback_poster = new AdminCheckbox( "is_feedback_poster" , $this );

			$this->user_submit = new AdminSubmit( "user_submit" , $this );
			
			$this->user_log = new AdminUserLogTable( "AdminUserLogTable" , $this );
		}

		public function modifyGetPost( &$g , &$p ) {
			$retval = parent::modifyGetPost( $g , $p );
			$olduser = false;
			if (isset( $this->aG["uid"] )) {
				$mm = new MemberModel();
				$member = $mm->getMemberFromMemberId( $this->aG["uid"] );
				if ($member["is_forditmail"]=="t") {
					$this->delete_mail = new AdminButton( "delete_mail" , $this );
					$this->delete_mail->setValue( __("Delete_forditmail") );
					$this->delete_mail->setAttribute( "onclick" , sprintf( "AdminDeleteForditMail( %d );" , $this->aG["uid"] ) );
					$this->use_email = new AdminRadio( "use_email" , $this );
				}
			}
			return $retval;
		}

		protected function initializeWidgets() {
			$this->reg_tstamp->setTitle( __("users_Created_at" ) );
			$this->reg_tstamp->setShowTitle( true );
			$this->reg_tstamp->setWidth( 120 );

			$this->status_senttstamp->setTitle( __("users_Status_sent_at") );
			$this->status_senttstamp->setShowTitle( true );
			$this->status_senttstamp->setWidth( 120 );

			$this->paid_now->setValue( __("Now","attr" ) . "»" );
			$this->paid_now->setAttribute( "onclick" , "AdminMemberUpdatePaid();" );

			$this->paid_tstamp->setTitle( __("users_Paid_at" ) );
			$this->paid_tstamp->setShowTitle( true );
			$this->paid_tstamp->setWidth( 120 );
			
			// $this->paid_method->setTitle( sprintf( "<span title=\"%s\">%s</span>" , __("users_Paid_with" ) ) );	// ezt most mar a loaddatasban toltjuk
			$this->paid_method->setShowTitle( true );
			$this->paid_method->setWidth( 120 );
			$this->paid_method->addOption( "" , "---" );
			$this->paid_method->addOption( PM_DIRECT_DEBIT , __("payment_directdebit","attr") );
			$this->paid_method->addOption( PM_CREDIT_CARD , __("payment_creditcard","attr") );
			$this->paid_method->addOption( PM_POSTAL_CHEQUE , __("payment_postalcheque","attr") );
			$this->paid_method->addOption( PM_COUPON , __("payment_coupon","attr") );
		

			$this->expires_tstamp->setTitle( __("users_Expires_at" ) );
			$this->expires_tstamp->setShowTitle( true );
			$this->expires_tstamp->setWidth( 120 );

			$this->npublic_comment->setTitle( __("users_Notpublic_comment" ) );
			$this->npublic_comment->setShowTitle( true );

			$this->member_id_text->setTitle( "Ügyfélszám" );
			$this->member_id_text->setShowTitle( true );
			$this->member_id_text->setWidth( 60 );

			$this->member_aliasname->setTitle( __( "members_Aliasname" ) );
			$this->member_aliasname->setShowTitle( true );
			$this->member_aliasname->setWidth( 200 );

			$this->member_username->setTitle( __( "users_Username" ) );
			$this->member_username->setShowTitle( true );
			$this->member_username->setWidth( 100 );
			
		
			/*	
			$this->email->setTitle( __( "users_Email" ) );
			$this->email->setShowTitle( true );
			*/

			$this->password->setTitle( __( "users_Password" ) );
			$this->password->setShowTitle( true );
			$this->password->setWidth( 100 );

			$utm = new UtypeModel();
			$this->utype->setVertical( true );
			$this->utype->addOptions( $utm->getUtypeOptions( false ) );

			if (isset( $this->use_email)) {
				$this->use_email->setVertical( true );
				$opts = array( "fo" , "own" , "both" );
				foreach ($opts as $o) $this->use_email->addOption( $o , __("use_email_".$o) );
			}
			
			$this->is_forditmail->setTitle( "ForditMAIL" );
			$this->is_forditmail->setShowTitle( true );

			$this->is_acknowledged->setTitle( __("users_Acknowledged" ) );
			$this->is_acknowledged->setShowTitle( true );
			
			$this->is_admin_acknowledged->setTitle( __("users_Admin_acknowledged" ) );
			$this->is_admin_acknowledged->setShowTitle( true );

			$this->is_instant_sender->setTitle( __("users_Instant_sender") );
			$this->is_instant_sender->setShowTitle( true );

			$this->is_forum_poster->setTitle( __("members_Forum_poster" ) );
			$this->is_forum_poster->setShowTitle( true );

			$this->is_feedback_poster->setTitle( __("members_Feedback_poster" ) );
			$this->is_feedback_poster->setShowTitle( true );

			switch ($this->aG["mode"]) {
			case "new":
				$this->user_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("members_New_member") );
				break;
			case "modify":
				$this->user_submit->setValue( __( "Modify","attr" ) );
				$this->setParam( "title" , __("members_Modify_member" ) );
				break;
			case "delete":
				$this->user_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("members_Delete_member" ) );
				break;
			}
			FNTHPage::setPageTitle( $this->getParam( "title" ) );
			switch ($this->aG["mode"]) {
			case "modify":
			case "delete":
				$this->loadDatas( $this->aG["uid"] );
			}
		}

		protected function loadDatas( $uid ) {
			$um = new UserModel();
			$mm = new MemberModel();

			$member = $mm->getMemberFromMemberId( $uid );
			$user = $um->getUserFromUserId( $member["user_id"] );

			$this->setParam( "user" , $user );
			$this->setParam( "member" , $member );

			$this->member_id_text->setValue( "F" . $member["member_id"] );
			$this->setParam( "title" , $this->getParam( "title" ) . " - " . $user["user_name"] );
			FNTHPage::setPageTitle( $this->getParam( "title" ) );
		
			$this->paid_method->setTitle( sprintf( "<span style=\"cursor: pointer;\" title=\"%s\">%s</span>" ,
				__("registration_Payment_information") . ": " . __("payment_".$member["preferred_paymode"] ),
				__("users_Paid_with" ) )
			);

			$this->reg_tstamp->setValue( substr( $user["reg_tstamp"] , 0 , 19 ) );
			$this->status_senttstamp->setValue( substr( $member["status_senttstamp"] , 0 , 19 ) );
			$this->paid_tstamp->setValue( substr( $member["paid_tstamp"] , 0 , 19 ) );
			if (isset( $member["paid_method"] ))
				$this->paid_method->setValue( $member["paid_method"] );
			$this->expires_tstamp->setValue( substr( $member["expires_tstamp"] , 0 , 19 ) );
			$this->npublic_comment->setValue( $member["npublic_comment"] );

			$this->member_username->setValue( $member["member_username"] );
			$this->member_aliasname->setValue( $member["member_aliasname"] );
			// $this->email->setValue( $user["email"] );
			$this->password->setValue( $user["pwd"] );
			$this->password->setAttribute( "readonly" , "readonly" );

			$this->utype->setValue( $member["member_type"] );

			if (isset( $this->use_email))
				$this->use_email->setValue( $member["use_email"] );

			$this->is_forditmail->setChecked( ($member["is_forditmail"]=="t") );
			$this->is_acknowledged->setChecked( ($user["is_acknowledged"]=="t") );
			$this->is_admin_acknowledged->setChecked( ($user["is_admin_acknowledged"]=="t") );

			$this->is_instant_sender->setChecked( ($user["is_instant_sender"]=="t") );
			$this->is_forum_poster->setChecked( ($member["is_forum_poster"]=="t") );
			$this->is_feedback_poster->setChecked( ($member["is_feedback_poster"]=="t") );

			$this->user_log->setUserId( $member["user_id"] );
			$this->user_log->setHeight( 140 );
		}

		protected function saveDatas( $uid ) {
			$this->refreshChildValuesFromArray();

			$mm = new MemberModel();
			$um = new UserModel();

			$member = $mm->getMemberFromMemberId( $uid );
			$user = $um->getUserFromUserId( $member["user_id"] );

			$p = array();
			$p["paid_tstamp"] = $this->paid_tstamp->getValue();
			$p["paid_method"] = $this->paid_method->getValue();

			$p["status_senttstamp"] = $this->status_senttstamp->getValue();
			$p["expires_tstamp"] = $this->expires_tstamp->getValue();
			$p["npublic_comment"] = $this->npublic_comment->getValue();
			$p["member_type"]=$this->utype->getValue();

			$p["member_username"]=$this->member_username->getValue();
			$p["member_aliasname"]=$this->member_aliasname->getValue();

			$p["is_forum_poster"]=$this->is_forum_poster->isChecked();
			$p["is_feedback_poster"]=$this->is_feedback_poster->isChecked();
			$p["is_forditmail"]=$this->is_forditmail->isChecked();

			if (isset( $this->use_email ))
				$p["use_email"]=$this->use_email->getValue();

			$mm->updateMember( $uid , $p );


			$p = array();
			$p["is_acknowledged"]=$this->is_acknowledged->isChecked();
			$p["is_admin_acknowledged"]=$this->is_admin_acknowledged->isChecked();
			$p["is_instant_sender"]=$this->is_instant_sender->isChecked();
			$p["reg_tstamp"]=$this->reg_tstamp->getValue();

			$um->updateUser( $user["user_id"] , $p );

			
		}
		
		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (isset( $this->aP["inajax"] ) && isset( $this->aP["ajaxevent"]) ) {
				switch ($this->aP["ajaxevent"]) {
				case "delete_forditmail":
					$mm = new MemberModel();
					$mm->deleteForditMail( $this->aP["member_id"] );
					FNTH_direct_echo( "document.location.href=document.location.href;" );
					fo3_exit();
				}
			}
			if (!isset( $this->aP[$this->user_submit->getId()] )) return parent::handleEvents();
			switch ($this->aG["mode"]) {
			// case "new":$this->saveDatas( 0 );break;
			case "modify":$this->saveDatas( $this->aG["uid"] );break;
			case "delete":$this->deleteDatas( $this->aG["uid"] );break;
			}
			$this->refreshWindowAndParent();
		}

		protected function getDirtyMenu() {
			$mym = new MyMemberModel();
			$member_id = $this->aG["uid"];
			$mm = new MemberModel();
			$member = $mm->getMemberFromMemberId( $member_id );
			$user_id = $member["user_id"];

			$dom = new DirtyObjectModel();
			$menu = $mym->getMenu( true );
			$newmenu = array();
			foreach ($menu as $menuitem) {
				$menuitem["admin_dirty"]=$dom->isDirtyObject( $user_id , $menuitem["name"] );
				$newmenu[]=$menuitem;
			}
			return $newmenu;
		}

		public function render() {
			if (!$this->isLoggedIn()) return parent::render();
			$this->initializeWidgets();
			if (isset( $this->aG["uid"] )) {
				$this->setParam( "member_id" , $this->aG["uid"] );
				$this->setParam( "membermenu" , $this->getDirtyMenu() );
			}
			return parent::render();
		}

	}

?>
