<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/CommunityToolbar.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/models/MyUser.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Coupon.inc.php" );
	require_once( $GLOBALS["site_root"] . "/includes/urlgen/MyMember.inc.php" );

	class CouponActivatePane extends Widget {

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->oCommunityToolbar = new CommunityToolbar( "CommunityToolbar" , $this );
			$this->oCouponCode = new FrontendTextbox( "coupon_code" , $this );
			$this->oSubmit = new FrontendSubmit( "smb_activate_coupon" , $this );
			$this->oSubmit->setValue( __("coupons_Activate_coupon","attr") );

			$this->cm = new CouponModel();
		}

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

			$this->mum = new MyUserModel();
			$user_id = $this->mum->getLoggedInUserId();

			// valos kod?
			$cc = $this->oCouponCode->getValue();
			if (!$this->cm->isCouponTicketExists( $cc )) {
				$this->setParam( "error_message" , __("coupons_error_Invalid_coupon_code") );
				return parent::handleEvents();
			}

			// hasznalt kupon
			if ($this->cm->isCouponTicketUsed( $cc )) {
				$this->setParam( "error_message" , __("coupons_error_Coupon_already_used") );
				return parent::handleEvents();
			}

			// nem hasznalt, de a kampanybol mar van
			$coupon_campaign_id = $this->cm->getCouponCampaignIdFromTicket( $cc );
			if (!$coupon_campaign_id) {
				// baszdmeg ag	
				$this->setParam( "error_message" , __("coupons_error_Invalid_coupon_code") );
				return parent::handleEvents();
			}
			if ($this->cm->isUserUsedCouponTicketOnCampaign( $user_id , $coupon_campaign_id )) {
				$this->setParam( "error_message" , __("coupons_error_Coupon_campaign_already_activated") );
				return parent::handleEvents();
			}

			$coupon_campaign = $this->cm->getCouponCampaignFromId( $coupon_campaign_id );
			// esetleg meg nem aktiv
			if (isset( $coupon_campaign["cc_avail_from"] )) {
				if ($coupon_campaign["cc_avail_from"]>fo3_date("Y-m-d")) {
					$this->setParam( "error_message" , __("coupons_error_Coupon_not_active_yet") );
					return parent::handleEvents();
				}
			}
			if (isset( $coupon_campaign["cc_avail_until"] )) {
				if ($coupon_campaign["cc_avail_until"]<fo3_date("Y-m-d")) {
					$this->setParam( "error_message" , __("coupons_error_Coupon_code_expired") );
					return parent::handleEvents();
				}
			}
			$this->cm->activateCouponToUser( $user_id , $cc );
			$success = __("coupons_Coupon_successfully_activated" );
			switch ($coupon_campaign["cc_coupon_type"] ) {
			case "freedays":
				$success.=" ";
				$success.=sprintf( "%s %d %s",
					__("coupons_freedays_added_prefix"),
					$coupon_campaign["cc_freedays"],
					__("coupons_freedays_added_postfix" )
				);
			}
			$success.="<br/><br/>";
			$url = MyMemberURL::renderMyStatusURL();
			$success.=sprintf( "<a class=\"nrm\" href=\"%s\">%s &raquo;</a>",
				$url , __("coupons_Back_to_status_page","attr") );
			$this->setParam( "success" , $success );
			return parent::handleEvents();
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->oCommunityToolbar->setTitle( __("coupons_Coupon_activation") );
			return parent::render();
		}
	}

?>
