<?php

class OrderDetailsAction extends CartAction {
	
	const OPERATION_SKIP = 'skip';
	
	public function execute($request) {
		
		sfContext::getInstance()->getConfiguration()->loadHelpers('I18N');
		
		$totalPurchasesCount = 0;
		
		$domainPurchases = ResourceLocator::getInstance()->cart->getDomainPurchases(true, $totalPurchasesCount);
		$certificatePurchases = ResourceLocator::getInstance()->cart->getCertificatePurchasesAndAddons(true, $totalPurchasesCount);
		$domainPurchase = $certificatePurchase = NULL;
		if (!empty($domainPurchases)) {
			$domainPurchase = array_shift($domainPurchases);
		} elseif (!empty($certificatePurchases)) {
			$certificatePurchase = array_shift($certificatePurchases);
		}

		if (!$totalPurchasesCount) {
			$this->_router->next(array(), false);
		}
		
		$purchaseContainer = $domainPurchase ? $domainPurchase : $certificatePurchase;
		
		if ($domainPurchase) {
			$plan = $this->_store->getPlan($purchaseContainer->getPlanId());
			$price = $plan->getPriceById($purchaseContainer->getPriceId());
			$variantId = $price->productVariantID;
			$domainData = $purchaseContainer->getParameter('domainData');
			$SLD = $domainData['SLD'];
			$action = $purchaseContainer->getParameter('type');
			$this->form = ResourceLocator::getInstance()->billingDomainService->getDomainDetailsForm($this->_store->getId(), $variantId, $SLD, $action);
			$this->form->setAttribute('variantId', $variantId);
			$this->form->setAttribute('SLD', $SLD);
			$this->form->setAttribute('action', $action);
			$this->form->setAttribute('purchase', $purchaseContainer);
		} else if ($certificatePurchase) {
			$planId = $purchaseContainer instanceof Purchase ? $purchaseContainer->getPlanId() : $purchaseContainer->getAddonId();
			$this->form = ResourceLocator::getInstance()->billingCertificateService->getCertificateDetailsForm($this->_store->getId(), $planId);
			$this->form->setAttribute('planId', $planId);
			$this->form->setAttribute('purchase', $purchaseContainer);
		}
		
		if ($request->isMethod('post')) {
			
			$operation = $request->getParameter('operation');
			
			switch ($operation) {
				case self::OPERATION_SKIP:
					$this->_router->next();
					break;
				default:
					$this->form->bind($request->getParameter($this->form->getName()));
					if ($this->form->isValid()) {
						if ($this->form->save()) {
							if (!is_null($domainPurchases) || !is_null($certificatePurchases)) {
								$this->_router->refresh();
							} else {
								$this->_router->next();
							}
						}
					} else {
					}
			}
			
		}
		
		if ($domainPurchase) {
			$domainData = $purchaseContainer->getParameter('domainData');
			$this->title = __('TRANS_INFORMATION_FOR_DOMAIN', array('%NAME%' => $domainData['name']));
			$this->description = 'TRANS_INFORMATION_FOR_DOMAIN_DESCRIPTION';
		} else if ($certificatePurchase) {
			$this->title = __('TRANS_INFORMATION_FOR_SSL_CERTIFICATE');
			$this->description = 'TRANS_INFORMATION_FOR_SSL_CERTIFICATE_DESCRIPTION';
		} else {
			$this->title = __('TRANS_ADDITIONAL_INFORMATION');
			$this->description = 'TRANS_NO_ADITIONAL_INFO_REQUIRED';
		}
		
	}
}