<?php

namespace OfficeBrain\Bundle\CmsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

/**
 * @author Employee Id: 4464
 *
 * Description : Controller class for Sitemap
 */
class SitemapController extends Controller
{
    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to set member variables default value
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function __construct()
    {
        $this->sitemapManager = null;
        $this->responseArray = null;
        $this->request = null;
        $this->locale = null;
    }

    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to unset member variables
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     */
    public function __destruct()
    {
        unset($this->sitemapManager);
        unset($this->responseArray);
        unset($this->request);
        unset($this->locale);
    }

    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to initialize the variables
     *
     * @param : None
     *
     * @return NULL
     *
     * @throws Exception: Null
     */
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->sitemapManager = $this->get('office_brain_sitemap_manager');
        $this->responseArray = array();
        $this->locale = $this->request->get('_locale');
    }

    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to show the sitemap
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     * @Route("/{_locale}/sitemap", name="office_brain_cmsbundle_sitemap_sitemap")
     * @Template("OfficeBrainCmsBundle:Sitemap:sitemap.html.twig")
     */
    public function sitemapAction()
    {
        $this->initAction();

        $this->responseArray = $this->sitemapManager->getSystemRoutes();

        $file = $this->sitemapManager->generateSitemapFile($this->responseArray);
        unset($file);
        return $this->responseArray;
    }

    /**
     * @author Employee Id: 4464
     *
     * Description : This function is used to generate the xml file for sitemap
     *
     * @param : None
     *
     * @return: NULL
     *
     * @throws Exception: Null
     *
     * @Route("/{_locale}/sitemap/generate-xml",name="office_brain_cmsbundle_sitemap_generatexml")
     */
    public function generateXmlAction()
    {
        $this->initAction();
        $url = 'https://www.idmepromotions.com';//$this->container->getParameter('base_url');
        $this->responseArray = $this->sitemapManager->getSystemRoutes();
        $file = $this->sitemapManager->generateSitemapFile($this->responseArray, $url);
        unset($file);
        return $this->redirect($this->generateUrl('office_brain_cmsbundle_sitemap_sitemap', array('_locale' => $this->locale)));
    }
}
