Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Msi\CmsBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
- use Msi\CmsBundle\Entity\News;
- use Msi\CmsBundle\Form\NewsType;
- use Symfony\Component\HttpFoundation\File\UploadedFile;
- /**
- * News controller.
- *
- * @Route("/news")
- */
- class NewsController extends Controller
- {
- /**
- * Lists all News entities.
- *
- * @Route("/", name="news")
- * @Template()
- */
- public function indexAction()
- {
- $em = $this->getDoctrine()->getEntityManager();
- $entities = $em->getRepository('MsiCmsBundle:News')->findAll();
- return array('entities' => $entities);
- }
- /**
- * Finds and displays a News entity.
- *
- * @Route("/{id}/show", name="news_show")
- * @Template()
- */
- public function showAction($id)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $entity = $em->getRepository('MsiCmsBundle:News')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find News entity.');
- }
- $deleteForm = $this->createDeleteForm($id);
- return array(
- 'entity' => $entity,
- 'delete_form' => $deleteForm->createView(),
- );
- }
- /**
- * Displays a form to create a new News entity.
- *
- * @Route("/new", name="news_new")
- * @Template()
- */
- public function newAction()
- {
- $entity = new News();
- $form = $this->createForm(new NewsType(), $entity);
- return array(
- 'entity' => $entity,
- 'form' => $form->createView()
- );
- }
- /**
- * Creates a new News entity.
- *
- * @Route("/create", name="news_create")
- * @Method("post")
- * @Template("MsiCmsBundle:News:new.html.twig")
- */
- public function createAction()
- {
- $entity = new News();
- $request = $this->getRequest();
- $form = $this->createForm(new NewsType(), $entity);
- if ('POST' === $request->getMethod()) {
- $form->bindRequest($request);
- if ($form->isValid()) {
- $files=$request->files->get($form->getName());
- print_r($files);
- $file=$files["attachment"]["file"];
- $file->move(
- $_SERVER['DOCUMENT_ROOT']."/web/uploads",
- $file->getOriginalName()
- );
- $em = $this->getDoctrine()->getEntityManager();
- $entity->upload();
- $em->persist($entity);
- $em->flush();
- return $this->redirect($this->generateUrl('news'));
- }
- }
- return array(
- 'entity' => $entity,
- 'form' => $form->createView()
- );
- }
- /**
- * Displays a form to edit an existing News entity.
- *
- * @Route("/{id}/edit", name="news_edit")
- * @Template()
- */
- public function editAction($id)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $entity = $em->getRepository('MsiCmsBundle:News')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find News entity.');
- }
- $editForm = $this->createForm(new NewsType(), $entity);
- $deleteForm = $this->createDeleteForm($id);
- return array(
- 'entity' => $entity,
- 'form' => $editForm->createView(),
- 'delete_form' => $deleteForm->createView(),
- );
- }
- /**
- * Edits an existing News entity.
- *
- * @Route("/{id}/update", name="news_update")
- * @Method("post")
- * @Template("MsiCmsBundle:News:edit.html.twig")
- */
- public function updateAction($id)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $entity = $em->getRepository('MsiCmsBundle:News')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find News entity.');
- }
- $editForm = $this->createForm(new NewsType(), $entity);
- $deleteForm = $this->createDeleteForm($id);
- $request = $this->getRequest();
- if ('POST' === $request->getMethod()) {
- $editForm->bindRequest($request);
- if ($editForm->isValid()) {
- $em = $this->getDoctrine()->getEntityManager();
- $em = $this->getDoctrine()->getEntityManager();
- $entity->upload();
- $em->persist($entity);
- $em->flush();
- return $this->redirect($this->generateUrl('news'));
- }
- }
- return array(
- 'entity' => $entity,
- 'edit_form' => $editForm->createView(),
- 'delete_form' => $deleteForm->createView(),
- );
- }
- /**
- * Deletes a News entity.
- *
- * @Route("/{id}/delete", name="news_delete")
- * @Method("post")
- */
- public function deleteAction($id)
- {
- $em = $this->getDoctrine()->getEntityManager();
- $entity = $em->getRepository('MsiCmsBundle:News')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find News entity.');
- }
- $em->remove($entity);
- $em->flush();
- return $this->redirect($this->generateUrl('news'));
- }
- private function createDeleteForm($id)
- {
- return $this->createFormBuilder(array('id' => $id))
- ->add('id', 'hidden')
- ->getForm()
- ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement