]> BookStack Code Mirror - bookstack/blob - app/Activity/Tools/CommentTree.php
Framework: Re-add updated patched symfony-mailer
[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 array{comment: Comment, depth: int, children: array}[]
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 get(): array
40     {
41         return $this->tree;
42     }
43
44     public function canUpdateAny(): bool
45     {
46         foreach ($this->comments as $comment) {
47             if (userCan('comment-update', $comment)) {
48                 return true;
49             }
50         }
51
52         return false;
53     }
54
55     /**
56      * @param Comment[] $comments
57      */
58     protected function createTree(array $comments): array
59     {
60         $byId = [];
61         foreach ($comments as $comment) {
62             $byId[$comment->local_id] = $comment;
63         }
64
65         $childMap = [];
66         foreach ($comments as $comment) {
67             $parent = $comment->parent_id;
68             if (is_null($parent) || !isset($byId[$parent])) {
69                 $parent = 0;
70             }
71
72             if (!isset($childMap[$parent])) {
73                 $childMap[$parent] = [];
74             }
75             $childMap[$parent][] = $comment->local_id;
76         }
77
78         $tree = [];
79         foreach ($childMap[0] ?? [] as $childId) {
80             $tree[] = $this->createTreeForId($childId, 0, $byId, $childMap);
81         }
82
83         return $tree;
84     }
85
86     protected function createTreeForId(int $id, int $depth, array &$byId, array &$childMap): array
87     {
88         $childIds = $childMap[$id] ?? [];
89         $children = [];
90
91         foreach ($childIds as $childId) {
92             $children[] = $this->createTreeForId($childId, $depth + 1, $byId, $childMap);
93         }
94
95         return [
96             'comment' => $byId[$id],
97             'depth' => $depth,
98             'children' => $children,
99         ];
100     }
101
102     protected function loadComments(): array
103     {
104         if (!$this->enabled()) {
105             return [];
106         }
107
108         return $this->page->comments()
109             ->with('createdBy')
110             ->get()
111             ->all();
112     }
113 }