Пример #1
0
    public function editAdsAction($id,Request $request)
    {
    	 
    	if(!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
    		return $this->redirectToRoute('fos_user_security_login');
    	}
    	
    	$ad=$this->getDoctrine()
    	->getRepository("AdsmanagerBundle:AdsmanagerAds")
    	->find($id);
    	
    	if(!$ad){
    		return $this->redirectToRoute('my_ads');
    	} 

    	if(!$this->isAnUserOwner ( $ad->getUserId() )){
    		return $this->redirectToRoute('my_ads');
    	}
    	
    	$formData=new Formdata();
    	$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))
    	{
    		$form->handleRequest($request);
    		return $this->render('AdsmanagerBundle:Ads:editAds.html.twig',array("form"=>$form->createView(),"categories"=>$categories,"location"=>$locationCity,"image"=>$ad->getImages(),"id"=>$ad->getId()));
    	}
    
    	if(!$formData->isValidFormat($request,$form,$message))
    	{
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Ads:editAds.html.twig',array("form"=>$form->createView(),"categories"=>$categories,"location"=>$locationCity,"image"=>$ad->getImages(),"id"=>$ad->getId()));
    		 
    	}

    	$image = $request->files->get('imagen');
    
    	if(!$formData->isValidImageUpdate($image,$form,$ad,$message,'adHeadline')){
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Ads:editAds.html.twig',array("form"=>$form->createView(),"categories"=>$categories,"location"=>$locationCity,"image"=>$ad->getImages(),"id"=>$ad->getId()));
    		 
    	}
    	
    	$userId = $this->get('security.context')->getToken()->getUser()->getId();
    	$ad->setUserid($userId);
    	$ad->setPublished(0);
    	$date = new DateTime();
    	$ad->setDateModified($date);

   	    $formData->updateData($this);
    	$message->setSuccessMessages("El anuncio ha sido Modificado Exitosamente.")->show($this);
    	
    	if($image){
    	$formData->uploadImages($image,'images/ids/'.$ad->getId(),$ad);
    	$this->resizeImages($ad->getId(),$ad->getImages());
    	}
    	return $this->redirectToRoute('my_ads');
    }
    public function editAdsProductAction($id,Request $request)
    {
    	if(!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY') ){
    		return $this->redirectToRoute('fos_user_security_login');
    	}
    	
    	$adProduct=$this->getDoctrine()
    	->getRepository("AdsmanagerBundle:AdsmanagerProduct")
    	->find($id);
    	 
    	if(!$adProduct){
    		return $this->redirectToRoute('my_ads');
    	}
    	
    	$ad=$this->getDoctrine()
    	->getRepository("AdsmanagerBundle:AdsmanagerAds")
    	->find($adProduct->getIdAd());
    	
    	if(!$this->isAnUserOwner ( $ad->getUserId() )){
    		return $this->redirectToRoute('my_ads');
    	}

    	$formData=new Formdata();
    	$message=new Message();
    	
    	 
    	$form=$this->createForm(new AdsmanagerProductType(),$adProduct);
    	if(!$formData->isSetFormat($request))
    	{
    		return $this->render('AdsmanagerBundle:Product:editProductAds.html.twig',array("form"=>$form->createView(),"id"=>$adProduct->getIdAd(),"image"=>$adProduct->getImages(),"price"=>$adProduct->getPrice()));
    	}
    	 
    	if(!$formData->isValidFormat($request,$form,$message))
    	{
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Product:editProductAds.html.twig',array("form"=>$form->createView(),"id"=>$adProduct->getIdAd(),"image"=>$adProduct->getImages(),"price"=>$adProduct->getPrice()));
    		 
    	}
    	 
    	$image = $request->files->get('imagen');
    	 
    	if(!$formData->isValidImageUpdate($image,$form,$adProduct,$message,'name')){
    		$message->show($this);
    		return $this->render('AdsmanagerBundle:Product:editProductAds.html.twig',array("form"=>$form->createView(),"id"=>$adProduct->getIdAd(),"image"=>$adProduct->getImages(),"price"=>$adProduct->getPrice()));
    		 
    	}
    	$ad->setPublished(0);
    	$date = new DateTime();
    	$ad->setDateModified($date);
    	$formData->updateData($this);
    	
    	$price=$request->request->get('price');
    	$adProduct->setPrice(str_replace('.', '', $price));
    	$formData->updateData($this);
    	$message->setSuccessMessages("El Producto o servicio ha sido Modificado Exitosamente.")->show($this);
    	
    	if($image){
    		$formData->uploadImages($image,'images/ids/'.$adProduct->getIdAd(),$adProduct);
    		$this->resizeImages($adProduct->getIdAd(),$adProduct->getImages());
    	}

    	return $this->redirectToRoute('my_ads');
    }