Browse Source

Ajout de la fonctionnalité "ajouter un tag".

bastien 13 years ago
parent
commit
e82fba3ed1

+ 3 - 0
app/Resources/translations/userui.fr.yml View File

@@ -31,6 +31,9 @@ tags:
31 31
            la liste.
32 32
   inputtext:
33 33
     help:      Saisissez vos tags ici (un par un)
34
+  search:
35
+    message_found:  "Tags correspondant à \"%string%\":"
36
+    display_more:   Afficher les autres résultats
34 37
            
35 38
 filter:
36 39
   network: "Résultats de "

+ 84 - 0
src/Muzich/CoreBundle/Controller/CoreController.php View File

@@ -13,6 +13,8 @@ use Muzich\CoreBundle\Entity\Element;
13 13
 use Symfony\Component\HttpFoundation\RedirectResponse;
14 14
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
15 15
 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
16
+use Muzich\CoreBundle\Util\StrictCanonicalizer;
17
+use Muzich\CoreBundle\Entity\Tag;
16 18
 
17 19
 class CoreController extends Controller
18 20
 {
@@ -349,4 +351,86 @@ class CoreController extends Controller
349 351
     ));
350 352
   }
351 353
   
354
+  /**
355
+   * Ajout d'un tag en base.
356
+   */
357
+  public function addTagAction($name)
358
+  {
359
+    if ($this->getUser() == 'anon.')
360
+    {
361
+      if ($this->getRequest()->isXmlHttpRequest())
362
+      {
363
+        return $this->jsonResponse(array(
364
+          'status' => 'mustbeconnected'
365
+        ));
366
+      }
367
+      else
368
+      {
369
+        return $this->redirect($this->generateUrl('index'));
370
+      }
371
+    }
372
+    
373
+    $canonicalizer = new StrictCanonicalizer();
374
+    $name_canonicalized = $canonicalizer->canonicalize($name);
375
+    
376
+    // Check avant de commencer: On regarde si ce tag n'existe pas déjà en tag
377
+    // public (en cas de gruge)
378
+    if (($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
379
+      ->findOneBy(array(
380
+        'slug'       => $name_canonicalized,
381
+        'tomoderate' => false
382
+      ))))
383
+    {
384
+      // Si il existe déjà pas besoin de l'ajouter on retourne l'id et ne nom
385
+      return $this->jsonResponse(array(
386
+        'status'   => 'success',
387
+        'tag_id'   => $tag->getId(),
388
+        'tag_name' => $tag->getName()
389
+      ));
390
+    }
391
+    
392
+    // Première étape, on regarde en base si quelqu'un a pas déjà ajouté ce tag
393
+    if (($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')
394
+      ->findOneBy(array(
395
+        'slug'       => $name_canonicalized,
396
+        'tomoderate' => true
397
+      ))))
398
+    {
399
+      // Si il existe déjà pas besoin de l'ajouter on retourne l'id et ne nom
400
+      // après avoir ajouté cet utilisateurs a la liste de ceux pouvant le voir
401
+      $privatesids = json_decode($tag->getPrivateids());
402
+      if (!in_array($this->getUserId(), $privatesids))
403
+      {
404
+        $privatesids[] = (string)$this->getUserId();
405
+      }
406
+      $tag->setPrivateids(json_encode($privatesids));
407
+      
408
+      $this->getDoctrine()->getEntityManager()->persist($tag);
409
+      $this->getDoctrine()->getEntityManager()->flush();
410
+      
411
+      return $this->jsonResponse(array(
412
+        'status'   => 'success',
413
+        'tag_id'   => $tag->getId(),
414
+        'tag_name' => $tag->getName()
415
+      ));
416
+    }
417
+    else
418
+    {
419
+      // Sinon on l'ajoute en base
420
+      $tag = new Tag();
421
+      $tag->setName(ucfirst(strtolower($name)));
422
+      $tag->setTomoderate(true);
423
+      $tag->setPrivateids(json_encode(array((string)$this->getUserId())));
424
+      
425
+      $this->getDoctrine()->getEntityManager()->persist($tag);
426
+      $this->getDoctrine()->getEntityManager()->flush();
427
+      
428
+      return $this->jsonResponse(array(
429
+        'status'   => 'success',
430
+        'tag_id'   => $tag->getId(),
431
+        'tag_name' => $tag->getName()
432
+      ));
433
+    }
434
+  }
435
+  
352 436
 }

