public function addAction(Request $request) { $news = new \Acme\InfoBundle\Entity\News(); $news->setTitle(''); $news->setDescription(''); $news->setImage(''); $form = $this->createFormBuilder($news)->add('title', 'text')->add('description', 'textarea')->add('image', 'file')->getForm(); $form->handleRequest($request); $validator = $this->get('validator'); $errors = $validator->validate($news); if (count($errors) > 0) { return $this->render('AcmeInfoBundle:Newslist:add.html.twig', array('form' => $form->createView())); } else { $a = $request->request->get('form'); $img = $form['image']->getData()->getClientOriginalName(); $news = new News(); $news->setTitle($a['title']); $news->setDescription($a['description']); $news->setImage($img); $em = $this->getDoctrine()->getManager(); $em->persist($news); $em->flush(); $dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/news/' . $news->getId(); mkdir($dir); $form->get('image')->getData()->move($dir, $img); return $this->redirect($this->generateUrl('acme_info_newslist')); } return $this->render('AcmeInfoBundle:Newslist:add.html.twig', array('form' => $form->createView())); }
public function editAction(Request $request) { $id = $request->get('id'); $em = $this->getDoctrine()->getManager(); $new = $em->getRepository('AcmeInfoBundle:News')->find($id); $oldimg = $new->getImage(); $news = new \Acme\InfoBundle\Entity\News(); $news->setTitle($new->getTitle()); $news->setDescription($new->getDescription()); $form = $this->createFormBuilder($news)->add('title', 'text')->add('description', 'textarea')->add('image', 'file', array('error_bubbling' => TRUE))->getForm(); $form->handleRequest($request); $validator = $this->get('validator'); $errors = $validator->validate($news); if ($request->getMethod() == 'POST') { if (count($errors) > 1) { return $this->render('AcmeInfoBundle:Newslist:edit.html.twig', array('form' => $form->createView(), 'news' => $news, 'id' => $id, 'image' => $oldimg)); } else { if ($_FILES['form']['name']['image']) { $new = $em->getRepository('AcmeInfoBundle:News')->find($id); if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/uploads/news/' . $new->getId() . '/' . $oldimg)) { unlink($_SERVER['DOCUMENT_ROOT'] . '/uploads/news/' . $new->getId() . '/' . $oldimg); } $newimg = $form['image']->getData()->getClientOriginalName(); $new->setImage($newimg); $em->persist($new); $em->flush(); $img = $form['image']->getData()->getClientOriginalName(); $dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads/news/' . $new->getId() . '/'; $form->get('image')->getData()->move($dir, $img); return $this->redirect($this->generateUrl('acme_info_newslist')); } else { $a = $request->request->get('form'); $new->setImage($oldimg); $new->setTitle($a['title']); $new->setDescription($a['description']); $em->persist($new); $em->flush(); return $this->redirect($this->generateUrl('acme_info_newslist')); } } } return $this->render('AcmeInfoBundle:Newslist:edit.html.twig', array('form' => $form->createView(), 'news' => $news, 'id' => $id, 'image' => $oldimg)); }
public function newsAction(Request $request) { $news = $this->get('doctrine')->getRepository('AcmeInfoBundle:News')->findAll(); $order_by = ''; //get Entity manager instance $em = $this->get('doctrine.orm.entity_manager'); //get repository for class 'Language' : LanguageRepository.php $repository = $em->getRepository('Acme\\InfoBundle\\Entity\\News'); //get count of languages for using with Paginator class //Using custom made database query function in LanguageRepository class $languagesCount = $repository->getIdCount(); //When creating new paginator object it takes care for pages and items //organization based on numbers of items from database and limit variable in $_GET $paginator = new News($languagesCount); //get returned HTML string from Paginator to render paginator HTML //elements in the template $strPaginator = $paginator->RenderPaginator(); //If we have POST variable defined, than it is defined order of items //from inside form (clicking on sorting column for example) $sort_direction = "asc"; $news = $this->get('doctrine')->getRepository('AcmeInfoBundle:News')->findAll(); $page = $languagesCount / 3; $news = $repository->getProductListWithPagination($sort_direction, $paginator->getOffset(), $paginator->getLimit()); $newsletter = new \Acme\HomeBundle\Entity\Newsletter(); $newsletter->setEmail(''); $form = $this->createFormBuilder($newsletter)->add('email')->getForm(); $form->handleRequest($request); $validator = $this->get('validator'); $errors = $validator->validate($newsletter); if (count($errors) > 0) { return $this->render('AcmeHomeBundle:Default:news.html.twig', array('news' => $news, 'sort_dir' => $sort_direction, 'paginator' => $strPaginator, 'count' => $languagesCount, 'page' => $page, 'form' => $form->createView())); } else { if ($request->getMethod() == 'POST') { $a = $request->request->get('form'); $add = Newsletter::addNewsletter($a['email']); return $this->render('AcmeHomeBundle:Default:news.html.twig', array('news' => $news, 'sort_dir' => $sort_direction, 'paginator' => $strPaginator, 'count' => $languagesCount, 'page' => $page, 'form' => $form->createView())); } } return $this->render('AcmeHomeBundle:Default:news.html.twig', array('news' => $news, 'sort_dir' => $sort_direction, 'paginator' => $strPaginator, 'count' => $languagesCount, 'page' => $page, 'form' => $form->createView())); }
public function newsAction(Request $request) { $news = $this->get('doctrine')->getRepository('AcmeInfoBundle:News')->findAll(); $order_by = ''; //get Entity manager instance $em = $this->get('doctrine.orm.entity_manager'); //get repository for class 'Language' : LanguageRepository.php $repository = $em->getRepository('Acme\\InfoBundle\\Entity\\News'); //get count of languages for using with Paginator class //Using custom made database query function in LanguageRepository class $languagesCount = $repository->getIdCount(); //When creating new paginator object it takes care for pages and items //organization based on numbers of items from database and limit variable in $_GET $paginator = new News($languagesCount); //get returned HTML string from Paginator to render paginator HTML //elements in the template $strPaginator = $paginator->RenderPaginator(); //If we have POST variable defined, than it is defined order of items //from inside form (clicking on sorting column for example) $sort_direction = "asc"; /*if ('POST' === $this->get('request')->getMethod()) { $order_by = array($_POST['filter_order'] => $_POST['filter_order_Dir']); $sort_direction = $_POST['filter_order_Dir'] == 'asc' ? 'desc' : 'asc'; } else { //We know that nothing is changed for ordering columns - //this is alse default order of items when page is first opened $order_by = array('sort_order' => 'asc', 'id' => 'asc'); $sort_direction = 'desc'; }*/ //To fill $languages for forwarding it to the template, we first call database function //with $offset and $limit to get items we wanted //$languages = $repository->getProductListWithPagination($order_by, $paginator->getOffset(), $paginator->getLimit()); //Finally - return array to templating engine for displaying data. $news = $this->get('doctrine')->getRepository('AcmeInfoBundle:News')->findAll(); $page = $languagesCount / 3; $news = $repository->getProductListWithPagination($sort_direction, $paginator->getOffset(), $paginator->getLimit()); return $this->render('AcmeHomeBundle:Default:news.html.twig', array('news' => $news, 'sort_dir' => $sort_direction, 'paginator' => $strPaginator, 'count' => $languagesCount, 'page' => $page)); }
/** * {@inheritDoc} */ public function getOffset() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getOffset', array()); return parent::getOffset(); }