See own deleted thread

thuylinh

Active member
Registered
Joined
Feb 5, 2020
Messages
36
Points
18

Reputation:

If I create a forum.php file and add the following code, members can see their and others' deleted thread:
Code:
<?php
namespace ENXF\ViewDeleteThread\XF\Entity;
class Forum extends XFCP_Forum
{
    public function canViewDeletedThreads()
    {
        $allowed = parent::canViewDeletedThreads();
        if (!$allowed) {
            if (\XF::visitor()->hasNodePermission($this->node_id, 'top_viewDeletedThreads')) {
                $allowed = true;
            }
        }

        return $allowed;
    }
}
I want members to only be able to see own deleted thread, how do I fix the code?

Thank you!
 

Sagex

Well-known member
Registered
Joined
Sep 10, 2021
Messages
59
Points
28

Reputation:

Hi, Try This


<?php

namespace ENXF\ViewDeleteThread\XF\Entity;

class Forum extends XFCP_Forum
{
public function canViewDeletedThreads()
{
$allowed = parent::canViewDeletedThreads();

if (!$allowed) {
$visitor = \XF::visitor();

// Check if the visitor has permission to view any deleted threads
if ($visitor->hasNodePermission($this->node_id, 'top_viewDeletedThreads')) {
$allowed = true;
} elseif ($visitor->user_id) {
// Check if the visitor is logged in
$thread = $this->finder('XF:Thread')
->where('node_id', $this->node_id)
->where('user_id', $visitor->user_id)
->where('discussion_state', 'deleted')
->fetchOne();

// If the visitor is the author of a deleted thread, allow viewing
if ($thread) {
$allowed = true;
}
}
}

return $allowed;
}
}
 

thuylinh

Active member
Registered
Joined
Feb 5, 2020
Messages
36
Points
18

Reputation:

Hi, Try This


<?php

namespace ENXF\ViewDeleteThread\XF\Entity;

class Forum extends XFCP_Forum
{
public function canViewDeletedThreads()
{
$allowed = parent::canViewDeletedThreads();

if (!$allowed) {
$visitor = \XF::visitor();

// Check if the visitor has permission to view any deleted threads
if ($visitor->hasNodePermission($this->node_id, 'top_viewDeletedThreads')) {
$allowed = true;
} elseif ($visitor->user_id) {
// Check if the visitor is logged in
$thread = $this->finder('XF:Thread')
->where('node_id', $this->node_id)
->where('user_id', $visitor->user_id)
->where('discussion_state', 'deleted')
->fetchOne();

// If the visitor is the author of a deleted thread, allow viewing
if ($thread) {
$allowed = true;
}
}
}

return $allowed;
}
}
SagexThanks for share.
When I apply it, members can still see other people's deleted thread. I want members to only be able to see my deleted thread.
 

BattleKing

Spirit of darkness
Staff member
Administrator
Moderator
+Lifetime VIP+
S.V.I.P Member
Collaborate
Registered
Joined
May 24, 2020
Messages
3,519
Points
523

Reputation:

This will not work at this place, because it returns only true or false. the limitation need to be done on a different template, but I had not looked into it which one it is.
 
Top