+ 34 - 1
src/Muzich/CoreBundle/Controller/SearchController.php View File

@@ -287,6 +287,25 @@ class SearchController extends Controller
287 287
     {
288 288
       if ($counted['count'] > 1)
289 289
       {
290
+        // Ci-dessous on va chercher a voir si le tag et la recherche on le 
291
+        // même nombre de mots, si c'est le cas on pourra considérer cette 
292
+        // recherche comme lié a un tag connu.
293
+        
294
+        $words_search = array_merge(
295
+          explode(' ', $search), 
296
+          explode('-', $search)
297
+        );
298
+        
299
+        $words_tag = array_merge(
300
+          explode(' ', $tag['slug']), 
301
+          explode('-', $tag['slug'])
302
+        );
303
+        
304
+        if ($words_search == $words_tag)
305
+        {
306
+          $same_found = true;
307
+        }
308
+        
290 309
         $tag_sorted = $this->sort_addtop_if_isnt_in($tag_sorted, $counted['tag']);
291 310
       }
292 311
     }
@@ -308,7 +327,14 @@ class SearchController extends Controller
308 327
         {
309 328
           if (strtoupper($canonicalizer->canonicalize($word)) == strtoupper($tag['slug']))
310 329
           {
311
-            $same_found = true;
330
+            // Ci-dessous on déduit si le mot étant identique au tag représente bien
331
+            // le terme de recherche. De façon a si c'est le cas pouvoir dire:
332
+            // oui le terme recherché est connu.
333
+            (in_array($word, array(
334
+              $search,
335
+              str_replace(' ', '-', $search),
336
+              str_replace('-', ' ', $search)
337
+            ))) ? $same_found = true : 
312 338
             $tag_sorted = $this->sort_addtop_if_isnt_in($tag_sorted, $tag);
313 339
           }
314 340
         }
@@ -395,18 +421,25 @@ class SearchController extends Controller
395 421
         $sort_response = $this->sort_search_tags($tags_response, $string_search);
396 422
         $status = 'success';
397 423
         $error  = '';
424
+        $message = $this->trans(
425
+          'tags.search.message_found', 
426
+          array('%string%' => $string_search), 
427
+          'userui'
428
+        );
398 429
       }
399 430
       else
400 431
       {
401 432
         $status = 'error';
402 433
         $sort_response = array('tags' => array(), 'same_found' => false);
403 434
         $error = 'Vous devez saisir au moins deux caractères';
435
+        $message  = '';
404 436
       }
405 437
       
