public function newAdsProductAction($id,Request $request)
    {
    	if(!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
    		return $this->redirectToRoute('fos_user_security_login');
    	}
    	 
    	$formData=new Formdata();
    	$adProduct=new AdsmanagerProduct();
    	$message=new Message();

    	
    	$form=$this->createForm(new AdsmanagerProductType(),$adProduct);
    	if(!$formData->isSetFormat($request))
    	{
    		return $this->render('AdsmanagerBundle:Product:newProductAds.html.twig',array("form"=>$form->createView()));
    	}
    	
    	if(!$formData->isValidFormat($request,$form,$message))
    	{
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Product:newProductAds.html.twig',array("form"=>$form->createView()));
    		 
    	}
    	
    	$image = $request->files->get('imagen');
    	
    	if(!$formData->isValidImage($image,$form,$adProduct,$message,'name')){
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Product:newProductAds.html.twig',array("form"=>$form->createView()));
    		 
    	}
    	$price=$request->request->get('price');
    	$adProduct->setPrice(str_replace('.', '', $price));
    	$adProduct->setIdAd($id);
    	$formData->insertData($this,$adProduct);
    	$message->setSuccessMessages("El Producto o servicio ha sido Ingresado Exitosamente.")->show($this);
    	$this->setNameIdImages ( $formData,$adProduct, $image );
    	 
    	if($image){
    		$formData->uploadImages($image,'images/ids/'.$id,$adProduct);
    		$this->resizeImages($id,$adProduct->getImages());
    	}
    	 
    	return $this->redirectToRoute('my_ads');
    }
    public function newAdsAction(Request $request)
    {
    	
    	if(!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
    		return $this->redirectToRoute('fos_user_security_login');
    	}
    	
    	$formData=new Formdata();
    	$ad=new AdsmanagerAds();
    	$message=new Message();
    	
    	$categories=$this->getDoctrine()
    	->getRepository("AdsmanagerBundle:AdsmanagerCategories");
    	$query = $categories->createQueryBuilder('a')
    	->addOrderBy('a.name', 'ASC')
    	->getQuery();
    	
    	$categories = $query->getResult();
    	
    	$cities=new AdsmanagerCities();
    	$userLocation = $this->get('security.context')->getToken()->getUser()->getLocation();
    	$locationCity=$cities->getAdCity($this->getDoctrine()->getEntityManager(),$userLocation);
    	
    	    	
    	$form=$this->createForm(new AdsmanagerAdsType($this->getDoctrine()->getEntityManager()),$ad,array('validation_groups'=>'add'));
    	if(!$formData->isSetFormat($request))
    	{
			return $this->render('AdsmanagerBundle:Ads:newAds.html.twig',array("form"=>$form->createView(),"categories"=>$categories,"location"=>$locationCity));
    	}

    	if(!$formData->isValidFormat($request,$form,$message))
    	{
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Ads:newAds.html.twig',array("form"=>$form->createView(),"categories"=>$categories,"location"=>$locationCity));
    	
    	}

    	$image = $request->files->get('imagen');

    	if(!$formData->isValidImage($image,$form,$ad,$message,'adHeadline')){
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Ads:newAds.html.twig',array("form"=>$form->createView(),"categories"=>$categories,"location"=>$locationCity));
     		
    	}
    	$userId = $this->get('security.context')->getToken()->getUser()->getId();
    	$ad->setUserid($userId);
    	$date = new DateTime();
    	$ad->setDateCreated($date);
    	$createDate = new DateTime();
    	$ad->setExpirationDate($createDate->add(new DateInterval('P1Y')));
    	
    	$formData->insertData($this,$ad);
    	$message->setSuccessMessages("El anuncio ha sido Ingresado Exitosamente.")->show($this);
    	$this->setNameIdImages ( $formData, $ad, $image );
    	
    	if($image){
    	$formData->uploadImages($image,'images/ids/'.$ad->getId(),$ad);
    	$this->resizeImages($ad->getId(),$ad->getImages());
    	}
    	
    	return $this->redirectToRoute('my_ads');
    }
    public function newAdsProductAction($id,Request $request)
    {
    	if(!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
    		return $this->redirectToRoute('fos_user_security_login');
    	}
    	 
    	$formData=new Formdata();
    	$adProduct=new AdsmanagerProduct();
    	$message=new Message();

    	
    	$form=$this->createForm(new AdsmanagerProductType(),$adProduct);
    	if(!$formData->isSetFormat($request))
    	{
    		return $this->render('AdsmanagerBundle:Product:newProductAds.html.twig',array("form"=>$form->createView()));
    	}
    	
    	if(!$formData->isValidFormat($request,$form,$message))
    	{
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Product:newProductAds.html.twig',array("form"=>$form->createView()));
    		 
    	}
    	
    	$image = $request->files->get('imagen');
    	
    	if(!$formData->isValidImage($image,$form,$adProduct,$message,'name')){
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Product:newProductAds.html.twig',array("form"=>$form->createView()));
    		 
    	}



        $userId = $this->get('security.context')->getToken()->getUser()->getId();
        $userType=$this->getDoctrine()->getRepository("AdsmanagerBundle:AdsUsers")->find($userId);
        $numberAds = $this->getDoctrine()->getManager();
        $query = $numberAds->createQuery('SELECT a.id FROM AdsmanagerBundle:AdsmanagerAds a 
                                          Where a.userid= :idUser')->setParameter('idUser',$userId);
        $result = $query->getResult();

        $numberProducts=0;
        foreach ($result as $row) {
            $query = $numberAds->createQuery('SELECT COUNT(a.idAd) FROM AdsmanagerBundle:AdsmanagerProduct a 
            Where a.idAd= :id')->setParameter('id',$row['id']);
            $numberAdsResult = $query->getSingleScalarResult();
            $numberProducts=$numberAdsResult + $numberProducts;
        }

   
        $price=$request->request->get('price');
        
        if($userType->getAccounttype()==0 &&  $numberProducts <3)
            {   

             $this->setAddProduct($adProduct,$formData,$message,$price,$image,$id);
            }
        elseif($userType->getAccounttype()==1 &&  $numberProducts <10) 
            {
              $this->setAddProduct($adProduct,$formData,$message,$price,$image,$id);
            }
        elseif($userType->getAccounttype()==2 || $userType->getAccounttype()==3)
            {
               $this->setAddProduct($adProduct,$formData,$message,$price,$image,$id);
            }
        else{
          $message->setErrorMessages("Tiene que actualizar su cuenta para publicar un nuevo producto.")->show($this);   
        }
        
    	return $this->redirectToRoute('my_ads');

    }