From 2c2ca1a027b15efcf30d417eb8a96a09ed6dcb18 Mon Sep 17 00:00:00 2001
From: Dorian Merz <merz@ub.uni-leipzig.de>
Date: Fri, 11 Jun 2021 13:50:28 +0200
Subject: [PATCH] refs #19719 [fid_bbi] show own tags without date restriction

---
 .../fid_bbi/Db/Table/ResourceTagsTrait.php    | 31 ++++++++++++-------
 module/fid_bbi/src/fid_bbi/Db/Table/Tags.php  |  2 +-
 2 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/module/fid_bbi/src/fid_bbi/Db/Table/ResourceTagsTrait.php b/module/fid_bbi/src/fid_bbi/Db/Table/ResourceTagsTrait.php
index 728740aad6c..be84e7ea4c6 100644
--- a/module/fid_bbi/src/fid_bbi/Db/Table/ResourceTagsTrait.php
+++ b/module/fid_bbi/src/fid_bbi/Db/Table/ResourceTagsTrait.php
@@ -1,13 +1,11 @@
 <?php
-
 namespace fid_bbi\Db\Table;
 
-use  Zend\Db\Sql\Select,
-     Zend\Db\Sql\Expression;
+use  Zend\Db\Sql\Expression;
+use Zend\Db\Sql\Select;
 
 trait ResourceTagsTrait
 {
-
     /**
      * Get a subquery used for determining first post date of a tag.
      *
@@ -26,19 +24,28 @@ trait ResourceTagsTrait
         return $sub;
     }
 
-    protected function addFirstPosted($select,$olderThan=true,$age = "7 days",$resourceTagsTableName = 'rt') {
-
+    protected function addFirstPosted($select, $userId = null, $olderThan = true, $age = "7 days", $resourceTagsTableName = 'rt')
+    {
         $select->join(
             ['first' => $this->getFirstPostedSubquery()],
-            $resourceTagsTableName.'.tag_id = first.tag_id',
+            $resourceTagsTableName . '.tag_id = first.tag_id',
             [],
             Select::JOIN_INNER
         );
         if ($olderThan) {
-            $select->where->lessThan(
-                'first.first_posted',
-                date('Y-m-d H:i:s', strtotime("-" . $age))
-            );
+            if ($userId) {
+                // in case the current user is querying for their own tags
+                // we do not have to fulfil date constarints
+                $select->where->nest->lessThan(
+                    'first.first_posted',
+                    date('Y-m-d H:i:s', strtotime("-" . $age))
+                )->or->equalTo('user_id', $userId);
+            } else {
+                $select->where->lessThan(
+                    'first.first_posted',
+                    date('Y-m-d H:i:s', strtotime("-" . $age))
+                );
+            }
         } else {
             // otherwise we look for younger entries
             $select->where->greaterThan(
@@ -47,4 +54,4 @@ trait ResourceTagsTrait
             );
         }
     }
-}
\ No newline at end of file
+}
diff --git a/module/fid_bbi/src/fid_bbi/Db/Table/Tags.php b/module/fid_bbi/src/fid_bbi/Db/Table/Tags.php
index f08117adf55..1c5c3513ff0 100644
--- a/module/fid_bbi/src/fid_bbi/Db/Table/Tags.php
+++ b/module/fid_bbi/src/fid_bbi/Db/Table/Tags.php
@@ -249,7 +249,7 @@ class Tags extends BaseTags
                 if (null !== $user) {
                     $select->where->equalTo('rt.user_id', $user);
                 }
-                $this->addFirstPosted($select);
+                $this->addFirstPosted($select, $userToCheck);
             }
         );
     }
-- 
GitLab