<?php

namespace OfficeBrain\Bundle\SettingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\JsonResponse;
use OfficeBrain\Bundle\SettingBundle\Form\SettingpageType;
use OfficeBrain\Bundle\SettingBundle\Entity\Settingpage;


class SettingAdminController extends Controller
{
    /**
     *
     * @author Employee Id: 4471
     * To set member variables default value
     * @return NULL
     */
    public function __construct()
    {
        $this->request = null;
        $this->responseArray = array();
        $this->commonLibraryManager = null;
        $this->page = null;
        $this->recordsPerPage = null;
        $this->settingManager = null;
        $this->session = null;
    }
    
    /**
     *
     * @author Employee Id: 4471
     * To unset member variables
     * @return NULL
     */
    public function __destruct()
    {
       
        unset($this->request);
        unset($this->responseArray);
        unset($this->commonLibraryManager);
        unset($this->page);
        unset($this->recordsPerPage);
        unset($this->settingManager);
        unset($this->session);
    }
    
    
    /**
     *
     * @author Employee Id: 4471
     * Create various objects required action in this class
     * @return NULL
     */
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->responseArray['status'] = 'error';
        $this->commonLibraryManager = $this->get('office_brain_common_library');
        $this->page = $this->request->get('page', '1');
        $this->recordsPerPage = 10;
        $this->settingManager = $this->get('office_brain_setting_email_manager');
         $this->session = $this->request->getSession();
    }
    

    /**
     * @author Employee Id: 4471
     * Description : save setting page
     * @Route("admin/{_locale}/setting-page-edit", name="office_brain_settingbundle_admin_editsettingpage", options={"expose"=true})
     * @method({"GET,POST"})
     * @Template("OfficeBrainSettingBundle:Admin:editsettingpage.html.twig")
     */
    public function editsettingpageAction()
    {
        $this->initAction();
        $id= $this->request->get('id',1);
        $obj = $this->settingManager->getSettingPageById($id);
         $status = ($obj!=NULL) ? $obj->getStatus(): 'Active';
        $this->form = $this->createForm(new SettingpageType($status , 'edit',$id),$obj);;
        if ($this->request->getMethod() == 'POST')
        {
            $this->form->handleRequest($this->request);
            if ($this->form->isValid()) {
                $status = $this->settingManager->saveSettingPage($this->form->getData(),$obj);

                 $this->session->getFlashBag()->set('success_message', 'Data updated successfully.');

                return $this->redirect($this->generateUrl('office_brain_settingbundle_admin_editsettingpage'));
            }
        }
       $this->responseArray['form'] = $this->form->createView();
        return $this->responseArray;
    }
    
}
