public function editAction(Request $request)
 {
     $id = $request->get('id');
     $em = $this->getDoctrine()->getManager();
     $imageo = $em->getRepository('AcmeImageBundle:Image')->find($id);
     $em = $this->getDoctrine()->getManager();
     $image = $em->getRepository('AcmeImageBundle:Image')->find($id);
     $image1 = new \Acme\ImageBundle\Entity\Image();
     $image1->setTitle($image->getTitle());
     $image1->setLink($image->getLink());
     $image1->setDescription($image->getDescription());
     $form = $this->createFormBuilder($image1, array('action' => 'edit?id=' . $id))->add('title', 'text')->add('link', 'text')->add('description', 'textarea')->add('image', 'file', array('error_bubbling' => true, 'required' => false))->add('backgroundimage', 'file', array('error_bubbling' => true, 'required' => false))->getForm();
     $form->handleRequest($request);
     $validator = $this->get('validator');
     $errors = $validator->validate($image1);
     if (count($errors) >= 3) {
         return $this->render('AcmeImageBundle:Default:edit.html.twig', array('form' => $form->createView(), 'image' => $imageo->getImage(), 'backgroundimage' => $imageo->getBackgroundimage(), 'id' => $id));
     } else {
         if ($request->isMethod('post') == 'edit') {
             $dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
             $slider_background_image_dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/slider_background_image/';
             $em = $this->getDoctrine()->getManager();
             $f = $request->request->get('form');
             $image = $em->getRepository('AcmeImageBundle:Image')->find($id);
             //$image = $this->getDoctrine()->getRepository('AcmeImageBundle:Image')->find($request->get('id'));
             $image->setTitle($f['title']);
             $image->setLink($f['link']);
             $image->setDescription($f['description']);
             $old_image = $image->getImage();
             $old_background_image = $image->getBackgroundimage();
             if ($_FILES['form']['name']['image']) {
                 if (file_exists($dir . "/" . $old_image)) {
                     unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $old_image);
                     $new_image = rand() . '_' . $form['image']->getData()->getClientOriginalName();
                     $image->setImage($new_image);
                     $form->get('image')->getData()->move($dir, $new_image);
                 }
             }
             if ($_FILES['form']['name']['backgroundimage']) {
                 if (file_exists($slider_background_image_dir . "/" . $old_background_image)) {
                     unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/slider_background_image/' . $old_background_image);
                     $new_background_image = rand() . '_' . $form['backgroundimage']->getData()->getClientOriginalName();
                     $image->setBackgroundimage($new_background_image);
                     $form->get('backgroundimage')->getData()->move($slider_background_image_dir, $new_background_image);
                 }
             }
             $em->flush();
             return $this->redirect($this->generateUrl('acme_image'));
         }
     }
     return $this->render('AcmeImageBundle:Default:edit.html.twig', array('form' => $form->createView(), 'image' => $imageo->getImage(), 'backgroundimage' => $imageo->getBackgroundimage(), 'id' => $id));
 }
 public function editAction(Request $request)
 {
     $id = $request->get('id');
     $em = $this->getDoctrine()->getManager();
     $imageo = $em->getRepository('AcmeImageBundle:Image')->find($id);
     $em = $this->getDoctrine()->getManager();
     $image = $em->getRepository('AcmeImageBundle:Image')->find($id);
     $image = new \Acme\ImageBundle\Entity\Image();
     $image->setImage($image->getImage());
     $form = $this->createFormBuilder($image, array('action' => 'edit?id=' . $id))->add('image', 'file', array('error_bubbling' => TRUE))->getForm();
     $form->handleRequest($request);
     $validator = $this->get('validator');
     $errors = $validator->validate($image);
     if (count($errors) > 1) {
         return $this->redirect($this->generateUrl('acme_image'));
     } else {
         if ($request->isMethod('post') == 'edit') {
             if ($_FILES['form']['name']['image'] != NULL) {
                 $dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
                 $em = $this->getDoctrine()->getManager();
                 $image = $em->getRepository('AcmeImageBundle:Image')->find($id);
                 $old_image = $image->getImage();
                 unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/' . $old_image);
                 $new_image = rand() . '_' . $form['image']->getData()->getClientOriginalName();
                 $image = $this->getDoctrine()->getRepository('AcmeImageBundle:Image')->find($request->get('id'));
                 $image->setImage($new_image);
                 $em->flush();
                 $form->get('image')->getData()->move($dir, $new_image);
                 return $this->redirect($this->generateUrl('acme_image'));
             } else {
                 return $this->redirect($this->generateUrl('acme_image'));
             }
         }
     }
     return $this->render('AcmeImageBundle:Default:edit.html.twig', array('form' => $form->createView(), 'image' => $imageo->getImage(), 'id' => $id));
 }