Pārlūkot izejas kodu

Refactorisation moderation tags.

bastien 12 gadus atpakaļ
vecāks
revīzija
a8062faaa5

+ 8 - 104
src/Muzich/AdminBundle/Controller/ModerateController.php Parādīt failu

@@ -7,6 +7,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7 7
 //use Muzich\CoreBundle\Util\TagLike;
8 8
 use Muzich\CoreBundle\Entity\UsersTagsFavorites;
9 9
 use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
10
+use Muzich\CoreBundle\Managers\TagManager;
10 11
 
11 12
 class ModerateController extends Controller
12 13
 {
@@ -58,10 +59,8 @@ class ModerateController extends Controller
58 59
       }
59 60
     }
60 61
     
61
-    if (!($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneBy(array(
62
-      'id'         => $tag_id,
63
-      'tomoderate' => true
64
-    ))))
62
+    $tagManager = new TagManager();
63
+    if (!$tagManager->moderateTag($this->getDoctrine(), $tag_id, true))
65 64
     {
66 65
       return $this->jsonResponse(array(
67 66
         'status'  => 'error',
@@ -69,11 +68,6 @@ class ModerateController extends Controller
69 68
       ));
70 69
     }
71 70
     
72
-    $tag->setTomoderate(false);
73
-    $tag->setPrivateids(null);
74
-    $this->getDoctrine()->getEntityManager()->persist($tag);
75
-    $this->getDoctrine()->getEntityManager()->flush();
76
-    
77 71
     return $this->jsonResponse(array(
78 72
       'status' => 'success'
79 73
     ));
@@ -95,10 +89,8 @@ class ModerateController extends Controller
95 89
       }
96 90
     }
97 91
     
98
-    if (!($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneBy(array(
99
-      'id'         => $tag_id,
100
-      'tomoderate' => true
101
-    ))))
92
+    $tagManager = new TagManager();
93
+    if (!$tagManager->moderateTag($this->getDoctrine(), $tag_id, false))
102 94
     {
103 95
       return $this->jsonResponse(array(
104 96
         'status'  => 'error',
@@ -106,9 +98,6 @@ class ModerateController extends Controller
106 98
       ));
107 99
     }
108 100
     
109
-    $this->getDoctrine()->getEntityManager()->remove($tag);
110
-    $this->getDoctrine()->getEntityManager()->flush();
111
-    
112 101
     return $this->jsonResponse(array(
113 102
       'status' => 'success'
114 103
     ));
@@ -148,13 +137,8 @@ class ModerateController extends Controller
148 137
     }
149 138
     $tag_new_id = $tag_array[0];
150 139
     
151
-    if (
152
-      !($tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneBy(array(
153
-        'id'         => $tag_id,
154
-        'tomoderate' => true
155
-      )))
156
-      || !($new_tag = $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneById($tag_new_id))
157
-    )
140
+    $tagManager = new TagManager();
141
+    if (!$tagManager->moderateTag($this->getDoctrine(), $tag_id, false, $tag_new_id))
158 142
     {
159 143
       return $this->jsonResponse(array(
160 144
         'status'  => 'error',
@@ -162,90 +146,10 @@ class ModerateController extends Controller
162 146
       ));
163 147
     }
164 148
     
165
-    /*
166
-     * Trois cas de figures ou sont utilisés les tags
167
-     *  * Sur un élément
168
-     *  * Tag favori
169
-     *  * Tag d'un groupe
170
-     */
171
-    $em = $this->getDoctrine()->getEntityManager();
172
-    
173
-    // Sur un élément
174
-    foreach ($elements = $this->getDoctrine()->getEntityManager()->createQuery("
175
-      SELECT e, t FROM MuzichCoreBundle:Element e
176
-      JOIN e.tags t
177
-      WHERE t.id  = :tid
178
-    ")
179
-      ->setParameter('tid', $tag_id)
180
-      ->getResult() as $element)
181
-    {
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);
241
-    }
242
-    
243
-    $em->remove($tag);
244
-    $em->flush();
245
-    
246 149
     return $this->jsonResponse(array(
247 150
       'status' => 'success'
248 151
     ));
152
+    
249 153
   }
250 154
   
251 155
 }

+ 125 - 0
src/Muzich/CoreBundle/Managers/TagManager.php Parādīt failu

@@ -8,6 +8,8 @@ use Symfony\Component\DependencyInjection\Container;
8 8
 use FOS\UserBundle\Util\CanonicalizerInterface;
9 9
 use Symfony\Bundle\DoctrineBundle\Registry;
10 10
 use Muzich\CoreBundle\Util\StrictCanonicalizer;
11
+use Muzich\CoreBundle\Entity\UsersTagsFavorites;
12
+use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
11 13
 
