<?php

namespace OfficeBrain\Bundle\BannerBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use OfficeBrain\Bundle\BannerBundle\Entity\BannerMaster;
use OfficeBrain\Bundle\BannerBundle\Form\BannerMasterType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Validator\Constraints\Count;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 *
 * @author OfficeBrain 4571 <info@officebrain.com>
 * 
 * Description: Manage Banner Types to perform CRUD operation
 *
 * @Route("admin/{_locale}")
 *
 */

class BannerMasterAdminController extends Controller
{
    private $request;
    private $manager;
    private $responseArray;
    private $language;
    private $locale;
    
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : To set member variables default value
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function __construct()
    {
        $this->request = null;
        $this->bannerMastermanager = null;
        $this->responseArray = array();
        $this->locale = null;
        $this->language = null;
    }
    
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : To unset member variables
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function __destruct()
    {
        unset($this->request);
        unset($this->bannerMastermanager);
        unset($this->responseArray);
        unset($this->locale);
        unset($this->language);
    }
    
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : To initialize request and services
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->translator = $this->get('translator');
        $this->bannerMastermanager = $this->get('office_brain_banner_master_manager');
        $this->commonLibraryManager = $this->get('office_brain_common_library');        
        $this->locale = $this->request->get('_locale');
        $this->language = $this->getParameter('language');
    }

    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : Add Banner Type action
     *
     * @param : null
     *
     * @return Response Array to Twig
     *
     * @throws null
     *
     * @Route("/banner-type/add/{instance_id}", name="office_brain_bannerbundle_bannermasteradmin_addbannertype",
     * defaults={"instance_id"="0"},
     * options={"expose"=true}
     * )
     * 
     * @Method("GET")
     * 
     * @Template("OfficeBrainBannerBundle:BannerMasterAdmin:addBannerType.html.twig")
     *
     **/
    public function addBannerTypeAction()
    {
        $this->initAction();
        $param = $this->request->get('_route_params');        
        $param['action']='add';
        $form = $this->createForm(new BannerMasterType($this->get('office_brain_search_manager'), $this->locale, $this->language), new BannerMaster(), array(
                'action' => $this->generateUrl('office_brain_bannerbundle_bannermaster_processbannertypesubmit', array('instance_id'=>$param['instance_id'])),
                'method' => 'POST',
        ));
        
        $param['action']='add';
        $this->responseArray['action']=$param['action'];
        $this->responseArray['form']=$form->createView();
        
        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4571
     * 
     * Description : to process banner type submit action
     *
     * @param : null
     *
     * @return Response Array to Twig
     *
     * @throws null
     *
     * @Route("/banner-type/add/{instance_id}", name="office_brain_bannerbundle_bannermaster_processbannertypesubmit",
     * defaults={"instance_id"="0"},
     * options={"expose"=true}
     * )
     * 
     * @Method("POST")
     * 
     * @Template("OfficeBrainBannerBundle:BannerMasterAdmin:addBannerType.html.twig")
     *
     **/
    public function processBannerTypeSubmitAction()
    {
        $this->initAction();
        $param = $this->request->get('_route_params');
        $bannerMasterObj = new BannerMaster();
        $param['action']='add';
        $form = $this->createForm(new BannerMasterType($this->get('office_brain_search_manager'), $this->locale, $this->language), $bannerMasterObj);
        $form->handleRequest($this->request);
        if ($form->isValid()) { 
        	
            $response = $this->bannerMastermanager->setBannerMaster($form->getData());
            if ($this->request->isXmlHttpRequest()) {
            	$this->responseArray['success_message'] = $this->translator->trans('bannertype.header.form.add_banner_type');
            	$this->responseArray['bannertype_url'] = $this->generateUrl('office_brain_bannerbundle_bannermaster_listbannertype',array('instance_id' =>  $param['instance_id']));
            	return new JsonResponse($this->responseArray);
            }
            $this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('bannertype.header.form.add_banner_type'));            
            return $this->redirect($this->generateUrl('office_brain_bannerbundle_bannermaster_listbannertype'));
        }
        else {
        	$this->responseArray = $this->commonLibraryManager->prepareFormJsonError($form);
        	if ($this->request->isXmlHttpRequest())
        		return new JsonResponse($this->responseArray);
        }
        $param['action']='add';
        $this->responseArray['action']=$param['action'];
        $this->responseArray['form']=$form->createView();
         
        return $this->responseArray;
    }
    
        
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : To display Listing of Banner Type
     *
     * @param : null
     *
     * @return Response Array to Twig
     *
     * @throws null
     *
     * @Route("/banner-type/list2",
     * name="office_brain_bannerbundle_bannermaster_listbannertypeold",
     * requirements={"records_per_page"="12|24|48|96|996","page"="\d+"},
     * defaults={"records_per_page"=12,"page"=1})
     * 
     * @Template("OfficeBrainBannerBundle:BannerMasterAdmin:listBannerType.html.twig")
     *
     **/
    public function listBannerTypeOldAction()
    {
        $this->initAction();
        
        $culture = $this->request->get('culture');
        $type = $this->request->get('type');
        $param = $this->request->get('_route_params');
        $page = $this->request->get('page', '1');
        $records_per_page=$this->request->get('records_per_page', $param['records_per_page']);
        $bannerManager = $this->bannerMastermanager->getBannerTypeList();
        $paginator  = $this->get('knp_paginator');
        $this->responseArray['pagination'] = $paginator->paginate($bannerManager, $page, $records_per_page, array(), count($bannerManager));
            
        return $this->responseArray;
    }
    
    
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : for Listing Banner type
     *
     * @param  : null
     *
     * @return Response Array
     *
     * @throws null
     *
     * @Route("/banner-type/list/{page}/{instance_id}", name="office_brain_bannerbundle_bannermaster_listbannertype",
     * requirements={"records_per_page"="12|24|48|96|996", "page"="\d+"},
     * defaults={"records_per_page"=12,"page"=1, "instance_id"="0"},
     * options={"expose"=true}
     * )
     * 
     * @Template("OfficeBrainBannerBundle:BannerMasterAdmin:listBannerType.html.twig")
     *
     **/
    public function listBannerTypeAction()
    {
        $this->initAction();
        $this->responseArray = $this->bannerMastermanager->getBannerTypeList();
        $this->responseArray['records_per_page'] = '12';
        return $this->responseArray;
    }
    
    
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : List Banner Ajax action
     *
     * @param  : null
     *
     * @return Array
     *
     * @throws null
     *
     * @Route("/banner-type/list-ajax/{page}", name="office_brain_bannerbundle_bannermaster_listbannertypecontentajax",
     *  requirements={"records_per_page"="12|24|48|96|996", "page"="\d+"},
     *  defaults={"records_per_page"=12,"page"=1})
     * 
     * @Template("OfficeBrainBannerBundle:BannerMasterAdmin:bannerTypeContents.html.twig")
     *
     **/
    public function listBannerTypeContentAjaxAction()
    {
        $this->initAction();
        $this->responseArray = $this->bannerMastermanager->getBannerTypeListAjax();
        return $this->responseArray;
    }
    
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : To Search BannerType
     *
     * @param  : null
     *
     * @return Response
     *
     * @throws null
     *
     * @Route("/banner-type/search/{page}/{instance_id}", name="office_brain_bannerbundle_bannermaster_searchbannertypeajax", 
     * requirements={"records_per_page"="12|24|48|96|996", "page"="\d+"}, 
     *  defaults={"records_per_page"=12,"page"=1, "instance_id"="0"},
     * options={"expose"=true}
     * )
     * 
     * @Template("OfficeBrainBannerBundle:BannerMasterAdmin:bannerTypeContents.html.twig")
     *
     **/
    public function searchBannerTypeAjaxAction()
    {
        $this->initAction();
    
        $this->responseArray = $this->bannerMastermanager->getBannerTypeListAjax();
    
        return $this->responseArray;
    }
    
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : Get the banner type details
     *
     * @param  : null
     *
     * @return Array
     *
     * @throws null
     *
     * @Route("/banner-type/details/{instance_id}", name="office_brain_bannerbundle_bannermaster_bannertypeajaxdetails",
     * requirements={"instance_id"="\d+"},
     * defaults={"instance_id"="0"},
     * options={"expose"=true}
     * )
     * 
     * @Template()
     *
     **/
    public function bannertypeAjaxDetailsAction()
    {
        $this->initAction();
    
        $this->responseArray = $this->bannerMastermanager->getBannerTypeDimensions();
        
        //return array('data'=>$this->responseArray);
        return new JsonResponse(array('success' => 'success', 'resp'=>$this->responseArray));
    }
    
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : To Modify BannerType
     *
     * @param : null
     *
     * @return Response Array to Twig
     *
     * @throws null
     *
     * @Route("/banner-type/edit/{id}/{instance_id}", name="office_brain_bannerbundle_bannermaster_editbannertype",
     * defaults={"instance_id"="0"},
     * options={"expose"=true}
     * )
     * 
     * @Method("GET")
     * 
     * @Template("OfficeBrainBannerBundle:BannerMasterAdmin:addBannerType.html.twig")
     *
     **/
    public function editBannerTypeAction()
    {
        $this->initAction();
        
        $param=$this->request->get('_route_params');
        $param['action'] = "update";
        $bannerMaster=new BannerMaster();
        $bannerMaster= $this->bannerMastermanager->getBannerType($param['id']);
            
        $form=$this->createForm(new BannerMasterType($this->get('office_brain_search_manager'), $this->locale, $this->language), $bannerMaster, array(
                     'action' => $this->generateUrl('office_brain_bannerbundle_bannermaster_processeditbannertypesubmit', array('id'=>$param['id'],'instance_id'=>$param['instance_id'])),
                     'method' => 'POST',
             ));
                    
        $this->responseArray['id']= $param['id'];
        $this->responseArray['action'] = $param['action'] ;
        $this->responseArray['form']= $form->createView();
             
        return $this->responseArray;
    }
    
    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : To Modify BannerType submit action
     *
     * @param : null
     *
     * @return Response Array to Twig
     *
     * @throws null
     *
     * @Route("/banner-type/edit/{id}/{instance_id}", name="office_brain_bannerbundle_bannermaster_processeditbannertypesubmit",
     * defaults={"instance_id"="0"},
     * options={"expose"=true}
     * )
     * 
     * @Method("POST")
     * 
     * @Template("OfficeBrainBannerBundle:BannerMasterAdmin:addBannerType.html.twig")
     *
     **/
    public function processEditBannerTypeSubmitAction()
    {
        $this->initAction();
       
        $param = $this->request->get('_route_params');
        $param['action'] = "update";
        $id = $this->request->get('id');
        $bannerMaster = $this->bannerMastermanager->getBannerType($id);
           
        $form = $this->createForm(new BannerMasterType($this->get('office_brain_search_manager'), $this->locale, $this->language), $bannerMaster);
        $form->handleRequest($this->request);
         
        if ($form->isValid()) {
            $this->bannerMastermanager->updateBannerType($form->getData());
            if ($this->request->isXmlHttpRequest()) {
            	$this->responseArray['success_message'] = $this->translator->trans('bannertype.header.form.edit_banner_type');
            	$this->responseArray['bannertype_url'] = $this->generateUrl('office_brain_bannerbundle_bannermaster_listbannertype',array('instance_id' =>  $param['instance_id']));
            	return new JsonResponse($this->responseArray);
            }
            $this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('bannertype.header.form.edit_banner_type'));            
            return $this->redirect($this->generateUrl('office_brain_bannerbundle_bannermaster_listbannertype'));
        }
        else {
        	$this->responseArray = $this->commonLibraryManager->prepareFormJsonError($form);
        	if ($this->request->isXmlHttpRequest()) 
        	return new JsonResponse($this->responseArray);
        }        
        $this->responseArray['id'] = $param['id'];
        $this->responseArray['action'] = $param['action'];
        $this->responseArray['form'] = $form->createView();
            
        return $this->responseArray;
    }
    

    /**
     *
     * @author Employee Id: 4571
     * 
     * Description : To delete BannerType
     *
     * @param : null
     *
     * @return Response Array to Twig
     *
     * @throws null
     *
     * @Route("/banner-type/delete", name="office_brain_bannerbundle_bannermaster_deletebannertype")
     * 
     * @Template()
     *
     **/
    public function deleteBannerTypeAction()
    {
        $this->initAction();
        
        $bannerGroup = $this->bannerMastermanager->deleteBannerType();
              
        if (array_key_exists("ERR_CAN_NOT_DELETE", $bannerGroup)) {
        	
            $this->get('session')->getFlashBag()->add('error_message', $this->translator->trans('bannertype.header.form.bannertype_err_delete_msg'));
            return new JsonResponse(array('fail' => 'fail', 'page_url' => $this->generateUrl('office_brain_bannerbundle_bannermaster_listbannertype', array())));
        } else {
        	
            $this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('bannertype.header.form.bannertype_delete_msg'));
            return new JsonResponse(array('success' => 'success', 'page_url' => $this->generateUrl('office_brain_bannerbundle_bannermaster_listbannertype', array())));
        }
    }
    
    /**
     *
     * @author Employee Id: 4273
     *
     * Description : Change Status Service Group.
     *
     * @param 1 : Rule Master Id
     *
     * @return type : JsonResponse object
     *
     * @throws Exception: No exception
     *
     * @Route("/banner-type/change-status", name="office_brain_bannerbundle_bannermaster_change_status")
     *
     * @Method({"POST"})
     *
     * @Template()
     *
     */
    public function changeStatusAction()
    {
    	$this->initAction();
    
    	$id = $this->request->get('id');
    
    	if ($id) {
    		$status = $this->bannerMastermanager->changeStatus($id);
    		if ($status) {
    			return new JsonResponse(array('status' => 'success', 'status_message' => $this->translator->trans('bannertype.admin.status_message_success')));
    		}
    	}

    	return new JsonResponse(array('status' => 'fail', 'status_message' => $this->translator->trans('bannertype.admin.status_message_fail')));
    }
}
