123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?php
-
- namespace Muzich\CoreBundle\Controller;
-
- use Muzich\CoreBundle\lib\Controller;
- use Muzich\CoreBundle\ElementFactory\ElementManager;
-
- class ElementController extends Controller
- {
-
- /**
- *
- * @param type $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;
- }
-
- /**
- *
- */
- public function editAction($element_id)
- {
- if (($response = $this->mustBeConnected()))
- {
- return $response;
- }
-
- $element = $this->checkExistingAndOwned($element_id);
-
- $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;
- }
-
- /**
- *
- */
- public function updateAction($element_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->bindRequest($this->getRequest());
-
- $errors = array();
- $html = '';
- if ($form->isValid())
- {
- $status = 'success';
- $em = $this->getDoctrine()->getEntityManager();
- $factory = new ElementManager($element, $em, $this->container);
- $factory->proceedFill($user);
- // Si il y avais un groupe on le remet
- $element->setGroup($group);
- $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,
- '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()
- ));
- }
-
- public function removeAction($element_id)
- {
- if (($response = $this->mustBeConnected()))
- {
- return $response;
- }
-
- try {
- $element = $this->checkExistingAndOwned($element_id);
- $em = $this->getDoctrine()->getEntityManager();
- $em->remove($element);
- $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'));
- }
- }
-
- 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 (($response = $this->mustBeConnected()))
- {
- return $response;
- }
-
- $es = $this->getElementSearcher();
- $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(), '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;
- }
-
- $es = $this->getElementSearcher();
- $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,
- 'invertcolor' => false
- ))->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)
- ));
- }
-
- }
|