406 438
       $return_array = array(
407 439
         'status'     => $status,
408 440
         'timestamp'  => $timestamp,
409 441
         'error'      => $error,
442
+        'message'    => $message,
410 443
         'same_found' => $sort_response['same_found'],
411 444
         'data'       => $sort_response['tags']
412 445
         

+ 37 - 0
src/Muzich/CoreBundle/Entity/Tag.php View File

@@ -72,6 +72,23 @@ class Tag
72 72
   protected $count = 0;
73 73
   
74 74
   /**
75
+   * Booléen permettant de savoir si le tag est à modérer
76
+   * 
77
+   * @ORM\Column(type="boolean", nullable=true)
78
+   * @var type string
79
+   */
80
+  protected $tomoderate = false;
81
+  
82
+  /**
83
+   * Lorsque le tag est a modérer on stocke ici les ids d'utilisateurs (json)
84
+   * qui ont voulu l'utiliser. Afin qu'il n'y est que eux a le voir.
85
+   * 
86
+   * @ORM\Column(type="text", nullable=true)
87
+   * @var type string
88
+   */
89
+  protected $privateids = "";
90
+  
91
+  /**
75 92
    * 
76 93
    */
77 94
   public function __construct()
@@ -184,4 +201,24 @@ class Tag
184 201
   {
185 202
     return $this->groups_favorites;
186 203
   }
204
+  
205
+  public function setTomoderate($tomoderate)
206
+  {
207
+    $this->tomoderate = $tomoderate;
208
+  }
209
+  
210
+  public function getTomoderate()
211
+  {
212
+    return $this->tomoderate;
213
+  }
214
+  
215
+  public function setPrivateids($privateids)
216
+  {
217
+    $this->privateids = $privateids;
218
+  }
219
+  
220
+  public function getPrivateids()
221
+  {
222
+    return $this->privateids;
223
+  }
187 224
 }

+ 5 - 0
src/Muzich/CoreBundle/Resources/config/routing.yml View File

@@ -44,6 +44,11 @@ ajax_get_favorites_tags:
44 44
   pattern: /ajax/my-favorites-tags
45 45
   defaults: { _controller: MuzichCoreBundle:Core:getFavoriteTags }
46 46
 
47
+ajax_add_tag:
48
+  pattern: /ajax/add-tag/{name}
49
+  defaults: { _controller: MuzichCoreBundle:Core:addTag, name: null }
50
+  
51
+
47 52
 ####
48 53
 
49 54
 info_about:

+ 1 - 1
src/Muzich/CoreBundle/Resources/views/Tag/tagsPrompt.html.twig View File

@@ -21,7 +21,7 @@
21 21
      </div>
22 22
      <ul class="search_tag_list"></ul>
23 23
      <a class="more button" href="#" style="display: none;">
24
-       Afficher les autres tags correspondant a la recherche
24
+       {{ 'tags.search.display_more'|trans({}, 'userui') }}
25 25
      </a>
26 26
    </div>
27 27
 </div>

+ 1 - 0
src/Muzich/CoreBundle/Resources/views/layout.html.twig View File

@@ -32,6 +32,7 @@
32 32
     url_index = "{{ path('index') }}";
33 33
     url_search_tag = "{{ path('search_tag') }}";
34 34
     url_get_favorites_tags = "{{ path('ajax_get_favorites_tags') }}";
35
+    url_add_tag = "{{ path('ajax_add_tag') }}";
35 36
   </script>
36 37
   {% block js %}{% endblock %}
37 38
   

+ 29 - 2
web/bundles/muzichcore/css/main.css View File

@@ -676,7 +676,7 @@ div.search_tag_list
676 676
   position: absolute;  
677 677
   background-color: white;
678 678
   width: 400px;
679
-  padding: 2px;
679
+  padding: 5px;
680 680
   
681 681
   border: 0px solid #d5d5d5;
682 682
   border-radius: 7px;
@@ -712,19 +712,46 @@ div.search_tag_list ul.search_tag_list li
712 712
   margin-left: 4px;
713 713
   margin-top: 2px;
714 714
   margin-bottom: 2px;
715
-  padding: 2px;
715
+  padding: 2px 4px 2px 4px;
716 716
   background-color: #d9f7ff;
717
+  
718
+  
719
+  border-radius: 7px;
720
+  -moz-border-radius: 7px;
721
+  -webkit-border-radius: 7px;
722
+  
723
+  -webkit-box-shadow: #666 0px 2px 3px;
724
+  -moz-box-shadow: #666 0px 2px 3px;
725
+  box-shadow: #666 0px 2px 3px;
726
+}
727
+
728
+div.search_tag_list ul.search_tag_list li.new
729
+{
730
+  font-weight: bold;
731
+  background-color: #e0e0e0;
717 732
 }
