<?php

namespace OfficeBrain\Bundle\FlipBookBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use OfficeBrain\Bundle\FlipBookBundle\Form\FlipBookType;
use OfficeBrain\Bundle\FlipBookBundle\Entity\FlipBook;
class FlipBookAdminController extends Controller
{
    public function initAction()
    {
        $this->request = $this->getRequest();
        $this->projectSetting = $this->request->get('project_setting');
        $this->locale = $this->projectSetting['culture_code'];
        $this->responseArray = array();
        $this->flipBookManager = $this->get('office_brain_flip_book_manager');
        $this->page = $this->request->get('page', '1');
        $this->recordsPerPage = $this->request->get('records_per_page', 10);
    }

    /**
     *
     * @author Employee Id: 4571
     *
     * Description : List action
     *
     * @param  : null
     *
     * @return Array
     *
     * @throws null
     *
     * @Route("/admin/{_locale}/flipbook/list", name="office_brain_flipbookbundle_flipbookadmin_list")
     *
     *
     **/
    public function listAction()
    {
        $this->initAction();
        $keywords = $this->request->get('keywords');
        //$this->page = 1;
        //$this->recordsPerPage = 1;
        $this->responseArray['records_per_page'] = $this->recordsPerPage;
        $this->responseArray['page'] = $this->page;
        $this->responseArray['data'] = $this->flipBookManager->getList($keywords, $this->page, $this->recordsPerPage);

        return $this->render('OfficeBrainFlipBookBundle:FlipBookAdmin:list.html.twig',$this->responseArray);
    }

     /**
     *
     * @author Employee Id: 4571
     *
     * Description : Add action
     *
     * @param  : null
     *
     * @return Array
     *
     * @throws null
     *
     * @Route("/admin/{_locale}/flipbook/add", name="office_brain_flipbookbundle_flipbookadmin_create")
     *
     * @Template("OfficeBrainFlipBookBundle:FlipBookAdmin:add.html.twig")
     *
     **/
    public function createAction(Request $request)
    {
        //$em = $this->getDoctrine()->getManager();
        $em = $this->get('doctrine.orm.entity_manager');
        $entity = new FlipBook();
        $form = $this->createForm(new FlipBookType(), $entity, array(
            'action' => $this->generateUrl('office_brain_flipbookbundle_flipbookadmin_create'),
            'method' => 'POST',
        ));
        
        $form->handleRequest($request);
        
        if ($form->isValid()) { 
            $em->persist($entity);
            $em->flush();
            $this->addFlash('success_message', 'Record added successfully.');
            return $this->redirect($this->generateUrl('office_brain_flipbookbundle_flipbookadmin_list'));
        }
        
        return array(
            'entity' => $entity,
            'form'   => $form->createView(),

        );
    }


     /**
     *
     * @author Employee Id: 4571
     *
     * Description : Edit action
     *
     * @param  : null
     *
     * @return Array
     *
     * @throws null
     *
     * @Route("/admin/{_locale}/flipbook/edit/{id}", name="office_brain_flipbookbundle_flipbookadmin_edit")
     *
     * @Template("OfficeBrainFlipBookBundle:FlipBookAdmin:edit.html.twig")
     *
     **/
    public function editAction(Request $request)
    {
        $id = $request->get('id');
        $flipBookManager = $this->get('office_brain_flip_book_manager');
        $entity = $flipBookManager->getById($id);
        $em = $this->get('doctrine.orm.entity_manager');
        $form = $this->createForm(new FlipBookType(), $entity);
        $form->handleRequest($request);
        if ($form->isValid()) { 
            $em->persist($entity);
            $em->flush();
            $this->addFlash('success_message','Record edit successfully.');
            return $this->redirect($this->generateUrl('office_brain_flipbookbundle_flipbookadmin_list'));
        }
        
        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
            'id'   => $id
        );
    }

    /**
     *
     * @author Employee Id: 4571
     *
     * Description :Delete action
     *
     * @param  : null
     *
     * @return Array
     *
     * @throws null
     *
     * @Route("/admin/{_locale}/flipbook/delete/{id}", name="office_brain_flipbookbundle_flipbookadmin_delete")
     *
     *
     **/
    public function deleteAction()
    {
        $this->initAction();
        $id = $this->request->get('id');
        $this->flipBookManager->deleteById($id);
        $this->addFlash('success_message', 'Record deleted successfully.');
        return $this->redirect($this->generateUrl('office_brain_flipbookbundle_flipbookadmin_list'));
    }
}
