|
| 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 | + /** |
| 16 | + * @var EntityTable[] $entities |
| 17 | + */ |
| 18 | + protected array $entities; |
| 19 | + |
| 20 | + protected bool $loadTags = false; |
| 21 | + protected bool $loadParents = false; |
| 22 | + |
| 23 | + public function __construct(array $entities, bool $loadTags = false, bool $loadParents = false) |
| 24 | + { |
| 25 | + $this->entities = $entities; |
| 26 | + $this->loadTags = $loadTags; |
| 27 | + $this->loadParents = $loadParents; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Hydrate the entities of this hydrator to return a list of entities represented |
| 32 | + * in their original intended models. |
| 33 | + * @return Entity[] |
| 34 | + */ |
| 35 | + public function hydrate(): array |
| 36 | + { |
| 37 | + $hydrated = []; |
| 38 | + |
| 39 | + foreach ($this->entities as $entity) { |
| 40 | + $data = $entity->toArray(); |
| 41 | + $instance = Entity::instanceFromType($entity->type); |
| 42 | + |
| 43 | + if ($instance instanceof Page) { |
| 44 | + $data['text'] = $data['description']; |
| 45 | + unset($data['description']); |
| 46 | + } |
| 47 | + |
| 48 | + $instance->forceFill($data); |
| 49 | + $hydrated[] = $instance; |
| 50 | + } |
| 51 | + |
| 52 | + if ($this->loadTags) { |
| 53 | + $this->loadTagsIntoModels($hydrated); |
| 54 | + } |
| 55 | + |
| 56 | + if ($this->loadParents) { |
| 57 | + $this->loadParentsIntoModels($hydrated); |
| 58 | + } |
| 59 | + |
| 60 | + return $hydrated; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param Entity[] $entities |
| 65 | + */ |
| 66 | + protected function loadTagsIntoModels(array $entities): void |
| 67 | + { |
| 68 | + $idsByType = []; |
| 69 | + $entityMap = []; |
| 70 | + foreach ($entities as $entity) { |
| 71 | + if (!isset($idsByType[$entity->type])) { |
| 72 | + $idsByType[$entity->type] = []; |
| 73 | + } |
| 74 | + $idsByType[$entity->type][] = $entity->id; |
| 75 | + $entityMap[$entity->type . ':' . $entity->id] = $entity; |
| 76 | + } |
| 77 | + |
| 78 | + $query = Tag::query(); |
| 79 | + foreach ($idsByType as $type => $ids) { |
| 80 | + $query->orWhere(function ($query) use ($type, $ids) { |
| 81 | + $query->where('entity_type', '=', $type) |
| 82 | + ->whereIn('entity_id', $ids); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + $tags = empty($idsByType) ? [] : $query->get()->all(); |
| 87 | + $tagMap = []; |
| 88 | + foreach ($tags as $tag) { |
| 89 | + $key = $tag->entity_type . ':' . $tag->entity_id; |
| 90 | + if (!isset($tagMap[$key])) { |
| 91 | + $tagMap[$key] = []; |
| 92 | + } |
| 93 | + $tagMap[$key][] = $tag; |
| 94 | + } |
| 95 | + |
| 96 | + foreach ($entityMap as $key => $entity) { |
| 97 | + $entityTags = new Collection($tagMap[$key] ?? []); |
| 98 | + $entity->setRelation('tags', $entityTags); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param Entity[] $entities |
| 104 | + */ |
| 105 | + protected function loadParentsIntoModels(array $entities): void |
| 106 | + { |
| 107 | + $parentsByType = ['book' => [], 'chapter' => []]; |
| 108 | + |
| 109 | + foreach ($entities as $entity) { |
| 110 | + if ($entity->getAttribute('book_id') !== null) { |
| 111 | + $parentsByType['book'][] = $entity->getAttribute('book_id'); |
| 112 | + } |
| 113 | + if ($entity->getAttribute('chapter_id') !== null) { |
| 114 | + $parentsByType['chapter'][] = $entity->getAttribute('chapter_id'); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // TODO - Inject in? |
| 119 | + $queries = app()->make(EntityQueries::class); |
| 120 | + |
| 121 | + $parentQuery = $queries->visibleForList(); |
| 122 | + $filtered = count($parentsByType['book']) > 0 || count($parentsByType['chapter']) > 0; |
| 123 | + $parentQuery = $parentQuery->where(function ($query) use ($parentsByType) { |
| 124 | + foreach ($parentsByType as $type => $ids) { |
| 125 | + if (count($ids) > 0) { |
| 126 | + $query = $query->orWhere(function ($query) use ($type, $ids) { |
| 127 | + $query->where('type', '=', $type) |
| 128 | + ->whereIn('id', $ids); |
| 129 | + }); |
| 130 | + } |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + $parents = $filtered ? (new EntityHydrator($parentQuery->get()->all()))->hydrate() : []; |
| 135 | + $parentMap = []; |
| 136 | + foreach ($parents as $parent) { |
| 137 | + $parentMap[$parent->type . ':' . $parent->id] = $parent; |
| 138 | + } |
| 139 | + |
| 140 | + foreach ($entities as $entity) { |
| 141 | + if ($entity instanceof Page || $entity instanceof Chapter) { |
| 142 | + $key = 'book:' . $entity->getAttribute('book_id'); |
| 143 | + $entity->setRelation('book', $parentMap[$key] ?? null); |
| 144 | + } |
| 145 | + if ($entity instanceof Page) { |
| 146 | + $key = 'chapter:' . $entity->getAttribute('chapter_id'); |
| 147 | + $entity->setRelation('chapter', $parentMap[$key] ?? null); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments