Browse Source

Evolution #584: Propositions de stags: cacher

Sevajol Bastien 11 years ago
parent
commit
48ed7e3113

+ 28 - 0
src/Muzich/CoreBundle/Controller/ElementController.php View File

@@ -8,10 +8,12 @@ use Muzich\CoreBundle\Propagator\EventElement;
8 8
 use Muzich\CoreBundle\Entity\ElementTagsProposition;
9 9
 use Symfony\Component\HttpFoundation\Request;
10 10
 use Muzich\CoreBundle\Entity\Element;
11
+use Muzich\CoreBundle\Entity\Event;
11 12
 use Muzich\CoreBundle\Util\TagLike;
12 13
 use Muzich\CoreBundle\Entity\User;
13 14
 use Muzich\CoreBundle\lib\AutoplayManager;
14 15
 use Muzich\CoreBundle\Searcher\ElementSearcher;
16
+use Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException;
15 17
 
16 18
 class ElementController extends Controller
17 19
 {
@@ -784,6 +786,9 @@ class ElementController extends Controller
784 786
       $this->getDoctrine()->getEntityManager()->remove($proposition);
785 787
     }
786 788
     
789
+    // Traitement de l'Event si il y a
790
+    $this->removeElementFromEvent($element->getId(), Event::TYPE_TAGS_PROPOSED);
791
+    
787 792
     $this->getDoctrine()->getEntityManager()->flush();
788 793
     $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
789 794
       ->findOneById($element->getId())
@@ -824,6 +829,10 @@ class ElementController extends Controller
824 829
     {
825 830
       $this->getDoctrine()->getEntityManager()->remove($proposition);
826 831
     }
832
+    
833
+    // Traitement de l'Event si il y a
834
+    $this->removeElementFromEvent($element->getId(), Event::TYPE_TAGS_PROPOSED);
835
+    
827 836
     // On spécifie qu'il n'y as plus de proposition
828 837
     $element->setHasTagProposition(false);
829 838
     $this->getDoctrine()->getEntityManager()->persist($element);
@@ -834,6 +843,25 @@ class ElementController extends Controller
834 843
     ));
835 844
   }
836 845
   
846
+  
847
+  protected function removeElementFromEvent($element_id, $event_type)
848
+  {
849
+    if (($event = $this->getEntityManager()->getRepository('MuzichCoreBundle:Event')
850
+          ->findUserEventWithElementId($this->getUserId(), $element_id, $event_type)))
851
+    {
852
+      $event->removeId($element_id);
853
+      if (!$event->getCount())
854
+      {
855
+        $this->remove($event);
856
+        $this->flush();
857
+        return;
858
+      }
859
+      
860
+      $this->persist($event);
861
+      $this->flush();
862
+    }
863
+  }
864
+  
837 865
   public function reshareAction(Request $request, $element_id, $token)
838 866
   {
839 867
     if (($response = $this->mustBeConnected(true)))

+ 18 - 1
src/Muzich/CoreBundle/Entity/Event.php View File

@@ -139,7 +139,24 @@ class Event
139 139
     }
140 140
     $this->setIds($ids);
141 141
   }
142
-    
142
+  
143
+  public function removeId($id)
144
+  {
145
+    if ($this->hasId($id))
146
+    {
147
+      $new_ids = array();
148
+      $ids = $this->getIds();
149
+      foreach ($ids as $i => $idc)
150
+      {
151
+        if ($id != $idc)
152
+        {
153
+          $new_ids[] = $idc;
154
+        }
155
+      }
156
+      $this->setIds($new_ids);
157
+      $this->setCount(count($new_ids));
158
+    }
159
+  }
143 160
   /**
144 161
    * Répond vrai si l'id transmis fait partis des ids
145 162
    * 

+ 21 - 0
src/Muzich/CoreBundle/Repository/EventRepository.php View File

@@ -26,4 +26,25 @@ class EventRepository extends EntityRepository
26 26
     return $events;
27 27
   }
28 28
   
29
+  public function findUserEventWithElementId($user_id, $element_id, $event_type)
30
+  {
31
+    $query =  $this->getEntityManager()
32
+      ->createQuery('SELECT e FROM MuzichCoreBundle:Event e
33
+        WHERE e.user = :uid AND
34
+        e.ids LIKE :eid AND
35
+        e.type = :etype'
36
+      )
37
+      ->setParameters(array(
38
+        'uid' => $user_id,
39
+        'eid' => '%"'.$element_id.'"%',
40
+        'etype' => $event_type
41
+      ))
42
+    ;
43
+    try {
44
+        return $query->getSingleResult();
45
+    } catch (\Doctrine\ORM\NoResultException $e) {
46
+        return null;
47
+    }
48
+  }
49
+  
29 50
 }

+ 161 - 0
src/Muzich/CoreBundle/Tests/Controller/EventTest.php View File

@@ -788,4 +788,165 @@ class EventTest extends FunctionalTest
788 788
     $this->assertEquals(count($result), 0);
789 789
   }
790 790
   
791
+  public function testElementTagsPropositions()
792
+  {
793
+    $this->client = self::createClient();
794
+    $this->connectUser('bux', 'toor');
795
+    
796
+    $paul = $this->getUser('paul');
797
+    $bux = $this->getUser('bux');
798
+    
799
+    $element_1 = $this->findOneBy('Element', 'Infected Mushroom - Psycho');
800
+    $element_2 = $this->findOneBy('Element', 'CardioT3K - Juggernaut Trap ');
801
+    $tag_1     = $this->findOneBy('Tag', 'Metal');
802
+    
803
+    // Bux propose un tag de remplacement sur son element 1
804
+    $this->eventCount($paul, Event::TYPE_TAGS_PROPOSED, 0);
805
+    $this->proposeElementTags($element_1, $bux, array($tag_1->getId()));
806
+    $this->eventCount($paul, Event::TYPE_TAGS_PROPOSED, 1);
807
+    $this->eventHasElementId($paul, Event::TYPE_TAGS_PROPOSED, $element_1->getId());
808
+    // Deuxieme proposition
809
+    $this->proposeElementTags($element_2, $bux, array($tag_1->getId()));
810
+    $this->eventCount($paul, Event::TYPE_TAGS_PROPOSED, 1);
811
+    $this->eventHasElementId($paul, Event::TYPE_TAGS_PROPOSED, $element_1->getId());
812
+    $event = $this->eventHasElementId($paul, Event::TYPE_TAGS_PROPOSED, $element_2->getId());
813
+    // On connecte paul
814
+    $this->disconnectUser();
815
+    $this->connectUser('paul', 'toor');
816
+    $this->acceptTagProposition($paul, $this->getElementTagProposition($element_1->getId(), $bux->getId())->getId());
817
+    $this->eventCount($paul, Event::TYPE_TAGS_PROPOSED, 1);
818
+    $this->eventHasNotElementId($paul, Event::TYPE_TAGS_PROPOSED, $element_1->getId());
819
+    $this->eventHasElementId($paul, Event::TYPE_TAGS_PROPOSED, $element_2->getId());
820
+    $this->refuseTagProposition($paul, $element_2->getId());
821
+    $this->eventCount($paul, Event::TYPE_TAGS_PROPOSED, 0);
822
+  }
823
+  
824
+  protected function eventCount($user, $type, $count)
825
+  {
826
+    $events = $this->getDoctrine()->getEntityManager()
827
+      ->createQuery('SELECT e FROM MuzichCoreBundle:Event e
828
+        WHERE e.user = :uid AND e.type = :type'
829
+      )
830
+      ->setParameters(array(
831
+        'uid' => $user->getId(),
832
+        'type' => $type
833
+      ))
834
+      ->getArrayResult()
835
+    ;
836
+    $this->assertEquals(count($events), $count);
837
+  }
838
+  
839
+  protected function proposeElementTags($element, $user, $tags_ids)
840
+  {
841
+    $crawler = $this->client->request(
842
+      'POST', 
843
+      $this->generateUrl('ajax_element_propose_tags_proceed', 
844
+        array('element_id' => $element->getId(), 'token' => $user->getPersonalHash())
845
+      ), 
846
+      array(
847
+        'element_tag_proposition_'.$element->getId() => array(
848
+          'tags' => json_encode($tags_ids)
849
+        )
850
+      ), 
851
+      array(), 
852
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
853
+    );
854
+    
855
+    $this->isResponseSuccess();
856
+    
857
+    $response = json_decode($this->client->getResponse()->getContent(), true);
858
+    $this->assertEquals($response['status'], 'success');
859
+  }
860
+  
861
+  protected function eventHasElementId($user, $type, $element_id)
862
+  {
863
+    $events = $this->getDoctrine()->getEntityManager()
864
+      ->createQuery('SELECT e FROM MuzichCoreBundle:Event e
865
+        WHERE e.user = :uid AND e.type = :type AND e.ids LIKE :eid'
866
+      )
867
+      ->setParameters(array(
868
+        'uid' => $user->getId(),
869
+        'type' => $type,
870
+        'eid' => '%"'.$element_id.'"%'
871
+      ))
872
+      ->getArrayResult()
873
+    ;
874
+    $this->assertEquals(count($events), 1);
875
+    return $events[0];
876
+  }
877
+  
878
+  protected function eventHasNotElementId($user, $type, $element_id)
879
+  {
880
+    $events = $this->getDoctrine()->getEntityManager()
881
+      ->createQuery('SELECT e FROM MuzichCoreBundle:Event e
882
+        WHERE e.user = :uid AND e.type = :type AND e.ids LIKE :eid'
883
+      )
884
+      ->setParameters(array(
885
+        'uid' => $user->getId(),
886
+        'type' => $type,
887
+        'eid' => '%"'.$element_id.'"%'
888
+      ))
889
+      ->getArrayResult()
890
+    ;
891
+    $this->assertEquals(count($events), 0);
892
+  }
893
+  
894
+  protected function acceptTagProposition($user, $proposition_id)
895
+  {
896
+    $crawler = $this->client->request(
897
+      'POST', 
898
+      $this->generateUrl('ajax_element_proposed_tags_accept', 
899
+        array(
900
+          'proposition_id' => $proposition_id,
901
+          'token' => $user->getPersonalHash()
902
+        )
903
+      ), 
904
+      array(), 
905
+      array(), 
906
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
907
+    );
908
+    
909
+    $this->isResponseSuccess();
910
+    
911
+    $response = json_decode($this->client->getResponse()->getContent(), true);
912
+    $this->assertEquals($response['status'], 'success');
913
+  }        
914
+  
915
+  protected function refuseTagProposition($user, $element_id)
916
+  {
917
+    $crawler = $this->client->request(
918
+      'POST', 
919
+      $this->generateUrl('ajax_element_proposed_tags_refuse', 
920
+        array(
921
+          'element_id' => $element_id,
922
+          'token' => $user->getPersonalHash()
923
+        )
924
+      ), 
925
+      array(), 
926
+      array(), 
927
+      array('HTTP_X-Requested-With' => 'XMLHttpRequest')
928
+    );
929
+    
930
+    $this->isResponseSuccess();
931
+    
932
+    $response = json_decode($this->client->getResponse()->getContent(), true);
933
+    $this->assertEquals($response['status'], 'success');
934
+  }
935
+  
936
+  protected function getElementTagProposition($element_id, $user_id)
937
+  {
938
+    $propositions = $this->getDoctrine()->getEntityManager()
939
+      ->createQuery('SELECT p, t FROM MuzichCoreBundle:ElementTagsProposition p'
940
+        .' JOIN p.tags t WHERE p.element = :eid AND p.user = :uid')
941
+      ->setParameters(array(
942
+        'eid' => $element_id,
943
+        'uid' => $user_id
944
+      ))
945
+      ->getResult();
946
+    if (count($propositions))
947
+    {
948
+      return $propositions[0];
949
+    }
950
+  }
951
+  
791 952
 }

+ 9 - 0
src/Muzich/CoreBundle/lib/Controller.php View File

@@ -436,6 +436,15 @@ class Controller extends BaseController
436 436
   }
437 437
   
438 438
   /**
439
+   *
440
+   * @param object $entity 
441
+   */
442
+  public function remove($entity)
443
+  {
444
+    $this->getEntityManager()->remove($entity);
445
+  }
446
+  
447
+  /**
439 448
    * 
440 449
    */
