public function addAction(Request $request) { $image = new \Acme\ImageBundle\Entity\Image(); $image->setTitle(''); $image->setLink(''); $image->setDescription(''); $image->setImage(''); $image->setBackgroundimage(''); $form = $this->createFormBuilder($image)->add('title', 'text')->add('link', 'text')->add('description', 'textarea')->add('image', 'file')->add('backgroundimage', 'file')->getForm(); $form->handleRequest($request); $validator = $this->get('validator'); $errors = $validator->validate($image); if (count($errors) > 0) { return $this->render('AcmeImageBundle:Default:add.html.twig', array('form' => $form->createView())); } else { if ($request->isMethod('post') == 'add') { $dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads'; $slider_background_image_dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/slider_background_image/'; $a = $request->request->get('form'); $og_image = rand() . '_' . $form['image']->getData()->getClientOriginalName(); $og_backgroundimage = rand() . '_' . $form['backgroundimage']->getData()->getClientOriginalName(); $image = new \Acme\ImageBundle\Entity\Image(); $image->setTitle($a['title']); $image->setLink($a['link']); $image->setDescription($a['description']); $image->setImage($og_image); $image->setBackgroundimage($og_backgroundimage); $em = $this->getDoctrine()->getManager(); $em->persist($image); $em->flush(); $form->get('image')->getData()->move($dir, $og_image); $form->get('backgroundimage')->getData()->move($slider_background_image_dir, $og_backgroundimage); return $this->redirect($this->generateUrl('acme_image')); } } return $this->render('AcmeImageBundle:Default:add.html.twig', array('form' => $form->createView())); }
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)); }