ElementController.php 25KB

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