]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/EntityHydrator.php
Search: Added pagination, updated other search uses
[bookstack] / app / Entities / Tools / EntityHydrator.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Activity\Models\Tag;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\Models\EntityTable;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Queries\EntityQueries;
11 use Illuminate\Database\Eloquent\Collection;
12
13 class EntityHydrator
14 {
15     public function __construct(
16         protected EntityQueries $entityQueries,
17     ) {
18     }
19
20     /**
21      * Hydrate the entities of this hydrator to return a list of entities represented
22      * in their original intended models.
23      * @param EntityTable[] $entities
24      * @return Entity[]
25      */
26     public function hydrate(array $entities, bool $loadTags = false, bool $loadParents = false): array
27     {
28         $hydrated = [];
29
30         foreach ($entities as $entity) {
31             $data = $entity->getRawOriginal();
32             $instance = Entity::instanceFromType($entity->type);
33
34             if ($instance instanceof Page) {
35                 $data['text'] = $data['description'];
36                 unset($data['description']);
37             }
38
39             $instance = $instance->setRawAttributes($data, true);
40             $hydrated[] = $instance;
41         }
42
43         if ($loadTags) {
44             $this->loadTagsIntoModels($hydrated);
45         }
46
47         if ($loadParents) {
48             $this->loadParentsIntoModels($hydrated);
49         }
50
51         return $hydrated;
52     }
53
54     /**
55      * @param Entity[] $entities
56      */
57     protected function loadTagsIntoModels(array $entities): void
58     {
59         $idsByType = [];
60         $entityMap = [];
61         foreach ($entities as $entity) {
62             if (!isset($idsByType[$entity->type])) {
63                 $idsByType[$entity->type] = [];
64             }
65             $idsByType[$entity->type][] = $entity->id;
66             $entityMap[$entity->type . ':' . $entity->id] = $entity;
67         }
68
69         $query = Tag::query();
70         foreach ($idsByType as $type => $ids) {
71             $query->orWhere(function ($query) use ($type, $ids) {
72                 $query->where('entity_type', '=', $type)
73                     ->whereIn('entity_id', $ids);
74             });
75         }
76
77         $tags = empty($idsByType) ? [] : $query->get()->all();
78         $tagMap = [];
79         foreach ($tags as $tag) {
80             $key = $tag->entity_type . ':' . $tag->entity_id;
81             if (!isset($tagMap[$key])) {
82                 $tagMap[$key] = [];
83             }
84             $tagMap[$key][] = $tag;
85         }
86
87         foreach ($entityMap as $key => $entity) {
88             $entityTags = new Collection($tagMap[$key] ?? []);
89             $entity->setRelation('tags', $entityTags);
90         }
91     }
92
93     /**
94      * @param Entity[] $entities
95      */
96     protected function loadParentsIntoModels(array $entities): void
97     {
98         $parentsByType = ['book' => [], 'chapter' => []];
99
100         foreach ($entities as $entity) {
101             if ($entity->getAttribute('book_id') !== null) {
102                 $parentsByType['book'][] = $entity->getAttribute('book_id');
103             }
104             if ($entity->getAttribute('chapter_id') !== null) {
105                 $parentsByType['chapter'][] = $entity->getAttribute('chapter_id');
106             }
107         }
108
109         $parentQuery = $this->entityQueries->visibleForList();
110         $filtered = count($parentsByType['book']) > 0 || count($parentsByType['chapter']) > 0;
111         $parentQuery = $parentQuery->where(function ($query) use ($parentsByType) {
112             foreach ($parentsByType as $type => $ids) {
113                 if (count($ids) > 0) {
114                     $query = $query->orWhere(function ($query) use ($type, $ids) {
115                         $query->where('type', '=', $type)
116                             ->whereIn('id', $ids);
117                     });
118                 }
119             }
120         });
121
122         $parentModels = $filtered ? $parentQuery->get()->all() : [];
123         $parents = $this->hydrate($parentModels);
124         $parentMap = [];
125         foreach ($parents as $parent) {
126             $parentMap[$parent->type . ':' . $parent->id] = $parent;
127         }
128
129         foreach ($entities as $entity) {
130             if ($entity instanceof Page || $entity instanceof Chapter) {
131                 $key = 'book:' . $entity->getRawAttribute('book_id');
132                 $entity->setAttribute('book', $parentMap[$key] ?? null);
133             }
134             if ($entity instanceof Page) {
135                 $key = 'chapter:' . $entity->getRawAttribute('chapter_id');
136                 $entity->setAttribute('chapter', $parentMap[$key] ?? null);
137             }
138         }
139     }
140 }