12 14
 /**
13 15
  * 
@@ -90,4 +92,127 @@ class TagManager
90 92
     }
91 93
   }
92 94
   
95
+  protected function replaceTagByAnother(EntityManager $em, $tag, $new_tag)
96
+  {
97
+    /*
98
+     * Trois cas de figures ou sont utilisés les tags
99
+     *  * Sur un élément
100
+     *  * Tag favori
101
+     *  * Tag d'un groupe
102
+     */
103
+    
104
+    // Sur un élément
105
+    foreach ($elements = $em->createQuery("
106
+      SELECT e, t FROM MuzichCoreBundle:Element e
107
+      JOIN e.tags t
108
+      WHERE t.id  = :tid
109
+    ")
110
+      ->setParameter('tid', $tag->getId())
111
+      ->getResult() as $element)
112
+    {
113
+      // Pour chaque elements lié a ce tag
114
+      // On ajoute un lien vers le nouveau tag si il n'en n'a pas déjà
115
+      if (!$element->hasTag($new_tag))
116
+      {
117
+        $element->addTag($new_tag);
118
+        $em->persist($element);
119
+      }
120
+    }
121
+    
122
+    // Tag favoris
123
+    foreach ($favorites = $em->createQuery("
124
+      SELECT f FROM MuzichCoreBundle:UsersTagsFavorites f
125
+      WHERE f.tag  = :tid
126
+    ")
127
+      ->setParameter('tid', $tag->getId())
128
+      ->getResult() as $fav)
129
+    {
130
+      // Pour chaque favoris utilisant ce tag on regarde si l'utilisateur
131
+      // n'a pas déjà le nouveau tag en favoris.
132
+      if (!$em->createQuery("
133
+        SELECT COUNT(f.id) FROM MuzichCoreBundle:UsersTagsFavorites f
134
+        WHERE f.tag  = :tid AND f.user = :uid
135
+      ")
136
+      ->setParameters(array('tid' => $new_tag->getId(), 'uid' => $fav->getUser()->getId()))
137
+      ->getSingleScalarResult())
138
+      {
139
+        $new_fav = new UsersTagsFavorites();
140
+        $new_fav->setTag($new_tag);
141
+        $new_fav->setUser($fav->getUser());
142
+        $new_fav->setPosition($fav->getPosition());
143
+        $em->persist($new_fav);
144
+      }
145
+      $em->remove($fav);
146
+    }
147
+    
148
+    // groupe
149
+    foreach ($em->createQuery("
150
+      SELECT f FROM MuzichCoreBundle:GroupsTagsFavorites f
151
+      WHERE f.tag  = :tid
152
+    ")
153
+      ->setParameter('tid', $tag->getId())
154
+      ->getResult() as $fav)
155
+    {
156
+      // Pour chaque favoris utilisant ce tag on regarde si le groupe
157
+      // n'a pas déjà le nouveau tag en favoris.
158
+      if (!$em->createQuery("
159
+        SELECT COUNT(f.id) FROM MuzichCoreBundle:GroupsTagsFavorites f
160
+        WHERE f.tag  = :tid AND f.group = :gid
161
+      ")
162
+      ->setParameters(array('tid' => $new_tag->getId(), 'gid' => $fav->getGroup()->getId()))
163
+      ->getSingleScalarResult())
164
+      {
165
+        $new_fav = new GroupsTagsFavorites();
166
+        $new_fav->setTag($new_tag);
167
+        $new_fav->setGroup($fav->getGroup());
168
+        $new_fav->setPosition($fav->getPosition());
169
+        $em->persist($new_fav);
170
+      }
171
+      $em->remove($fav);
172
+    }
173
+    
174
+    $em->remove($tag);
175
+    $em->flush();
176
+  }
177
+  
178
+  public function moderateTag(Registry $doctrine, $tag_id, $accept, $replacing_id = null)
179
+  {
180
+    if (($tag = $doctrine->getRepository('MuzichCoreBundle:Tag')->findOneBy(array(
181
+      'id'         => $tag_id,
182
+      'tomoderate' => true
183
+    ))))
184
+    {
185
+      if ($accept)
186
+      {
187
+        $tag->setTomoderate(false);
188
+        $tag->setPrivateids(null);
189
+        $doctrine->getEntityManager()->persist($tag);
190
+        $doctrine->getEntityManager()->flush();
191
+      }
192
+      else
193
+      {
194
+        if ($replacing_id)
195
+        {
196
+          // Si c'est un remplacement on envoit la sauce
197
+          if (!($new_tag = $doctrine->getRepository('MuzichCoreBundle:Tag')->findOneById($replacing_id)))
198
+          {
199
+            return false;
200
+          }
201
+          $this->replaceTagByAnother($doctrine->getEntityManager(), $tag, $new_tag);
202
+        }
203
+        else
204
+        {
205
+          $doctrine->getEntityManager()->remove($tag);
206
+          $doctrine->getEntityManager()->flush();
207
+        }
208
+        
209
+      }
210
+      return true;
211
+    }
212
+    else
213
+    {
214
+      return false;
215
+    }
216
+  }
217
+  
93 218
 }