]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/ApiBundle/Controller/WallabagRestController.php
Merge pull request #2397 from wallabag/api-orphan-tags
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / WallabagRestController.php
index 104720a9ef98aa9567f83c8c6ab2c5770114c8fc..cc6923a043cc1d8c818893e03d8c43edbae1d956 100644 (file)
@@ -40,7 +40,7 @@ class WallabagRestController extends FOSRestController
         $url = $request->query->get('url', '');
 
         if (empty($url)) {
-            throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$user->getId());
+            throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
         }
 
         $res = $this->getDoctrine()
@@ -82,8 +82,8 @@ class WallabagRestController extends FOSRestController
         $order = $request->query->get('order', 'desc');
         $page = (int) $request->query->get('page', 1);
         $perPage = (int) $request->query->get('perPage', 30);
-        $since = $request->query->get('since', 0);
         $tags = $request->query->get('tags', '');
+        $since = $request->query->get('since', 0);
 
         $pager = $this->getDoctrine()
             ->getRepository('WallabagCoreBundle:Entry')
@@ -95,7 +95,20 @@ class WallabagRestController extends FOSRestController
         $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
         $paginatedCollection = $pagerfantaFactory->createRepresentation(
             $pager,
-            new Route('api_get_entries', [], UrlGeneratorInterface::ABSOLUTE_URL)
+            new Route(
+                'api_get_entries',
+                [
+                    'archive' => $isArchived,
+                    'starred' => $isStarred,
+                    'sort' => $sort,
+                    'order' => $order,
+                    'page' => $page,
+                    'perPage' => $perPage,
+                    'tags' => $tags,
+                    'since' => $since,
+                ],
+                UrlGeneratorInterface::ABSOLUTE_URL
+            )
         );
 
         $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
@@ -387,6 +400,8 @@ class WallabagRestController extends FOSRestController
             ->getRepository('WallabagCoreBundle:Entry')
             ->removeTag($this->getUser()->getId(), $tag);
 
+        $this->cleanOrphanTag($tag);
+
         $json = $this->get('serializer')->serialize($tag, 'json');
 
         return (new JsonResponse())->setJson($json);
@@ -427,6 +442,8 @@ class WallabagRestController extends FOSRestController
             ->getRepository('WallabagCoreBundle:Entry')
             ->removeTags($this->getUser()->getId(), $tags);
 
+        $this->cleanOrphanTag($tags);
+
         $json = $this->get('serializer')->serialize($tags, 'json');
 
         return (new JsonResponse())->setJson($json);
@@ -451,6 +468,8 @@ class WallabagRestController extends FOSRestController
             ->getRepository('WallabagCoreBundle:Entry')
             ->removeTag($this->getUser()->getId(), $tag);
 
+        $this->cleanOrphanTag($tag);
+
         $json = $this->get('serializer')->serialize($tag, 'json');
 
         return (new JsonResponse())->setJson($json);
@@ -472,6 +491,28 @@ class WallabagRestController extends FOSRestController
         return (new JsonResponse())->setJson($json);
     }
 
+    /**
+     * Remove orphan tag in case no entries are associated to it.
+     *
+     * @param Tag|array $tags
+     */
+    private function cleanOrphanTag($tags)
+    {
+        if (!is_array($tags)) {
+            $tags = [$tags];
+        }
+
+        $em = $this->getDoctrine()->getManager();
+
+        foreach ($tags as $tag) {
+            if (count($tag->getEntries()) === 0) {
+                $em->remove($tag);
+            }
+        }
+
+        $em->flush();
+    }
+
     /**
      * Validate that the first id is equal to the second one.
      * If not, throw exception. It means a user try to access information from an other user.