<?php

/**
 * This Gateway is slightly special, because as well as providing functions that need to be activated, there are offline payment functions that are always there e.g. adding manual payments.
 * @author marcus
 */
class EM_Gateway_Offline extends EM_Gateway {

	var $gateway                    = 'offline';
	var $title                      = 'Offline';
	var $status                     = 5;
	var $button_enabled             = true;
	var $count_pending_spaces       = true;
	var $supports_multiple_bookings = true;

	/**
	 * Sets up gateway and registers actions/filters
	 */
	function __construct() {
		parent::__construct();
		add_action( 'init', array( &$this, 'actions' ), 10 );
		//Booking Interception
		add_filter( 'em_booking_set_status', array( &$this, 'em_booking_set_status' ), 1, 2 );
		add_filter( 'em_bookings_pending_count', array( &$this, 'em_bookings_pending_count' ), 1, 1 );
		add_filter( 'em_bookings_table_booking_actions_5', array( &$this, 'bookings_table_actions' ), 1, 2 );
		add_filter( 'em_wp_localize_script', array( &$this, 'em_wp_localize_script' ), 1, 1 );
		add_action( 'em_bookings_single_metabox_footer', array( &$this, 'add_payment_form' ), 1, 1 ); //add payment to booking
	}

	/**
	 * Run on init, actions that need taking regarding offline bookings are caught here, e.g. registering manual bookings and adding payments
	 */
	function actions() {
		global $EM_Notices, $EM_Booking;
		//Check if manual payment has been added
		if ( ! empty( $_REQUEST['booking_id'] ) && ! empty( $_REQUEST['action'] ) && ! empty( $_REQUEST['_wpnonce'] ) ) {
			$EM_Booking = em_get_booking( $_REQUEST['booking_id'] );
			if ( $_REQUEST['action'] == 'gateway_add_payment' && is_object( $EM_Booking ) && $EM_Booking->can_manage( 'manage_bookings', 'manage_others_bookings' ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'gateway_add_payment' ) ) {
				if ( ! empty( $_REQUEST['transaction_total_amount'] ) && is_numeric( $_REQUEST['transaction_total_amount'] ) ) {
					$this->record_transaction( $EM_Booking, $_REQUEST['transaction_total_amount'], get_option( 'dbem_bookings_currency' ), current_time( 'mysql' ), '', 'Completed', $_REQUEST['transaction_note'] );
					$string = __( 'Payment has been registered.', 'em-pro' );
					$total  = $EM_Booking->get_total_paid();
					if ( $total >= $EM_Booking->get_price() ) {
						$EM_Booking->approve();
						$string .= ' ' . __( 'Booking is now fully paid and confirmed.', 'em-pro' );
					}
					$EM_Notices->add_confirm( $string, true );
					do_action( 'em_payment_processed', $EM_Booking, $this );
					wp_redirect( em_wp_get_referer() );
					exit();
				} else {
					$EM_Notices->add_error( __( 'Please enter a valid payment amount. Numbers only, use negative number to credit a booking.', 'em-pro' ) );
					unset( $_REQUEST['action'] );
					unset( $_POST['action'] );
				}
			}
		}
	}

	function em_wp_localize_script( $vars ) {
		if ( is_user_logged_in() && get_option( 'dbem_rsvp_enabled' ) ) {
			$vars['offline_confirm'] = __( 'Be aware that by approving a booking awaiting payment, a full payment transaction will be registered against this booking, meaning that it will be considered as paid.', 'em-pro' );
		}
		return $vars;
	}

	/*
	 * --------------------------------------------------
	 * Booking Interception - functions that modify booking object behaviour
	 * --------------------------------------------------
	 */


	/**
	 * Intercepts return JSON and adjust feedback messages when booking with this gateway.
	 * @param array $return
	 * @param EM_Booking $EM_Booking
	 * @return array
	 */
	function booking_form_feedback( $return, $EM_Booking = false ) {
		if ( ! empty( $return['result'] ) && ! empty( $EM_Booking->booking_meta['gateway'] ) && ! empty( $EM_Booking->booking_status ) ) { //check emtpies
			if ( $EM_Booking->booking_status == 5 && $this->uses_gateway( $EM_Booking ) ) { //check values
				$return['message'] = get_option( 'em_' . $this->gateway . '_booking_feedback' );
				if ( ! empty( $EM_Booking->email_not_sent ) ) {
					$return['message'] .= ' ' . get_option( 'dbem_booking_feedback_nomail' );
				}
				return apply_filters( 'em_gateway_offline_booking_add', $return, $EM_Booking->get_event(), $EM_Booking );
			}
		}
		return $return;
	}

	/**
	 * Sets booking status and records a full payment transaction if new status is from pending payment to completed.
	 * @param int $status
	 * @param EM_Booking $EM_Booking
	 */
	function em_booking_set_status( $result, $EM_Booking ) {
		if ( $EM_Booking->booking_status == 1 && $EM_Booking->previous_status == $this->status && $this->uses_gateway( $EM_Booking ) && ( empty( $_REQUEST['action'] ) || ! in_array( $_REQUEST['action'], array( 'gateway_add_payment' ) ) ) ) {
			$this->record_transaction( $EM_Booking, $EM_Booking->get_price( false, false, true ), get_option( 'dbem_bookings_currency' ), current_time( 'mysql' ), '', 'Completed', '' );
		}
		return $result;
	}

	function em_bookings_pending_count( $count ) {
		return $count + EM_Bookings::count( array( 'status' => '5' ) );
	}

	/*
	 * --------------------------------------------------
	 * Booking UI - modifications to booking pages and tables containing offline bookings
	 * --------------------------------------------------
	 */

