<?php
namespace SabaiApps\Directories\Component\Membership\FrontendSubmitRestrictor;

use SabaiApps\Directories\Component\FrontendSubmit\Restrictor\AbstractRestrictor;

class MembershipFrontendSubmitRestrictor extends AbstractRestrictor {

	protected function _frontendsubmitRestrictorInfo() {
		return array(
			'label' => __( 'Restrict by user membership', 'directories-pro' ),
		);
	}

	public function frontendsubmitRestrictorEnabled() {
		return $this->_application->getComponent( 'Membership' )->isPaidMembershipEnabled();
	}

	protected function _getRestrictionGroups() {
		$plans       = array();
		$user_bundle = $this->_application->getComponent( 'User' )->userDirectoryExists();
		foreach ( $this->_application->Payment_Plans( $user_bundle->name, array( 'subscription', 'base' ) ) as $plan_id => $plan ) {
			$plans[ $plan_id ] = $plan->paymentPlanTitle();
		}
		$plans[0] = '— ' . __( 'No membership', 'directories-pro' ) . ' —';
		return $plans;
	}

	protected function _getRestrictionGroupsByUser( $userId, $settings ) {
		if ( empty( $userId )
			|| ( ! $entity = $this->_application->Entity_Entity( 'user', $userId ) )
			|| ( ! $plan_id = $entity->getSingleFieldValue( 'payment_plan', 'plan_id' ) )
		) {
			return array( 0 ); // Anonymous or no membership
		}

		return array( $plan_id );
	}
}
