<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OfficeBrain\CustomBundle\CmsBundle\Entity\OrderCatalog;
use OfficeBrain\CustomBundle\CmsBundle\Form\OrderCatalogType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 *
 * @author Employee Id: 5412
 *
 * Description : Controller class for order catalog
 *
 * @Route("/{_locale}")
 */
class OrderCatalogController extends Controller
{
	/**
	 * @author Employee Id: 5412
	 *
	 * Description : This function is used to set member variables default value
	 *
	 * @param : None
	 *
	 * @return: NULL
	 *
	 * @throws Exception: Null
	 */
	public function __construct()
	{
		$this->responseArray = null;
		$this->request = null;
		$this->translator = null;
		$this->projectSetting = null;
		$this->user_id = null;
		$this->session = null;
	}
	
	/**
	 * @author Employee Id: 5412
	 *
	 * Description : This function is used to unset member variables
	 *
	 * @param : None
	 *
	 * @return NULL
	 *
	 * @throws Exception: Null
	 */
	public function __destruct()
	{
		unset($this->responseArray);
		unset($this->request);
		unset($this->translator);
		unset($this->projectSetting);
		unset($this->user_id);
		unset($this->session);
	}
	
	/**
	 * @author Employee Id: 5412
	 *
	 * Description : This function is used to initialize the variables
	 *
	 * @param : None
	 *
	 * @return NULL
	 *
	 * @throws Exception: Null
	 */
	public function initAction()
	{
		$this->request = $this->getRequest();

		$this->responseArray = array();
		$this->translator = $this->get('translator');
		$this->projectSetting = $this->request->get('project_setting');
		$this->user_id = $this->projectSetting['user_id'];
		$this->em = $this->container->get('doctrine')->getEntityManager();
		$this->userId = $this->projectSetting['user_id'];
		$this->session = $this->get('session');

		$this->orderCatalogCustomManager = $this->get('office_brain_cms_order_catalog_manager_custom');
	}
	
	/**
	 * @author Employee Id: 4242
	 *
	 * Description : This function is used to add new order catalog
	 *
	 * @param : None
	 *
	 * @return NULL
	 *
	 * @throws Exception: Null
	 *
	 * @Route("/order-catalog", name="office_brain_custombundle_cmsbundle_order_catalog_add")
	 * @Method("GET")
	 * @Template("OfficeBrainCustomBundleCmsBundle:OrderCatalog:add.html.twig")
	 */
	public function addAction()
	{
		$this->initAction();

		if ($this->userId == 0) {
			$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 OrderCatalogType($this->container), new OrderCatalog());
		$this->responseArray['form'] = $this->form->createView();
		//$this->responseArray['form'] = $this->createForm(new OrderCatalogType())->createView();
		
		return $this->responseArray;
	}
	
	/**
	 * @author Employee Id: 4242
	 *
	 * Description : This function is used to add new order catalog
	 *
	 * @param : None
	 *
	 * @return NULL
	 *
	 * @throws Exception: Null
	 *
	 * @Route("/order-catalog", name="office_brain_custombundle_cmsbundle_order_catalog_process_add")
	 * @Method("POST")
	 * @Template("OfficeBrainCustomBundleCmsBundle:OrderCatalog:add.html.twig")
	 */
	public function processAddAction()
	{
		$this->initAction();
	
		if ($this->userId == 0) {
			$this->get('session')->set('_security.main.target_path', $this->generateUrl($this->request->get('_route')));
			return $this->redirect($this->generateUrl('office_brain_userbundle_login_login'));
		}
	
		$orderCatalog = new OrderCatalog();
		$form= $this->createForm(new OrderCatalogType($this->container), new OrderCatalog());
		//$form = $this->createForm(new OrderCatalogType(), $orderCatalog);
	
		$form->handleRequest($this->request);
		
		if ($form->isValid()) {
			$this->orderCatalogCustomManager->setOrderCatalog($form->getData());
			
			$this->session->getFlashBag()->set('success_message', $this->translator->trans('cms.front.order_catalog.action.add_success'));
			
			return $this->redirect($this->generateUrl('office_brain_custombundle_cmsbundle_order_catalog_add'));
		}
		
		$this->responseArray['form'] = $form->createView();
		return $this->responseArray;
	}
}
