<?php

namespace OfficeBrain\Bundle\FlyerBundle\Form;


use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use OfficeBrain\Bundle\FlyerBundle\Entity\FlyerBanner;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormError;



class FlyerType extends AbstractType
{
	
	public function __construct($mod, $locale, $userManager, $projectSetting, $loginUserType, $parentcategoryid) // $role
	{
		//$this->container = $container;
		$this->mod = $mod;		
		$this->locale = $locale;
		$this->userManager = $userManager;	 
		//$this->role = $this->getUserRole();		
		$this->projectSetting = $projectSetting;
		$this->userId = $this->projectSetting['user_id'];
		$this->loginUserType = $loginUserType;
		$this->parentcategoryid = $parentcategoryid;
	}
	
	
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {	
    	$builder
        ->add('title', 'text', array(
        		'required'  => false,
        		'constraints' => array(new Assert\NotBlank(array('message'=>'admin.flyer_management.title'))),
        		'data_class' => null,
        ))
        ->add('validUntilDate', 'text', array(
        		'required'  => false,
        		'read_only' => true,
        		'constraints' => array(new Assert\NotBlank(array('message'=>'admin.flyer_management.validUntildate'))),
        ));      

         
        $builder->add('flyers_category_id', 'choice', array (
        		'choices'   => $this->getFlyerSubCategories(),
        		'required'  => true,
        		'disabled' => false,
        		'empty_value' => 'admin.flyer_management.flyers_category_id.popup_label',
        		'constraints' => array(new Assert\NotBlank(array('message'=>'admin.flyer_management.flyerscategoryId'))),
        ));
        
        //if($this->role!="" && sizeof($this->role)>0){ // admin
//         if($this->loginUserType!="supplier"){ 
// 		    $builder->add('supplier_id', 'choice', array (
// 						'choices'   => $this->getUserInfo(),
// 						'required'  => false,
// 						'empty_value' => 'admin.flyer_management.supplier_id.popup_label',
// 		      		    'constraints' => array(new Assert\NotBlank(array('message'=>'admin.flyer_management.supplierId'))),	      			
// 			   ));
//          }else{
//          	$builder->add('supplier_id', 'choice', array (
//          			'choices'   => $this->getUserInfo(),
//          			'required'  => false,
//          			'disabled' => true,     
//          			'data' => $this->userId,    			
//          			'empty_value' => 'admin.flyer_management.supplier_id.popup_label',
//          			'constraints' => array(new Assert\NotBlank(array('message'=>'admin.flyer_management.supplierId'))),
//          	));
         	
// 			//$builder->add ('supplier_id', 'hidden', array('data' => $this->userId));
//         } 
        
        if($this->mod == 'add')
        {
		    $builder->add('flyerUploadedPath', 'file', array(
		        		'required'  => false,        		
		        		'constraints' => array(new Assert\NotBlank(array('message'=>'admin.flyer_management.fileUpload.not_blank')),
		        		new Assert\File(array('mimeTypes' => array('image/jpg','image/jpeg','image/png','image/bmp','image/gif'), 'mimeTypesMessage' => "admin.flyer_management.fileUpload.image_type"))),
			    		'data_class' => null,
		        ));
		    $builder->add('extraFlyerUploadedPath', 'file', array(
		    		'required'  => false,
		    		'constraints' => array(new Assert\NotBlank(array('message'=>'admin.flyer_management.fileUpload.not_blank')),
		    				new Assert\File(array('mimeTypes' => array('image/jpg','image/jpeg','image/png','image/bmp','image/gif'), 'mimeTypesMessage' => "admin.flyer_management.fileUpload.image_type"))),
		    		'data_class' => null,
		    ));
		    $builder->add('flyerUploadedPdf', 'file', array(
		    			  'required'  => false,
		    				 'constraints' => array(new Assert\NotBlank(array('message'=>'admin.flyer_management.fileUpload.not_blank')),
		    				new Assert\File(array('mimeTypes' => array('application/pdf','application/x-pdf'), 'mimeTypesMessage' => "admin.flyer_management.fileUpload.pdf_type"))),
		    				'data_class' => null,
		    ));
		    
		    $builder->add('extraFlyerUploadedPdf', 'file', array(
		    		'required'  => false,
		    		'constraints' => array(new Assert\NotBlank(array('message'=>'admin.flyer_management.fileUpload.not_blank')),
		    				new Assert\File(array('mimeTypes' => array('application/pdf','application/x-pdf'), 'mimeTypesMessage' => "admin.flyer_management.fileUpload.pdf_type"))),
		    		'data_class' => null,
		    ));
        
        }
        else 
        {
        	$builder->add('flyerUploadedPath', 'file', array(
        			'required'  => false,
        			'constraints' => array(new Assert\File(array('mimeTypes' => array('image/jpg','image/jpeg','image/png','image/bmp','image/gif'), 'mimeTypesMessage' => "admin.flyer_management.fileUpload.image_type"))),
        		    'data_class' => null,
        	));
        	$builder->add('extraFlyerUploadedPath', 'file', array(
        			'required'  => false,
        			'constraints' => array(new Assert\File(array('mimeTypes' => array('image/jpg','image/jpeg','image/png','image/bmp','image/gif'), 'mimeTypesMessage' => "admin.flyer_management.fileUpload.image_type"))),
        			'data_class' => null,
        	));
        	$builder->add('flyerUploadedPdf', 'file', array(
        			'required'  => false,
        			'constraints' => array(new Assert\File(array('mimeTypes' => array('application/pdf','application/x-pdf'), 'mimeTypesMessage' => "admin.flyer_management.fileUpload.pdf_type"))),
        			'data_class' => null,
        	));
        	
        	$builder->add('extraFlyerUploadedPdf', 'file', array(
        			'required'  => false,
        			'constraints' => array(new Assert\File(array('mimeTypes' => array('application/pdf','application/x-pdf'), 'mimeTypesMessage' => "admin.flyer_management.fileUpload.pdf_type"))),
        			'data_class' => null,
        	));
        
        }
       // $builder->add ('supplier_id', 'hidden');
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'OfficeBrain\Bundle\FlyerBundle\Entity\Flyer'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        //return 'officebrain_bundle_flyerbundle_flyerbanner';
    	return 'flyerown';
    }
    
    
    public function getUserRole()
    {
    	return $this->userManager->findIsAdmin();     	
    	
    }
    
    /**
     * @author Employee Id: 4571
     *
     * Description : This function fetches all the suppliers
     *
     **/
    public function getFlyerSubCategories()
    {    	
    	$userInfo = $this->userManager->getFlyerSubCategories($this->parentcategoryid);
    	$tempArr = array();
    	$id= '';
    	foreach ($userInfo as $vals){
			$id = $vals['id'];
    		$tempArr[$id] = $vals['title'];
    	}
    	return $tempArr;
    }
    
}
