<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class User_VerificationController extends Core_Controller_Action_Standard {

  /**
   * @var User_Model_User
   */
  protected $_user;

  /**
   * @var Zend_Session_Namespace
   */
  protected $_session;

  /**
   * @var Payment_Model_Order
   */
  protected $_order;

  /**
   * @var Payment_Model_Gateway
   */
  protected $_gateway;

  public function init() {
    
    // If there are no enabled gateways or packages, disable
    if( Engine_Api::_()->getDbtable('gateways', 'payment')->getEnabledGatewayCount() <= 0 ) {
      return $this->_helper->redirector->gotoRoute(array(), 'default', true);
    }
    
    // Get user and session
    $this->_user = $user = Engine_Api::_()->user()->getViewer();
    $this->_session = new Zend_Session_Namespace('Payment_Verification');
    $this->_session->gateway_id = $this->_getParam('gateway_id', 3000);
    $this->_session->user_id = $user_id = $this->_getParam('user_id', 0);

		$this->_session->using_wallet = $using_wallet = $this->_getParam('using_wallet', false);

    // Check viewer and user
    if( !$user || !$user->getIdentity() ) {
      if( !empty($this->_session->user_id) ) {
        $this->_user = $user = Engine_Api::_()->getItem('user', $this->_session->user_id);
      }
      // If no user, redirect to home?
      if( !$user || !$user->getIdentity() ) {
        $this->_session->unsetAll();
				if($using_wallet) {
					echo json_encode(array('status' => false, 'message' => $this->view->translate("Invalid Member.")));die;
				} else {
        	return $this->_helper->redirector->gotoRoute(array(), 'default', true);
				}
      }
    }
		
    $this->_session->user_id = $user->getIdentity();
  }

  public function indexAction() {
    return $this->_forward('gateway');
  }
  
  public function completeAction() {
  
    // Get gateway
		$user = $this->_user;
    $gatewayId = $this->_getParam('gateway_id', $this->_session->gateway_id);
		$user_id = $this->_getParam('user_id', $this->_session->user_id);
		$this->_session->using_wallet = $using_wallet = $this->_getParam('using_wallet', false);
		
		if (!$gatewayId) {
      echo json_encode(array('status' => false, 'message' => $this->view->translate("Invalid Method.")));die;
    }
    
    $verificationpackage = Engine_Api::_()->getDbTable('verificationpackages', 'payment')->getPackage(array('level_id' => $user->level_id));
    if($verificationpackage->price > $user->wallet_amount) { 
      $message = $this->view->translate("You don't have enough balance for verification subscription, please first recharge your ") . '<a target="_blank" href="'.$this->view->url(array("module" => 'payment', 'controller' => 'settings', 'action' => 'wallet'), 'default', true).'">'.$this->view->translate(" wallet").'.</a>';
      echo json_encode(array('status' => false, 'message' => $message));die;
    }
		
    //Process
    $ordersTable = Engine_Api::_()->getDbTable('orders', 'payment');
    if (!empty($this->_session->order_id)) {
      $previousOrder = $ordersTable->find($this->_session->order_id)->current();
      if ($previousOrder && $previousOrder->state == 'pending') {
        $previousOrder->state = 'incomplete';
        $previousOrder->save();
      }
    }
    
    // $recurrence = Engine_Api::_()->authorization()->getPermission($this->_user, 'user', 'recurrence');
    // $price = Engine_Api::_()->authorization()->getPermission($this->_user, 'user', 'price_verified');

    // //Order table for verification
    // $verificationsTable = Engine_Api::_()->getDbTable('verifications', 'payment');
    // $db = $verificationsTable->getAdapter();
    // $db->beginTransaction();
    // try {
    //   $verifications = $verificationsTable->createRow();
    //   $verifications->user_id = $this->_user->getIdentity();
    //   $verifications->params = json_encode(array('recurrence' => json_decode($recurrence), 'price' => $price));
    //   $verifications->save();
    //   // Commit
    //   $db->commit();
    //   $verificationsId = $verifications->getIdentity();
    // } catch (Exception $e) {
    //   $db->rollBack();
    //   throw $e;
    // }
    
    // Insert the new temporary subscription
    $subscriptionsTable = Engine_Api::_()->getDbtable('subscriptions', 'payment');
    $db = $subscriptionsTable->getAdapter();
    $db->beginTransaction();

    try {
      $subscription = $subscriptionsTable->createRow();
      $subscription->setFromArray(array(
        'package_id' => $verificationpackage->getIdentity(),
        'user_id' => $user->getIdentity(),
        'status' => 'initial',
        'active' => false, // Will set to active on payment success
        'creation_date' => new Zend_Db_Expr('NOW()'),
        'gateway_id' => $gatewayId,
				'resource_id' => $verificationpackage->getIdentity(),
        'resource_type' => $verificationpackage->getType(),
      ));
      $subscription->save();

      // If the package is free, let's set it active now and cancel the other
//       if( $package->isFree() ) {
//         $subscription->setActive(true);
//         $subscription->onPaymentSuccess();
//         if( $currentSubscription ) {
//           $currentSubscription->cancel();
//         }
//       }

      $subscription_id = $subscription->subscription_id;

      $db->commit();
    } catch( Exception $e ) {
      //$db->rollBack();
      //throw $e;
      echo json_encode(array('status' => false, 'message' => $this->view->translate("Invalid Method.")));die;
    }
    $this->_session->subscription_id = $subscription_id;

		// Create order
    $ordersTable->insert(array(
			'user_id' => $user->getIdentity(),
			'gateway_id' => $gatewayId, //$gateway->gateway_id,
			'state' => 'pending',
			'creation_date' => new Zend_Db_Expr('NOW()'),
			'source_type' => $verificationpackage->getType(),
			'source_id' => $verificationpackage->getIdentity(),
    ));
    $this->_session->order_id = $order_id = $ordersTable->getAdapter()->lastInsertId();
    
    // Unset certain keys
    unset($this->_session->gateway_id);
    
//     // Get gateway plugin
//     $this->view->gatewayPlugin = $gatewayPlugin = $gateway->getGateway();
//     $plugin = $gateway->getPlugin();

    // Prepare host info
//     $schema = _ENGINE_SSL ? 'https://' : 'http://';
//     $host = $_SERVER['HTTP_HOST'];

    // Prepare transaction
    $params = array();
    $params['language'] = $user->language;
    $localeParts = explode('_', $user->language);
    if( engine_count($localeParts) > 1 ) {
      $params['region'] = $localeParts[1];
    }
    $params['vendor_order_id'] = $order_id;
		$params['price'] = $verificationpackage->price;
    $params['recurrence'] = $verificationpackage->recurrence;
    
//     $this->view->returnUrl = $params['return_url'] = $schema . $host
//       . $this->view->url(array('action' => 'return'))
//       . '?order_id=' . $order_id
//       //. '?gateway_id=' . $this->_gateway->gateway_id
//       //. '&subscription_id=' . $this->_subscription->subscription_id
//       . '&state=' . 'return';
//     $params['cancel_url'] = $schema . $host
//       . $this->view->url(array('action' => 'return'))
//       . '?order_id=' . $order_id
//       //. '?gateway_id=' . $this->_gateway->gateway_id
//       //. '&subscription_id=' . $this->_subscription->subscription_id
//       . '&state=' . 'cancel';
//     $params['ipn_url'] = $schema . $host
//       . $this->view->url(array('action' => 'index', 'controller' => 'ipn'))
//       . '?order_id=' . $order_id;
//       //. '?gateway_id=' . $this->_gateway->gateway_id
//       //. '&subscription_id=' . $this->_subscription->subscription_id;
  
    $params['subscription_id'] = $subscription_id;

    // Get order
    if( !$user ||
        !($orderId = $this->_getParam('order_id', $this->_session->order_id)) ||
        !($order = Engine_Api::_()->getItem('payment_order', $orderId)) ||
        $order->user_id != $user->getIdentity() ||
        $order->source_type != 'payment_verificationpackage' ||
        !($verificationpackage = $order->getSource()) ) {
			echo json_encode(array('status' => false, 'message' => $this->view->translate("Invalid Method.")));die;
    }

    //$this->_subscription = $subscription;
    // Get gateway plugin
//     $this->view->gatewayPlugin = $gatewayPlugin = $gateway->getGateway();
//     $plugin = $gateway->getPlugin();

    // Process return
    unset($this->_session->errorMessage);
    try {
      $status = $verificationpackage->onSubscriptionTransactionReturn($order, $params);
      
      if(($status == 'active' || $status == 'free')) {
        $admins = Engine_Api::_()->user()->getSuperAdmins();
        $user = Engine_Api::_()->getItem('user', $order->user_id);
        
        $adminLink = 'https://' . $_SERVER['HTTP_HOST'] . Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module' => 'payment', 'controller' => 'index', 'action' => 'index'), 'admin_default', true);
        
        foreach($admins as $admin){
          Engine_Api::_()->getApi('mail', 'core')->sendSystem($admin,'payment_manual_verification', array(
            'payment_method' => $gateway->title,
            'sender_name' => $user->getTitle(),
            'admin_link' => $adminLink,
          ));
        }
      }
    } catch( Payment_Model_Exception $e ) {
      $status = 'failure';
      $this->_session->errorMessage = $e->getMessage();
			echo json_encode(array('status' => false, 'message' => $this->view->translate("Payment Failed.")));die;
    }

    return $this->_finishPayment($status, $using_wallet);
    

		/*    
    // Process transaction
    $transaction = $plugin->createVerificationTransaction($this->_user, $params);

    // Pull transaction params
    $this->view->transactionUrl = $transactionUrl = $gatewayPlugin->getGatewayUrl();
    $this->view->transactionMethod = $transactionMethod = $gatewayPlugin->getGatewayMethod();
    $this->view->transactionData = $transactionData = $transaction->getData();

    // Handle redirection
    if( $transactionMethod == 'GET' ) {
      $transactionUrl .= '?' . http_build_query($transactionData);
      return $this->_helper->redirector->gotoUrl($transactionUrl, array('prependBase' => false));
    }*/

    // Post will be handled by the view script
		
		
  }

  public function processAction() {
  
    // Get gateway
    $gatewayId = $this->_getParam('gateway_id', $this->_session->gateway_id);
		$user = $this->_user;
		$user_id = $this->_getParam('user_id', $this->_session->user_id);

    if (!$gatewayId || !($gateway = Engine_Api::_()->getDbtable('verificationgateways', 'payment')->find($gatewayId)->current()) || !($gateway->enabled)) {
      return $this->_helper->redirector->gotoRoute(array('action' => 'gateway'));
    }
    $this->view->gateway = $gateway;

    //Process
    // Create order
    $ordersTable = Engine_Api::_()->getDbtable('orders', 'payment');
    if (!empty($this->_session->order_id)) {
      $previousOrder = $ordersTable->find($this->_session->order_id)->current();
      if ($previousOrder && $previousOrder->state == 'pending') {
        $previousOrder->state = 'incomplete';
        $previousOrder->save();
      }
    }
    
		
		$verificationpackage = Engine_Api::_()->getDbTable('verificationpackages', 'payment')->getPackage(array('level_id' => $user->level_id));
		$price = $verificationpackage->price;
    $recurrence = $verificationpackage->recurrence;

    // //Order table for verification
    // $verificationsTable = Engine_Api::_()->getDbTable('verifications', 'payment');
    // $db = $verificationsTable->getAdapter();
    // $db->beginTransaction();
    // try {
    //   $verifications = $verificationsTable->createRow();
    //   $verifications->user_id = $this->_user->getIdentity();
    //   $verifications->params = json_encode(array('recurrence' => json_decode($recurrence), 'price' => $price));
    //   $verifications->save();
    //   // Commit
    //   $db->commit();
    //   $verificationsId = $verifications->getIdentity();
    // } catch (Exception $e) {
    //   $db->rollBack();
    //   throw $e;
    // }

    // Insert the new temporary subscription
    $subscriptionsTable = Engine_Api::_()->getDbtable('subscriptions', 'payment');
    $db = $subscriptionsTable->getAdapter();
    $db->beginTransaction();

    try {
      $subscription = $subscriptionsTable->createRow();
      $subscription->setFromArray(array(
        'package_id' => $verificationpackage->getIdentity(),
        'user_id' => $user->getIdentity(),
        'status' => 'initial',
        'active' => false, // Will set to active on payment success
        'creation_date' => new Zend_Db_Expr('NOW()'),
        'gateway_id' => $gatewayId,
				'resource_id' => $verificationpackage->getIdentity(),
        'resource_type' => $verificationpackage->getType(),
      ));
      $subscription->save();

      // If the package is free, let's set it active now and cancel the other
//       if( $package->isFree() ) {
//         $subscription->setActive(true);
//         $subscription->onPaymentSuccess();
//         if( $currentSubscription ) {
//           $currentSubscription->cancel();
//         }
//       }

      $subscription_id = $subscription->subscription_id;

      $db->commit();
    } catch( Exception $e ) {
      $db->rollBack();
      throw $e;
      //echo json_encode(array('status' => false, 'message' => $this->view->translate("Invalid Method.")));die;
    }
    $this->_session->subscription_id = $subscription_id;

		// Create order
    $ordersTable->insert(array(
			'user_id' => $user->getIdentity(),
			'gateway_id' => $gatewayId, //$gateway->gateway_id,
			'state' => 'pending',
			'creation_date' => new Zend_Db_Expr('NOW()'),
			'source_type' => $verificationpackage->getType(),
			'source_id' => $verificationpackage->getIdentity(),
			'gateway_type' => 'payment_verificationgateway',
    ));
    $this->_session->order_id = $order_id = $ordersTable->getAdapter()->lastInsertId();
    
    // Unset certain keys
    unset($this->_session->gateway_id);
    
    // Get gateway plugin
    $this->view->gatewayPlugin = $gatewayPlugin = $gateway->getGateway();
    $plugin = $gateway->getPlugin();

    // Prepare host info
    $schema = _ENGINE_SSL ? 'https://' : 'http://';
    $host = $_SERVER['HTTP_HOST'];

    // Prepare transaction
    $params = array();
    $params['language'] = $this->_user->language;
    $localeParts = explode('_', $this->_user->language);
    if( engine_count($localeParts) > 1 ) {
      $params['region'] = $localeParts[1];
    }
    $params['vendor_order_id'] = $order_id;

		$action = 'index';
		if($gateway->plugin == "Payment_Plugin_Gateway_Stripe") {
			$action = 'stripe';
		}

    $this->view->returnUrl = $params['return_url'] = $schema . $host
      . $this->view->url(array('action' => 'return'))
      . '?order_id=' . $order_id
      //. '?gateway_id=' . $this->_gateway->gateway_id
      //. '&subscription_id=' . $this->_subscription->subscription_id
      . '&state=' . 'return';
    $params['cancel_url'] = $schema . $host
      . $this->view->url(array('action' => 'return'))
      . '?order_id=' . $order_id
      //. '?gateway_id=' . $this->_gateway->gateway_id
      //. '&subscription_id=' . $this->_subscription->subscription_id
      . '&state=' . 'cancel';
    $params['ipn_url'] = $schema . $host
      . $this->view->url(array('action' => $action, 'controller' => 'ipn'))
      . '?order_id=' . $order_id;
      //. '?gateway_id=' . $this->_gateway->gateway_id
      //. '&subscription_id=' . $this->_subscription->subscription_id;

		$this->view->gateway_id = $gateway_id = $gateway->getIdentity();
  
		$params['price'] = $params['amount'] = $verificationpackage->price;
		$params['recurrence'] = $verificationpackage->recurrence;
		$params['order_id'] = $order_id;
		$params['type'] = 'user';
		$params["email"] = $this->_user->email;
		$params['currency'] = Engine_Api::_()->getApi('settings', 'core')->getSetting('payment.currency', 'USD');
		$params['subscription_id'] = $subscription_id;
		$this->view->params = $params;	

		if($gateway->plugin == "Payment_Plugin_Gateway_Stripe") {  
			$this->view->publishKey = $publishKey = $gateway->config['publish'];
			$secretKey = $gateway->config['secret'];
			\Stripe\Stripe::setApiKey($secretKey);
			$this->view->session = $plugin->createVerificationTransaction($this->_user, $params);
		} else if($gateway->plugin == "Payment_Plugin_Gateway_Authorize") {

			$this->renderScript('/application/modules/Authorizepmnt/views/scripts/wallet/index.tpl');

		} else if($gateway->plugin == "Payment_Plugin_Gateway_Coinpayment") {
				$params['currency'] = $this->_getParam('currency');
				
				// Process transaction
				$transaction = $plugin->createVerificationTransaction($this->_user, $params);
				$this->view->transactionUrl = $transaction["invoices"][0]["checkoutLink"];
				$this->view->transactionData = false;
		} else {
			// Process transaction
			$transaction = $plugin->createVerificationTransaction($this->_user, $params);

			// Pull transaction params
			$this->view->transactionUrl = $transactionUrl = $gatewayPlugin->getGatewayUrl();
			$this->view->transactionMethod = $transactionMethod = $gatewayPlugin->getGatewayMethod();
			$this->view->transactionData = $transactionData = $transaction->getData();

			// Handle redirection
			if( $transactionMethod == 'GET' ) {
				$transactionUrl .= '?' . http_build_query($transactionData);
				return $this->_helper->redirector->gotoUrl($transactionUrl, array('prependBase' => false));
			}
		}
		// Post will be handled by the view script
  }
  
  public function returnAction() {
		
    // Get order
    if( !$this->_user ||
        !($orderId = $this->_getParam('order_id', $this->_session->order_id)) ||
        !($order = Engine_Api::_()->getItem('payment_order', $orderId)) ||
        $order->user_id != $this->_user->getIdentity() ||
        $order->source_type != 'payment_verificationpackage' ||
        !($gateway = Engine_Api::_()->getItem('payment_verificationgateway', $order->gateway_id)) ) {
      return $this->_helper->redirector->gotoRoute(array(), 'default', true);
    }
    //$this->_subscription = $subscription;
    // Get gateway plugin
    $this->view->gatewayPlugin = $gatewayPlugin = $gateway->getGateway();
    $plugin = $gateway->getPlugin();

		
		$params = $this->_getAllParams();
		$params['subscription_id'] = $this->_session->subscription_id;

    // Process return
    unset($this->_session->errorMessage);
    try {
      $status = $plugin->onVerificationTransactionReturn($order, $params);
      
      if(($status == 'active' || $status == 'free')) {
        $admins = Engine_Api::_()->user()->getSuperAdmins();
        $user = Engine_Api::_()->getItem('user', $order->user_id);
        
        $translate = Zend_Registry::get('Zend_Translate');
        $adminLink = 'https://' . $_SERVER['HTTP_HOST'] . Zend_Controller_Front::getInstance()->getRouter()->assemble(array('module' => 'payment', 'controller' => 'index', 'action' => 'index'), 'admin_default', true);
        
        foreach($admins as $admin){
          Engine_Api::_()->getApi('mail', 'core')->sendSystem($admin,'payment_manual_verification', array(
            'payment_method' => $gateway->title,
            'sender_name' => $user->getTitle(),
            'admin_link' => $adminLink,
          ));
        }
      }
    } catch( Payment_Model_Exception $e ) {
      $status = 'failure';
			if($gateway->plugin == "Payment_Plugin_Gateway_Authorize") {
				print_r($e->getMessage());die;
        echo json_encode(array("status" => 1,"message"=> $e->getMessage()));die;
      }
      $this->_session->errorMessage = $e->getMessage();
    }

		if($gateway->plugin == "Payment_Plugin_Gateway_Authorize") {
      echo json_encode(array("status" => 1,"url"=>$this->view->url(array('action' => 'finish', 'state' => $status, 'user_id' => $this->_user->getIdentity(), 'signup' => false))));die;
    }

    return $this->_finishPayment($status);
  }
  
  protected function _finishPayment($state = 'active', $using_wallet = false)
  {
    $viewer = Engine_Api::_()->user()->getViewer();
    $user = $this->_user;

    // No user?
    if( !$user ) {
			if($using_wallet) {
				echo json_encode(array('status' => false, 'message' => $this->view->translate("Invalid Member.")));die;
			} else {
				return $this->_helper->redirector->gotoRoute(array(), 'default', true);
			}
    }

    // Clear session
    $errorMessage = $this->_session->errorMessage;
    $userIdentity = $this->_session->user_id;
    $this->_session->unsetAll();
    $this->_session->user_id = $userIdentity;
    $this->_session->errorMessage = $errorMessage;

    // Redirect
    if( $state == 'free' ) {
			if($using_wallet) {
				echo json_encode(array('status' => true, 'message' => $this->view->translate("Thank you! Your payment has completed successfully.")));die;
				//return $this->_helper->redirector->gotoRoute(array(), 'default', true);
			} else {
      	return $this->_helper->redirector->gotoRoute(array(), 'default', true);
			}
    } else {
			if($using_wallet) {
				echo json_encode(array('status' => true, 'message' => $this->view->translate("Thank you! Your payment has completed successfully.")));die;
				//return $this->_helper->redirector->gotoRoute(array('action' => 'finish', 'state' => $state, 'user_id' => $user->getIdentity()));
			} else {
      	return $this->_helper->redirector->gotoRoute(array('action' => 'finish', 'state' => $state, 'user_id' => $user->getIdentity()));
			}
    }
  }
  
  public function finishAction()
  {
    $this->view->status = $status = $this->_getParam('state');
    $this->view->error = $this->_session->errorMessage;
    $this->view->user_id = $user_id = $this->_getParam('user_id', null);
    $this->view->url = $this->view->url(array(), 'default', true);
  }
  
  public function cancelAction() {
  
		$subscriptionId = $this->_getParam('subscription_id', null);
    $subscription = Engine_Api::_()->getItem('payment_subscription', $subscriptionId);
    $order = Engine_Api::_()->getItem('payment_order', $subscription->order_id);
    $verificationsubscription = $order->getSource();

		if(!$subscriptionId || !$subscription)
			return $this->_forward('notfound', 'error', 'core');
			
    // In smoothbox
    $this->_helper->layout->setLayout('default-simple');

    // Make form
    $this->view->form = $form = new Payment_Form_Payment_Cancel();
    $form->setTitle('Cancel This Subscription?');
    $form->setDescription('Are you sure you want to cancel your verification subscription? You\'ll need to reapply if canceled.');
    $form->submit->setLabel('Yes');

    if (!$this->getRequest()->isPost()) {
      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
      return;
    }
		$gateway = Engine_Api::_()->getItem('payment_verificationgateway', $subscription->gateway_id);
    try {
			//Cancel for Manual Payment gateway
			if(engine_in_array($gateway->getIdentity(), array(3, 4, 5, 6, 3000))) {
				$verificationsubscription->onCancel($subscription);
      } else {
				if( !empty($subscription->gateway_id) && !empty($subscription->gateway_profile_id) ) {
					if( $gateway ) {
						$gatewayPlugin = $gateway->getPlugin();
						if( method_exists($gatewayPlugin, 'cancelSubscription') ) {
							$r = $gatewayPlugin->cancelSubscription($subscription->gateway_profile_id, "Cancelled");
							$verificationsubscription->onCancel($subscription);
						}
					}
				}
			}
      
      $this->view->status = true;
      $this->view->message = Zend_Registry::get('Zend_Translate')->_('Verification Subscription cancelled successfully.');
      return $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => 10,
        'parentRefresh' => 10,
        'messages' => array($this->view->message)
      ));
    } catch (Exception $e) {
      throw $e;
    }
  }
  
  public function sendVerificationRequestAction() {
  
		$user_id = $this->_getParam('user_id', null);
    $viewer = Engine_Api::_()->getItem('user', $user_id);
    
		if(!$user_id || !$viewer)
			return $this->_forward('notfound', 'error', 'core');
			
    // In smoothbox
    $this->_helper->layout->setLayout('default-simple');

    // Make form
    $this->view->form = $form = new User_Form_Verification_Send();

    if (!$this->getRequest()->isPost()) {
      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
      return;
    }
    
    if (!$form->isValid($this->getRequest()->getPost()))
      return;
    
    $values = $form->getValues();
    $table = Engine_Api::_()->getDbTable('verificationrequests', 'user');
    
    $db = $table->getAdapter();
    $db->beginTransaction();
    
    try {

      $row = $table->createRow();
      $row->user_id = $user_id;
      $row->message = $values['message'];
      $row->creation_date = date('Y-m-d H:i:s');
      $row->save();
      
      //Send notification and mail to all admins
      $allAdmins = Engine_Api::_()->getItemTable('user')->getAllAdmin();
      foreach ($allAdmins as $admin) {
        Engine_Api::_()->getDbTable('notifications', 'activity')->addNotification($admin, $viewer, $admin, 'user_verirequestto_superadmin');
      }
      
      $db->commit();
      
      $this->view->status = true;
      $this->view->message = Zend_Registry::get('Zend_Translate')->_('Your verification request has been successfully submitted.');
      return $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => 10,
        'parentRefresh' => 10,
        'messages' => array($this->view->message)
      ));
    } catch (Exception $e) {
      throw $e;
    }
  }
  
  public function cancelVerificationRequestAction() {
  
		$user_id = $this->_getParam('user_id', null);
    $user = Engine_Api::_()->getItem('user', $user_id);
    
    $verificationrequest_id = $this->_getParam('verificationrequest_id', null);
    $verificationrequest = Engine_Api::_()->getItem('user_verificationrequest', $verificationrequest_id);
    
		if(!$user_id || !$user)
			return $this->_forward('notfound', 'error', 'core');
			
    if(!$verificationrequest_id || !$verificationrequest)
			return $this->_forward('notfound', 'error', 'core');
			
    // In smoothbox
    $this->_helper->layout->setLayout('default-simple');

    // Make form
    $this->view->form = $form = new User_Form_Verification_Cancel();

    if (!$this->getRequest()->isPost()) {
      $this->view->status = false;
      $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
      return;
    }
    
    if (!$form->isValid($this->getRequest()->getPost()))
      return;

    try {
      $verificationrequest->delete();
      
      $this->view->status = true;
      $this->view->message = Zend_Registry::get('Zend_Translate')->_('Verification request cancelled successfully.');
      return $this->_forward('success', 'utility', 'core', array(
        'smoothboxClose' => 10,
        'parentRefresh' => 10,
        'messages' => array($this->view->message)
      ));
    } catch (Exception $e) {
      throw $e;
    }
  }
}