	/**
	 * Outputs extra custom information, e.g. payment details or procedure, which is displayed when this gateway is selected when booking (not when using Quick Pay Buttons)
	 */
	function booking_form() {
		echo get_option( 'em_' . $this->gateway . '_form' );
	}

	/**
	 * Adds relevant actions to booking shown in the bookings table
	 * @param EM_Booking $EM_Booking
	 */
	function bookings_table_actions( $actions, $EM_Booking ) {
		return array(
			'approve' => '<a class="em-bookings-approve em-bookings-approve-offline" href="' . em_add_get_params(
				$_SERVER['REQUEST_URI'],
				array(
					'action'     => 'bookings_approve',
					'booking_id' => $EM_Booking->booking_id,
				)
			) . '">' . esc_html__emp( 'Approve', 'events-manager' ) . '</a>',
			'reject'  => '<a class="em-bookings-reject" href="' . em_add_get_params(
				$_SERVER['REQUEST_URI'],
				array(
					'action'     => 'bookings_reject',
					'booking_id' => $EM_Booking->booking_id,
				)
			) . '">' . esc_html__emp( 'Reject', 'events-manager' ) . '</a>',
			'delete'  => '<span class="trash"><a class="em-bookings-delete" href="' . em_add_get_params(
				$_SERVER['REQUEST_URI'],
				array(
					'action'     => 'bookings_delete',
					'booking_id' => $EM_Booking->booking_id,
				)
			) . '">' . esc_html__emp( 'Delete', 'events-manager' ) . '</a></span>',
			'edit'    => '<a class="em-bookings-edit" href="' . em_add_get_params(
				$EM_Booking->get_event()->get_bookings_url(),
				array(
					'booking_id' => $EM_Booking->booking_id,
					'em_ajax'    => null,
					'em_obj'     => null,
				)
			) . '">' . esc_html__emp( 'Edit/View', 'events-manager' ) . '</a>',
		);
	}

	/**
	 * Adds a payment form which can be used to submit full or partial offline payments for a booking.
	 */
	function add_payment_form() {
		?>
		<div id="em-gateway-payment" class="stuffbox">
			<h3>
				<?php _e( 'Add Offline Payment', 'em-pro' ); ?>
			</h3>
			<div class="inside">
				<div>
					<form method="post" action="" style="padding:5px;">
						<table class="form-table">
							<tbody>
								<tr valign="top">
									<th scope="row"><?php _e( 'Amount', 'em-pro' ); ?></th>
										<td><input type="text" name="transaction_total_amount" value="
										<?php
										if ( ! empty( $_REQUEST['transaction_total_amount'] ) ) {
											echo esc_attr( $_REQUEST['transaction_total_amount'] );}
										?>
										" />
										<br />
										<em><?php _e( 'Please enter a valid payment amount (e.g. 10.00). Use negative numbers to credit a booking.', 'em-pro' ); ?></em>
									</td>
								</tr>
								<tr valign="top">
									<th scope="row"><?php _e( 'Comments', 'em-pro' ); ?></th>
									<td>
										<textarea name="transaction_note">
										<?php
										if ( ! empty( $_REQUEST['transaction_note'] ) ) {
											echo esc_attr( $_REQUEST['transaction_note'] );}
										?>
										</textarea>
									</td>
								</tr>
							</tbody>
						</table>
						<input type="hidden" name="action" value="gateway_add_payment" />
						<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'gateway_add_payment' ); ?>" />
						<input type="hidden" name="redirect_to" value="<?php echo ( ! empty( $_REQUEST['redirect_to'] ) ) ? $_REQUEST['redirect_to'] : em_wp_get_referer(); ?>" />
						<input type="submit" class="
						<?php
						if ( is_admin() ) {
							echo 'button-primary';}
						?>
						" value="<?php _e( 'Add Offline Payment', 'em-pro' ); ?>" />
					</form>
				</div>					
			</div>
		</div> 
		<?php
	}

	/*
	 * --------------------------------------------------
	 * Settings pages and functions
	 * --------------------------------------------------
	 */

	/**
	 * Outputs custom offline setting fields in the settings page
	 */
	function mysettings() {

		global $EM_options;
		?>
		<table class="form-table">
		<tbody>
			<?php em_options_input_text( esc_html__( 'Success Message', 'em-pro' ), 'em_' . $this->gateway . '_booking_feedback', esc_html__( 'The message that is shown to a user when a booking with offline payments is successful.', 'em-pro' ) ); ?>
		</tbody>
		</table>
		<?php
	}

	/*
	 * Run when saving PayPal settings, saves the settings available in EM_Gateway_Paypal::mysettings()
	 */
	function update() {
		$gateway_options = array( 'em_' . $this->gateway . '_booking_feedback' );
		foreach ( $gateway_options as $option_wpkses ) {
			add_filter( 'gateway_update_' . $option_wpkses, 'wp_kses_post' );
		}
		return parent::update( $gateway_options );
	}

	/**
	 * Checks an EM_Booking object and returns whether or not this gateway is/was used in the booking.
	 * @param EM_Booking $EM_Booking
	 * @return boolean
	 */
	function uses_gateway( $EM_Booking ) {
		//for all intents and purposes, if there's no gateway assigned but this booking status matches, we assume it's offline
		return parent::uses_gateway( $EM_Booking ) || ( empty( $EM_Booking->booking_meta['gateway'] ) && $EM_Booking->booking_status == $this->status );
	}
}
EM_Gateways::register_gateway( 'offline', 'EM_Gateway_Offline' );
?>
