[TikiWiki-commits] [Git][tikiwiki/tiki][27.x] [BP][FIX] usergroup_tracker: Fix broken view links for user and group

"Baraka Kinywa \(@bkinywa24\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <688b23449537a_2ca41d209099e@gitlab-sidekiq-low-urgency-cpu-bound-v2-78fc7d7b7-x45v5.mail>

Baraka Kinywa pushed to branch 27.x at Tiki Wiki CMS Groupware / Tiki


Commits:
7560014d by MAGENE Sem Joel at 2025-07-31T10:56:20+03:00
[BP][FIX] usergroup_tracker: Fix broken view links for user and group
---
* [BP][FIX] usergroup_tracker: Fix broken view links for user and group
---
* [FIX] usergroup_tracker: Fix broken view links for user and group

See merge request tikiwiki/tiki!7908

See merge request tikiwiki/tiki!8140

- - - - -


1 changed file:

- tiki-view_tracker_item.php


Changes:

=====================================
tiki-view_tracker_item.php
=====================================
@@ -59,25 +59,36 @@ if (isset($_REQUEST['itemId'])) {
     $itemId = $_REQUEST['itemId'];
 }
 $special = false;
-if (! isset($trackerId) && $prefs['userTracker'] == 'y' && ! isset($_REQUEST['user'])) {
-    if (isset($_REQUEST['view']) and $_REQUEST['view'] == ' user') {
+
+// --- SCENARIO 1: CURRENT LOGGED-IN USER'S TRACKER (`?view=user` with no user specified)
+// Handles the "my profile" use case.
+if (! isset($trackerId) && $prefs['userTracker'] == 'y' && empty($_REQUEST['user'])) {
+    if (isset($_REQUEST['view']) && $_REQUEST['view'] == ' user') {
         if (empty($user)) {
             $smarty->assign('msg', tra("You are not logged in"));
-            $smarty->assign('errortype', '402');
+            $smarty->assign('errortype', '401');
             $smarty->display("error.tpl");
             die;
         }
+
+        // Fetch tracker config for the user's group.
         $utid = $userlib->get_tracker_usergroup($user);
-        if (isset($utid['usersTrackerId'])) {
+        if (isset($utid['usersTrackerId']) && ! empty($utid['usersFieldId'])) {
             $trackerId = $utid['usersTrackerId'];
             $itemId = $trklib->get_item_id($trackerId, $utid['usersFieldId'], $user);
-            if ($itemId == null) {
+
+            // KEY FEATURE: Auto-creates item on first visit and redirects
+            if (empty($itemId)) {
                 $addit = [];
+                // 1. Set the user field specified in the user's group settings.
                 $addit[] = [
                     'fieldId' => $utid['usersFieldId'],
                     'type' => 'u',
                     'value' => $user,
+                    'trackerId' => $trackerId,
                 ];
+
+                // 2. Also check for the tracker's primary "User" field and set it if it's different.
                 $definition = Tracker_Definition::get($trackerId);
                 if ($definition && $f = $definition->getUserField()) {
                     if ($f != $utid['usersFieldId']) {
@@ -85,21 +96,33 @@ if (! isset($trackerId) && $prefs['userTracker'] == 'y' && ! isset($_REQUEST['us
                             'fieldId' => $f,
                             'type' => 'u',
                             'value' => $user,
+                            'trackerId' => $trackerId,
                         ];
                     }
                 }
+
+                // 3. Also check for and set the "Writer Group" field for permissions.
                 if ($definition && $f = $definition->getWriterGroupField()) {
                     $addit[] = [
                         'fieldId' => $f,
                         'type' => 'g',
                         'value' => $group,
+                        'trackerId' => $trackerId,
                     ];
                 }
+
+                // Create the item with the complete field data and redirect.
                 $itemId = $trklib->replace_item($trackerId, 0, ['data' => $addit], 'o');
+                $access->redirect('tiki-view_tracker_item.php?itemId=' . $itemId);
             }
             $special = 'user';
+        } else {
+            $smarty->assign('msg', tra("User Tracker feature is enabled but not configured for this user's group."));
+            $smarty->display("error.tpl");
+            die;
         }
-    } elseif (isset($_REQUEST["usertracker"]) and $tiki_p_admin == 'y') {
+    } elseif (isset($_REQUEST["usertracker"]) && $tiki_p_admin == 'y') {
+        // Legacy admin-only path for fetching a user's tracker.
         $utid = $userlib->get_tracker_usergroup($_REQUEST['usertracker']);
         if (isset($utid['usersTrackerId'])) {
             $trackerId = $utid['usersTrackerId'];
@@ -107,23 +130,36 @@ if (! isset($trackerId) && $prefs['userTracker'] == 'y' && ! isset($_REQUEST['us
         }
     }
 }
+
+// --- SCENARIO 2: GROUP'S TRACKER (`?view=group`)
 if (! isset($trackerId) && $prefs['groupTracker'] == 'y') {
-    if (isset($_REQUEST['view']) and $_REQUEST['view'] == ' group') {
+    if (isset($_REQUEST['view']) && $_REQUEST['view'] == ' group') {
+        if (empty($_REQUEST['group'])) {
+            $smarty->assign('msg', tra("No group specified. Please add '&group=groupname' to the URL."));
+            $smarty->assign('errortype', 400);
+            $smarty->display("error.tpl");
+            die;
+        }
+        $group = $_REQUEST['group'];
+
         $gtid = $userlib->get_grouptrackerid($group);
-        if (isset($gtid['groupTrackerId'])) {
+        if (isset($gtid['groupTrackerId']) && ! empty($gtid['groupFieldId'])) {
             $trackerId = $gtid['groupTrackerId'];
             $itemId = $trklib->get_item_id($trackerId, $gtid['groupFieldId'], $group);
-            if ($itemId == null) {
-                $addit = ['data' => [
-                    'fieldId' => $gtid['groupFieldId'],
-                    'type' => 'g',
-                    'value' => $group,
-                ]];
-                $itemId = $trklib->replace_item($trackerId, 0, $addit, 'o');
+
+            if (empty($itemId)) {
+                $fieldInfo = $trklib->get_tracker_field($gtid['groupFieldId']);
+                $addit = [array_merge($fieldInfo, ['value' => $group])];
+                $itemId = $trklib->replace_item($trackerId, 0, ['data' => $addit], 'o');
+                $access->redirect('tiki-view_tracker_item.php?itemId=' . $itemId);
             }
             $special = 'group';
+        } else {
+            $smarty->assign('msg', tra("Group Tracker feature is enabled but not configured for this group."));
+            $smarty->display("error.tpl");
+            die;
         }
-    } elseif (isset($_REQUEST["grouptracker"]) and $tiki_p_admin == 'y') {
+    } elseif (isset($_REQUEST["grouptracker"]) && $tiki_p_admin == 'y') {
         $gtid = $userlib->get_grouptrackerid($_REQUEST["grouptracker"]);
         if (isset($gtid['groupTrackerId'])) {
             $trackerId = $gtid['groupTrackerId'];
@@ -131,9 +167,13 @@ if (! isset($trackerId) && $prefs['groupTracker'] == 'y') {
         }
     }
 }
+
 $smarty->assign_by_ref('special', $special);
-//url to a user user tracker tiki-view_tracker_item.php?user=yyyyy&view=+user or tiki-view_tracker_item.php?greoup=yyy&user=yyyyy&view=+user or tiki-view_tracker_item.php?trackerId=xxx&user=yyyyy&view=+user
-if ($prefs['userTracker'] == 'y' && isset($_REQUEST['view']) && $_REQUEST['view'] = ' user' && ! empty($_REQUEST['user'])) {
+
+// --- SCENARIO 3: VIEWING ANOTHER USER'S TRACKER (`?view=user&user=somebody`)
+//url to a user user tracker tiki-view_tracker_item.php?user=yyyyy&view=+user or tiki-view_tracker_item.php?group=yyy&user=yyyyy&view=+user or tiki-view_tracker_item.php?trackerId=xxx&user=yyyyy&view=+user
+if ($prefs['userTracker'] == 'y' && isset($_REQUEST['view']) && $_REQUEST['view'] == ' user' && ! empty($_REQUEST['user'])) {
+    // Infer `trackerId` and `fieldId` if not already known.
     if (empty($trackerId)) {
         if (empty($_REQUEST['group'])) {
             $_REQUEST['group'] = $userlib->get_user_default_group($_REQUEST['user']);
@@ -147,17 +187,20 @@ if ($prefs['userTracker'] == 'y' && isset($_REQUEST['view']) && $_REQUEST['view'
         }
     }
     if (! empty($trackerId)) {
+        // Fallback to the tracker's primary user field if not found via group config.
         if (empty($fieldId)) {
             $definition = Tracker_Definition::get($trackerId);
             if ($definition) {
                 $fieldId = $definition->getUserField();
             }
         }
+        // With all parts, attempt final lookup.
         if (! empty($fieldId)) {
             $itemId = $trklib->get_item_id($trackerId, $fieldId, $_REQUEST['user']);
 
             // Unlike scenarios 1 & 2, this fails hard if the item doesn't exist.
             // It does NOT auto-create for another user.
+            // NOTE: The `Feedback` call below has a known issue where it escapes HTML and doesn't display the link (Need to be fixed)
             if (! $itemId) {
                 $smarty->assign(
                     'msg',
@@ -169,6 +212,7 @@ if ($prefs['userTracker'] == 'y' && isset($_REQUEST['view']) && $_REQUEST['view'
         }
     }
 }
+
 if ((! isset($trackerId) || ! $trackerId) && isset($itemId)) {
     $item_info = $trklib->get_tracker_item($itemId);
     if (isset($item_info['trackerId'])) {
@@ -176,13 +220,15 @@ if ((! isset($trackerId) || ! $trackerId) && isset($itemId)) {
     }
 }
 if (! isset($trackerId) || ! $trackerId) {
+    http_response_code(409);
     $smarty->assign('msg', tra("No tracker indicated"));
     $smarty->display("error.tpl");
     die;
 }
-if (! isset($utid) and ! isset($gtid) and (! isset($itemId) or ! $itemId) and ! isset($_REQUEST["offset"])) {
+if (! isset($utid) && ! isset($gtid) && (! isset($itemId) || ! $itemId) && ! isset($_REQUEST["offset"])) {
+    http_response_code(409);
     $smarty->assign('msg', tra("No item indicated"));
-    $smarty->display("error.tpl");
+    $smarty->display('error.tpl');
     die;
 }
 
@@ -265,7 +311,7 @@ foreach (
     }
 }
 if (isset($_REQUEST['filterfield'])) {
-    if (is_array($_REQUEST['filtervalue']) and isset($_REQUEST['filtervalue'][$tryfilterfield])) {
+    if (is_array($_REQUEST['filtervalue']) && isset($_REQUEST['filtervalue'][$tryfilterfield])) {
         $tryfiltervalue = $_REQUEST['filtervalue'][$tryfilterfield];
     } else {
         $tryfilterfield = preg_split('/\s*:\s*/', $_REQUEST['filterfield']);
@@ -344,16 +390,16 @@ $cat_type = 'trackeritem';
 $tracker_info = $definition->getInformation();
 $tracker_info_value = fn($key) => array_key_exists($key, $tracker_info) ? $tracker_info[$key] : null;
 
-if (! isset($tracker_info["writerCanModify"]) or (isset($utid) and ($trackerId != $utid['usersTrackerId']))) {
+if (! isset($tracker_info["writerCanModify"]) || (isset($utid) && ($trackerId != $utid['usersTrackerId']))) {
     $tracker_info["writerCanModify"] = 'n';
 }
-if (! isset($tracker_info["userCanSeeOwn"]) or (isset($utid) and ($trackerId != $utid['usersTrackerId']))) {
+if (! isset($tracker_info["userCanSeeOwn"]) || (isset($utid) && ($trackerId != $utid['usersTrackerId']))) {
     $tracker_info["userCanSeeOwn"] = 'n';
 }
-if (! isset($tracker_info["writerGroupCanModify"]) or (isset($gtid) and ($trackerId != $gtid['groupTrackerId']))) {
+if (! isset($tracker_info["writerGroupCanModify"]) || (isset($gtid) && ($trackerId != $gtid['groupTrackerId']))) {
     $tracker_info["writerGroupCanModify"] = 'n';
 }
-if (! isset($tracker_info["groupCanSeeOwn"]) or (isset($gtid) and ($trackerId != $gtid['groupTrackerId']))) {
+if (! isset($tracker_info["groupCanSeeOwn"]) || (isset($gtid) && ($trackerId != $gtid['groupTrackerId']))) {
     $tracker_info["groupCanSeeOwn"] = 'n';
 }
 $tikilib->get_perm_object($trackerId, 'tracker', $tracker_info);
@@ -379,8 +425,7 @@ if (! empty($_REQUEST['moveto'])) {
     if ($tiki_p_admin_trackers == 'y' && $perms->create_tracker_items) {
         // Move item to another tracker. This assumes certain similarities between the 2 trackers.
         $trklib->move_item($trackerId, $itemId, $_REQUEST['moveto']);
-        header('Location: ' . filter_out_sefurl('tiki-view_tracker_item.php?itemId=' . $itemId));
-        exit;
+        $access->redirect(filter_out_sefurl('tiki-view_tracker_item.php?itemId=' . $itemId));
     } else {
         $smarty->assign('errortype', 403);
         $smarty->assign('msg', tra("Permission denied"));
@@ -417,11 +462,10 @@ if (empty($tracker_info)) {
 $fieldFactory = $definition->getFieldFactory();
 
 $rateFieldId = $definition->getRateField();
-if (isset($tracker_info['useRatings']) and $tracker_info['useRatings'] == 'y' and $tiki_p_tracker_vote_ratings == 'y') {
-    if ($user and $tiki_p_tracker_vote_ratings == 'y' and isset($rateFieldId) and isset($_REQUEST['ins_' . $rateFieldId])) {
+if (isset($tracker_info['useRatings']) && $tracker_info['useRatings'] == 'y' && $tiki_p_tracker_vote_ratings == 'y') {
+    if ($user && isset($rateFieldId) && isset($_REQUEST['ins_' . $rateFieldId])) {
         $trklib->replace_rating($trackerId, $itemId, $rateFieldId, $user, $_REQUEST['ins_' . $rateFieldId]);
-        header('Location: tiki-view_tracker_item.php?trackerId=' . $trackerId . '&itemId=' . $itemId);
-        die;
+        $access->redirect('tiki-view_tracker_item.php?trackerId=' . $trackerId . '&itemId=' . $itemId);
     }
 }
 
@@ -492,7 +536,7 @@ if (isset($_REQUEST["save"]) || isset($_REQUEST["save_return"]) || isset($_REQUE
                 $groupalertlib->Notify(isset($_REQUEST['listtoalert']) ? $_REQUEST['listtoalert'] : '', "tiki-view_tracker_item.php?itemId=" . $itemId);
             }
             $access->checkCsrf();
-            if (! isset($_REQUEST["edstatus"]) or ($tracker_info["showStatus"] != 'y' and $tiki_p_admin_trackers != 'y')) {
+            if (! isset($_REQUEST["edstatus"]) || ($tracker_info["showStatus"] != 'y' && $tiki_p_admin_trackers != 'y')) {
                 $_REQUEST["edstatus"] = $tracker_info["modItemStatus"];
             }
             $trklib->replace_item($trackerId, $itemId, $ins_fields, $_REQUEST["edstatus"]);
@@ -533,8 +577,7 @@ if (isset($_REQUEST["save"]) || isset($_REQUEST["save_return"]) || isset($_REQUE
         }
         if (isset($_REQUEST['save_return']) && isset($_REQUEST['from'])) {
             $fromUrl = filter_out_sefurl('tiki-index.php?page=' . urlencode($_REQUEST['from']));
-            header("Location: {$fromUrl}");
-            exit;
+            $access->redirect($fromUrl);
         }
 
         if (isset($_REQUEST['save_and_comment'])) {
@@ -573,20 +616,18 @@ if (isset($_REQUEST["removeImage"])) {
 // ************* return to list ***************************
 if (isset($_REQUEST["returntracker"]) || isset($_REQUEST["save_return"])) {
     require_once('lib/smarty_tiki/block.self_link.php');
-    header(
-        'Location: ' . smarty_block_self_link(
-            [
-                '_script' => 'tiki-view_tracker.php',
-                '_tag' => 'n',
-                '_urlencode' => 'n',
-                'itemId' => 'NULL',
-                'trackerId' => $trackerId
-            ],
-            '',
-            $smarty->getEmptyInternalTemplate()
-        )
+    $returnUrl = smarty_block_self_link(
+        [
+            '_script' => 'tiki-view_tracker.php',
+            '_tag' => 'n',
+            '_urlencode' => 'n',
+            'itemId' => 'NULL',
+            'trackerId' => $trackerId
+        ],
+        '',
+        $smarty->getEmptyInternalTemplate()
     );
-    die;
+    $access->redirect($returnUrl);
 }
 // ********************************************************
 $info = $trklib->get_tracker_item($itemId);
@@ -664,8 +705,8 @@ $smarty->assign('tracker_info', $tracker_info);
 $smarty->assign_by_ref('info', $info);
 $smarty->assign_by_ref('fields', $fields["data"]);
 $smarty->assign_by_ref('ins_fields', $ins_fields["data"]);
-if ($prefs['feature_user_watches'] == 'y' and $tiki_p_watch_trackers == 'y') {
-    if ($user and isset($_REQUEST['watch'])) {
+if ($prefs['feature_user_watches'] == 'y' && $tiki_p_watch_trackers == 'y') {
+    if ($user && isset($_REQUEST['watch'])) {
         $access->checkCsrf();
         if ($_REQUEST['watch'] == 'add') {
             $tikilib->add_user_watch($user, 'tracker_item_modified', $itemId, 'tracker ' . $trackerId, $tracker_info['name'], "tiki-view_tracker_item.php?trackerId=" . $trackerId . "&amp;itemId=" . $itemId);
@@ -676,7 +717,7 @@ if ($prefs['feature_user_watches'] == 'y' and $tiki_p_watch_trackers == 'y') {
     }
     $smarty->assign('user_watching_tracker', 'n');
     $it = $tikilib->user_watches($user, 'tracker_item_modified', $itemId, 'tracker ' . $trackerId);
-    if ($user and $tikilib->user_watches($user, 'tracker_item_modified', $itemId, 'tracker ' . $trackerId)) {
+    if ($user && $tikilib->user_watches($user, 'tracker_item_modified', $itemId, 'tracker ' . $trackerId)) {
         $smarty->assign('user_watching_tracker', 'y');
     }
     // Check, if the user is watching this trackers' item by a category.



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/7560014d1fa64f828e8571a7d3680236e5d08270

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/7560014d1fa64f828e8571a7d3680236e5d08270
You're receiving this email because of your account on gitlab.com.

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.