Skip to content

Commit 1bd0dda

Browse files
committed
Entities: Removed use of contents system for data access
1 parent 4f21e54 commit 1bd0dda

22 files changed

+135
-220
lines changed

app/Entities/Models/Book.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace BookStack\Entities\Models;
44

55
use BookStack\Entities\Tools\EntityCover;
6+
use BookStack\Entities\Tools\EntityDefaultTemplate;
67
use BookStack\Sorting\SortRule;
7-
use BookStack\Uploads\Image;
88
use Illuminate\Database\Eloquent\Factories\HasFactory;
9+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
910
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1011
use Illuminate\Database\Eloquent\Relations\HasMany;
1112
use Illuminate\Support\Collection;
@@ -18,15 +19,13 @@
1819
* @property int $image_id
1920
* @property ?int $default_template_id
2021
* @property ?int $sort_rule_id
21-
* @property Image|null $cover
2222
* @property \Illuminate\Database\Eloquent\Collection $chapters
2323
* @property \Illuminate\Database\Eloquent\Collection $pages
2424
* @property \Illuminate\Database\Eloquent\Collection $directPages
2525
* @property \Illuminate\Database\Eloquent\Collection $shelves
26-
* @property ?Page $defaultTemplate
27-
* @property ?SortRule $sortRule
26+
* @property ?SortRule $sortRule
2827
*/
29-
class Book extends Entity implements DescriptionInterface, CoverInterface
28+
class Book extends Entity implements HasDescriptionInterface, HasCoverInterface, HasDefaultTemplateInterface
3029
{
3130
use HasFactory;
3231
use ContainerTrait;
@@ -43,15 +42,6 @@ public function getUrl(string $path = ''): string
4342
return url('/books/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
4443
}
4544

46-
/**
47-
* Returns a book cover image URL or a default URL if no cover image set.
48-
* TODO - Delete
49-
*/
50-
public function getCover(int $width = 440, int $height = 250): string
51-
{
52-
return $this->contents()->getCoverUrl($width, $height);
53-
}
54-
5545
public function cover(): EntityCover
5646
{
5747
return new EntityCover($this);
@@ -101,4 +91,17 @@ public function getDirectVisibleChildren(): Collection
10191

10292
return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
10393
}
94+
95+
public function defaultTemplate(): EntityDefaultTemplate
96+
{
97+
return new EntityDefaultTemplate($this);
98+
}
99+
100+
/**
101+
* Get the sort rule assigned to this container, if existing.
102+
*/
103+
public function sortRule(): BelongsTo
104+
{
105+
return $this->belongsTo(SortRule::class);
106+
}
104107
}

app/Entities/Models/Bookshelf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @property string $description
1111
* @property string $description_html
1212
*/
13-
class Bookshelf extends Entity implements DescriptionInterface, CoverInterface
13+
class Bookshelf extends Entity implements HasDescriptionInterface, HasCoverInterface
1414
{
1515
use HasFactory;
1616
use ContainerTrait;

app/Entities/Models/Chapter.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
namespace BookStack\Entities\Models;
44

5+
use BookStack\Entities\Tools\EntityDefaultTemplate;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Relations\HasMany;
78
use Illuminate\Support\Collection;
89

910
/**
1011
* @property Collection<Page> $pages
1112
* @property ?int $default_template_id
12-
* @property ?Page $defaultTemplate
13-
* @property string $description
14-
* @property string $description_html
13+
* @property string $description
14+
* @property string $description_html
1515
*/
16-
class Chapter extends BookChild implements DescriptionInterface
16+
class Chapter extends BookChild implements HasDescriptionInterface, HasDefaultTemplateInterface
1717
{
1818
use HasFactory;
1919
use ContainerTrait;
@@ -59,4 +59,9 @@ public function getVisiblePages(): Collection
5959
->orderBy('priority', 'asc')
6060
->get();
6161
}
62+
63+
public function defaultTemplate(): EntityDefaultTemplate
64+
{
65+
return new EntityDefaultTemplate($this);
66+
}
6267
}

app/Entities/Models/ContainerTrait.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,4 @@ public function relatedData(): HasOne
2323
return $this->hasOne(EntityContainerContents::class, 'entity_id', 'id')
2424
->where('entity_type', '=', $this->getMorphClass());
2525
}
26-
27-
public function contents(): EntityContainerContents
28-
{
29-
$data = parent::contents();
30-
if ($data instanceof EntityContainerContents) {
31-
return $data;
32-
}
33-
34-
/** @var EntityContainerContents $data */
35-
$data = $this->relatedData()->newModelInstance();
36-
$data->setRawAttributes([
37-
'entity_id' => $this->id,
38-
'entity_type' => $this->getMorphClass(),
39-
'description' => '',
40-
'description_html' => '',
41-
'default_template_id' => null,
42-
'image_id' => null,
43-
'sort_rule_id' => null,
44-
]);
45-
46-
return $data;
47-
}
4826
}

app/Entities/Models/Entity.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ abstract class Entity extends Model implements
104104
'owned_by',
105105
];
106106

107-
// TODO - To Remove, reduce contents/relatedData to just one of them.
108-
// TODO - Update/remove existing usages of contents/relatedData
109107
// TODO - Review usages of query-time update or mass insert of entity model data since those will still need to consider the multi-table layout.
110108