441 450
   public function flush()

+ 1 - 1
src/Muzich/UserBundle/Controller/EventController.php View File

@@ -38,7 +38,7 @@ class EventController extends Controller
38 38
           'errors' => array('NotFound')
39 39
         ));
40 40
       }
41
-      throw $this->createNotFoundException('Ressource ajax uniquement.');
41
+      return $this->redirect($this->generateUrl('index'));
42 42
     }
43 43
     
44 44
     if ($event->getUser()->getId() != $this->getUserId())

+ 8 - 6
src/Muzich/UserBundle/Resources/views/Event/elements.html.twig View File

@@ -19,12 +19,14 @@
19 19
     
20 20
     <div class="show_options">
21 21
      
22
-      <a class="button darkbutton" href="{{ path('event_delete', {
23
-        'event_id' : event.id,
24
-        'token'    : app.user.getPersonalHash(event.id)
25
-      }) }}" >
26
-        {{ 'events.view.link_delete'|trans({}, 'userui') }}
27
-      </a>
22
+      {% if event.type != event_const('TYPE_TAGS_PROPOSED') %}
23
+        <a class="button darkbutton" href="{{ path('event_delete', {
24
+          'event_id' : event.id,
25
+          'token'    : app.user.getPersonalHash(event.id)
26
+        }) }}" >
27
+          {{ 'events.view.link_delete'|trans({}, 'userui') }}
28
+        </a>
29
+      {% endif %}
28 30
     
29 31
     </div>
30 32