<?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 OfficeBrain\Bundle\CmsBundle\Form\CmsMenuMasterType;
use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * @author OfficeBrain 4494 <info@officebrain.com>
 *
 * Description : CMS Menu admin controller class
 *
 * @Route("/admin/{_locale}/cms/menu")
 **/
class CmsMenuAdminController extends Controller
{
    protected $request;

    protected $translator;

    protected $projectSetting;

    protected $cmsMenuManager;

    protected $responseArray;

    protected $commonLibraryManager;

    public function __construct()
    {
        $this->request = null;
        $this->translator = null;
        $this->projectSetting = array();
        $this->responseArray = array();
        $this->commonLibraryManager = null;
        $this->cmsMenuManager = null;
    }

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

    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->translator = $this->get('translator');
        $this->projectSetting = $this->request->get('project_setting');
        $this->cmsMenuManager = $this->get('office_brain_cms_menu_manager');
        $this->commonLibraryManager = $this->get('office_brain_common_library');
        $this->responseArray = array();
    }

    /**
     * @author Employee Id: 4494
     * Description : Display menu.
     *
     * @param 1 :Menu id
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/{slug}/{instance_id}", name="office_brain_cmsbundle_cmsmenuadmin_menu",
     * defaults={"slug"="header-menu", "instance_id"="0"},
     * requirements={"instance_id"="\d+"},
     * options={"expose"=true}
     * )
     *
     * @Method("GET")
     * @Template("OfficeBrainCmsBundle:CmsMenuAdmin:menu.html.twig")
     */
    public function menuAction()
    {
        $this->initAction();

        $this->responseArray = $this->cmsMenuManager->prepareMenu(true, true, true);

        if ($this->responseArray['menu'] == 'CMS_MENU_NOT_FOUND') {
        	throw $this->createNotFoundException($this->translator->trans('cms.admin.menu.error.no_record_found_slug').' '.$this->request->get('slug'));
        }

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Ajax menu detail.
     *
     * @param 1 :Menu slug
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/{slug}/{instance_id}", name="office_brain_cmsbundle_cmsmenuadmin_menuajax",
     * defaults={"slug"="header-menu", "instance_id"="0"},
     * requirements={"instance_id"="\d+"},
     * options={"expose"=true}
     * )
     *
     * @Method("POST")
     * @Template("OfficeBrainCmsBundle:CmsMenuAdmin:menuAjax.html.twig")
     */
    public function menuAjaxAction()
    {
        $this->initAction();

    	$this->responseArray = $this->cmsMenuManager->prepareMenu(true, true, true);

        if ($this->responseArray['menu'] == 'CMS_MENU_NOT_FOUND') {
        	throw $this->createNotFoundException($this->translator->trans('cms.admin.menu.error.no_record_found_slug').' '.$this->request->get('slug'));
        }

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Ajax menu detail.
     *
     * @param 1 :Menu id
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/add/menu-item/{id}", name="office_brain_cmsbundle_cmsmenuadmin_addmenuitemajax",
     * options={"expose"=true}
     * )
     *
     * @Method("POST")
     * @Template("OfficeBrainCmsBundle:CmsMenuAdmin:addMenuItemAjax.html.twig")
     */
    public function addMenuItemAjaxAction()
    {
        $this->initAction();

        $this->responseArray = $this->cmsMenuManager->setMenuItem();

        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4494
     * Description : Ajax menu detail.
     *
     * @param 1 :Menu item id
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/delete/menu-item/{id}", name="office_brain_cmsbundle_cmsmenuadmin_deletemenuitemajax",
     * options={"expose"=true}
     * )
     *
     * @Method("POST")
     */
    public function deleteMenuItemAjaxAction()
    {
        $this->initAction();

        $this->responseArray['menu_item_id'] = $this->cmsMenuManager->deleteMenuItem(0, false);
        $this->responseArray['status'] = strtolower($this->translator->trans('cms.admin.menu.form.status.success'));
        $this->responseArray['success_message'] = $this->translator->trans('cms.admin.menu.form.status.delete_success_message');

        return new JsonResponse($this->responseArray);
    }

    /**
     * @author Employee Id: 4494
     * Description : Ajax menu detail.
     *
     * @param 1 :Menu item id
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/save/menu-item/{id}/{instance_id}", name="office_brain_cmsbundle_cmsmenuadmin_savemenuitemajax",
     * defaults={"instance_id"="0"},
     * requirements={"instance_id"="\d+"},
     * options={"expose"=true}
     * )
     *
     * @Method("POST")
     */
    public function saveMenuItemAjaxAction()
    {
        $this->initAction();
        
        $this->cmsMenuManager->saveMenuItem();
        $this->responseArray['status'] = strtolower($this->translator->trans('cms.admin.menu.form.status.success'));
        $this->responseArray['success_message'] = $this->translator->trans('cms.admin.menu.form.status.success_message');

        return new JsonResponse($this->responseArray);
    }

    /**
     * @author Employee Id: 4494
     * Description : Ajax menu detail.
     *
     * @param 1 :Menu item id
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/upload/menu-item-image/{id}", name="office_brain_cmsbundle_cmsmenuadmin_uploadmenuitemImageajax",
     * options={"expose"=true}
     * )
     *
     * @Method("POST")
     */
    public function uploadMenuItemImageAjaxAction()
    {
        $this->initAction();

        $this->responseArray = $this->cmsMenuManager->uploadMenuItem();

        if ($this->responseArray != 'CMS_MENU_NOT_FOUND') {
            if ($this->responseArray != 'INVALID_FILE') {
                $this->responseArray['status'] = $this->translator->trans('cms.admin.menu.form.status.success');
                $this->responseArray['success_message'] = $this->translator->trans('cms.admin.menu.form.status.background_upload_success_message');
            } else {
                $this->responseArray['status'] = $this->translator->trans('cms.admin.menu.form.status.error');
                $this->responseArray['error_message'] = $this->translator->trans('cms.admin.menu.error.invalid_file');
            }
        } else {
            $this->responseArray['status'] = $this->translator->trans('cms.admin.menu.form.status.error');
            $this->responseArray['error_message'] = $this->translator->trans('cms.admin.menu.error.no_record_found_menu_item');
        }

        return new JsonResponse($this->responseArray);
    }
    
    /**
     * @author Employee Id: 4464
     * Description : Create page based on slug entered in the static page
     *
     * @param 1 :Menu item id
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/static-page/create", name="office_brain_cmsbundle_cmsmenuadmin_createpageajax",
     * options={"expose"=true}
     * )
     *
     * @Method("POST")
     */
    public function createPageAjaxAction()
    {
        $this->initAction();
        $this->responseArray['static_page_object'] = $this->cmsMenuManager->setCmsPageFromStaticPage();
    
        /* if (!empty($this->responseArray['static_page_object'])) {
            $this->responseArray['status'] = $this->translator->trans('cms.admin.menu.form.status.success');
            $this->responseArray['success_message'] = $this->translator->trans('cms.admin.menu.form.status.success_message');
        }else{
            $this->responseArray['status'] = $this->translator->trans('cms.admin.menu.form.status.error');
            $this->responseArray['success_message'] = $this->translator->trans('cms.admin.menu.form.status.error_message');
        } */
        
        return new JsonResponse($this->responseArray);
    }
    
    /**
     * @author Employee Id: 4465
     * Description : Display menu rights.
     *
     * @param 1 :Menu id
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/list/menu-rights/{slug}/{instance_id}", name="office_brain_cmsbundle_cmsmenuadmin_menurights",
     * defaults={"slug"="header-menu", "instance_id"="0"},
     * requirements={"instance_id"="\d+"},
     * options={"expose"=true}
     * )
     *
     * @Method("GET|POST")
     * @Template("OfficeBrainCmsBundle:CmsMenuAdmin:menuRights.html.twig")
     */
    public function menuRightsAction()
    {
        $this->initAction();

        $this->responseArray = $this->cmsMenuManager->prepareMenu();

        if ($this->responseArray['menu'] != 'CMS_MENU_NOT_FOUND') {
            $this->responseArray['form'] = $this->createForm(new CmsMenuMasterType(), $this->responseArray['menu'])->createView();
            $this->responseArray['cms_menu_rights'] = $this->cmsMenuManager->getCmsMenuItemRights();

            unset($this->responseArray['menu']);
        } else {
            throw $this->createNotFoundException($this->translator->trans('cms.admin.menu.error.no_record_found_slug').' '.$this->request->get('slug'));
        }

        return $this->responseArray;
    }
    
    /**
     * @author Employee Id: 4465
     * Description : Display menu rights.
     *
     * @param 1 :Menu id
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/save/menu-item-rights/{id}/{instance_id}", name="office_brain_cmsbundle_cmsmenuadmin_savemenuitemrightsajax",
     * defaults={"instance_id"="0"},
     * requirements={"instance_id"="\d+"},
     * options={"expose"=true}
     * )
     *
     * @Method("GET|POST")
     * 
     */
    public function saveMenuItemRightsAction()
    {
    	$this->initAction();
    	
    	$this->responseArray = $this->cmsMenuManager->setCmsMenuItemRights();
    	    	
    	return new JsonResponse($this->responseArray);
    }

    /**
     * @author Employee Id: 4494
     * Description : Copy menu item by country id.
     *
     * @param 1 :slug
     *
     * @param 2 :country_id
     *
     * @return type : Data.
     *
     * @throws Exception: Null
     *
     * @Route("/copy/menu-item/{slug}/{country_id}/{instance_id}", name="office_brain_cmsbundle_cmsmenuadmin_copymenubycountryid",
     * defaults={"instance_id"="0"},
     * requirements={"instance_id"="\d+"},
     * options={"expose"=true}
     * )
     *
     * @Method("GET")
     *
     */
    public function copyMenuByCountryIdAction()
    {
    	$this->initAction();

    	$this->responseArray = $this->cmsMenuManager->copyMenuItemByCountryId();

    	return new JsonResponse($this->responseArray);
    }
}
