ElementController.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <?php
  2. namespace Muzich\CoreBundle\Controller;
  3. use Muzich\CoreBundle\lib\Controller;
  4. use Muzich\CoreBundle\ElementFactory\ElementManager;
  5. use Muzich\CoreBundle\Propagator\EventElement;
  6. use Muzich\CoreBundle\Entity\ElementTagsProposition;
  7. class ElementController extends Controller
  8. {
  9. /**
  10. * Cette méthode est utilisé pour récupérer un objet Element tout en levant
  11. * une erreur si il n'existe pas ou si il n'appartient pas a l'utilisateur en
  12. * cours.
  13. *
  14. * @param int $element_id
  15. * @return Muzich\CoreBundle\Entity\Element
  16. */
  17. protected function checkExistingAndOwned($element_id)
  18. {
  19. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  20. ->findOneById($element_id)))
  21. {
  22. throw $this->createNotFoundException('Not found');
  23. }
  24. if ($element->getOwner()->getId() != $this->getUserId())
  25. {
  26. throw $this->createNotFoundException('Not found');
  27. }
  28. return $element;
  29. }
  30. /**
  31. * Action d'ouverture du formulaire de modification d'un élément.
  32. *
  33. * @param int $element_id
  34. * @return Response
  35. */
  36. public function editAction($element_id)
  37. {
  38. if (($response = $this->mustBeConnected()))
  39. {
  40. return $response;
  41. }
  42. $element = $this->checkExistingAndOwned($element_id);
  43. // On doit faire un chmilblik avec les tags pour
  44. // utiliser le javascript de tags (tagPrompt)
  45. // sur le formulaire
  46. $element_tags = $element->getTags();
  47. $element->setTags($element->getTagsIdsJson());
  48. $form = $this->getAddForm($element);
  49. $search_tags = array();
  50. foreach ($element_tags as $tag)
  51. {
  52. $search_tags[$tag->getId()] = $tag->getName();
  53. }
  54. $template = 'MuzichCoreBundle:Element:ajax.element.edit.html.twig';
  55. if (!$this->getRequest()->isXmlHttpRequest())
  56. {
  57. $template = 'MuzichCoreBundle:Element:element.edit.html.twig';
  58. }
  59. $response = $this->render($template, array(
  60. 'form' => $form->createView(),
  61. 'form_name' => 'element_'.$element->getId(),
  62. 'element_id' => $element->getId(),
  63. 'search_tags' => $search_tags
  64. ));
  65. if ($this->getRequest()->isXmlHttpRequest())
  66. {
  67. return $this->jsonResponse(array(
  68. 'status' => 'success',
  69. 'form_name' => 'element_'.$element->getId(),
  70. 'tags' => $search_tags,
  71. 'html' => $response->getContent()
  72. ));
  73. }
  74. return $response;
  75. }
  76. /**
  77. * Mise a jour des données d'un élément.
  78. *
  79. * @param int $element_id
  80. * @param string $dom_id
  81. * @return Response
  82. */
  83. public function updateAction($element_id, $dom_id)
  84. {
  85. if (($response = $this->mustBeConnected()))
  86. {
  87. return $response;
  88. }
  89. /**
  90. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  91. * Docrine le voit si on faire une requete directe.
  92. */
  93. $user = $this->getUser();
  94. if ($this->container->getParameter('env') == 'test')
  95. {
  96. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  97. $this->container->get('security.context')->getToken()->getUser()->getId(),
  98. array()
  99. )->getSingleResult();
  100. }
  101. $element = $this->checkExistingAndOwned($element_id);
  102. // Si il y a un groupe on le retire pour le bind
  103. $group = $element->getGroup();
  104. $element->setGroup(null);
  105. $form = $this->getAddForm($element);
  106. $form->bindRequest($this->getRequest());
  107. $errors = array();
  108. $html = '';
  109. if ($form->isValid())
  110. {
  111. $status = 'success';
  112. $em = $this->getDoctrine()->getEntityManager();
  113. // On utilise le manager d'élément
  114. $factory = new ElementManager($element, $em, $this->container);
  115. $factory->proceedFill($user);
  116. // Si il y avais un groupe on le remet
  117. $element->setGroup($group);
  118. $em->persist($element);
  119. $em->flush();
  120. // Récupération du li
  121. $html = $this->render('MuzichCoreBundle:SearchElement:element.html.twig', array(
  122. 'element' => $element
  123. ))->getContent();
  124. }
  125. else
  126. {
  127. $status = 'error';
  128. // Récupération des erreurs
  129. $validator = $this->container->get('validator');
  130. $errorList = $validator->validate($form);
  131. foreach ($errorList as $error)
  132. {
  133. $errors[] = $this->trans($error->getMessage(), array(), 'validators');
  134. }
  135. }
  136. if ($this->getRequest()->isXmlHttpRequest())
  137. {
  138. return $this->jsonResponse(array(
  139. 'status' => $status,
  140. 'dom_id' => $dom_id,
  141. 'html' => $html,
  142. 'errors' => $errors
  143. ));
  144. }
  145. if ($status == 'success')
  146. {
  147. return $this->redirect($this->generateUrl('home'));
  148. }
  149. $element->setTagsWithIds(
  150. $this->getDoctrine()->getEntityManager(),
  151. json_decode($element->getTags())
  152. );
  153. return $this->render('MuzichCoreBundle:Element:element.edit.html.twig', array(
  154. 'form' => $form->createView(),
  155. 'form_name' => 'element_'.$element->getId(),
  156. 'element_id' => $element->getId(),
  157. 'search_tags' => $element->getTagsIdsJson()
  158. ));
  159. }
  160. /**
  161. * Suppression d'un élément.
  162. *
  163. * @param int $element_id
  164. * @return Response
  165. */
  166. public function removeAction($element_id)
  167. {
  168. if (($response = $this->mustBeConnected()))
  169. {
  170. return $response;
  171. }
  172. try {
  173. $element = $this->checkExistingAndOwned($element_id);
  174. $em = $this->getDoctrine()->getEntityManager();
  175. $event = new EventElement($this->container);
  176. $event->elementRemoved($element);
  177. $em->persist($element->getOwner());
  178. $em->remove($element);
  179. $em->flush();
  180. if ($this->getRequest()->isXmlHttpRequest())
  181. {
  182. return $this->jsonResponse(array('status' => 'success'));
  183. }
  184. $this->setFlash('success', 'element.remove.success');
  185. return $this->redirect($this->container->get('request')->headers->get('referer'));
  186. }
  187. catch(Exception $e)
  188. {
  189. if ($this->getRequest()->isXmlHttpRequest())
  190. {
  191. return $this->jsonResponse(array('status' => 'error'));
  192. }
  193. $this->setFlash('error', 'element.remove.error');
  194. return $this->redirect($this->container->get('request')->headers->get('referer'));
  195. }
  196. }
  197. /**
  198. * Cette procédure retourne le lien a afficher sur la page home permettant
  199. * d'afficher des élément apparus entre temps.
  200. *
  201. * @param int $count
  202. * @return type
  203. */
  204. protected function getcountNewMessage($count)
  205. {
  206. if ($count == 1)
  207. {
  208. $transid = 'tags.new.has_news_one';
  209. $transidlink = 'tags.new.has_news_link_one';
  210. }
  211. else if ($count == 0)
  212. {
  213. return '';
  214. }
  215. else
  216. {
  217. $transid = 'tags.new.has_news';
  218. $transidlink = 'tags.new.has_news_link';
  219. }
  220. if ($count > ($limit = $this->container->getParameter('search_default_count')))
  221. {
  222. $link = $this->trans(
  223. 'tags.new.has_news_link_more_x',
  224. array(
  225. '%x%' => $limit
  226. ),
  227. 'userui'
  228. );
  229. }
  230. else
  231. {
  232. $link = $this->trans(
  233. $transidlink,
  234. array(),
  235. 'userui'
  236. );
  237. }
  238. $link = '<a href="#" class="show_new_elements" >'.$link.'</a>';
  239. return $this->trans(
  240. $transid,
  241. array(
  242. '%count%' => $count,
  243. '%link%' => $link
  244. ),
  245. 'userui'
  246. );
  247. }
  248. /**
  249. * Retourne le nombre de nouveaux éléments possible
  250. *
  251. * @param int $refid
  252. */
  253. public function countNewsAction($refid)
  254. {
  255. if (!$this->getRequest()->isXmlHttpRequest())
  256. {
  257. return $this->redirect($this->generateUrl('index'));
  258. }
  259. if (($response = $this->mustBeConnected()))
  260. {
  261. return $response;
  262. }
  263. $es = $this->getElementSearcher();
  264. $es->update(array(
  265. // On veux de nouveaux éléments
  266. 'searchnew' => true,
  267. // Notre id de référence
  268. 'id_limit' => $refid
  269. ));
  270. $count = $es->getElements($this->getDoctrine(), $this->getUserId(), 'count');
  271. return $this->jsonResponse(array(
  272. 'status' => 'success',
  273. 'count' => $count,
  274. 'message' => $this->getcountNewMessage($count)
  275. ));
  276. }
  277. /**
  278. * Cette action, utilisé en ajax seulement, retourne les x nouveaux éléments
  279. * depuis le refid transmis. Tout en respectant le filtre en cours.
  280. *
  281. * @param int $refid identifiant de l'élément de référence
  282. *
  283. * @return jsonResponse
  284. */
  285. public function getNewsAction($refid)
  286. {
  287. if (!$this->getRequest()->isXmlHttpRequest())
  288. {
  289. return $this->redirect($this->generateUrl('index'));
  290. }
  291. if (($response = $this->mustBeConnected()))
  292. {
  293. return $response;
  294. }
  295. $es = $this->getElementSearcher();
  296. $es->update(array(
  297. // On veux de nouveaux éléments
  298. 'searchnew' => true,
  299. // Notre id de référence
  300. 'id_limit' => $refid,
  301. // On en veut qu'un certain nombres
  302. 'count' => $this->container->getParameter('search_default_count')
  303. ));
  304. // Récupération de ces nouveaux élméents
  305. $elements = $es->getElements($this->getDoctrine(), $this->getUserId());
  306. // On en fait un rendu graphique
  307. $html_elements = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  308. 'user' => $this->getUser(),
  309. 'elements' => $elements,
  310. 'invertcolor' => false
  311. ))->getContent();
  312. // On calcule le nouveau compte de nouveaux
  313. $count = 0;
  314. if (count($elements))
  315. {
  316. $es->update(array(
  317. // On veux de nouveaux éléments
  318. 'searchnew' => true,
  319. // Notre id de référence
  320. 'id_limit' => $elements[0]->getId(),
  321. // On n'en récupère que x
  322. 'count' => $this->container->getParameter('search_default_count')
  323. ));
  324. $count = $es->getElements($this->getDoctrine(), $this->getUserId(), 'count');
  325. }
  326. return $this->jsonResponse(array(
  327. 'status' => 'success',
  328. 'html' => $html_elements,
  329. 'count' => $count,
  330. 'message' => $this->getcountNewMessage($count)
  331. ));
  332. }
  333. /**
  334. * Action (ajax) ajoutant son vote "good" sur un élément
  335. *
  336. * @param int $element_id
  337. * @param string $token
  338. * @return Response
  339. */
  340. public function addVoteGoodAction($element_id, $token)
  341. {
  342. if (($response = $this->mustBeConnected(true)))
  343. {
  344. return $response;
  345. }
  346. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  347. ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
  348. {
  349. return $this->jsonResponse(array(
  350. 'status' => 'error',
  351. 'errors' => array('NotFound')
  352. ));
  353. }
  354. if ($element->getOwner()->getId() == $this->getUserId())
  355. {
  356. return $this->jsonResponse(array(
  357. 'status' => 'error',
  358. 'errors' => array('NotAllowed')
  359. ));
  360. }
  361. // On ajoute un vote a l'élément
  362. $element->addVoteGood($this->getUser()->getId());
  363. // Puis on lance les actions propagés par ce vote
  364. $event = new EventElement($this->container);
  365. $event->onePointAdded($element);
  366. $this->getDoctrine()->getEntityManager()->persist($element);
  367. $this->getDoctrine()->getEntityManager()->flush();
  368. return $this->jsonResponse(array(
  369. 'status' => 'success',
  370. 'data' => array(
  371. 'a' => array(
  372. 'href' => $this->generateUrl('ajax_element_remove_vote_good', array(
  373. 'element_id' => $element->getId(),
  374. 'token' => $this->getUser()->getPersonalHash()
  375. ))
  376. ),
  377. 'img' => array(
  378. 'src' => $this->getAssetUrl('bundles/muzichcore/img/up_b.png')
  379. ),
  380. 'element' => array(
  381. 'points' => $element->getPoints()
  382. )
  383. )
  384. ));
  385. }
  386. /**
  387. * Action (ajax) de retrait de son vote good
  388. *
  389. * @param int $element_id
  390. * @param string $token
  391. * @return Response
  392. */
  393. public function removeVoteGoodAction($element_id, $token)
  394. {
  395. if (($response = $this->mustBeConnected(true)))
  396. {
  397. return $response;
  398. }
  399. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  400. ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
  401. {
  402. return $this->jsonResponse(array(
  403. 'status' => 'error',
  404. 'errors' => array('NotFound')
  405. ));
  406. }
  407. if ($element->getOwner()->getId() == $this->getUserId())
  408. {
  409. return $this->jsonResponse(array(
  410. 'status' => 'error',
  411. 'errors' => array('NotAllowed')
  412. ));
  413. }
  414. // Retrait du vote good
  415. $element->removeVoteGood($this->getUser()->getId());
  416. // Puis on lance les actions propagés par retrait de vote
  417. $event = new EventElement($this->container);
  418. $event->onePointRemoved($element);
  419. $this->getDoctrine()->getEntityManager()->persist($element);
  420. $this->getDoctrine()->getEntityManager()->flush();
  421. return $this->jsonResponse(array(
  422. 'status' => 'success',
  423. 'data' => array(
  424. 'a' => array(
  425. 'href' => $this->generateUrl('ajax_element_add_vote_good', array(
  426. 'element_id' => $element->getId(),
  427. 'token' => $this->getUser()->getPersonalHash()
  428. ))
  429. ),
  430. 'img' => array(
  431. 'src' => $this->getAssetUrl('bundles/muzichcore/img/up_bw.png')
  432. ),
  433. 'element' => array(
  434. 'points' => $element->getPoints()
  435. )
  436. )
  437. ));
  438. }
  439. /**
  440. * Retourne un json avec le form permettant a l'utilisateur de proposer des
  441. * tags sur un élément.
  442. *
  443. * @param int $element_id
  444. * @return Response
  445. */
  446. public function proposeTagsOpenAction($element_id)
  447. {
  448. if (($response = $this->mustBeConnected(true)))
  449. {
  450. return $response;
  451. }
  452. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  453. ->findOneById($element_id)))
  454. {
  455. return $this->jsonResponse(array(
  456. 'status' => 'error',
  457. 'errors' => array('NotFound')
  458. ));
  459. }
  460. $search_tags = array();
  461. foreach ($element->getTags() as $tag)
  462. {
  463. $search_tags[$tag->getId()] = $tag->getName();
  464. }
  465. $element->setTags($element->getTagsIdsJson());
  466. $form = $this->getAddForm($element, 'element_tag_proposition_'.$element->getId());
  467. $response = $this->render('MuzichCoreBundle:Element:tag.proposition.html.twig', array(
  468. 'form' => $form->createView(),
  469. 'form_name' => 'element_tag_proposition_'.$element->getId(),
  470. 'element_id' => $element->getId(),
  471. 'search_tags' => $search_tags
  472. ));
  473. return $this->jsonResponse(array(
  474. 'status' => 'success',
  475. 'form_name' => 'element_tag_proposition_'.$element->getId(),
  476. 'tags' => $search_tags,
  477. 'html' => $response->getContent()
  478. ));
  479. }
  480. public function proposeTagsProceedAction($element_id, $token)
  481. {
  482. if (($response = $this->mustBeConnected(true)))
  483. {
  484. return $response;
  485. }
  486. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  487. ->findOneById($element_id)) || $token != $this->getUser()->getPersonalHash())
  488. {
  489. return $this->jsonResponse(array(
  490. 'status' => 'error',
  491. 'errors' => array('NotFound')
  492. ));
  493. }
  494. // On ne doit pas pouvoir proposer de tags sur son propre élément
  495. if ($element->getOwner()->getId() == $this->getUserId())
  496. {
  497. return $this->jsonResponse(array(
  498. 'status' => 'error',
  499. 'errors' => array('NotAllowed')
  500. ));
  501. }
  502. $values = $this->getRequest()->request->get('element_tag_proposition_'.$element->getId());
  503. $tags_ids = json_decode($values['tags'], true);
  504. $tags = array();
  505. if (count($tags_ids))
  506. {
  507. // On récupère les tags en base
  508. $tags = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:Tag')
  509. ->getTagsWithIds($tags_ids)
  510. ;
  511. }
  512. if (!count($tags))
  513. {
  514. return $this->jsonResponse(array(
  515. 'status' => 'error',
  516. 'errors' => array($this->trans('element.tag_proposition.form.error.empty', array(), 'elements'))
  517. ));
  518. }
  519. /**
  520. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  521. * Docrine le voit si on faire une requete directe.
  522. */
  523. $user = $this->getUser();
  524. if ($this->container->getParameter('env') == 'test')
  525. {
  526. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  527. $this->container->get('security.context')->getToken()->getUser()->getId(),
  528. array()
  529. )->getSingleResult();
  530. }
  531. $proposition = new ElementTagsProposition();
  532. $proposition->setElement($element);
  533. $proposition->setUser($user);
  534. $date = new \DateTime(date('Y-m-d H:i:s'));
  535. $proposition->setCreated($date);
  536. foreach ($tags as $tag)
  537. {
  538. // Si le tag est a modérer, il faut que le propriétaire de l'élément
  539. // puisse voir ce tag, afin d'accepter en toute connaisance la proposition.
  540. if ($tag->getTomoderate())
  541. {
  542. if (!$tag->hasIdInPrivateIds($element->getOwner()->getId()))
  543. {
  544. // Si son id n'y est pas on la rajoute afin que le proprio puisse voir
  545. // ces nouveau tags
  546. $private_ids = json_decode($tag->getPrivateids(), true);
  547. $private_ids[] = $element->getOwner()->getId();
  548. $tag->setPrivateids(json_encode($private_ids));
  549. $this->getDoctrine()->getEntityManager()->persist($tag);
  550. }
  551. }
  552. $proposition->addTag($tag);
  553. }
  554. $element->setHasTagProposition(true);
  555. $this->getDoctrine()->getEntityManager()->persist($element);
  556. $this->getDoctrine()->getEntityManager()->persist($proposition);
  557. // Notifs etc
  558. $event = new EventElement($this->container);
  559. $event->tagsProposed($element);
  560. $this->getDoctrine()->getEntityManager()->flush();
  561. return $this->jsonResponse(array(
  562. 'status' => 'success',
  563. 'dom_id' => 'element_'.$element->getId()
  564. ));
  565. }
  566. public function proposedTagsViewAction($element_id)
  567. {
  568. if (($response = $this->mustBeConnected(true)))
  569. {
  570. return $response;
  571. }
  572. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  573. ->findOneById($element_id)))
  574. {
  575. return $this->jsonResponse(array(
  576. 'status' => 'error',
  577. 'errors' => array('NotFound')
  578. ));
  579. }
  580. if ($element->getOwner()->getId() != $this->getUserId())
  581. {
  582. return $this->jsonResponse(array(
  583. 'status' => 'error',
  584. 'errors' => array('NotAllowed')
  585. ));
  586. }
  587. // On récupére toute les propsotions pour cet élément
  588. $propositions = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:ElementTagsProposition')
  589. ->findByElement($element->getId())
  590. ;
  591. $response = $this->render('MuzichCoreBundle:Element:tag.propositions.html.twig', array(
  592. 'propositions' => $propositions,
  593. 'element_id' => $element->getId()
  594. ));
  595. return $this->jsonResponse(array(
  596. 'status' => 'success',
  597. 'html' => $response->getContent()
  598. ));
  599. }
  600. public function proposedTagsAcceptAction($proposition_id, $token)
  601. {
  602. if (($response = $this->mustBeConnected(true)))
  603. {
  604. return $response;
  605. }
  606. if (!($proposition = $this->getDoctrine()->getRepository('MuzichCoreBundle:ElementTagsProposition')
  607. ->findOneById($proposition_id)) || $token != $this->getUser()->getPersonalHash())
  608. {
  609. return $this->jsonResponse(array(
  610. 'status' => 'error',
  611. 'errors' => array('NotFound')
  612. ));
  613. }
  614. // On commence par appliquer les nouveaux tags a l'élément
  615. $element = $proposition->getElement();
  616. $element->setTags(null);
  617. foreach ($proposition->getTags() as $tag)
  618. {
  619. $element->addTag($tag);
  620. }
  621. $element->setHasTagProposition(false);
  622. $this->getDoctrine()->getEntityManager()->persist($element);
  623. $event = new EventElement($this->container);
  624. $event->tagsAccepteds($proposition);
  625. $propositions = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:ElementTagsProposition')
  626. ->findByElement($element->getId())
  627. ;
  628. // On supprime les proposition liés a cet élement
  629. foreach ($propositions as $proposition)
  630. {
  631. $this->getDoctrine()->getEntityManager()->remove($proposition);
  632. }
  633. $this->getDoctrine()->getEntityManager()->flush();
  634. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  635. ->findOneById($element->getId())
  636. ;
  637. // On récupère l'html de l'élément
  638. $html = $this->render('MuzichCoreBundle:SearchElement:element.html.twig', array(
  639. 'element' => $element
  640. ))->getContent();
  641. return $this->jsonResponse(array(
  642. 'status' => 'success',
  643. 'html' => $html
  644. ));
  645. }
  646. public function proposedTagsRefuseAction($element_id, $token)
  647. {
  648. if (($response = $this->mustBeConnected(true)))
  649. {
  650. return $response;
  651. }
  652. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  653. ->findOneById($element_id)) || $token != $this->getUser()->getPersonalHash())
  654. {
  655. return $this->jsonResponse(array(
  656. 'status' => 'error',
  657. 'errors' => array('NotFound')
  658. ));
  659. }
  660. // On supprime les proposition liés a cet élement
  661. $propositions = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:ElementTagsProposition')
  662. ->findByElement($element->getId())
  663. ;
  664. foreach ($propositions as $proposition)
  665. {
  666. $this->getDoctrine()->getEntityManager()->remove($proposition);
  667. }
  668. // On spécifie qu'il n'y as plus de proposition
  669. $element->setHasTagProposition(false);
  670. $this->getDoctrine()->getEntityManager()->persist($element);
  671. $this->getDoctrine()->getEntityManager()->flush();
  672. return $this->jsonResponse(array(
  673. 'status' => 'success'
  674. ));
  675. }
  676. }