733
+  
718 734
 
719 735
 div.search_tag_list ul.search_tag_list li:hover
720 736
 {
721 737
   background-color: #74dffe;
722 738
 }
723 739
 
740
+div.search_tag_list span.info
741
+{
742
+  text-shadow: 1px 1px 1px #666;
743
+  font-weight: bold;
744
+}
745
+
724 746
 #container ul.search_tag_list li a
725 747
 {
726 748
   color: black;
727 749
 }
750
+
751
+#container ul.search_tag_list li a:hover
752
+{
753
+  text-decoration: none;
754
+}
728 755
  
729 756
 .elements_loader_div
730 757
 {

+ 56 - 4
web/bundles/muzichcore/js/muzich.js View File

@@ -677,6 +677,7 @@ $(document).ready(function(){
677 677
       // Et on affiche une info
678 678
       span_info = divtags.find('span.info');
679 679
       span_info.show();
680
+      // TODO: multilingue !
680 681
       span_info.text("Recherche des tags correspondants à \""+input.val()+"\" ...");
681 682
 
682 683
       // C'est en fonction du nb de resultats qu'il sera affiché
@@ -765,15 +766,66 @@ $(document).ready(function(){
765 766
               {
766 767
                 divtags.find('a.more').show();
767 768
               }
768
-
769
-              // On cache l'info
770
-              span_info.hide();
769
+              
770
+              span_info.show();
771
+              span_info.text(data.message);
771 772
               // Et on affiche la liste
772 773
               search_tag_list.show();
773 774
             }
774 775
             else
775 776
             {
776
-              span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
777
+              span_info.show();
778
+              span_info.text(data.message);
779
+              search_tag_list.show();
780
+              
781
+              // Dans ce cas ou aucun tag n'a été trouvé, la proposition 
782
+              // d'ajout s'affichecf en dessous
783
+              
784
+              //span_info.text("Aucun tag de trouvé pour \""+inputTag.val()+"\"");
785
+            }
786
+            
787
+            // Si le tag ne semble pas connu en base
788
+            if (!data.same_found)
789
+            {
790
+              li_tag = 
791
+                $('<li>').addClass('new').append(
792
+                  $('<a>').attr('href','#new#'+$.trim(input.val()))
793
+                  // qui réagit quand on clique dessus
794
+                  .click(function(e){
795
+                    // On récupère le nom du tag
796
+                    name = $(this).attr('href').substr(1,$(this).attr('href').length);
797
+                    name = name.substr(strpos(name, '#')+1, name.length);
798
+                    
799
+                    $(this).parent('li').parent('ul').parent('div').find('img.tag_loader').show();
800
+
801
+                    // La on fait l'ajout en base en tant que nouveau tag
802
+                    $.getJSON(url_add_tag+'/'+name, function(response){
803
+      
804
+                      if (response.status == 'mustbeconnected')
805
+                      {
806
+                        $(location).attr('href', url_index);
807
+                      }
808
+                      
809
+                      tag_id   = response.tag_id;
810
+                      tag_name = response.tag_name;
811
+                      
812
+                      $('input#tags_selected_tag_'+form_name).val(tag_id);
813
+                      inputTag.val(tag_name);
814
+                      // Et on execute l'évènement selectTag de l'input
815
+                      inputTag.trigger("selectTag");
816
+                      // On cache la liste puisque le choix vient d'être fait
817
+                      divtags.hide();
818
+                      inputTag.val(tag_text_help); 
819
+                      
820
+                      $(this).parent('li').parent('ul').parent('div').find('img.tag_loader').hide();
821
+                    });
822
+
823
+                    
824
+                    return false;
825
+                  })
826
+                  .append($.trim(input.val()))
827
+              );
828
+              search_tag_list.prepend(li_tag);
777 829
             }
778 830
             
779 831
           }