Beispiel #1
0
 public function addAction(Request $request)
 {
     $work = new \Acme\InfoBundle\Entity\Work();
     $work->setTitle('');
     $work->setDescription('');
     $work->setImage('');
     $form = $this->createFormBuilder($work)->add('title', 'text')->add('description', 'textarea')->add('image', 'file')->getForm();
     $form->handleRequest($request);
     $validator = $this->get('validator');
     $errors = $validator->validate($work);
     if (count($errors) > 0) {
         return $this->render('AcmeInfoBundle:Work:add.html.twig', array('form' => $form->createView()));
     } else {
         $a = $request->request->get('form');
         $img = $form['image']->getData()->getClientOriginalName();
         $work = new Work();
         $work->setTitle($a['title']);
         $work->setDescription($a['description']);
         $work->setImage($img);
         $em = $this->getDoctrine()->getManager();
         $em->persist($work);
         $em->flush();
         $dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/works/' . $work->getId();
         mkdir($dir);
         $form->get('image')->getData()->move($dir, $img);
         return $this->redirect($this->generateUrl('acme_info_work'));
     }
     return $this->render('AcmeInfoBundle:Work:add.html.twig', array('form' => $form->createView()));
 }
Beispiel #2
0
 public function editAction(Request $request)
 {
     $id = $request->get('id');
     $em = $this->getDoctrine()->getManager();
     $work = $em->getRepository('AcmeInfoBundle:Work')->find($id);
     $oldimg = $work->getImage();
     $works = new \Acme\InfoBundle\Entity\Work();
     $works->setTitle($work->getTitle());
     $works->setDescription($work->getDescription());
     $form = $this->createFormBuilder($works)->add('title', 'text')->add('description', 'textarea')->add('image', 'file', array('error_bubbling' => TRUE, 'required' => false))->getForm();
     $form->handleRequest($request);
     $validator = $this->get('validator');
     $errors = $validator->validate($works);
     if ($request->getMethod() == 'POST') {
         if (count($errors) > 1) {
             return $this->render('AcmeInfoBundle:Work:edit.html.twig', array('form' => $form->createView(), 'work' => $work, 'id' => $id, 'image' => $oldimg));
         } else {
             if ($_FILES['form']['name']['image']) {
                 $serv = $em->getRepository('AcmeInfoBundle:Work')->find($id);
                 unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/works/' . $serv->getId() . '/' . $oldimg);
                 $newimg = $form['image']->getData()->getClientOriginalName();
                 $serv->setImage($newimg);
                 $em->persist($serv);
                 $em->flush();
                 $img = $form['image']->getData()->getClientOriginalName();
                 $dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/works/' . $serv->getId() . '/';
                 $form->get('image')->getData()->move($dir, $img);
                 return $this->redirect($this->generateUrl('acme_info_work'));
             } else {
                 $a = $request->request->get('form');
                 $work->setImage($oldimg);
                 $work->setTitle($a['title']);
                 $work->setDescription($a['description']);
                 $em->persist($work);
                 $em->flush();
                 return $this->redirect($this->generateUrl('acme_info_work'));
             }
         }
     }
     return $this->render('AcmeInfoBundle:Work:edit.html.twig', array('form' => $form->createView(), 'work' => $work, 'id' => $id, 'image' => $oldimg));
 }
 /**
  * {@inheritDoc}
  */
 public function getOffset()
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'getOffset', array());
     return parent::getOffset();
 }