123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- <?php
- namespace Plugin\TreeNotes;
- use App\Core\Modules\Plugin;
- use App\Entity\Note;
- class TreeNotes extends Plugin
- {
-
- public function onFormatNoteList(array $notes_in, ?array &$notes_out)
- {
- $roots = array_filter($notes_in, function (Note $note) { return $note->getReplyTo() == null; });
- $notes_out = $this->build_tree($roots, $notes_in);
- }
- private function build_tree(array $parents, array $notes)
- {
- $subtree = [];
- foreach ($parents as $p) {
- $subtree[] = $this->build_subtree($p, $notes);
- }
- return $subtree;
- }
- private function build_subtree(Note $parent, array $notes)
- {
- $children = array_filter($notes, function (Note $n) use ($parent) { return $parent->getId() == $n->getReplyTo(); });
- return ['note' => $parent, 'replies' => $this->build_tree($children, $notes)];
- }
- }
|