12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220 |
- <?php
-
- namespace Muzich\CoreBundle\Controller;
-
- use Muzich\CoreBundle\lib\Controller;
- use Muzich\CoreBundle\Managers\ElementManager;
- use Muzich\CoreBundle\Propagator\EventElement;
- use Muzich\CoreBundle\Entity\ElementTagsProposition;
- use Symfony\Component\HttpFoundation\Request;
- use Muzich\CoreBundle\Entity\Element;
- use Muzich\CoreBundle\Entity\Event;
- use Muzich\CoreBundle\Util\TagLike;
- use Muzich\CoreBundle\Entity\User;
- use Muzich\CoreBundle\lib\AutoplayManager;
- use Muzich\CoreBundle\Searcher\ElementSearcher;
- use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
- use Muzich\CoreBundle\Security\Context as SecurityContext;
-
- class ElementController extends Controller
- {
-
- /**
- * Cette méthode est utilisé pour récupérer un objet Element tout en levant
- * une erreur si il n'existe pas ou si il n'appartient pas a l'utilisateur en
- * cours.
- *
- * @param int $element_id
- * @return Muzich\CoreBundle\Entity\Element
- */
- protected function checkExistingAndOwned($element_id)
- {
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)))
- {
- throw $this->createNotFoundException('Not found');
- }
-
- if ($element->getOwner()->getId() != $this->getUserId())
- {
- throw $this->createNotFoundException('Not found');
- }
-
- return $element;
- }
-
- /**
- * Action d'ouverture du formulaire de modification d'un élément.
- *
- * @param int $element_id
- * @return Response
- */
- public function editAction($element_id)
- {
- if (($response = $this->mustBeConnected()))
- {
- return $response;
- }
-
- $element = $this->checkExistingAndOwned($element_id);
-
- // On doit faire un chmilblik avec les tags pour
- // utiliser le javascript de tags (tagPrompt)
- // sur le formulaire
- $element_tags = $element->getTags();
- $element->setTags($element->getTagsIdsJson());
- $form = $this->getAddForm($element);
-
- $search_tags = array();
- foreach ($element_tags as $tag)
- {
- $search_tags[$tag->getId()] = $tag->getName();
- }
-
- $template = 'MuzichCoreBundle:Element:ajax.element.edit.html.twig';
- if (!$this->getRequest()->isXmlHttpRequest())
- {
- $template = 'MuzichCoreBundle:Element:element.edit.html.twig';
- }
-
- $response = $this->render($template, array(
- 'form' => $form->createView(),
- 'form_name' => 'element_'.$element->getId(),
- 'element_id' => $element->getId(),
- 'search_tags' => $search_tags
- ));
-
- if ($this->getRequest()->isXmlHttpRequest())
- {
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'form_name' => 'element_'.$element->getId(),
- 'tags' => $search_tags,
- 'html' => $response->getContent()
- ));
- }
-
- return $response;
- }
-
- /**
- * Mise a jour des données d'un élément.
- *
- * @param int $element_id
- * @param string $dom_id
- * @return Response
- */
- public function updateAction($element_id, $dom_id)
- {
- if (($response = $this->mustBeConnected()))
- {
- return $response;
- }
-
- /**
- * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
- * Docrine le voit si on faire une requete directe.
- */
- $user = $this->getUser();
- if ($this->container->getParameter('env') == 'test')
- {
- $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
- $this->container->get('security.context')->getToken()->getUser()->getId(),
- array()
- )->getSingleResult();
- }
-
- $element = $this->checkExistingAndOwned($element_id);
- // Si il y a un groupe on le retire pour le bind
- $group = $element->getGroup();
- $element->setGroup(null);
- $form = $this->getAddForm($element);
- $form->bind($this->getRequest());
-
- $errors = array();
- $html = '';
- if ($form->isValid())
- {
- $status = 'success';
- $em = $this->getDoctrine()->getEntityManager();
- // On utilise le manager d'élément
- $factory = new ElementManager($element, $em, $this->container);
- $factory->proceedFill($user);
- // Si il y avais un groupe on le remet
- $element->setGroup($group);
-
- // On signale que cet user a modifié ses diffusions
- $user->setData(User::DATA_DIFF_UPDATED, true);
- $em->persist($user);
-
- $em->persist($element);
- $em->flush();
-
- // Récupération du li
- $html = $this->render('MuzichCoreBundle:SearchElement:element.html.twig', array(
- 'element' => $element
- ))->getContent();
- }
- else
- {
- $status = 'error';
- // Récupération des erreurs
- $validator = $this->container->get('validator');
- $errorList = $validator->validate($form);
-
- foreach ($errorList as $error)
- {
- $errors[] = $this->trans($error->getMessage(), array(), 'validators');
- }
- }
-
- if ($this->getRequest()->isXmlHttpRequest())
- {
- return $this->jsonResponse(array(
- 'status' => $status,
- 'dom_id' => $dom_id,
- 'html' => $html,
- 'errors' => $errors
- ));
- }
-
- if ($status == 'success')
- {
- return $this->redirect($this->generateUrl('home'));
- }
-
-
- $element->setTagsWithIds(
- $this->getDoctrine()->getEntityManager(),
- json_decode($element->getTags())
- );
-
- return $this->render('MuzichCoreBundle:Element:element.edit.html.twig', array(
- 'form' => $form->createView(),
- 'form_name' => 'element_'.$element->getId(),
- 'element_id' => $element->getId(),
- 'search_tags' => $element->getTagsIdsJson()
- ));
- }
-
- /**
- * Suppression d'un élément.
- *
- * @param int $element_id
- * @return Response
- */
- public function removeAction($element_id, $token)
- {
- if (($response = $this->mustBeConnected()))
- {
- return $response;
- }
-
- if ($token != $this->getUser()->getPersonalHash($element_id))
- {
- return $this->jsonResponse(array('status' => 'error'));
- }
-
- try {
- $element = $this->checkExistingAndOwned($element_id);
- $em = $this->getDoctrine()->getEntityManager();
-
- $event = new EventElement($this->container);
- $event->elementRemoved($element);
-
- $em->persist($element->getOwner());
- $em->remove($element);
-
- /**
- * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
- * Docrine le voit si on faire une requete directe.
- */
- $user = $this->getUser();
- if ($this->container->getParameter('env') == 'test')
- {
- $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
- $this->container->get('security.context')->getToken()->getUser()->getId(),
- array()
- )->getSingleResult();
- }
-
- // On signale que cet user a modifié ses diffusions
- $user->setData(User::DATA_DIFF_UPDATED, true);
- $em->persist($user);
-
- $em->flush();
-
- if ($this->getRequest()->isXmlHttpRequest())
- {
- return $this->jsonResponse(array('status' => 'success'));
- }
- $this->setFlash('success', 'element.remove.success');
- return $this->redirect($this->container->get('request')->headers->get('referer'));
- }
- catch(Exception $e)
- {
- if ($this->getRequest()->isXmlHttpRequest())
- {
- return $this->jsonResponse(array('status' => 'error'));
- }
- $this->setFlash('error', 'element.remove.error');
- return $this->redirect($this->container->get('request')->headers->get('referer'));
- }
- }
-
- /**
- * Cette procédure retourne le lien a afficher sur la page home permettant
- * d'afficher des élément apparus entre temps.
- *
- * @param int $count
- * @return type
- */
- protected function getcountNewMessage($count)
- {
- if ($count == 1)
- {
- $transid = 'tags.new.has_news_one';
- $transidlink = 'tags.new.has_news_link_one';
- }
- else if ($count == 0)
- {
- return '';
- }
- else
- {
- $transid = 'tags.new.has_news';
- $transidlink = 'tags.new.has_news_link';
- }
-
-
- if ($count > ($limit = $this->container->getParameter('search_default_count')))
- {
- $link = $this->trans(
- 'tags.new.has_news_link_more_x',
- array(
- '%x%' => $limit
- ),
- 'userui'
- );
- }
- else
- {
- $link = $this->trans(
- $transidlink,
- array(),
- 'userui'
- );
- }
-
- $link = '<a href="#" class="show_new_elements" >'.$link.'</a>';
-
- return $this->trans(
- $transid,
- array(
- '%count%' => $count,
- '%link%' => $link
- ),
- 'userui'
- );
- }
-
- /**
- * Retourne le nombre de nouveaux éléments possible
- *
- * @param int $refid
- */
- public function countNewsAction($refid)
- {
- if (!$this->getRequest()->isXmlHttpRequest())
- {
- return $this->redirect($this->generateUrl('index'));
- }
-
- if ($this->getRequest()->getMethod() != 'POST')
- {
- throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
- }
-
- /*
- * On met à jour l'ElementSearcher avec le form
- */
- $es = $this->getElementSearcher(null, true);
- $search_form = $this->getSearchForm($es);
- $search_form->bind($this->getRequest());
-
- if ($search_form->isValid())
- {
- $es->update($search_form->getData());
- }
-
- $es->update(array(
- // On veux de nouveaux éléments
- 'searchnew' => true,
- // Notre id de référence
- 'id_limit' => $refid
- ));
-
- $count = $es->getElements($this->getDoctrine(), $this->getUserId(true), 'count');
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'count' => $count,
- 'message' => $this->getcountNewMessage($count)
- ));
- }
-
- /**
- * Cette action, utilisé en ajax seulement, retourne les x nouveaux éléments
- * depuis le refid transmis. Tout en respectant le filtre en cours.
- *
- * @param int $refid identifiant de l'élément de référence
- *
- * @return jsonResponse
- */
- public function getNewsAction($refid)
- {
- if (!$this->getRequest()->isXmlHttpRequest())
- {
- return $this->redirect($this->generateUrl('index'));
- }
-
- if (($response = $this->mustBeConnected()))
- {
- return $response;
- }
-
- if ($this->getRequest()->getMethod() != 'POST')
- {
- throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
- }
-
- /*
- * On met à jour l'ElementSearcher avec le form
- */
- $es = $this->getElementSearcher(null, true);
- $search_form = $this->getSearchForm($es);
- $search_form->bind($this->getRequest());
-
- if ($search_form->isValid())
- {
- $es->update($search_form->getData());
- }
-
- $es->update(array(
- // On veux de nouveaux éléments
- 'searchnew' => true,
- // Notre id de référence
- 'id_limit' => $refid,
- // On en veut qu'un certain nombres
- 'count' => $this->container->getParameter('search_default_count')
- ));
-
- // Récupération de ces nouveaux élméents
- $elements = $es->getElements($this->getDoctrine(), $this->getUserId());
-
- // On en fait un rendu graphique
- $html_elements = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
- 'user' => $this->getUser(),
- 'elements' => $elements
- ))->getContent();
-
- // On calcule le nouveau compte de nouveaux
- $count = 0;
- if (count($elements))
- {
- $es->update(array(
- // On veux de nouveaux éléments
- 'searchnew' => true,
- // Notre id de référence
- 'id_limit' => $elements[0]->getId(),
- // On n'en récupère que x
- 'count' => $this->container->getParameter('search_default_count')
- ));
- $count = $es->getElements($this->getDoctrine(), $this->getUserId(), 'count');
- }
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'html' => $html_elements,
- 'count' => $count,
- 'message' => $this->getcountNewMessage($count)
- ));
- }
-
- /**
- * Action (ajax) ajoutant son vote "good" sur un élément
- *
- * @param int $element_id
- * @param string $token
- * @return Response
- */
- public function addVoteGoodAction($element_id, $token)
- {
- if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_ELEMENT_NOTE)) !== false)
- {
- return $this->jsonResponseError($non_condition);
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $this->getUser()->getPersonalHash($element_id) != $token)
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- if ($element->getOwner()->getId() == $this->getUserId())
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotAllowed')
- ));
- }
-
- // On ajoute un vote a l'élément
- $element->addVoteGood($this->getUser()->getId());
- // Puis on lance les actions propagés par ce vote
- $event = new EventElement($this->container);
- $event->onePointAdded($element);
-
- $this->getDoctrine()->getEntityManager()->persist($element);
- $this->getDoctrine()->getEntityManager()->flush();
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'data' => array(
- 'a' => array(
- 'href' => $this->generateUrl('ajax_element_remove_vote_good', array(
- 'element_id' => $element->getId(),
- 'token' => $this->getUser()->getPersonalHash($element->getId())
- ))
- ),
- 'img' => array(
- 'src' => $this->getAssetUrl('/img/icon_thumb_red.png')
- ),
- 'element' => array(
- 'points' => $element->getPoints()
- )
- )
- ));
- }
-
- /**
- * Action (ajax) de retrait de son vote good
- *
- * @param int $element_id
- * @param string $token
- * @return Response
- */
- public function removeVoteGoodAction($element_id, $token)
- {
- if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_ELEMENT_NOTE)) !== false)
- {
- return $this->jsonResponseError($non_condition);
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $this->getUser()->getPersonalHash($element_id) != $token)
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- if ($element->getOwner()->getId() == $this->getUserId())
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotAllowed')
- ));
- }
-
- // Retrait du vote good
- $element->removeVoteGood($this->getUser()->getId());
- // Puis on lance les actions propagés par retrait de vote
- $event = new EventElement($this->container);
- $event->onePointRemoved($element);
-
- $this->getDoctrine()->getEntityManager()->persist($element);
- $this->getDoctrine()->getEntityManager()->flush();
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'data' => array(
- 'a' => array(
- 'href' => $this->generateUrl('ajax_element_add_vote_good', array(
- 'element_id' => $element->getId(),
- 'token' => $this->getUser()->getPersonalHash($element->getId())
- ))
- ),
- 'img' => array(
- 'src' => $this->getAssetUrl('/img/icon_thumb.png')
- ),
- 'element' => array(
- 'points' => $element->getPoints()
- )
- )
- ));
- }
-
- /**
- * Retourne un json avec le form permettant a l'utilisateur de proposer des
- * tags sur un élément.
- *
- * @param int $element_id
- * @return Response
- */
- public function proposeTagsOpenAction($element_id)
- {
- if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION)) !== false)
- {
- return $this->jsonResponseError($non_condition);
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- $search_tags = array();
- foreach ($element->getTags() as $tag)
- {
- $search_tags[$tag->getId()] = $tag->getName();
- }
-
- $element->setTags($element->getTagsIdsJson());
- $form = $this->getAddForm($element, 'element_tag_proposition_'.$element->getId());
- $response = $this->render('MuzichCoreBundle:Element:tag.proposition.html.twig', array(
- 'form' => $form->createView(),
- 'form_name' => 'element_tag_proposition_'.$element->getId(),
- 'element_id' => $element->getId(),
- 'search_tags' => $search_tags
- ));
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'form_name' => 'element_tag_proposition_'.$element->getId(),
- 'tags' => $search_tags,
- 'html' => $response->getContent()
- ));
- }
-
- public function proposeTagsProceedAction($element_id, $token)
- {
- if (($non_condition = $this->userHaveNonConditionToMakeAction(SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION)) !== false)
- {
- return $this->jsonResponseError($non_condition);
- }
-
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $token != $this->getUser()->getPersonalHash())
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- // On ne doit pas pouvoir proposer de tags sur son propre élément
- if ($element->getOwner()->getId() == $this->getUserId())
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotAllowed')
- ));
- }
-
- $values = $this->getRequest()->request->get('element_tag_proposition_'.$element->getId());
- $tags_ids = json_decode($values['tags'], true);
-
- $tags = array();
- if (count($tags_ids))
- {
- // On récupère les tags en base
- $tags = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:Tag')
- ->getTagsWithIds($tags_ids)
- ;
- }
-
- if (!count($tags))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array($this->trans('element.tag_proposition.form.error.empty', array(), 'elements'))
- ));
- }
-
- /**
- * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
- * Docrine le voit si on faire une requete directe.
- */
- $user = $this->getUser();
- if ($this->container->getParameter('env') == 'test')
- {
- $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
- $this->container->get('security.context')->getToken()->getUser()->getId(),
- array()
- )->getSingleResult();
- }
-
- $proposition = new ElementTagsProposition();
- $proposition->setElement($element);
- $proposition->setUser($user);
- $date = new \DateTime(date('Y-m-d H:i:s'));
- $proposition->setCreated($date);
-
- foreach ($tags as $tag)
- {
- // Si le tag est a modérer, il faut que le propriétaire de l'élément
- // puisse voir ce tag, afin d'accepter en toute connaisance la proposition.
- if ($tag->getTomoderate())
- {
- if (!$tag->hasIdInPrivateIds($element->getOwner()->getId()))
- {
- // Si son id n'y est pas on la rajoute afin que le proprio puisse voir
- // ces nouveau tags
- $private_ids = json_decode($tag->getPrivateids(), true);
- $private_ids[] = $element->getOwner()->getId();
- $tag->setPrivateids(json_encode($private_ids));
- $this->getDoctrine()->getEntityManager()->persist($tag);
- }
- }
-
- $proposition->addTag($tag);
- }
-
- $element->setHasTagProposition(true);
-
- $this->getDoctrine()->getEntityManager()->persist($element);
- $this->getDoctrine()->getEntityManager()->persist($proposition);
-
- // Notifs etc
- $event = new EventElement($this->container);
- $event->tagsProposed($element);
-
- $this->getDoctrine()->getEntityManager()->flush();
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'dom_id' => 'element_'.$element->getId()
- ));
- }
-
- public function proposedTagsViewAction($element_id)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- if ($element->getOwner()->getId() != $this->getUserId())
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotAllowed')
- ));
- }
-
- // On récupére toute les propsotions pour cet élément
- $propositions = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:ElementTagsProposition')
- ->findByElement($element->getId())
- ;
-
- $response = $this->render('MuzichCoreBundle:Element:tag.propositions.html.twig', array(
- 'propositions' => $propositions,
- 'element_id' => $element->getId()
- ));
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'html' => $response->getContent()
- ));
-
- }
-
- public function proposedTagsAcceptAction($proposition_id, $token)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($proposition = $this->getDoctrine()->getRepository('MuzichCoreBundle:ElementTagsProposition')
- ->findOneById($proposition_id)) || $token != $this->getUser()->getPersonalHash($proposition_id))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- // On commence par appliquer les nouveaux tags a l'élément
- $element = $proposition->getElement();
- $element->setTags(null);
- foreach ($proposition->getTags() as $tag)
- {
- $element->addTag($tag);
- }
- $element->setHasTagProposition(false);
- $element->setNeedTags(false);
- $this->getDoctrine()->getEntityManager()->persist($element);
-
- $event = new EventElement($this->container);
- $event->tagsAccepteds($proposition);
-
- $propositions = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:ElementTagsProposition')
- ->findByElement($element->getId())
- ;
-
- // On supprime les proposition liés a cet élement
- foreach ($propositions as $proposition)
- {
- $this->getDoctrine()->getEntityManager()->remove($proposition);
- }
-
- // Traitement de l'Event si il y a
- $this->removeElementFromEvent($element->getId(), Event::TYPE_TAGS_PROPOSED);
-
- $this->getDoctrine()->getEntityManager()->flush();
- $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element->getId())
- ;
-
- // On récupère l'html de l'élément
- $html = $this->render('MuzichCoreBundle:SearchElement:element.html.twig', array(
- 'element' => $element
- ))->getContent();
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'html' => $html
- ));
- }
-
- public function proposedTagsRefuseAction($element_id, $token)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)) || $token != $this->getUser()->getPersonalHash($element_id))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotFound')
- ));
- }
-
- // On supprime les proposition liés a cet élement
- $propositions = $this->getDoctrine()->getEntityManager()->getRepository('MuzichCoreBundle:ElementTagsProposition')
- ->findByElement($element->getId())
- ;
- foreach ($propositions as $proposition)
- {
- $this->getDoctrine()->getEntityManager()->remove($proposition);
- }
-
- // Traitement de l'Event si il y a
- $this->removeElementFromEvent($element->getId(), Event::TYPE_TAGS_PROPOSED);
-
- // On spécifie qu'il n'y as plus de proposition
- $element->setHasTagProposition(false);
- $this->getDoctrine()->getEntityManager()->persist($element);
- $this->getDoctrine()->getEntityManager()->flush();
-
- return $this->jsonResponse(array(
- 'status' => 'success'
- ));
- }
-
-
- protected function removeElementFromEvent($element_id, $event_type)
- {
- if (($event = $this->getEntityManager()->getRepository('MuzichCoreBundle:Event')
- ->findUserEventWithElementId($this->getUserId(), $element_id, $event_type)))
- {
- $event->removeId($element_id);
- if (!$event->getCount())
- {
- $this->remove($event);
- $this->flush();
- return;
- }
-
- $this->persist($event);
- $this->flush();
- }
- }
-
- public function reshareAction(Request $request, $element_id, $token)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- if ($this->getUser()->getPersonalHash('reshare_'.$element_id) != $token)
- {
- throw new \Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException();
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)))
- {
- throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
- }
-
- if ($element->getOwner()->getId() == $this->getUserId())
- {
- throw new \Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException();
- }
-
-
- /**
- * Bug lors des tests: L'user n'est pas 'lié' a celui en base par doctrine.
- * Docrine le voit si on faire une requete directe.
- */
- $user = $this->getUser();
- if ($this->container->getParameter('env') == 'test')
- {
- $user = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
- $this->container->get('security.context')->getToken()->getUser()->getId(),
- array()
- )->getSingleResult();
- }
-
- // Pour le repartage on crée un nouvel élément
- $element_reshared = new Element();
- $element_reshared->setUrl($element->getUrl());
- $element_reshared->setName($element->getName());
- $element_reshared->addTags($element->getTags());
- $element_reshared->setParent($element);
-
- // On utilise le gestionnaire d'élément
- $factory = new ElementManager($element_reshared, $this->getEntityManager(), $this->container);
- $factory->proceedFill($user, false);
-
- // On se retrouve maintenant avec un nouvel element tout neuf
- $this->persist($element_reshared);
- $this->flush();
-
- $html_element = $this->render('MuzichCoreBundle:SearchElement:li.element.html.twig', array(
- 'element' => $element_reshared,
- 'class_color' => 'odd' // TODO: n'est plus utilisé
- ))->getContent();
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'html' => $html_element,
- 'groups' => $this->isAddedElementCanBeInGroup($element_reshared)
- ));
- }
-
- protected function findTagsWithProposeds($tags)
- {
- $tag_like = new TagLike($this->getDoctrine()->getEntityManager());
- $tags_with_likes = array();
- foreach ($tags as $tag_name)
- {
- // On va determiner si on connais ces tags
- $tag_like_tag = $tag_like->getSimilarTags($tag_name, $this->getUserId());
-
- // Premièrement: Si on a trouvé des équivalents en base
- if (array_key_exists('tags', $tag_like_tag))
- {
- if (count($tag_like_tag['tags']))
- {
- // Deuxièmement: Si nos algorythmes on déterminés qu'on l'a en base
- if ($tag_like_tag['same_found'])
- {
- // A ce moment là on le considère comme "bon"
- // Et on prend le premier
- $tags_with_likes[] = array(
- 'original_name' => $tag_name,
- 'like_found' => true,
- 'like' => $tag_like_tag['tags'][0]
- );
- }
- // On considère ce tag comme inconnu, l'utilisateur peut toute fois
- // l'ajouté a notre base.
- else
- {
- $tags_with_likes[] = array(
- 'original_name' => $tag_name,
- 'like_found' => false,
- 'like' => array()
- );
- }
- }
- }
- }
-
- return $tags_with_likes;
- }
-
- public function getDatasApiAction(Request $request)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- $url = null;
- if (count(($element_add_values = $request->get('element_add'))))
- {
- $url = trim($element_add_values['url']);
- }
-
- // On vérifie la tête de l'url quand même
- if (filter_var($url, FILTER_VALIDATE_URL) === false)
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array(
- $this->trans('error.url.invalid', array(), 'validators')
- )
- ));
- }
-
- // On construit l'élèment qui va nous permettre de travailler avec l'api
- $element = new Element();
- $element->setUrl($url);
-
- $factory = new ElementManager($element, $this->getEntityManager(), $this->container);
- $factory->proceedFill($this->getUser());
-
- // On gère les tags proposés
- $tags_propositions = array();
- if (count($tags = $element->getProposedTags()))
- {
- $tags_propositions = $this->findTagsWithProposeds($tags);
- }
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'name' => $element->getProposedName(),
- 'tags' => $tags_propositions,
- 'thumb' => $element->getThumbnailUrl()
- ));
- }
-
- /**
- * Retourne les données permettant de faire une playlist
- *
- * @param Request $request
- * @param "filter"|"show"|"favorites" $type
- * @param ~ $data
- */
- public function getDatasAutoplayAction(Request $request, $element_id, $type, $data, $show_type = null, $show_id = null)
- {
- $elements = array();
- $elements_json = array();
-
- if ($type == 'filter')
- {
- // Pour cette option on utilise le dernier filtre appliqué
- $search_object = $this->getElementSearcher();
- $search_object->update(array(
- 'count' => $this->container->getParameter('autoplay_max_elements'),
- 'id_limit' => $element_id+1
- ));
- $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId(true));
- }
- elseif ($type == 'show')
- {
- if ($show_type != 'user' && $show_type != 'group')
- {
- throw $this->createNotFoundException('Not found');
- }
-
- $tags = null;
- $tag_ids = json_decode($data);
- $search_object = new ElementSearcher();
-
- if (count($tag_ids))
- {
- $tags = array();
- foreach ($tag_ids as $id)
- {
- $tags[$id] = $id;
- }
- }
-
- $search_object->init(array(
- 'tags' => $tags,
- $show_type.'_id' => $show_id,
- 'count' => $this->container->getParameter('autoplay_max_elements'),
- 'id_limit' => $element_id+1
- ));
-
- $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId());
- }
- elseif ($type == 'favorite')
- {
- $tags = null;
- $tag_ids = json_decode($data);
- $search_object = new ElementSearcher();
-
- if (count($tag_ids))
- {
- $tags = array();
- foreach ($tag_ids as $id)
- {
- $tags[$id] = $id;
- }
- }
-
- $search_object->init(array(
- 'tags' => $tags,
- 'user_id' => $show_id,
- 'favorite' => true,
- 'count' => $this->container->getParameter('autoplay_max_elements'),
- 'id_limit' => $element_id+1
- ));
-
- $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId(true));
- }
-
- if (count($elements))
- {
- // On récupère les élements
- $autoplaym = new AutoplayManager($elements, $this->container);
- $elements_json = $autoplaym->getList();
- }
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'data' => $elements_json
- ));
- }
-
- public function getOneDomAction(Request $request, $element_id, $type)
- {
- if (!in_array($type, array('autoplay')))
- {
- return $this->jsonResponse(array(
- 'status' => 'error',
- 'errors' => array('NotAllowed')
- ));
- }
-
- // variables pour le template
- $display_edit_actions = true;
- $display_player = true;
- $display_comments = true;
-
- if ($type == 'autoplay')
- {
- $display_edit_actions = false;
- $display_player = false;
- $display_comments = false;
- }
-
- // On prépare la récupèration de l'élèment
- $es = new ElementSearcher();
- $es->init(array(
- 'ids' => array($element_id)
- ));
-
- if (!($element = $es->getElements($this->getDoctrine(), $this->getUserId(true), 'single')))
- {
- throw $this->createNotFoundException('Not found');
- }
-
- $html = $this->render('MuzichCoreBundle:SearchElement:element.html.twig', array(
- 'element' => $element,
- 'display_edit_actions' => $display_edit_actions,
- 'display_player' => $display_player,
- 'display_comments' => $display_comments
- ))->getContent();
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'data' => $html
- ));
- }
-
- public function geJamendotStreamDatasAction(Request $request, $element_id)
- {
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)))
- {
- throw $this->createNotFoundException('Not found');
- }
-
- $manager = new ElementManager($element, $this->getEntityManager(), $this->container);
- $stream_data = $manager->getFactory()->getStreamData();
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'data' => $stream_data,
- ));
- }
-
- public function getEmbedCodeAction($element_id)
- {
- if (!$element_id)
- {
- return $this->jsonNotFoundResponse();
- }
-
- if (!($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)))
- {
- return $this->jsonNotFoundResponse();
- }
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'data' => $element->getEmbed(),
- ));
- }
-
- public function removeFromGroupAction($group_id, $element_id, $token)
- {
- if (!($group = $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
- ->findOneById($group_id))
- || !($element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
- ->findOneById($element_id)))
- {
- return $this->jsonNotFoundResponse();
- }
-
- if ($token != $this->getUser()->getPersonalHash('remove_from_group_'.$element->getId())
- || $group->getOwner()->getId() != $this->getUserId())
- {
- return $this->jsonNotFoundResponse();
- }
-
- $element->setGroup(null);
- $this->persist($element);
- $this->flush();
-
- return $this->jsonResponse(array(
- 'status' => 'success'
- ));
- }
-
- }
|