111109
/**
@@ -468,21 +466,4 @@ protected function getContentsAttributes(): array
468466

469467
return $contentFields;
470468
}
471-
472-
/**
473-
* Get the contents model of this entity.
474-
* Should only really be used if accessing the methods of the contents model
475-
* since the properties would already be part of the fetched entity model.
476-
*/
477-
public function contents(): EntityContainerContents|EntityPageContents
478-
{
479-
$contentsData = $this->getContentsAttributes();
480-
481-
/**
482-
* @var EntityContainerContents|EntityPageContents $instance
483-
*/
484-
$instance = $this->relatedData()->newModelInstance()->newFromBuilder($contentsData, $this->getConnection());
485-
486-
return $instance;
487-
}
488469
}

app/Entities/Models/EntityContainerContents.php

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22

33
namespace BookStack\Entities\Models;
44

5-
use BookStack\Sorting\SortRule;
6-
use BookStack\Uploads\Image;
7-
use Exception;
85
use Illuminate\Database\Eloquent\Builder;
96
use Illuminate\Database\Eloquent\Model;
10-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
11-
use Illuminate\Database\Eloquent\Relations\HasOne;
127

138
/**
14-
* @property int $entity_id
15-
* @property string $entity_type
16-
* @property string $description
17-
* @property string $description_html
9+
* @property int $entity_id
10+
* @property string $entity_type
11+
* @property string $description
12+
* @property string $description_html
1813
* @property ?int $default_template_id
1914
* @property ?int $image_id
2015
* @property ?int $sort_rule_id
@@ -25,9 +20,6 @@ class EntityContainerContents extends Model
2520
protected $primaryKey = 'entity_id';
2621
public $incrementing = false;
2722

28-
// TODO - Should put the entity methods and relations back onto the original models
29-
// if we're going back to mostly keeping to the models.
30-
3123
/**
3224
* Override the default set keys for save query method to make it work with composite keys.
3325
*/
@@ -40,7 +32,7 @@ public function setKeysForSaveQuery($query): Builder
4032
}
4133

4234
/**
43-
* Override the default set keys for select query method to make it work with composite keys.
35+
* Override the default set keys for a select query method to make it work with composite keys.
4436
*/
4537
protected function setKeysForSelectQuery($query): Builder
4638
{
@@ -49,66 +41,4 @@ protected function setKeysForSelectQuery($query): Builder
4941

5042
return $query;
5143
}
52-
53-
/**
54-
* Relation for the cover image for this entity.
55-
* @return HasOne<Image, $this>
56-
*/
57-
public function cover(): HasOne
58-
{
59-
return $this->hasOne(Image::class, 'image_id');
60-
}
61-
62-
public function getCover(): Image|null
63-
{
64-
return $this->cover()->first();
65-
}
66-
67-
/**
68-
* Returns a shelf cover image URL, if cover not exists return default cover image.
69-
*/
70-
public function getCoverUrl(int $width = 440, int $height = 250, string|null $default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='): string|null
71-
{
72-
if (!$this->image_id) {
73-
return $default;
74-
}
75-
76-
try {
77-
return $this->getCover()?->getThumb($width, $height, false) ?? $default;
78-
} catch (Exception $err) {
79-
return $default;
80-
}
81-
}
82-
83-
/**
84-
* Check if this data supports having a default template assigned.
85-
*/
86-
public function supportsDefaultTemplate(): bool
87-
{
88-
return in_array($this->entity_type, ['book', 'chapter']);
89-
}
90-
91-
/**
92-
* Check this data supports having a cover image assigned.
93-
*/
94-
public function supportsCoverImage(): bool
95-
{
96-
return in_array($this->entity_type, ['book', 'bookshelf']);
97-
}
98-
99-
/**
100-
* Get the Page used as a default template to be used for new items within this container.
101-
*/
102-
public function defaultTemplate(): BelongsTo
103-
{
104-
return $this->belongsTo(Page::class, 'default_template_id');
105-
}
106-
107-
/**
108-
* Get the sort rule assigned to this container, if existing.
109-
*/
110-
public function sortRule(): BelongsTo
111-
{
112-
return $this->belongsTo(SortRule::class);
113-
}
11444
}

app/Entities/Models/CoverInterface.php renamed to app/Entities/Models/HasCoverInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use BookStack\Entities\Tools\EntityCover;
66

7-
interface CoverInterface
7+
interface HasCoverInterface
88
{
99
public function cover(): EntityCover;
1010
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace BookStack\Entities\Models;
4+
5+
use BookStack\Entities\Tools\EntityDefaultTemplate;
6+
7+
interface HasDefaultTemplateInterface
8+
{
9+
public function defaultTemplate(): EntityDefaultTemplate;
10+
}

app/Entities/Models/DescriptionInterface.php renamed to app/Entities/Models/HasDescriptionInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use BookStack\Entities\Tools\EntityHtmlDescription;
66

7-
interface DescriptionInterface
7+
interface HasDescriptionInterface
88
{
99
public function description(): EntityHtmlDescription;
1010
}

app/Entities/Models/Page.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,28 +153,4 @@ public function relatedData(): HasOne
153153
{
154154
return $this->hasOne(EntityPageContents::class, 'page_id', 'id');
155155
}
156-
157-
public function contents(): EntityPageContents
158-
{
159-
$data = parent::contents();
160-
if ($data instanceof EntityPageContents) {
161-
return $data;
162-
}
163-
164-
/** @var EntityPageContents $data */
165-
$data = $this->relatedData()->newModelInstance();
166-
$data->setRawAttributes([
167-
'page_id' => $this->id,
168-
'chapter_id' => null,
169-
'draft' => false,
170-
'template' => false,
171-
'revision_count' => 0,
172-
'editor' => PageEditorType::WysiwygTinymce->value,
173-
'html' => '',
174-
'text' => '',
175-
'markdown' => '',
176-
]);
177-
178-
return $data;
179-
}
180156
}

0 commit comments

Comments
 (0)