]> BookStack Code Mirror - bookstack/blob - app/Activity/Tools/CommentTree.php
Comments: Started archive display, created mode for tree node
[bookstack] / app / Activity / Tools / CommentTree.php
1 <?php
2
3 namespace BookStack\Activity\Tools;
4
5 use BookStack\Activity\Models\Comment;
6 use BookStack\Entities\Models\Page;
7
8 class CommentTree
9 {
10     /**
11      * The built nested tree structure array.
12      * @var CommentTreeNode[]
13      */
14     protected array $tree;
15     protected array $comments;
16
17     public function __construct(
18         protected Page $page
19     ) {
20         $this->comments = $this->loadComments();
21         $this->tree = $this->createTree($this->comments);
22     }
23
24     public function enabled(): bool
25     {
26         return !setting('app-disable-comments');
27     }
28
29     public function empty(): bool
30     {
31         return count($this->tree) === 0;
32     }
33
34     public function count(): int
35     {
36         return count($this->comments);
37     }
38
39     public function getActive(): array
40     {
41         return array_filter($this->tree, fn (CommentTreeNode $node) => !$node->comment->archived);
42     }
43
44     public function getArchived(): array
45     {
46         return array_filter($this->tree, fn (CommentTreeNode $node) => $node->comment->archived);
47     }
48
49     public function getCommentNodeForId(int $commentId): ?CommentTreeNode
50     {
51         foreach ($this->tree as $node) {
52             if ($node->comment->id === $commentId) {
53                 return $node;
54             }
55         }
56
57         return null;
58     }
59
60     public function canUpdateAny(): bool
61     {
62         foreach ($this->comments as $comment) {
63             if (userCan('comment-update', $comment)) {
64                 return true;
65             }
66         }
67
68         return false;
69     }
70
71     /**
72      * @param Comment[] $comments
73      * @return CommentTreeNode[]
74      */
75     protected function createTree(array $comments): array
76     {
77         $byId = [];
78         foreach ($comments as $comment) {
79             $byId[$comment->local_id] = $comment;
80         }
81
82         $childMap = [];
83         foreach ($comments as $comment) {
84             $parent = $comment->parent_id;
85             if (is_null($parent) || !isset($byId[$parent])) {
86                 $parent = 0;
87             }
88
89             if (!isset($childMap[$parent])) {
90                 $childMap[$parent] = [];
91             }
92             $childMap[$parent][] = $comment->local_id;
93         }
94
95         $tree = [];
96         foreach ($childMap[0] ?? [] as $childId) {
97             $tree[] = $this->createTreeNodeForId($childId, 0, $byId, $childMap);
98         }
99
100         return $tree;
101     }
102
103     protected function createTreeNodeForId(int $id, int $depth, array &$byId, array &$childMap): CommentTreeNode
104     {
105         $childIds = $childMap[$id] ?? [];
106         $children = [];
107
108         foreach ($childIds as $childId) {
109             $children[] = $this->createTreeNodeForId($childId, $depth + 1, $byId, $childMap);
110         }
111
112         return new CommentTreeNode($byId[$id], $depth, $children);
113     }
114
115     protected function loadComments(): array
116     {
117         if (!$this->enabled()) {
118             return [];
119         }
120
121         return $this->page->comments()
122             ->with('createdBy')
123             ->get()
124             ->all();
125     }
126 }