123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
-
- namespace Muzich\CoreBundle\Controller;
-
- use Muzich\CoreBundle\lib\Controller;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
-
- use Muzich\CoreBundle\Searcher\ElementSearcher;
- use Muzich\CoreBundle\Form\Search\ElementSearchForm;
- use Symfony\Component\HttpFoundation\Response;
- use Muzich\CoreBundle\Util\TagLike;
-
- use Symfony\Component\HttpFoundation\Request;
- use Muzich\CoreBundle\Searcher\GlobalSearcher;
-
- class SearchController extends Controller
- {
-
- /**
- * Procédure qui construit un réponse json contenant le html
- * par defalt de la liste d'élément.
- *
- * @param Collection $elements
- * @param sring $message
- * @param string $session_id
- * @return Response
- */
- protected function searchElementsMore($elements, $message, $session_id)
- {
-
- $data = array();
- $end = (($count = count($elements)) < $this->container->getParameter('search_ajax_more'));
- $html = '';
- if ($count)
- {
- $html = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
- 'user' => $this->getUser(),
- 'elements' => $elements
- ))->getContent();
-
- $data['more_link_href'] = $this->generateUrl('search_elements_more', array(
- 'id_limit' => $elements[count($elements)-1]->getId(),
- 'session_id' => $session_id
- ));
- }
-
- return $this->jsonResponse(array(
- 'status' => 'success',
- 'count' => $count,
- 'message' => $message,
- 'html' => $html,
- 'end' => $end,
- 'data' => $data
- ));
- }
-
- /**
- * Procédure de recherche, qui met a jour l'objet de recherche (ainsi
- * que les paramétres en session).
- *
- */
- public function searchElementsAction($id_limit = null, $session_id = null)
- {
- if (($response = $this->mustBeConnected()))
- {
- return $response;
- }
-
- $request = $this->getRequest();
- $search_object = $this->getElementSearcher(null, false, $session_id);
-
- $search_form = $this->getSearchForm($search_object);
-
- $form_submited = false;
- if ($request->getMethod() == 'POST')
- {
- $form_submited = true;
- $search_form->bindRequest($request);
- // Si le formulaire est valide
- if ($search_form->isValid())
- {
- // On met a jour l'objet avec les nouveaux paramétres saisie dans le form
- $data = $search_form->getData();
-
- // Le formulaire nous permet de récupérer uniquement les ids.
- // On va donc chercher les name en base pour le passer a l'objet
- // ElementSearch
- $data['tags'] = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
- ->getTagsForElementSearch(json_decode($data['tags'], true));
-
- $search_object->update($data);
- // Et on met a jour la "mémoire" de la recherche
- $this->setElementSearcherParams($search_object->getParams());
- }
- }
-
- if ($this->getRequest()->isXmlHttpRequest())
- {
- if ($form_submited && !$id_limit)
- {
- $message = $this->trans(
- 'noelements.sentence_filter',
- array('%link_string%' => $this->trans(
- 'noelements.sentence_filter_link_string',
- array(),
- 'elements'
- )),
- 'elements'
- );
- }
- else
- {
- $message = $this->trans(
- 'elements.ajax.more.noelements',
- array(),
- 'elements'
- );
- }
-
- // template qui apelle doSearchElementsAction
- $search_object->update(array(
- 'count' => $this->container->getParameter('search_ajax_more'),
- 'id_limit' => $id_limit
- ));
-
- $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId());
-
- return $this->searchElementsMore($elements, $message, $session_id);
- }
- else
- {
- return $this->redirect($this->generateUrl('home'));
- }
- }
-
- /**
- * Action (ajax) de récupération d'éléments en plus
- * [a check pour être sur] N'EST PLUS UTILISE
- *
- * @param string $type
- * @param string $object_id
- * @param int $id_limit
- * @return Response
- */
- public function searchElementsShowAction($type, $object_id, $id_limit)
- {
- if ($this->getRequest()->isXmlHttpRequest())
- {
- $object = null;
- $param_id = '';
- if ($type == 'user')
- {
- $object = $this->getDoctrine()
- ->getRepository('MuzichCoreBundle:User')
- ->findOneBy(array('id' => $object_id))
- ;
- $param_id = 'user_id';
- }
- elseif ($type == 'group')
- {
- $object = $this->getDoctrine()
- ->getRepository('MuzichCoreBundle:Group')
- ->findOneById($object_id)
- ;
- $param_id = 'group_id';
- }
-
- if (!$object)
- {
- throw new \Exception('Object Unknow');
- }
-
- $search = $this->createSearchObject(array(
- $param_id => $object->getId(),
- 'count' => $this->container->getParameter('search_ajax_more'),
- 'id_limit' => $id_limit
- ));
-
- $elements = $search->getElements($this->getDoctrine(), $this->getUserId());
-
- return $this->searchElementsMore($elements,
- $this->trans(
- 'elements.ajax.more.noelements',
- array(),
- 'elements'
- )
- );
- }
-
- throw new \Exception('XmlHttpRequest only for this action');
- }
-
- /**
- * Action permettant d'afficher plus de résultats (éléments) dans
- * une global search.
- *
- * @param Request $request
- * @param int $last_id
- * @param string $string
- * @return Response
- */
- public function globalSearchMoreAction(Request $request, $last_id, $string)
- {
- if (($response = $this->mustBeConnected(true)))
- {
- return $response;
- }
-
- $search = $this->createSearchObject(array(
- 'count' => $this->container->getParameter('search_ajax_more'),
- 'id_limit' => $last_id,
- 'string' => $string
- ));
-
- $elements = $search->getElements($this->getDoctrine(), $this->getUserId());
-
- return $this->searchElementsMore($elements,
- $this->trans(
- 'elements.ajax.more.noelements',
- array(),
- 'elements'
- ),
- null
- );
- }
-
- /**
- * Procédure (ajax) de recherche de tags. Essentielement utilisé dans
- * le tagPrompt.
- *
- * @param string $string_search
- * @param int $timestamp
- * @return Response
- */
- public function searchTagAction($timestamp)
- {
- if (($response = $this->mustBeConnected()))
- {
- return $response;
- }
-
- $string_search = $this->getRequest()->request->get('string_search');
-
- if ($this->getRequest()->isXmlHttpRequest())
- {
- if (strlen(trim($string_search)) > 1)
- {
- // On utilise l'objet TagLike
- $TagLike = new TagLike($this->getDoctrine());
- // Pour trier nos tags d'une manière plus humaine
- $sort_response = $TagLike->getSimilarTags($string_search, $this->getUserId());
-
- $status = 'success';
- $error = '';
- $message = $this->trans(
- 'tags.search.message_found',
- array('%string%' => $string_search),
- 'userui'
- );
- }
- else
- {
- $status = 'error';
- $sort_response = array('tags' => array(), 'same_found' => false);
- $error = 'Vous devez saisir au moins deux caractères';
- $message = '';
- }
-
- $return_array = array(
- 'status' => $status,
- 'timestamp' => $timestamp,
- 'error' => $error,
- 'message' => $message,
- 'same_found' => $sort_response['same_found'],
- 'data' => $sort_response['tags']
-
- );
-
- $response = new Response(json_encode($return_array));
- $response->headers->set('Content-Type', 'application/json; charset=utf-8');
- return $response;
- }
-
- throw $this->createNotFoundException('Cette ressource n\'est pas accessible');
- }
-
- /**
- * Récupére l'id d'un tag (ajax)
- * [A check] mais ne doit et n'est plus utilisé.
- *
- * @param type $string_search
- * @return Response
- */
- public function searchTagIdAction($string_search)
- {
- if ($this->getRequest()->isXmlHttpRequest())
- {
- $tag_id = $this->getDoctrine()->getEntityManager()->createQuery("
- SELECT t.id FROM MuzichCoreBundle:Tag t
- WHERE t.name = :str
- ORDER BY t.name ASC"
- )->setParameter('str', $string_search)
- ->getSingleScalarResult()
- ;
-
- $response = new Response(json_encode($tag_id));
- $response->headers->set('Content-Type', 'application/json; charset=utf-8');
- return $response;
- }
-
- throw $this->createNotFoundException('Cette ressource n\'est pas accessible');
- }
-
- /**
- * Retourne une réponse contenant le dom du formulaire de recherche global
- *
- * @return \Symfony\Component\HttpFoundation\Response
- */
- public function renderGlobalSearchFormAction()
- {
- return $this->render(
- 'MuzichCoreBundle:GlobalSearch:form.html.twig',
- array('form' => $this->getGlobalSearchForm()->createView())
- );
- }
-
- /**
- * Page d'affichage des résultats pour une recherche globale.
- * * Users
- * * Groups
- * * Partages
- *
- * @return \Symfony\Component\HttpFoundation\Response
- * @Template("MuzichCoreBundle:GlobalSearch:results.html.twig")
- */
- public function globalAction(Request $request)
- {
- $form = $this->getGlobalSearchForm($searcher = new GlobalSearcher());
- $results = array(
- 'users' => null,
- 'groups' => null,
- 'elements' => null
- );
-
- if ($request->getMethod() == 'POST')
- {
- $form->bindRequest($request);
- if ($form->isValid())
- {
- $results = $searcher->getResults(
- $this->getDoctrine(),
- $this->getUserId(),
- $this->container->getParameter('search_default_count'),
- $this->container->getParameter('search_global_elements_word_min_length')
- );
- }
- }
-
- return array(
- 'form' => $form->createView(),
- 'results' => $results,
- 'display_more_button' => (count($results['elements']))? (count($results['elements']) >= $this->container->getParameter('search_default_count'))? true : false : false
- );
- }
-
- }
|