<?php

namespace OfficeBrain\Bundle\ThemeBundle\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;

/**
 * @author OfficeBrain 4494 <info@officebrain.com>
 *
 * Description : Theme Admin class.
 *
 * @Route("/admin/{_locale}/theme")
 **/
class ThemeAdminController extends Controller
{
	protected $translator;
	
	protected $request;
	
	protected $projectSetting;
	
	protected $responseArray;
	
	protected $recordsPerPage;
	
	protected $session;
	
	protected $subShopManager;
	
	protected $subshopThemeManager;
	
	protected $commonLibraryManager;
	
	protected $form;
	
	public function __construct()
	{
		$this->translator = null;
		$this->request = null;
		$this->projectSetting = array();
		$this->responseArray = array();
		$this->recordsPerPage = null;
		$this->session = null;
		$this->subShopManager = null;
		$this->subshopThemeManager = null;
		$this->form = null;
	}
	
	public function __destruct()
	{
		unset($this->translator);
		unset($this->request);
		unset($this->projectSetting);
		unset($this->responseArray);
		unset($this->recordsPerPage);
		unset($this->session);
		unset($this->subShopManager);
		unset($this->subshopThemeManager);
		unset($this->form);
	}
	
	public function initAction()
	{
		$this->translator = $this->get('translator');
		$this->request = $this->getRequest();
		$this->session = $this->get('session');
		$this->projectSetting = $this->request->get('project_setting');
		$this->subShopManager = $this->get('office_brain_subshop_manager');
		
		$this->responseArray = array();
	}
	/**
	 * @author Employee Id: 4494
	 * Description : Edit subshop form
	 *
	 * @param 1 : no param
	 *
	 * @return type : Form view and data.
	 *
	 * @throws Exception: No site found
	 *
	 * @Route("/publish/site-theme", name="office_brain_themebundle_themeadmin_publishsitethemeajax", options={"expose"=true})
	 *
	 * @Method("POST")
	 */
	public function publishSiteThemeAjaxAction()
	{
		$this->initAction();
	
		$masterSite = $this->subShopManager->publishMasterSiteDesign();
	
		if ($masterSite) {
			$this->responseArray['status'] = strtolower($this->translator->trans('subshop_content.admin.action.success'));
			$this->responseArray['success_message'] = $this->translator->trans('subshop_content.admin.design.action.subshop_theme_publish_success');
			$this->responseArray['publish_url'] = $this->projectSetting['base_url'];
		} else {
			$this->responseArray['status'] = $this->translator->trans('subshop_content.admin.action.error');
			$this->responseArray['error_message'] = $this->translator->trans('subshop_content.admin.action.technical_issue');
		}
	
		unset($masterSite);
	
		return new JsonResponse($this->responseArray);
	}
}
