ElementController.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. namespace Muzich\CoreBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Muzich\CoreBundle\ElementFactory\ElementManager;
  5. class ElementController extends Controller
  6. {
  7. /**
  8. *
  9. * @param type $element_id
  10. * @return Muzich\CoreBundle\Entity\Element
  11. */
  12. protected function checkExistingAndOwned($element_id)
  13. {
  14. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  15. ->findOneById($element_id)))
  16. {
  17. throw $this->createNotFoundException('Not found');
  18. }
  19. if ($element->getOwner()->getId() != $this->getUserId())
  20. {
  21. throw $this->createNotFoundException('Not found');
  22. }
  23. return $element;
  24. }
  25. /**
  26. *
  27. */
  28. public function editAction($element_id)
  29. {
  30. if (($response = $this->mustBeConnected()))
  31. {
  32. return $response;
  33. }
  34. $element = $this->checkExistingAndOwned($element_id);
  35. $element_tags = $element->getTags();
  36. $element->setTags($element->getTagsIdsJson());
  37. $form = $this->getAddForm($element);
  38. $search_tags = array();
  39. foreach ($element_tags as $tag)
  40. {
  41. $search_tags[$tag->getId()] = $tag->getName();
  42. }
  43. $template = 'MuzichCoreBundle:Element:ajax.element.edit.html.twig';
  44. if (!$this->getRequest()->isXmlHttpRequest())
  45. {
  46. $template = 'MuzichCoreBundle:Element:element.edit.html.twig';
  47. }
  48. $response = $this->render($template, array(
  49. 'form' => $form->createView(),
  50. 'form_name' => 'element_'.$element->getId(),
  51. 'element_id' => $element->getId(),
  52. 'search_tags' => $search_tags
  53. ));
  54. if ($this->getRequest()->isXmlHttpRequest())
  55. {
  56. return $this->jsonResponse(array(
  57. 'status' => 'success',
  58. 'form_name' => 'element_'.$element->getId(),
  59. 'tags' => $search_tags,
  60. 'html' => $response->getContent()
  61. ));
  62. }
  63. return $response;
  64. }
  65. /**
  66. *
  67. */
  68. public function updateAction($element_id)
  69. {
  70. if (($response = $this->mustBeConnected()))
  71. {
  72. return $response;
  73. }
  74. /**
  75. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  76. * Docrine le voit si on faire une requete directe.
  77. */
  78. $user = $this->getUser();
  79. if ($this->container->getParameter('env') == 'test')
  80. {
  81. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  82. $this->container->get('security.context')->getToken()->getUser()->getId(),
  83. array()
  84. )->getSingleResult();
  85. }
  86. $element = $this->checkExistingAndOwned($element_id);
  87. // Si il y a un groupe on le retire pour le bind
  88. $group = $element->getGroup();
  89. $element->setGroup(null);
  90. $form = $this->getAddForm($element);
  91. $form->bindRequest($this->getRequest());
  92. $errors = array();
  93. $html = '';
  94. if ($form->isValid())
  95. {
  96. $status = 'success';
  97. $em = $this->getDoctrine()->getEntityManager();
  98. $factory = new ElementManager($element, $em, $this->container);
  99. $factory->proceedFill($user);
  100. // Si il y avais un groupe on le remet
  101. $element->setGroup($group);
  102. $em->persist($element);
  103. $em->flush();
  104. // Récupération du li
  105. $html = $this->render('MuzichCoreBundle:SearchElement:element.html.twig', array(
  106. 'element' => $element
  107. ))->getContent();
  108. }
  109. else
  110. {
  111. $status = 'error';
  112. // Récupération des erreurs
  113. $validator = $this->container->get('validator');
  114. $errorList = $validator->validate($form);
  115. foreach ($errorList as $error)
  116. {
  117. $errors[] = $this->trans($error->getMessage(), array(), 'validators');
  118. }
  119. }
  120. if ($this->getRequest()->isXmlHttpRequest())
  121. {
  122. return $this->jsonResponse(array(
  123. 'status' => $status,
  124. 'html' => $html,
  125. 'errors' => $errors
  126. ));
  127. }
  128. if ($status == 'success')
  129. {
  130. return $this->redirect($this->generateUrl('home'));
  131. }
  132. $element->setTagsWithIds(
  133. $this->getDoctrine()->getEntityManager(),
  134. json_decode($element->getTags())
  135. );
  136. return $this->render('MuzichCoreBundle:Element:element.edit.html.twig', array(
  137. 'form' => $form->createView(),
  138. 'form_name' => 'element_'.$element->getId(),
  139. 'element_id' => $element->getId(),
  140. 'search_tags' => $element->getTagsIdsJson()
  141. ));
  142. }
  143. public function removeAction($element_id)
  144. {
  145. if (($response = $this->mustBeConnected()))
  146. {
  147. return $response;
  148. }
  149. try {
  150. $element = $this->checkExistingAndOwned($element_id);
  151. $em = $this->getDoctrine()->getEntityManager();
  152. $em->remove($element);
  153. $em->flush();
  154. if ($this->getRequest()->isXmlHttpRequest())
  155. {
  156. return $this->jsonResponse(array('status' => 'success'));
  157. }
  158. $this->setFlash('success', 'element.remove.success');
  159. return $this->redirect($this->container->get('request')->headers->get('referer'));
  160. }
  161. catch(Exception $e)
  162. {
  163. if ($this->getRequest()->isXmlHttpRequest())
  164. {
  165. return $this->jsonResponse(array('status' => 'error'));
  166. }
  167. $this->setFlash('error', 'element.remove.error');
  168. return $this->redirect($this->container->get('request')->headers->get('referer'));
  169. }
  170. }
  171. protected function getcountNewMessage($count)
  172. {
  173. if ($count == 1)
  174. {
  175. $transid = 'tags.new.has_news_one';
  176. $transidlink = 'tags.new.has_news_link_one';
  177. }
  178. else if ($count == 0)
  179. {
  180. return '';
  181. }
  182. else
  183. {
  184. $transid = 'tags.new.has_news';
  185. $transidlink = 'tags.new.has_news_link';
  186. }
  187. if ($count > ($limit = $this->container->getParameter('search_default_count')))
  188. {
  189. $link = $this->trans(
  190. 'tags.new.has_news_link_more_x',
  191. array(
  192. '%x%' => $limit
  193. ),
  194. 'userui'
  195. );
  196. }
  197. else
  198. {
  199. $link = $this->trans(
  200. $transidlink,
  201. array(),
  202. 'userui'
  203. );
  204. }
  205. $link = '<a href="#" class="show_new_elements" >'.$link.'</a>';
  206. return $this->trans(
  207. $transid,
  208. array(
  209. '%count%' => $count,
  210. '%link%' => $link
  211. ),
  212. 'userui'
  213. );
  214. }
  215. /**
  216. * Retourne le nombre de nouveaux éléments possible
  217. *
  218. * @param int $refid
  219. */
  220. public function countNewsAction($refid)
  221. {
  222. if (!$this->getRequest()->isXmlHttpRequest())
  223. {
  224. return $this->redirect($this->generateUrl('index'));
  225. }
  226. if (($response = $this->mustBeConnected()))
  227. {
  228. return $response;
  229. }
  230. $es = $this->getElementSearcher();
  231. $es->update(array(
  232. // On veux de nouveaux éléments
  233. 'searchnew' => true,
  234. // Notre id de référence
  235. 'id_limit' => $refid
  236. ));
  237. $count = $es->getElements($this->getDoctrine(), $this->getUserId(), 'count');
  238. return $this->jsonResponse(array(
  239. 'status' => 'success',
  240. 'count' => $count,
  241. 'message' => $this->getcountNewMessage($count)
  242. ));
  243. }
  244. /**
  245. * Cette action, utilisé en ajax seulement, retourne les x nouveaux éléments
  246. * depuis le refid transmis. Tout en respectant le filtre en cours.
  247. *
  248. * @param int $refid identifiant de l'élément de référence
  249. *
  250. * @return jsonResponse
  251. */
  252. public function getNewsAction($refid)
  253. {
  254. if (!$this->getRequest()->isXmlHttpRequest())
  255. {
  256. return $this->redirect($this->generateUrl('index'));
  257. }
  258. if (($response = $this->mustBeConnected()))
  259. {
  260. return $response;
  261. }
  262. $es = $this->getElementSearcher();
  263. $es->update(array(
  264. // On veux de nouveaux éléments
  265. 'searchnew' => true,
  266. // Notre id de référence
  267. 'id_limit' => $refid,
  268. // On en veut qu'un certain nombres
  269. 'count' => $this->container->getParameter('search_default_count')
  270. ));
  271. // Récupération de ces nouveaux élméents
  272. $elements = $es->getElements($this->getDoctrine(), $this->getUserId());
  273. // On en fait un rendu graphique
  274. $html_elements = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  275. 'user' => $this->getUser(),
  276. 'elements' => $elements,
  277. 'invertcolor' => false
  278. ))->getContent();
  279. // On calcule le nouveau compte de nouveaux
  280. $count = 0;
  281. if (count($elements))
  282. {
  283. $es->update(array(
  284. // On veux de nouveaux éléments
  285. 'searchnew' => true,
  286. // Notre id de référence
  287. 'id_limit' => $elements[0]->getId(),
  288. // On n'en récupère que x
  289. 'count' => $this->container->getParameter('search_default_count')
  290. ));
  291. $count = $es->getElements($this->getDoctrine(), $this->getUserId(), 'count');
  292. }
  293. return $this->jsonResponse(array(
  294. 'status' => 'success',
  295. 'html' => $html_elements,
  296. 'count' => $count,
  297. 'message' => $this->getcountNewMessage($count)
  298. ));
  299. }
  300. }