ElementController.php 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. use Muzich\CoreBundle\Util\TagLike;
  10. use Muzich\CoreBundle\Entity\User;
  11. use Muzich\CoreBundle\lib\AutoplayManager;
  12. use Muzich\CoreBundle\Searcher\ElementSearcher;
  13. class ElementController extends Controller
  14. {
  15. /**
  16. * Cette méthode est utilisé pour récupérer un objet Element tout en levant
  17. * une erreur si il n'existe pas ou si il n'appartient pas a l'utilisateur en
  18. * cours.
  19. *
  20. * @param int $element_id
  21. * @return Muzich\CoreBundle\Entity\Element
  22. */
  23. protected function checkExistingAndOwned($element_id)
  24. {
  25. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  26. ->findOneById($element_id)))
  27. {
  28. throw $this->createNotFoundException('Not found');
  29. }
  30. if ($element->getOwner()->getId() != $this->getUserId())
  31. {
  32. throw $this->createNotFoundException('Not found');
  33. }
  34. return $element;
  35. }
  36. /**
  37. * Action d'ouverture du formulaire de modification d'un élément.
  38. *
  39. * @param int $element_id
  40. * @return Response
  41. */
  42. public function editAction($element_id)
  43. {
  44. if (($response = $this->mustBeConnected()))
  45. {
  46. return $response;
  47. }
  48. $element = $this->checkExistingAndOwned($element_id);
  49. // On doit faire un chmilblik avec les tags pour
  50. // utiliser le javascript de tags (tagPrompt)
  51. // sur le formulaire
  52. $element_tags = $element->getTags();
  53. $element->setTags($element->getTagsIdsJson());
  54. $form = $this->getAddForm($element);
  55. $search_tags = array();
  56. foreach ($element_tags as $tag)
  57. {
  58. $search_tags[$tag->getId()] = $tag->getName();
  59. }
  60. $template = 'MuzichCoreBundle:Element:ajax.element.edit.html.twig';
  61. if (!$this->getRequest()->isXmlHttpRequest())
  62. {
  63. $template = 'MuzichCoreBundle:Element:element.edit.html.twig';
  64. }
  65. $response = $this->render($template, array(
  66. 'form' => $form->createView(),
  67. 'form_name' => 'element_'.$element->getId(),
  68. 'element_id' => $element->getId(),
  69. 'search_tags' => $search_tags
  70. ));
  71. if ($this->getRequest()->isXmlHttpRequest())
  72. {
  73. return $this->jsonResponse(array(
  74. 'status' => 'success',
  75. 'form_name' => 'element_'.$element->getId(),
  76. 'tags' => $search_tags,
  77. 'html' => $response->getContent()
  78. ));
  79. }
  80. return $response;
  81. }
  82. /**
  83. * Mise a jour des données d'un élément.
  84. *
  85. * @param int $element_id
  86. * @param string $dom_id
  87. * @return Response
  88. */
  89. public function updateAction($element_id, $dom_id)
  90. {
  91. if (($response = $this->mustBeConnected()))
  92. {
  93. return $response;
  94. }
  95. /**
  96. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  97. * Docrine le voit si on faire une requete directe.
  98. */
  99. $user = $this->getUser();
  100. if ($this->container->getParameter('env') == 'test')
  101. {
  102. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  103. $this->container->get('security.context')->getToken()->getUser()->getId(),
  104. array()
  105. )->getSingleResult();
  106. }
  107. $element = $this->checkExistingAndOwned($element_id);
  108. // Si il y a un groupe on le retire pour le bind
  109. $group = $element->getGroup();
  110. $element->setGroup(null);
  111. $form = $this->getAddForm($element);
  112. $form->bindRequest($this->getRequest());
  113. $errors = array();
  114. $html = '';
  115. if ($form->isValid())
  116. {
  117. $status = 'success';
  118. $em = $this->getDoctrine()->getEntityManager();
  119. // On utilise le manager d'élément
  120. $factory = new ElementManager($element, $em, $this->container);
  121. $factory->proceedFill($user);
  122. // Si il y avais un groupe on le remet
  123. $element->setGroup($group);
  124. // On signale que cet user a modifié ses diffusions
  125. $user->setData(User::DATA_DIFF_UPDATED, true);
  126. $em->persist($user);
  127. $em->persist($element);
  128. $em->flush();
  129. // Récupération du li
  130. $html = $this->render('MuzichCoreBundle:SearchElement:element.html.twig', array(
  131. 'element' => $element
  132. ))->getContent();
  133. }
  134. else
  135. {
  136. $status = 'error';
  137. // Récupération des erreurs
  138. $validator = $this->container->get('validator');
  139. $errorList = $validator->validate($form);
  140. foreach ($errorList as $error)
  141. {
  142. $errors[] = $this->trans($error->getMessage(), array(), 'validators');
  143. }
  144. }
  145. if ($this->getRequest()->isXmlHttpRequest())
  146. {
  147. return $this->jsonResponse(array(
  148. 'status' => $status,
  149. 'dom_id' => $dom_id,
  150. 'html' => $html,
  151. 'errors' => $errors
  152. ));
  153. }
  154. if ($status == 'success')
  155. {
  156. return $this->redirect($this->generateUrl('home'));
  157. }
  158. $element->setTagsWithIds(
  159. $this->getDoctrine()->getEntityManager(),
  160. json_decode($element->getTags())
  161. );
  162. return $this->render('MuzichCoreBundle:Element:element.edit.html.twig', array(
  163. 'form' => $form->createView(),
  164. 'form_name' => 'element_'.$element->getId(),
  165. 'element_id' => $element->getId(),
  166. 'search_tags' => $element->getTagsIdsJson()
  167. ));
  168. }
  169. /**
  170. * Suppression d'un élément.
  171. *
  172. * @param int $element_id
  173. * @return Response
  174. */
  175. public function removeAction($element_id)
  176. {
  177. if (($response = $this->mustBeConnected()))
  178. {
  179. return $response;
  180. }
  181. try {
  182. $element = $this->checkExistingAndOwned($element_id);
  183. $em = $this->getDoctrine()->getEntityManager();
  184. $event = new EventElement($this->container);
  185. $event->elementRemoved($element);
  186. $em->persist($element->getOwner());
  187. $em->remove($element);
  188. /**
  189. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  190. * Docrine le voit si on faire une requete directe.
  191. */
  192. $user = $this->getUser();
  193. if ($this->container->getParameter('env') == 'test')
  194. {
  195. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  196. $this->container->get('security.context')->getToken()->getUser()->getId(),
  197. array()
  198. )->getSingleResult();
  199. }
  200. // On signale que cet user a modifié ses diffusions
  201. $user->setData(User::DATA_DIFF_UPDATED, true);
  202. $em->persist($user);
  203. $em->flush();
  204. if ($this->getRequest()->isXmlHttpRequest())
  205. {
  206. return $this->jsonResponse(array('status' => 'success'));
  207. }
  208. $this->setFlash('success', 'element.remove.success');
  209. return $this->redirect($this->container->get('request')->headers->get('referer'));
  210. }
  211. catch(Exception $e)
  212. {
  213. if ($this->getRequest()->isXmlHttpRequest())
  214. {
  215. return $this->jsonResponse(array('status' => 'error'));
  216. }
  217. $this->setFlash('error', 'element.remove.error');
  218. return $this->redirect($this->container->get('request')->headers->get('referer'));
  219. }
  220. }
  221. /**
  222. * Cette procédure retourne le lien a afficher sur la page home permettant
  223. * d'afficher des élément apparus entre temps.
  224. *
  225. * @param int $count
  226. * @return type
  227. */
  228. protected function getcountNewMessage($count)
  229. {
  230. if ($count == 1)
  231. {
  232. $transid = 'tags.new.has_news_one';
  233. $transidlink = 'tags.new.has_news_link_one';
  234. }
  235. else if ($count == 0)
  236. {
  237. return '';
  238. }
  239. else
  240. {
  241. $transid = 'tags.new.has_news';
  242. $transidlink = 'tags.new.has_news_link';
  243. }
  244. if ($count > ($limit = $this->container->getParameter('search_default_count')))
  245. {
  246. $link = $this->trans(
  247. 'tags.new.has_news_link_more_x',
  248. array(
  249. '%x%' => $limit
  250. ),
  251. 'userui'
  252. );
  253. }
  254. else
  255. {
  256. $link = $this->trans(
  257. $transidlink,
  258. array(),
  259. 'userui'
  260. );
  261. }
  262. $link = '<a href="#" class="show_new_elements" >'.$link.'</a>';
  263. return $this->trans(
  264. $transid,
  265. array(
  266. '%count%' => $count,
  267. '%link%' => $link
  268. ),
  269. 'userui'
  270. );
  271. }
  272. /**
  273. * Retourne le nombre de nouveaux éléments possible
  274. *
  275. * @param int $refid
  276. */
  277. public function countNewsAction($refid)
  278. {
  279. if (!$this->getRequest()->isXmlHttpRequest())
  280. {
  281. return $this->redirect($this->generateUrl('index'));
  282. }
  283. if (($response = $this->mustBeConnected()))
  284. {
  285. return $response;
  286. }
  287. if ($this->getRequest()->getMethod() != 'POST')
  288. {
  289. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  290. }
  291. /*
  292. * On met à jour l'ElementSearcher avec le form
  293. */
  294. $es = $this->getElementSearcher(null, true);
  295. $search_form = $this->getSearchForm($es);
  296. $search_form->bindRequest($this->getRequest());
  297. if ($search_form->isValid())
  298. {
  299. $es->update($search_form->getData());
  300. }
  301. $es->update(array(
  302. // On veux de nouveaux éléments
  303. 'searchnew' => true,
  304. // Notre id de référence
  305. 'id_limit' => $refid
  306. ));
  307. $count = $es->getElements($this->getDoctrine(), $this->getUserId(), 'count');
  308. return $this->jsonResponse(array(
  309. 'status' => 'success',
  310. 'count' => $count,
  311. 'message' => $this->getcountNewMessage($count)
  312. ));
  313. }
  314. /**
  315. * Cette action, utilisé en ajax seulement, retourne les x nouveaux éléments
  316. * depuis le refid transmis. Tout en respectant le filtre en cours.
  317. *
  318. * @param int $refid identifiant de l'élément de référence
  319. *
  320. * @return jsonResponse
  321. */
  322. public function getNewsAction($refid)
  323. {
  324. if (!$this->getRequest()->isXmlHttpRequest())
  325. {
  326. return $this->redirect($this->generateUrl('index'));
  327. }
  328. if (($response = $this->mustBeConnected()))
  329. {
  330. return $response;
  331. }
  332. if ($this->getRequest()->getMethod() != 'POST')
  333. {
  334. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  335. }
  336. /*
  337. * On met à jour l'ElementSearcher avec le form
  338. */
  339. $es = $this->getElementSearcher(null, true);
  340. $search_form = $this->getSearchForm($es);
  341. $search_form->bindRequest($this->getRequest());
  342. if ($search_form->isValid())
  343. {
  344. $es->update($search_form->getData());
  345. }
  346. $es->update(array(
  347. // On veux de nouveaux éléments
  348. 'searchnew' => true,
  349. // Notre id de référence
  350. 'id_limit' => $refid,
  351. // On en veut qu'un certain nombres
  352. 'count' => $this->container->getParameter('search_default_count')
  353. ));
  354. // Récupération de ces nouveaux élméents
  355. $elements = $es->getElements($this->getDoctrine(), $this->getUserId());
  356. // On en fait un rendu graphique
  357. $html_elements = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
  358. 'user' => $this->getUser(),
  359. 'elements' => $elements
  360. ))->getContent();
  361. // On calcule le nouveau compte de nouveaux
  362. $count = 0;
  363. if (count($elements))
  364. {
  365. $es->update(array(
  366. // On veux de nouveaux éléments
  367. 'searchnew' => true,
  368. // Notre id de référence
  369. 'id_limit' => $elements[0]->getId(),
  370. // On n'en récupère que x
  371. 'count' => $this->container->getParameter('search_default_count')
  372. ));
  373. $count = $es->getElements($this->getDoctrine(), $this->getUserId(), 'count');
  374. }
  375. return $this->jsonResponse(array(
  376. 'status' => 'success',
  377. 'html' => $html_elements,
  378. 'count' => $count,
  379. 'message' => $this->getcountNewMessage($count)
  380. ));
  381. }
  382. /**
  383. * Action (ajax) ajoutant son vote "good" sur un élément
  384. *
  385. * @param int $element_id
  386. * @param string $token
  387. * @return Response
  388. */
  389. public function addVoteGoodAction($element_id, $token)
  390. {
  391. if (($response = $this->mustBeConnected(true)))
  392. {
  393. return $response;
  394. }
  395. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  396. ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
  397. {
  398. return $this->jsonResponse(array(
  399. 'status' => 'error',
  400. 'errors' => array('NotFound')
  401. ));
  402. }
  403. if ($element->getOwner()->getId() == $this->getUserId())
  404. {
  405. return $this->jsonResponse(array(
  406. 'status' => 'error',
  407. 'errors' => array('NotAllowed')
  408. ));
  409. }
  410. // On ajoute un vote a l'élément
  411. $element->addVoteGood($this->getUser()->getId());
  412. // Puis on lance les actions propagés par ce vote
  413. $event = new EventElement($this->container);
  414. $event->onePointAdded($element);
  415. $this->getDoctrine()->getEntityManager()->persist($element);
  416. $this->getDoctrine()->getEntityManager()->flush();
  417. return $this->jsonResponse(array(
  418. 'status' => 'success',
  419. 'data' => array(
  420. 'a' => array(
  421. 'href' => $this->generateUrl('ajax_element_remove_vote_good', array(
  422. 'element_id' => $element->getId(),
  423. 'token' => $this->getUser()->getPersonalHash()
  424. ))
  425. ),
  426. 'img' => array(
  427. 'src' => $this->getAssetUrl('/img/icon_thumb_red.png')
  428. ),
  429. 'element' => array(
  430. 'points' => $element->getPoints()
  431. )
  432. )
  433. ));
  434. }
  435. /**
  436. * Action (ajax) de retrait de son vote good
  437. *
  438. * @param int $element_id
  439. * @param string $token
  440. * @return Response
  441. */
  442. public function removeVoteGoodAction($element_id, $token)
  443. {
  444. if (($response = $this->mustBeConnected(true)))
  445. {
  446. return $response;
  447. }
  448. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  449. ->findOneById($element_id)) || $this->getUser()->getPersonalHash() != $token)
  450. {
  451. return $this->jsonResponse(array(
  452. 'status' => 'error',
  453. 'errors' => array('NotFound')
  454. ));
  455. }
  456. if ($element->getOwner()->getId() == $this->getUserId())
  457. {
  458. return $this->jsonResponse(array(
  459. 'status' => 'error',
  460. 'errors' => array('NotAllowed')
  461. ));
  462. }
  463. // Retrait du vote good
  464. $element->removeVoteGood($this->getUser()->getId());
  465. // Puis on lance les actions propagés par retrait de vote
  466. $event = new EventElement($this->container);
  467. $event->onePointRemoved($element);
  468. $this->getDoctrine()->getEntityManager()->persist($element);
  469. $this->getDoctrine()->getEntityManager()->flush();
  470. return $this->jsonResponse(array(
  471. 'status' => 'success',
  472. 'data' => array(
  473. 'a' => array(
  474. 'href' => $this->generateUrl('ajax_element_add_vote_good', array(
  475. 'element_id' => $element->getId(),
  476. 'token' => $this->getUser()->getPersonalHash()
  477. ))
  478. ),
  479. 'img' => array(
  480. 'src' => $this->getAssetUrl('/img/icon_thumb.png')
  481. ),
  482. 'element' => array(
  483. 'points' => $element->getPoints()
  484. )
  485. )
  486. ));
  487. }
  488. /**
  489. * Retourne un json avec le form permettant a l'utilisateur de proposer des
  490. * tags sur un élément.
  491. *
  492. * @param int $element_id
  493. * @return Response
  494. */
  495. public function proposeTagsOpenAction($element_id)
  496. {
  497. if (($response = $this->mustBeConnected(true)))
  498. {
  499. return $response;
  500. }
  501. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  502. ->findOneById($element_id)))
  503. {
  504. return $this->jsonResponse(array(
  505. 'status' => 'error',
  506. 'errors' => array('NotFound')
  507. ));
  508. }
  509. $search_tags = array();
  510. foreach ($element->getTags() as $tag)
  511. {
  512. $search_tags[$tag->getId()] = $tag->getName();
  513. }
  514. $element->setTags($element->getTagsIdsJson());
  515. $form = $this->getAddForm($element, 'element_tag_proposition_'.$element->getId());
  516. $response = $this->render('MuzichCoreBundle:Element:tag.proposition.html.twig', array(
  517. 'form' => $form->createView(),
  518. 'form_name' => 'element_tag_proposition_'.$element->getId(),
  519. 'element_id' => $element->getId(),
  520. 'search_tags' => $search_tags
  521. ));
  522. return $this->jsonResponse(array(
  523. 'status' => 'success',
  524. 'form_name' => 'element_tag_proposition_'.$element->getId(),
  525. 'tags' => $search_tags,
  526. 'html' => $response->getContent()
  527. ));
  528. }
  529. public function proposeTagsProceedAction($element_id, $token)
  530. {
  531. if (($response = $this->mustBeConnected(true)))
  532. {
  533. return $response;
  534. }
  535. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  536. ->findOneById($element_id)) || $token != $this->getUser()->getPersonalHash())
  537. {
  538. return $this->jsonResponse(array(
  539. 'status' => 'error',
  540. 'errors' => array('NotFound')
  541. ));
  542. }
  543. // On ne doit pas pouvoir proposer de tags sur son propre élément
  544. if ($element->getOwner()->getId() == $this->getUserId())
  545. {
  546. return $this->jsonResponse(array(
  547. 'status' => 'error',
  548. 'errors' => array('NotAllowed')
  549. ));
  550. }
  551. $values = $this->getRequest()->request->get('element_tag_proposition_'.$element->getId());
  552. $tags_ids = json_decode($values['tags'], true);
  553. $tags = array();
  554. if (count($tags_ids))
  555. {
  556. // On récupère les tags en base
  557. $tags = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:Tag')
  558. ->getTagsWithIds($tags_ids)
  559. ;
  560. }
  561. if (!count($tags))
  562. {
  563. return $this->jsonResponse(array(
  564. 'status' => 'error',
  565. 'errors' => array($this->trans('element.tag_proposition.form.error.empty', array(), 'elements'))
  566. ));
  567. }
  568. /**
  569. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  570. * Docrine le voit si on faire une requete directe.
  571. */
  572. $user = $this->getUser();
  573. if ($this->container->getParameter('env') == 'test')
  574. {
  575. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  576. $this->container->get('security.context')->getToken()->getUser()->getId(),
  577. array()
  578. )->getSingleResult();
  579. }
  580. $proposition = new ElementTagsProposition();
  581. $proposition->setElement($element);
  582. $proposition->setUser($user);
  583. $date = new \DateTime(date('Y-m-d H:i:s'));
  584. $proposition->setCreated($date);
  585. foreach ($tags as $tag)
  586. {
  587. // Si le tag est a modérer, il faut que le propriétaire de l'élément
  588. // puisse voir ce tag, afin d'accepter en toute connaisance la proposition.
  589. if ($tag->getTomoderate())
  590. {
  591. if (!$tag->hasIdInPrivateIds($element->getOwner()->getId()))
  592. {
  593. // Si son id n'y est pas on la rajoute afin que le proprio puisse voir
  594. // ces nouveau tags
  595. $private_ids = json_decode($tag->getPrivateids(), true);
  596. $private_ids[] = $element->getOwner()->getId();
  597. $tag->setPrivateids(json_encode($private_ids));
  598. $this->getDoctrine()->getEntityManager()->persist($tag);
  599. }
  600. }
  601. $proposition->addTag($tag);
  602. }
  603. $element->setHasTagProposition(true);
  604. $this->getDoctrine()->getEntityManager()->persist($element);
  605. $this->getDoctrine()->getEntityManager()->persist($proposition);
  606. // Notifs etc
  607. $event = new EventElement($this->container);
  608. $event->tagsProposed($element);
  609. $this->getDoctrine()->getEntityManager()->flush();
  610. return $this->jsonResponse(array(
  611. 'status' => 'success',
  612. 'dom_id' => 'element_'.$element->getId()
  613. ));
  614. }
  615. public function proposedTagsViewAction($element_id)
  616. {
  617. if (($response = $this->mustBeConnected(true)))
  618. {
  619. return $response;
  620. }
  621. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  622. ->findOneById($element_id)))
  623. {
  624. return $this->jsonResponse(array(
  625. 'status' => 'error',
  626. 'errors' => array('NotFound')
  627. ));
  628. }
  629. if ($element->getOwner()->getId() != $this->getUserId())
  630. {
  631. return $this->jsonResponse(array(
  632. 'status' => 'error',
  633. 'errors' => array('NotAllowed')
  634. ));
  635. }
  636. // On récupére toute les propsotions pour cet élément
  637. $propositions = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:ElementTagsProposition')
  638. ->findByElement($element->getId())
  639. ;
  640. $response = $this->render('MuzichCoreBundle:Element:tag.propositions.html.twig', array(
  641. 'propositions' => $propositions,
  642. 'element_id' => $element->getId()
  643. ));
  644. return $this->jsonResponse(array(
  645. 'status' => 'success',
  646. 'html' => $response->getContent()
  647. ));
  648. }
  649. public function proposedTagsAcceptAction($proposition_id, $token)
  650. {
  651. if (($response = $this->mustBeConnected(true)))
  652. {
  653. return $response;
  654. }
  655. if (!($proposition = $this->getDoctrine()->getRepository('MuzichCoreBundle:ElementTagsProposition')
  656. ->findOneById($proposition_id)) || $token != $this->getUser()->getPersonalHash())
  657. {
  658. return $this->jsonResponse(array(
  659. 'status' => 'error',
  660. 'errors' => array('NotFound')
  661. ));
  662. }
  663. // On commence par appliquer les nouveaux tags a l'élément
  664. $element = $proposition->getElement();
  665. $element->setTags(null);
  666. foreach ($proposition->getTags() as $tag)
  667. {
  668. $element->addTag($tag);
  669. }
  670. $element->setHasTagProposition(false);
  671. $element->setNeedTags(false);
  672. $this->getDoctrine()->getEntityManager()->persist($element);
  673. $event = new EventElement($this->container);
  674. $event->tagsAccepteds($proposition);
  675. $propositions = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:ElementTagsProposition')
  676. ->findByElement($element->getId())
  677. ;
  678. // On supprime les proposition liés a cet élement
  679. foreach ($propositions as $proposition)
  680. {
  681. $this->getDoctrine()->getEntityManager()->remove($proposition);
  682. }
  683. $this->getDoctrine()->getEntityManager()->flush();
  684. $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  685. ->findOneById($element->getId())
  686. ;
  687. // On récupère l'html de l'élément
  688. $html = $this->render('MuzichCoreBundle:SearchElement:element.html.twig', array(
  689. 'element' => $element
  690. ))->getContent();
  691. return $this->jsonResponse(array(
  692. 'status' => 'success',
  693. 'html' => $html
  694. ));
  695. }
  696. public function proposedTagsRefuseAction($element_id, $token)
  697. {
  698. if (($response = $this->mustBeConnected(true)))
  699. {
  700. return $response;
  701. }
  702. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  703. ->findOneById($element_id)) || $token != $this->getUser()->getPersonalHash())
  704. {
  705. return $this->jsonResponse(array(
  706. 'status' => 'error',
  707. 'errors' => array('NotFound')
  708. ));
  709. }
  710. // On supprime les proposition liés a cet élement
  711. $propositions = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:ElementTagsProposition')
  712. ->findByElement($element->getId())
  713. ;
  714. foreach ($propositions as $proposition)
  715. {
  716. $this->getDoctrine()->getEntityManager()->remove($proposition);
  717. }
  718. // On spécifie qu'il n'y as plus de proposition
  719. $element->setHasTagProposition(false);
  720. $this->getDoctrine()->getEntityManager()->persist($element);
  721. $this->getDoctrine()->getEntityManager()->flush();
  722. return $this->jsonResponse(array(
  723. 'status' => 'success'
  724. ));
  725. }
  726. public function reshareAction(Request $request, $element_id, $token)
  727. {
  728. if (($response = $this->mustBeConnected(true)))
  729. {
  730. return $response;
  731. }
  732. if ($this->getUser()->getPersonalHash('reshare_'.$element_id) != $token)
  733. {
  734. throw new \Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException();
  735. }
  736. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  737. ->findOneById($element_id)))
  738. {
  739. throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
  740. }
  741. if ($element->getOwner()->getId() == $this->getUserId())
  742. {
  743. throw new \Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException();
  744. }
  745. /**
  746. * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
  747. * Docrine le voit si on faire une requete directe.
  748. */
  749. $user = $this->getUser();
  750. if ($this->container->getParameter('env') == 'test')
  751. {
  752. $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
  753. $this->container->get('security.context')->getToken()->getUser()->getId(),
  754. array()
  755. )->getSingleResult();
  756. }
  757. // Pour le repartage on crée un nouvel élément
  758. $element_reshared = new Element();
  759. $element_reshared->setUrl($element->getUrl());
  760. $element_reshared->setName($element->getName());
  761. $element_reshared->addTags($element->getTags());
  762. $element_reshared->setParent($element);
  763. // On utilise le gestionnaire d'élément
  764. $factory = new ElementManager($element_reshared, $this->getEntityManager(), $this->container);
  765. $factory->proceedFill($user, false);
  766. // On se retrouve maintenant avec un nouvel element tout neuf
  767. $this->persist($element_reshared);
  768. $this->flush();
  769. $html_element = $this->render('MuzichCoreBundle:SearchElement:li.element.html.twig', array(
  770. 'element' => $element_reshared,
  771. 'class_color' => 'odd' // TODO: n'est plus utilisé
  772. ))->getContent();
  773. return $this->jsonResponse(array(
  774. 'status' => 'success',
  775. 'html' => $html_element,
  776. 'groups' => $this->isAddedElementCanBeInGroup($element_reshared)
  777. ));
  778. }
  779. protected function findTagsWithProposeds($tags)
  780. {
  781. $tag_like = new TagLike($this->getDoctrine());
  782. $tags_with_likes = array();
  783. foreach ($tags as $tag_name)
  784. {
  785. // On va determiner si on connais ces tags
  786. $tag_like_tag = $tag_like->getSimilarTags($tag_name, $this->getUserId());
  787. // Premièrement: Si on a trouvé des équivalents en base
  788. if (array_key_exists('tags', $tag_like_tag))
  789. {
  790. if (count($tag_like_tag['tags']))
  791. {
  792. // Deuxièmement: Si nos algorythmes on déterminés qu'on l'a en base
  793. if ($tag_like_tag['same_found'])
  794. {
  795. // A ce moment là on le considère comme "bon"
  796. // Et on prend le premier
  797. $tags_with_likes[] = array(
  798. 'original_name' => $tag_name,
  799. 'like_found' => true,
  800. 'like' => $tag_like_tag['tags'][0]
  801. );
  802. }
  803. // On considère ce tag comme inconnu, l'utilisateur peut toute fois
  804. // l'ajouté a notre base.
  805. else
  806. {
  807. $tags_with_likes[] = array(
  808. 'original_name' => $tag_name,
  809. 'like_found' => false,
  810. 'like' => array()
  811. );
  812. }
  813. }
  814. }
  815. }
  816. return $tags_with_likes;
  817. }
  818. public function getDatasApiAction(Request $request)
  819. {
  820. if (($response = $this->mustBeConnected(true)))
  821. {
  822. return $response;
  823. }
  824. $url = null;
  825. if (count(($element_add_values = $request->get('element_add'))))
  826. {
  827. $url = $element_add_values['url'];
  828. }
  829. // On vérifie la tête de l'url quand même
  830. if (filter_var($url, FILTER_VALIDATE_URL) === false)
  831. {
  832. return $this->jsonResponse(array(
  833. 'status' => 'error',
  834. 'errors' => array(
  835. $this->trans('error.url.invalid', array(), 'validators')
  836. )
  837. ));
  838. }
  839. // On construit l'élèment qui va nous permettre de travailler avec l'api
  840. $element = new Element();
  841. $element->setUrl($url);
  842. $factory = new ElementManager($element, $this->getEntityManager(), $this->container);
  843. $factory->proceedFill($this->getUser());
  844. // On gère les tags proposés
  845. $tags_propositions = array();
  846. if (count($tags = $element->getProposedTags()))
  847. {
  848. $tags_propositions = $this->findTagsWithProposeds($tags);
  849. }
  850. return $this->jsonResponse(array(
  851. 'status' => 'success',
  852. 'name' => $element->getProposedName(),
  853. 'tags' => $tags_propositions,
  854. 'thumb' => $element->getThumbnailUrl()
  855. ));
  856. }
  857. /**
  858. * Retourne les données permettant de faire une playlist
  859. *
  860. * @param Request $request
  861. * @param "filter"|"show"|"favorites" $type
  862. * @param ~ $data
  863. */
  864. public function getDatasAutoplayAction(Request $request, $type, $data, $show_type = null, $show_id = null)
  865. {
  866. if (($response = $this->mustBeConnected(true)))
  867. {
  868. return $response;
  869. }
  870. $elements = array();
  871. $elements_json = array();
  872. if ($type == 'filter')
  873. {
  874. // Pour cette option on utilise le dernier filtre appliqué
  875. $search_object = $this->getElementSearcher();
  876. $search_object->update(array(
  877. 'count' => $this->container->getParameter('autoplay_max_elements')
  878. ));
  879. $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId());
  880. }
  881. elseif ($type == 'show')
  882. {
  883. if ($show_type != 'user' && $show_type != 'group')
  884. {
  885. throw $this->createNotFoundException('Not found');
  886. }
  887. $tags = null;
  888. $tag_ids = json_decode($data);
  889. $search_object = new ElementSearcher();
  890. if (count($tag_ids))
  891. {
  892. $tags = array();
  893. foreach ($tag_ids as $id)
  894. {
  895. $tags[$id] = $id;
  896. }
  897. }
  898. $search_object->init(array(
  899. 'tags' => $tags,
  900. $show_type.'_id' => $show_id,
  901. 'count' => $this->container->getParameter('autoplay_max_elements')
  902. ));
  903. $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId());
  904. }
  905. elseif ($type == 'favorite')
  906. {
  907. $tags = null;
  908. $tag_ids = json_decode($data);
  909. $search_object = new ElementSearcher();
  910. if (count($tag_ids))
  911. {
  912. $tags = array();
  913. foreach ($tag_ids as $id)
  914. {
  915. $tags[$id] = $id;
  916. }
  917. }
  918. $search_object->init(array(
  919. 'tags' => $tags,
  920. 'user_id' => $show_id,
  921. 'favorite' => true,
  922. 'count' => $this->container->getParameter('autoplay_max_elements')
  923. ));
  924. $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId());
  925. }
  926. if (count($elements))
  927. {
  928. // On récupère les élements
  929. $autoplaym = new AutoplayManager($elements, $this->container);
  930. $elements_json = $autoplaym->getList();
  931. }
  932. return $this->jsonResponse(array(
  933. 'status' => 'success',
  934. 'data' => $elements_json
  935. ));
  936. }
  937. public function getOneDomAction(Request $request, $element_id, $type)
  938. {
  939. if (($response = $this->mustBeConnected()))
  940. {
  941. return $response;
  942. }
  943. if (!in_array($type, array('autoplay')))
  944. {
  945. return $this->jsonResponse(array(
  946. 'status' => 'error',
  947. 'errors' => array('NotAllowed')
  948. ));
  949. }
  950. // variables pour le template
  951. $display_edit_actions = true;
  952. $display_player = true;
  953. $display_comments = true;
  954. if ($type == 'autoplay')
  955. {
  956. $display_edit_actions = false;
  957. $display_player = false;
  958. $display_comments = false;
  959. }
  960. // On prépare la récupèration de l'élèment
  961. $es = new ElementSearcher();
  962. $es->init(array(
  963. 'ids' => array($element_id)
  964. ));
  965. if (!($element = $es->getElements($this->getDoctrine(), $this->getUserId(), 'single')))
  966. {
  967. throw $this->createNotFoundException('Not found');
  968. }
  969. $html = $this->render('MuzichCoreBundle:SearchElement:element.html.twig', array(
  970. 'element' => $element,
  971. 'display_edit_actions' => $display_edit_actions,
  972. 'display_player' => $display_player,
  973. 'display_comments' => $display_comments
  974. ))->getContent();
  975. return $this->jsonResponse(array(
  976. 'status' => 'success',
  977. 'data' => $html
  978. ));
  979. }
  980. public function geJamendotStreamDatasAction(Request $request, $element_id)
  981. {
  982. if (($response = $this->mustBeConnected(true)))
  983. {
  984. return $response;
  985. }
  986. if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
  987. ->findOneById($element_id)))
  988. {
  989. throw $this->createNotFoundException('Not found');
  990. }
  991. $manager = new ElementManager($element, $this->getEntityManager(), $this->container);
  992. $stream_data = $manager->getFactory()->getStreamData();
  993. return $this->jsonResponse(array(
  994. 'status' => 'success',
  995. 'data' => $stream_data,
  996. ));
  997. }
  998. }