<?php

namespace OfficeBrain\Bundle\CmsBundle\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\HttpKernel\Exception\NotFoundHttpException;

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

    private $session;

    private $translator;

    private $projectSetting;

    private $recordsPerPage;

    private $cmsPageManager;

    private $responseArray;

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

    public function 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: 4795
     * Description : Get the page content and render page on fronend
     * 	 *
     * @Route("/{slug}", name="office_brain_cmsbundle_cmspage_page",options={"expose"=true})
     * @Template("OfficeBrainCmsBundle:CmsPage:page.html.twig")
     */
    public function pageAction()
    {
        $this->initAction();
        $this->responseArray = $this->cmsPageManager->getCmsPageBySlug();
        if ($this->request->isXmlHttpRequest()) {
            return $this->render('OfficeBrainCmsBundle:CmsPage:pageAjax.html.twig', $this->responseArray);
        }
        if (!empty($this->responseArray['error']) && $this->responseArray['error'] == "PAGE_NOT_FOUND") {
            throw $this->createNotFoundException($this->translator->trans('cms.front.page_not_found'));
        }

        return $this->responseArray;
    }
    
    /**
     * * @author Employee Id: 4465
     * Description : Site Search
     * 	 *
     * @Route("/search/list", name="office_brain_cmsbundle_cmspage_page_search",
     * options={"expose"=true})
     * 
     * @Method("GET")
     * 
     * @Template("OfficeBrainCmsBundle:CmsPage:pageSearch.html.twig")
     */
    public function pageSearchAction()
    {
    	$this->initAction();
    	
    	$this->responseArray = $this->cmsPageManager->siteSearch($this->request);

    	return $this->responseArray;
    }
    
    /**
     * * @author Employee Id: 4465
     * Description : Site Search
     * 	 *
     * @Route("/search/list", name="office_brain_cmsbundle_cmspage_page_search_ajax",
     * options={"expose"=true})
     *
     * @Method("POST")
     *
     * @Template("OfficeBrainCmsBundle:CmsPage:pageSearchAjax.html.twig")
     */
    public function pageSearchAjaxAction()
    {
    	$this->initAction();
    	
    	$this->responseArray = $this->cmsPageManager->getSiteSearchList($this->request);
    	
    	return $this->responseArray;
    }

    /**
     * @author Employee Id: 4589
     * Description : save inline edit page
     *
     * @param string slug
     *
     * @return JSON Response
     *
     * @throws NULL
     *
     * @Route("/inline/edit/{slug}", name="office_brain_cmsbundle_cmspage_pageinlineedit",
     * options={"expose"=true})
     *
     * @Method("POST")
     *
     * @return array
     */
    public function pageInlineEditAction()
    {
        $this->initAction();

        if ($this->cmsPageManager->updateCmsPageInline()) {
            $this->responseArray = array('status' => 'success', 'success_message' => $this->translator->trans('cms.inline_edit.page_save_success'));
        } else {
            $this->responseArray = array('status' => 'error', 'error_message' => $this->translator->trans('cms.inline_edit.page_save_error'));
        }

        return new JsonResponse($this->responseArray);
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Preview Template
     *
     * @Route("/cms/preview", name="office_brain_cmsbundle_cmspage_cmspagepreview")
     * @Method("POST")
     * @Template("OfficeBrainCmsBundle:CmsPage:page.html.twig")
     */
    public function cmsPagePreviewAction()
    {
    	$this->initAction();
    
    	$this->responseArray = $this->cmsPageManager->previewCmsPage();
    
    	return $this->responseArray;
    }
}
