Browse Source

Evolution #113: Administration: Gestion des tags à modérer

bastien 12 years ago
parent
commit
5ba9cf2c1f

+ 63 - 20
src/Muzich/AdminBundle/Controller/ModerateController.php View File

@@ -5,6 +5,8 @@ namespace Muzich\AdminBundle\Controller;
5 5
 use Muzich\CoreBundle\lib\Controller;
6 6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7 7
 //use Muzich\CoreBundle\Util\TagLike;
8
+use Muzich\CoreBundle\Entity\UsersTagsFavorites;
9
+use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
8 10
 
9 11
 class ModerateController extends Controller
10 12
 {
@@ -168,33 +170,74 @@ class ModerateController extends Controller
168 170
      */
169 171
     $em = $this->getDoctrine()->getEntityManager();
170 172
     
171
-    $netags = array();
173
+    // Sur un élément
172 174
     foreach ($elements = $this->getDoctrine()->getEntityManager()->createQuery("
173 175
       SELECT e, t FROM MuzichCoreBundle:Element e
174 176
       JOIN e.tags t
175 177
       WHERE t.id  = :tid
176 178
     ")
177
-      ->setParameter('tid', $tag_new_id)
179
+      ->setParameter('tid', $tag_id)
178 180
       ->getResult() as $element)
179 181
     {
180
-      
181
-      // TODO: a faire ...
182
-//      foreach ($element->getTags() as $etag)
183
-//      {
184
-//        if ($etag->getId() != $tag->getId())
185
-//        {
186
-//          $netags[] = $etag;
187
-//        }
188
-//      }
189
-//      $netags[] = $new_tag;
190
-//      
191
-//      $element->setTags(array());
192
-//      $em->persist($element);
193
-//      $em->flush();
194
-//      
195
-//      $element->setTags($netags);
196
-//      $em->persist($element);
197
-//      $em->flush();
182
+      // Pour chaque elements lié a ce tag
183
+      // On ajoute un lien vers le nouveau tag si il n'en n'a pas déjà
184
+      if (!$element->hasTag($new_tag))
185
+      {
186
+        $element->addTag($new_tag);
187
+        $em->persist($element);
188
+      }
189
+    }
190
+    
191
+    // Tag favoris
192
+    foreach ($favorites = $this->getDoctrine()->getEntityManager()->createQuery("
193
+      SELECT f FROM MuzichCoreBundle:UsersTagsFavorites f
194
+      WHERE f.tag  = :tid
195
+    ")
196
+      ->setParameter('tid', $tag_id)
197
+      ->getResult() as $fav)
198
+    {
199
+      // Pour chaque favoris utilisant ce tag on regarde si l'utilisateur
200
+      // n'a pas déjà le nouveau tag en favoris.
201
+      if (!$this->getDoctrine()->getEntityManager()->createQuery("
202
+        SELECT COUNT(f.id) FROM MuzichCoreBundle:UsersTagsFavorites f
203
+        WHERE f.tag  = :tid AND f.user = :uid
204
+      ")
205
+      ->setParameters(array('tid' => $tag_new_id, 'uid' => $fav->getUser()->getId()))
206
+      ->getSingleScalarResult())
207
+      {
208
+        $new_fav = new UsersTagsFavorites();
209
+        $new_fav->setTag($new_tag);
210
+        $new_fav->setUser($fav->getUser());
211
+        $new_fav->setPosition($fav->getPosition());
212
+        $em->persist($new_fav);
213
+      }
214
+      $em->remove($fav);
215
+    }
216
+    
217
+    // groupe
218
+    foreach ($this->getDoctrine()->getEntityManager()->createQuery("
219
+      SELECT f FROM MuzichCoreBundle:GroupsTagsFavorites f
220
+      WHERE f.tag  = :tid
221
+    ")
222
+      ->setParameter('tid', $tag_id)
223
+      ->getResult() as $fav)
224
+    {
225
+      // Pour chaque favoris utilisant ce tag on regarde si le groupe
226
+      // n'a pas déjà le nouveau tag en favoris.
227
+      if (!$this->getDoctrine()->getEntityManager()->createQuery("
228
+        SELECT COUNT(f.id) FROM MuzichCoreBundle:GroupsTagsFavorites f
229
+        WHERE f.tag  = :tid AND f.group = :gid
230
+      ")
231
+      ->setParameters(array('tid' => $tag_new_id, 'gid' => $fav->getGroup()->getId()))
232
+      ->getSingleScalarResult())
233
+      {
234
+        $new_fav = new GroupsTagsFavorites();
235
+        $new_fav->setTag($new_tag);
236
+        $new_fav->setGroup($fav->getGroup());
237
+        $new_fav->setPosition($fav->getPosition());
238
+        $em->persist($new_fav);
239
+      }
240
+      $em->remove($fav);
198 241
     }
199 242
     
200 243
     $em->remove($tag);

+ 18 - 0
src/Muzich/CoreBundle/Entity/Element.php View File

@@ -8,6 +8,7 @@ use Gedmo\Mapping\Annotation as Gedmo;
8 8
 use Doctrine\ORM\EntityManager;
9 9
 use Symfony\Component\Validator\Constraints as Assert;
10 10
 use Muzich\CoreBundle\Validator as MuzichAssert;
11
+use Muzich\CoreBundle\Entity\Tag;
11 12
 
12 13
 /**
13 14
  * L'Element est l'Element central de l'application. C'est cet
@@ -415,4 +416,21 @@ class Element
415 416
     $this->group = $this->group->getId();
416 417
   }
417 418
   
419
+//  public function deleteTag(Tag $tag)
420
+//  {
421
+//    $this->tags->removeElement($tag);
422
+//  }
423
+  
424
+  public function hasTag(Tag $tag_t)
425
+  {
426
+    foreach ($this->getTags() as $tag)
427
+    {
428
+      if ($tag_t->getId() == $tag->getId())
429
+      {
430
+        return true;
431
+      }
432
+    }
433
+    return false;
434
+  }
435
+  
418 436
 }