/src/CreativeFolio/EditorBundle/Controller/StyleController.php

https://bitbucket.org/sebastien247/creativefolio · PHP · 307 lines · 155 code · 69 blank · 83 comment · 12 complexity · 51e4885a1a8ca35509788950eada27f5 MD5 · raw file

  1. <?php
  2. namespace CreativeFolio\EditorBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use CreativeFolio\EditorBundle\Entity\Style;
  7. use CreativeFolio\EditorBundle\Form\StyleType;
  8. /**
  9. * Style controller.
  10. *
  11. */
  12. class StyleController extends Controller
  13. {
  14. /**
  15. * Lists all Style entities.
  16. *
  17. */
  18. public function indexAction()
  19. {
  20. $em = $this->getDoctrine()->getManager();
  21. $entities = $em->getRepository('EditorBundle:Style')->findAll();
  22. return $this->render('EditorBundle:Style:index.html.twig', array(
  23. 'entities' => $entities,
  24. ));
  25. }
  26. /**
  27. * Finds and displays a Style entity.
  28. *
  29. */
  30. public function showAction($id)
  31. {
  32. $em = $this->getDoctrine()->getManager();
  33. $entity = $em->getRepository('EditorBundle:Style')->find($id);
  34. if (!$entity) {
  35. throw $this->createNotFoundException('Unable to find Style entity.');
  36. }
  37. $deleteForm = $this->createDeleteForm($id);
  38. return $this->render('EditorBundle:Style:show.html.twig', array(
  39. 'entity' => $entity,
  40. 'delete_form' => $deleteForm->createView(), ));
  41. }
  42. /**
  43. * Displays a form to create a new Style entity.
  44. *
  45. */
  46. public function newAction()
  47. {
  48. $entity = new Style();
  49. $form = $this->createForm(new StyleType(), $entity);
  50. return $this->render('EditorBundle:Style:new.html.twig', array(
  51. 'entity' => $entity,
  52. 'form' => $form->createView(),
  53. ));
  54. }
  55. /**
  56. * Creates a new Style entity.
  57. *
  58. */
  59. public function createAction(Request $request)
  60. {
  61. $entity = new Style();
  62. $form = $this->createForm(new StyleType(), $entity);
  63. $form->bind($request);
  64. if ($form->isValid()) {
  65. $em = $this->getDoctrine()->getManager();
  66. $em->persist($entity);
  67. $em->flush();
  68. return $this->redirect($this->generateUrl('style_show', array('id' => $entity->getId())));
  69. /*//Instancier une "réponse" grâce à l'objet "Response"
  70. $response = new Response(json_encode( array ('style' => $entity ) ));
  71. //Lui indiquer le type de format dans le quelle est envoyé la reponse
  72. $response->headers->set('Content-Type', 'application/json');
  73. return $response;*/
  74. }
  75. //exit;//return false;
  76. return $this->render('EditorBundle:Style:new.html.twig', array(
  77. 'entity' => $entity,
  78. 'form' => $form->createView(),
  79. ));
  80. }
  81. /**
  82. * Displays a form to edit an existing Style entity.
  83. *
  84. */
  85. public function editAction($id)
  86. {
  87. $em = $this->getDoctrine()->getManager();
  88. $entity = $em->getRepository('EditorBundle:Style')->find($id);
  89. if (!$entity) {
  90. throw $this->createNotFoundException('Unable to find Style entity.');
  91. }
  92. $editForm = $this->createForm(new StyleType(), $entity);
  93. $deleteForm = $this->createDeleteForm($id);
  94. return $this->render('EditorBundle:Style:edit.html.twig', array(
  95. 'entity' => $entity,
  96. 'edit_form' => $editForm->createView(),
  97. 'delete_form' => $deleteForm->createView(),
  98. ));
  99. }
  100. /**
  101. * JsonBlocsAction
  102. *
  103. */
  104. public function getBlocsJsonAction(Request $request)
  105. {
  106. $em = $this->getDoctrine()->getManager();
  107. $user = $this->container->get('security.context')->getToken()->getUser();
  108. $entity = $em->getRepository('EditorBundle:Cyberfolio')->findOneByUser($user)->getStyle();
  109. if (!$entity) {
  110. throw $this->createNotFoundException('Unable to find Style entity.');
  111. }
  112. $response = new Response( json_encode( $entity->getCss() ) );
  113. $response->headers->set('Content-Type', 'application/json');
  114. return $response;
  115. }
  116. /**
  117. * Edits an existing Style entity.
  118. *
  119. */
  120. public function updateOrderAction(Request $request)
  121. {
  122. $em = $this->getDoctrine()->getManager();
  123. $user = $this->container->get('security.context')->getToken()->getUser();
  124. $entity = $em->getRepository('EditorBundle:Cyberfolio')->findOneByUser($user)->getStyle();
  125. if (!$entity) {
  126. throw $this->createNotFoundException('Unable to find Style entity.');
  127. }
  128. $editForm = $this->createForm(new StyleType(), $entity);
  129. // Blocs dans la Base De Données
  130. var_dump($entity->getCss());
  131. $css = json_encode( $entity->getCss() ,true);
  132. var_dump($css);
  133. var_dump("toto");
  134. // Recupere le nouveaux bloc dans le formulaire
  135. $editForm->bind($request);
  136. // Bloc a ajouter
  137. var_dump($entity->getCss());
  138. $css2 = json_decode( $entity->getCss() , true);
  139. var_dump($request->request);
  140. var_dump($css2);
  141. // Ajoute le bloc
  142. $css['blocs'] = $css2;
  143. // Encode en obj json
  144. $css = json_encode( $css );
  145. var_dump("Encoder : ");
  146. var_dump($css);
  147. if($request->getMethod() == 'POST')
  148. {
  149. //if ($editForm->isValid()) {
  150. /* $entity->setCss($css);
  151. $em->persist($entity);
  152. $em->flush();*/
  153. exit; //return $this->redirect($this->generateUrl('style_edit', array('id' => $id)));
  154. //}
  155. }
  156. var_dump("not");
  157. exit;
  158. }
  159. /**
  160. * Edits an existing Style entity.
  161. *
  162. */
  163. public function updateAction(Request $request)
  164. {
  165. $em = $this->getDoctrine()->getManager();
  166. $user = $this->container->get('security.context')->getToken()->getUser();
  167. $entity = $em->getRepository('EditorBundle:Cyberfolio')->findOneByUser($user)->getStyle();
  168. //$entity = $em->getRepository('EditorBundle:Style')->find($id);
  169. if (!$entity) {
  170. throw $this->createNotFoundException('Unable to find Style entity.');
  171. }
  172. /* $deleteForm = $this->createDeleteForm($id);*/
  173. $editForm = $this->createForm(new StyleType(), $entity);
  174. // Blocs dans la Base De Données
  175. $css = json_decode( $entity->getCss() ,true);
  176. // Recupere le nouveaux bloc dans le formulaire
  177. $editForm->bind($request);
  178. // Bloc a ajouter
  179. //$css2 = json_decode( $entity->getCss() , true);
  180. $css = json_encode( $entity->getCss());
  181. // Ajoute le bloc
  182. /*$css['blocs'][] = $css2;
  183. // Tri par poids
  184. $tmp = Array();
  185. foreach($css['blocs'] as &$ma)
  186. $tmp[] = &$ma["poid"];
  187. array_multisort($tmp, $css['blocs']);
  188. // Encode en obj json
  189. $css = json_encode( $css );
  190. var_dump("Encoder : ");
  191. var_dump($css);*/
  192. if($request->getMethod() == 'POST')
  193. {
  194. //if ($editForm->isValid()) {
  195. $entity->setCss($css);
  196. $em->persist($entity);
  197. $em->flush();
  198. exit; //return $this->redirect($this->generateUrl('style_edit', array('id' => $id)));
  199. //}
  200. }
  201. var_dump("not");
  202. exit;
  203. /*return $this->render('EditorBundle:Style:edit.html.twig', array(
  204. 'entity' => $entity,
  205. 'edit_form' => $editForm->createView(),
  206. 'delete_form' => $deleteForm->createView(),
  207. ));*/
  208. }
  209. /**
  210. * Deletes a Style entity.
  211. *
  212. */
  213. public function deleteAction(Request $request, $id)
  214. {
  215. $form = $this->createDeleteForm($id);
  216. $form->bind($request);
  217. if ($form->isValid()) {
  218. $em = $this->getDoctrine()->getManager();
  219. $entity = $em->getRepository('EditorBundle:Style')->find($id);
  220. if (!$entity) {
  221. throw $this->createNotFoundException('Unable to find Style entity.');
  222. }
  223. $em->remove($entity);
  224. $em->flush();
  225. }
  226. return $this->redirect($this->generateUrl('style'));
  227. }
  228. private function createDeleteForm($id)
  229. {
  230. return $this->createFormBuilder(array('id' => $id))
  231. ->add('id', 'hidden')
  232. ->getForm()
  233. ;
  234. }
  235. }