<?php

namespace OfficeBrain\CustomBundle\RequestQuoteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Translation\Translator;
use OfficeBrain\Bundle\RequestQuoteBundle\Form\RequestQuoteCommentType;
use OfficeBrain\Bundle\RequestQuoteBundle\Entity\RequestQuoteComment;
use OfficeBrain\Bundle\UserBundle\Form\ContactBookType;
use OfficeBrain\Bundle\UserBundle\Entity\ContactBook;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use OfficeBrain\Bundle\RequestQuoteBundle\Form\UploadArtWorkDetailType as UploadArtWorkDetailType;
use OfficeBrain\Bundle\RequestQuoteBundle\Form\RequestQuoteAddGuestShippingType;
use OfficeBrain\Bundle\RequestQuoteBundle\Form\RequestQuoteGuestUserType;
use OfficeBrain\Bundle\RequestQuoteBundle\Controller\RequestQuoteController as BaseRequestQuoteController;

/**
 *
 * @author Employee Id: 5058
 *         OB eCommerce Product -Request Quote
 *         - List All the Request Quote Inquiry
 *         - Add New Request Quote Inquiry
 *
 * @Route("", defaults={"rule_service"="rule_charges"})
 */
class RequestQuoteController extends BaseRequestQuoteController {
	/**
	 *
	 * @author Employee Id: 5058
	 *         To set member variables default value
	 * @return NULL
	 */
	public function __construct() {
        parent::__construct();
	}

	/**
	 *
	 * @author Employee Id: 5058
	 *         To unset member variables
	 * @return NULL
	 */
	public function __destruct() {
        parent::__destruct();
	}

	/**
	 *
	 * @author Employee Id: 5058
	 *         Create various objects required action in this class
	 * @return NULL
	 */
	public function initAction() {
        parent::initAction();        
	}
	
	/**
	 *
	 * @author Employee Id: 5058
	 *         Description : Use to add the new shipping address
	 *
	 *         @Route("/{_locale}/request-quote-add-address",
	 *         name="office_brain_requestquotebundle_saverequestquoteaddress",
	 *         )
	 *         @Method({"GET"})
	 *         @Template("OfficeBrainRequestQuoteBundle:RequestQuote:addContactBook.html.twig")
	 */
	public function addContactBookAction() {
		$this->initAction ();
		if ($this->userId == 0) {
			// Commented code is to redirect the guest user at login page, when guest user click on add new user link from request quote page
			
			 if ($this->request->isXmlHttpRequest()) 
			 { 
			 	$this->responseArray['status'] = 'error'; 
			 	$this->responseArray['error_type'] = 'loginError'; 
			 	$this->responseArray['page_url'] = $this->generateUrl('office_brain_userbundle_login_login'); 
			 	return new JsonResponse($this->responseArray); 
			 }else{ 
			 	$this->get('session')->set('_security.main.target_path', $this->generateUrl($this->request->get('_route')));
			 	return $this->redirect($this->generateUrl('office_brain_userbundle_login_login')); 
			 }
			
			$this->form = $this->createForm ( new RequestQuoteAddGuestShippingType ( $this->coreManager, 'add' ) );
			$this->form->handleRequest ( $this->request );
			return $this->render ( 'OfficeBrainRequestQuoteBundle:RequestQuote:addGuestContactBook.html.twig', array (
					'contactBookForm' => $this->form->createView ()
			) );
		} else {
			$this->form = $this->createForm ( new ContactBookType ( $this->container ), new ContactBook () );
			$this->form->handleRequest ( $this->request );
			$this->responseArray ['contactBookForm'] = $this->form->createView ();
			return $this->responseArray;
		}
	}
}