3 namespace BookStack\Entities\Tools;
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;
15 public function __construct(
16 protected EntityQueries $entityQueries,
21 * Hydrate the entities of this hydrator to return a list of entities represented
22 * in their original intended models.
23 * @param EntityTable[] $entities
26 public function hydrate(array $entities, bool $loadTags = false, bool $loadParents = false): array
30 foreach ($entities as $entity) {
31 $data = $entity->getRawOriginal();
32 $instance = Entity::instanceFromType($entity->type);
34 if ($instance instanceof Page) {
35 $data['text'] = $data['description'];
36 unset($data['description']);
39 $instance = $instance->setRawAttributes($data, true);
40 $hydrated[] = $instance;
44 $this->loadTagsIntoModels($hydrated);
48 $this->loadParentsIntoModels($hydrated);
55 * @param Entity[] $entities
57 protected function loadTagsIntoModels(array $entities): void
61 foreach ($entities as $entity) {
62 if (!isset($idsByType[$entity->type])) {
63 $idsByType[$entity->type] = [];
65 $idsByType[$entity->type][] = $entity->id;
66 $entityMap[$entity->type . ':' . $entity->id] = $entity;
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);
77 $tags = empty($idsByType) ? [] : $query->get()->all();
79 foreach ($tags as $tag) {
80 $key = $tag->entity_type . ':' . $tag->entity_id;
81 if (!isset($tagMap[$key])) {
84 $tagMap[$key][] = $tag;
87 foreach ($entityMap as $key => $entity) {
88 $entityTags = new Collection($tagMap[$key] ?? []);
89 $entity->setRelation('tags', $entityTags);
94 * @param Entity[] $entities
96 protected function loadParentsIntoModels(array $entities): void
98 $parentsByType = ['book' => [], 'chapter' => []];
100 foreach ($entities as $entity) {
101 if ($entity->getAttribute('book_id') !== null) {
102 $parentsByType['book'][] = $entity->getAttribute('book_id');
104 if ($entity->getAttribute('chapter_id') !== null) {
105 $parentsByType['chapter'][] = $entity->getAttribute('chapter_id');
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);
122 $parentModels = $filtered ? $parentQuery->get()->all() : [];
123 $parents = $this->hydrate($parentModels);
125 foreach ($parents as $parent) {
126 $parentMap[$parent->type . ':' . $parent->id] = $parent;
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);
134 if ($entity instanceof Page) {
135 $key = 'chapter:' . $entity->getRawAttribute('chapter_id');
136 $entity->setAttribute('chapter', $parentMap[$key] ?? null);