<?php

namespace OfficeBrain\Bundle\MarginBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Translation\Translator;
use OfficeBrain\Bundle\CoreBundle\CustomeHandler as FormErrorManager;
use OfficeBrain\Bundle\MarginBundle\Form\MarginMasterType;
use OfficeBrain\Bundle\MarginBundle\Entity\MarginMaster;

/**
 * 
 * @Route("/admin/margin/supplier")
 */
class MarginSupplierAdminController extends Controller
{
	public function __construct()
	{
		$this->form = null;
		$this->formData = null;
		$this->request = null;
		$this->session = null;
		$this->entityManager = null;
		$this->user = null;
		$this->userId = null;
		$this->userRole = null;
		$this->isLoggedIn = null;
		$this->errorArray = null;
		$this->translator = null;
		$this->formErrorManager = new FormErrorManager\JsonError ();
		$this->emailLibrary = null;
		$this->viewTwigData = array();
		$this->OBCommon = null;
	}
	
	public function __destruct()
	{
		unset ( $this->form );
		unset ( $this->formData );
		unset ( $this->request );
		unset ( $this->session );
		unset ( $this->user );
		unset ( $this->userId );
		unset ( $this->userRole );
		unset ( $this->isLoggedIn );
		unset ( $this->errorArray );
		unset ( $this->translator );
		unset ( $this->entityManager );
		unset ( $this->formErrorManager );
		unset ( $this->emailLibrary );
		unset ( $this->viewTwigData );
		unset ( $this->OBCommon );
	}
	
	public function initAction()
	{
		$this->request = $this->getRequest ();
		$this->entityManager = $this->getDoctrine()->getManager();
		$this->session = $this->request->getSession();
		$this->translator = $this->get('translator');
		//$this->emailLibrary = new EmailLibrary\Email ($this->container, $this->translator);
		$this->OBCommon = new CoreLibrary\OBCommon ($this->container);
		if($this->getUser()) {
			$this->user = $this->getUser();
			$this->userId = $this->user->getId();
			$this->isLoggedIn = true;
		}
	}
	
	private function prepareFormJsonError()
	{
		return array (
				'form_name' => $this->form->getName (),
				'form_status' => 'error',
				'form_errors' => $this->formErrorManager->prepareErrorArray ( $this->get ( 'validator' )->validate ( $this->form ) )
		);
	}
	
    /**
	 * @Route("/list/{page}",
	 * name="office_brain_margin_bundle_margin_supplier_list",
	 * requirements={"records_per_page"="12|24|48|96|996","page"="\d+"},
	 * defaults={"records_per_page"=12,"page"=1}),
     * @Template("OfficeBrainMarginBundle:SupplierAdmin:list.html.twig")
	 */
    public function listAction()
    {
        return array(
                // ...
            );    
    }
    
    /**
     * @Route("/add",
     * name="office_brain_margin_bundle_margin_supplier_add"),
     * @Template("OfficeBrainMarginBundle:SupplierAdmin:add.html.twig")
     */
    public function addAction()
    {
    	$this->form = $this->createForm ( new MarginMasterType (new MarginMaster()) );
		$this->viewTwigData['form'] = $this->form->createView ();
		return $this->viewTwigData;
    }
    
    /**
     * @Route("/edit",
     * name="office_brain_margin_bundle_margin_supplier_edit"),
     * @Template("OfficeBrainMarginBundle:SupplierAdmin:edit.html.twig")
     */
    public function editAction()
    {
    	return array(
    			// ...
    	);
    }
    
    /**
     * @Route("/save",
     * name="office_brain_margin_bundle_margin_supplier_save"),
     */
    public function saveAction()
    {
    	return array(
    			// ...
    	);
    }

}
