<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use OfficeBrain\Bundle\CmsBundle\Form\CmsPageMasterType;
use OfficeBrain\Bundle\CmsBundle\Controller\CmsPageAdminController as BaseCmsPageAdminController;

/**
 * @author OfficeBrain 4494 <info@officebrain.com>
 *
 * Description : CMS Admin controller class
 *
 * @Route("/admin/{_locale}/cms/page")
 **/
class CmsPageAdminController extends BaseCmsPageAdminController
{
    private $request;

    private $session;

    private $translator;

    private $projectSetting;

    private $recordsPerPage;

    private $cmsPageManager;

    private $responseArray;

    public function __destruct()
    {
    	parent::__destruct();
        unset($this->request);
        unset($this->session);
        unset($this->translator);
        unset($this->projectSetting);
        unset($this->cmsPageManager);
        unset($this->responseArray);
    }

    public function initAction()
    {
    	parent::initAction();
        $this->request = $this->getRequest();
        $this->session = $this->request->getSession();
        $this->translator = $this->get('translator');
        $this->projectSetting = $this->request->get('project_setting');
        $this->cmsPageManager = $this->get('office_brain_cms_page_manager');
        $this->responseArray = array();
    }


    /**
     * @author Employee Id: 4494
     * Description : Display page form.
     *
     * @param 1 : No paramater
     *
     * @return type : Form view.
     *
     * @throws Exception: Null
     *
     * @Route("/add/{instance_id}", name="office_brain_cmsbundle_cmspageadmin_pageadd",
     * defaults={"instance_id"="0"},
     * requirements={"instance_id"="\d+"},
     * options={"expose"=true})
     * @Method("GET")
     * @Template("OfficeBrainCustomBundleCmsBundle:CmsPageAdmin:add.html.twig")
     */
    public function pageAddAction()
    {
        $this->initAction();

        $this->responseArray['form'] = $this->createForm(new CmsPageMasterType($this->container), $this->cmsPageManager->getCmsPageMasterWithLanguage())->createView();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4136
     * Description : Save page content
     *
     * @param 1 : No paramater
     *
     * @return type : Form view.
     *
     * @throws Exception: Null
     *
     * @Route("/add/{instance_id}", name="office_brain_cmsbundle_cmspageadmin_processpageadd",
     * defaults={"instance_id"="0"},
     * requirements={"instance_id"="\d+"},
     * options={"expose"=true})
     *
     * @Method({"POST"})
     *
     * @Template("OfficeBrainCustomBundleCmsBundle:CmsPageAdmin:add.html.twig")
     */
    public function processPageAddAction()
    {
        $this->initAction();

        $form = $this->createForm(new CmsPageMasterType($this->container), $this->cmsPageManager->getCmsPageMasterWithLanguage());

        $form->handleRequest($this->request);

        $form = $this->cmsPageManager->filterFormData($form, $this->translator);

        if ($form->isValid()) {
            $resultArray = $this->cmsPageManager->setCmsPage($form->getData());
            
            if ($resultArray == 'ERROR_SLUG_ALREADY_EXISTS') {
                $this->responseArray['form'] = $form->createView();  // if error than render form and show error
                 $this->get('session')->getFlashBag()->add('error_message', $this->translator->trans('cms.admin.action.page.cms_slug_duplicate_err'));
                                 
                return $this->responseArray;
            }
            
            $this->session->getFlashBag()->set('success_message', $this->translator->trans('cms.admin.action.page.cms_page_add_success'));
            
            if ($this->request->isXmlHttpRequest()) {
                return new JsonResponse(array('success' => 'success', 'page_url' => 'office_brain_cmsbundle_cmspageadmin_pagelist'));
            } else {
                return $this->redirect($this->generateUrl('office_brain_cmsbundle_cmspageadmin_pagelist'));
            }
        }

        $this->responseArray['form'] = $form->createView();

        return $this->responseArray;
    }

    
}
