PageRenderTime 1118ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/roomastic/src/Hoteles/BackendBundle/Controller/HomeController.php

https://gitlab.com/jpg/roomastic
PHP | 305 lines | 180 code | 62 blank | 63 comment | 17 complexity | 9bb0a66dfd66b42a5ff000da23ea1a6b MD5 | raw file
  1. <?php
  2. namespace Hoteles\BackendBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  7. use Hoteles\BackendBundle\Entity\Home;
  8. use Hoteles\BackendBundle\Form\HomeType;
  9. use Symfony\Component\Form\FormError;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. /**
  12. * Home controller.
  13. *
  14. * @Route("/admin/homeconfiguracion")
  15. */
  16. class HomeController extends Controller
  17. {
  18. /**
  19. * Lists all Home entities.
  20. *
  21. * @Route("/", name="admin_homeconfiguracion")
  22. * @Template()
  23. */
  24. public function indexAction()
  25. {
  26. $repository = $this->getDoctrine()->getRepository('HotelesBackendBundle:Home');
  27. $query = $repository->createQueryBuilder('f')
  28. ->orderBy('f.posicion', 'ASC')
  29. ->getQuery();
  30. $entities = $query->getResult();
  31. return array('entities' => $entities);
  32. }
  33. /**
  34. * reordena lista de faqs
  35. *
  36. * @Route("/ordena/{orden}", name="admin_ordenahome", options={"expose"=true})
  37. * @Template()
  38. */
  39. public function ordenaAction($orden)
  40. {
  41. $elementos = explode('_', $orden);
  42. unset($elementos[0]);
  43. $em = $this->getDoctrine()->getEntityManager();
  44. $entity = $em->getRepository('HotelesBackendBundle:Home')->findAll();
  45. $cont = 1;
  46. foreach ($elementos as $orden) {
  47. foreach ($entity as $faq) {
  48. if ($orden == $faq->getId()) {
  49. $faq->setPosicion($cont);
  50. $em->persist($faq);
  51. }
  52. }
  53. $cont = $cont + 1;
  54. }
  55. $em->flush();
  56. echo "1";
  57. exit;
  58. return array();
  59. }
  60. /**
  61. * Finds and displays a Home entity.
  62. *
  63. * @Route("/{id}/show", name="admin_homeconfiguracion_show")
  64. * @Template()
  65. */
  66. public function showAction($id)
  67. {
  68. $em = $this->getDoctrine()->getEntityManager();
  69. $entity = $em->getRepository('HotelesBackendBundle:Home')->find($id);
  70. if (!$entity) {
  71. throw $this->createNotFoundException('Unable to find Home entity.');
  72. }
  73. $deleteForm = $this->createDeleteForm($id);
  74. return array(
  75. 'entity' => $entity,
  76. 'delete_form' => $deleteForm->createView(),);
  77. }
  78. /**
  79. * Displays a form to create a new Home entity.
  80. *
  81. * @Route("/new", name="admin_homeconfiguracion_new")
  82. * @Template()
  83. */
  84. public function newAction()
  85. {
  86. $entity = new Home();
  87. $form = $this->createForm(new HomeType(), $entity);
  88. return array(
  89. 'entity' => $entity,
  90. 'form' => $form->createView()
  91. );
  92. }
  93. /**
  94. * Creates a new Home entity.
  95. *
  96. * @Route("/create", name="admin_homeconfiguracion_create")
  97. * @Method("post")
  98. * @Template("HotelesBackendBundle:Home:new.html.twig")
  99. */
  100. public function createAction()
  101. {
  102. $entity = new Home();
  103. $request = $this->getRequest();
  104. $form = $this->createForm(new HomeType(), $entity);
  105. $form->bindRequest($request);
  106. if ($_FILES["hoteles_backendbundle_hometype"]["name"]["imagen"] == '') {
  107. $error = new formerror("Debe de incluir una imagen");
  108. $form->get('imagen')->addError($error);
  109. }
  110. if ($form->isValid()) {
  111. $em = $this->getDoctrine()->getEntityManager();
  112. $totalFaqs = $em->getRepository('HotelesBackendBundle:Faqs')->findAll();
  113. $entity->setPosicion(count($totalFaqs) + 1);
  114. //if ($form['imagen']->getData() !== null ) {
  115. $someNewFilename = md5(uniqid(microtime(), true)) . '.png';
  116. $form['imagen']->getData()->move($this->get('kernel')->getRootDir() . "/../web/uploads/home", $someNewFilename);
  117. $entity->setImagen($someNewFilename);
  118. //}
  119. $em = $this->getDoctrine()->getEntityManager();
  120. $em->persist($entity);
  121. $em->flush();
  122. $this->get('session')->setFlash('success', 'Creado correctamente.');
  123. return $this->redirect($this->generateUrl('admin_homeconfiguracion'));
  124. } else {
  125. $this->get('session')->setFlash('nosuccess', 'No se pudo crear.');
  126. }
  127. return array(
  128. 'entity' => $entity,
  129. 'form' => $form->createView()
  130. );
  131. }
  132. /**
  133. * Displays a form to edit an existing Home entity.
  134. *
  135. * @Route("/{id}/edit", name="admin_homeconfiguracion_edit")
  136. * @Template()
  137. */
  138. public function editAction($id)
  139. {
  140. $em = $this->getDoctrine()->getEntityManager();
  141. $entity = $em->getRepository('HotelesBackendBundle:Home')->find($id);
  142. if (!$entity) {
  143. throw $this->createNotFoundException('Unable to find Home entity.');
  144. }
  145. $editForm = $this->createForm(new HomeType(), $entity);
  146. $deleteForm = $this->createDeleteForm($id);
  147. return array(
  148. 'entity' => $entity,
  149. 'edit_form' => $editForm->createView(),
  150. 'delete_form' => $deleteForm->createView(),
  151. );
  152. }
  153. /**
  154. * Edits an existing Home entity.
  155. *
  156. * @Route("/{id}/update", name="admin_homeconfiguracion_update")
  157. * @Method("post")
  158. * @Template("HotelesBackendBundle:Home:edit.html.twig")
  159. */
  160. public function updateAction($id)
  161. {
  162. $em = $this->getDoctrine()->getEntityManager();
  163. $entity = $em->getRepository('HotelesBackendBundle:Home')->find($id);
  164. $image = $entity->getImagen();
  165. if (!$entity) {
  166. throw $this->createNotFoundException('Unable to find Home entity.');
  167. }
  168. $editForm = $this->createForm(new HomeType(), $entity);
  169. $deleteForm = $this->createDeleteForm($id);
  170. $request = $this->getRequest();
  171. $editForm->bindRequest($request);
  172. if ($editForm->isValid()) {
  173. if ($editForm['imagen']->getData() == null) {
  174. $entity->setImagen($image);
  175. } else {
  176. $someNewFilename1 = md5(uniqid(microtime(), true)) . '.png';
  177. $editForm['imagen']->getData()->move($this->get('kernel')->getRootDir() . "/../web/uploads/home", $someNewFilename1);
  178. $entity->setImagen($someNewFilename1);
  179. }
  180. $em->persist($entity);
  181. $em->flush();
  182. $this->get('session')->setFlash('success', 'Editado correctamente.');
  183. return $this->redirect($this->generateUrl('admin_homeconfiguracion_edit', array('id' => $id)));
  184. } else {
  185. $this->get('session')->setFlash('nosuccess', 'No se pudo editar.');
  186. }
  187. return array(
  188. 'entity' => $entity,
  189. 'edit_form' => $editForm->createView(),
  190. 'delete_form' => $deleteForm->createView(),
  191. );
  192. }
  193. /**
  194. * Deletes a Home entity.
  195. *
  196. * @Route("/{id}/delete", name="admin_homeconfiguracion_delete")
  197. * @Method("post")
  198. */
  199. public function deleteAction($id)
  200. {
  201. $form = $this->createDeleteForm($id);
  202. $request = $this->getRequest();
  203. $form->bindRequest($request);
  204. if ($form->isValid()) {
  205. $em = $this->getDoctrine()->getEntityManager();
  206. $entity = $em->getRepository('HotelesBackendBundle:Home')->find($id);
  207. if (!$entity) {
  208. throw $this->createNotFoundException('Unable to find Home entity.');
  209. }
  210. $em->remove($entity);
  211. $em->flush();
  212. $this->get('session')->setFlash('success', 'Borrado correctamente.');
  213. }
  214. return $this->redirect($this->generateUrl('admin_homeconfiguracion'));
  215. }
  216. private function createDeleteForm($id)
  217. {
  218. return $this->createFormBuilder(array('id' => $id))
  219. ->add('id', 'hidden')
  220. ->getForm()
  221. ;
  222. }
  223. /**
  224. * Deletes a User entity.
  225. *
  226. * @Route("/{id}/delete", name="admin_homeconfiguracion_deleteget")
  227. * @Method("get")
  228. */
  229. public function deletegetAction($id)
  230. {
  231. $request = $this->getRequest();
  232. $em = $this->getDoctrine()->getEntityManager();
  233. $entity = $em->getRepository('HotelesBackendBundle:Home')->find($id);
  234. if (!$entity) {
  235. throw $this->createNotFoundException('Unable to find Home entity.');
  236. }
  237. $em->remove($entity);
  238. $em->flush();
  239. $this->get('session')->setFlash('success', 'Borrado correctamente.');
  240. $referer = $request->headers->get('referer');
  241. return new RedirectResponse($referer);
  242. }
  243. }