Skip to content

Commit 5c2908e

Browse files
committed
Entities: Updated model classes & relations for changes
1 parent b866dee commit 5c2908e

File tree

13 files changed

+52
-95
lines changed

13 files changed

+52
-95
lines changed

app/Entities/Models/Book.php

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,39 +43,11 @@ public function getUrl(string $path = ''): string
4343
}
4444

4545
/**
46-
* Returns book cover image, if book cover not exists return default cover image.
46+
* Returns a book cover image URL or a default URL if no cover image set.
4747
*/
48-
public function getBookCover(int $width = 440, int $height = 250): string
48+
public function getCover(int $width = 440, int $height = 250): string
4949
{
50-
$default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
51-
if (!$this->containerData->image_id || !$this->containerData->cover) {
52-
return $default;
53-
}
54-
55-
try {
56-
return $this->containerData->cover->getThumb($width, $height, false) ?? $default;
57-
} catch (Exception $err) {
58-
return $default;
59-
}
60-
}
61-
62-
// TODO - Still handle cover as relation through containerData (since it's used in code)
63-
// TODO - Remove above since we can access that via containerData
64-
65-
/**
66-
* Get the Page that is used as default template for newly created pages within this Book.
67-
*/
68-
public function defaultTemplate(): BelongsTo
69-
{
70-
return $this->belongsTo(Page::class, 'default_template_id');
71-
}
72-
73-
/**
74-
* Get the sort set assigned to this book, if existing.
75-
*/
76-
public function sortRule(): BelongsTo
77-
{
78-
return $this->belongsTo(SortRule::class);
50+
return $this->containerData->getCoverUrl($width, $height);
7951
}
8052

8153
/**

app/Entities/Models/Bookshelf.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace BookStack\Entities\Models;
44

5-
use Exception;
65
use Illuminate\Database\Eloquent\Factories\HasFactory;
76
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
87

@@ -41,27 +40,6 @@ public function getUrl(string $path = ''): string
4140
return url('/shelves/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
4241
}
4342

44-
/**
45-
* Returns shelf cover image, if cover not exists return default cover image.
46-
*/
47-
public function getBookCover(int $width = 440, int $height = 250): string
48-
{
49-
// TODO - Make generic, focused on books right now, Perhaps set-up a better image
50-
$default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
51-
if (!$this->containerData->image_id || !$this->containerData->cover) {
52-
return $default;
53-
}
54-
55-
try {
56-
return $this->containerData->cover->getThumb($width, $height, false) ?? $default;
57-
} catch (Exception $err) {
58-
return $default;
59-
}
60-
}
61-
62-
// TODO - Still handle cover as relation through containerData (since it's used in code)
63-
// TODO - Remove above since we can access that via containerData
64-
6543
/**
6644
* Check if this shelf contains the given book.
6745
*/

app/Entities/Models/Chapter.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use Illuminate\Support\Collection;
99

1010
/**
11-
* Class Chapter.
12-
*
1311
* @property Collection<Page> $pages
1412
* @property ?int $default_template_id
1513
* @property ?Page $defaultTemplate
@@ -47,14 +45,6 @@ public function getUrl(string $path = ''): string
4745
return url('/' . implode('/', $parts));
4846
}
4947

50-
/**
51-
* Get the Page that is used as default template for newly created pages within this Chapter.
52-
*/
53-
public function defaultTemplate(): BelongsTo
54-
{
55-
return $this->belongsTo(Page::class, 'default_template_id');
56-
}
57-
5848
/**
5949
* Get the visible pages in this chapter.
6050
* @return Collection<Page>

app/Entities/Models/Entity.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* This is not a database model in itself but extended.
3939
*
4040
* @property int $id
41+
* @property string $type
4142
* @property string $name
4243
* @property string $slug
4344
* @property Carbon $created_at
@@ -123,8 +124,8 @@ public function scopeVisible(Builder $query): Builder
123124
public function scopeWithLastView(Builder $query)
124125
{
125126
$viewedAtQuery = View::query()->select('updated_at')
126-
->whereColumn('viewable_id', '=', $this->getTable() . '.id')
127-
->where('viewable_type', '=', $this->getMorphClass())
127+
->whereColumn('viewable_id', '=', 'entities.id')
128+
->whereColumn('viewable_type', '=', 'entities.type')
128129
->where('user_id', '=', user()->id)
129130
->take(1);
130131

@@ -134,11 +135,12 @@ public function scopeWithLastView(Builder $query)
134135
/**
135136
* Query scope to get the total view count of the entities.
136137
*/
137-
public function scopeWithViewCount(Builder $query)
138+
public function scopeWithViewCount(Builder $query): void
138139
{
139140
$viewCountQuery = View::query()->selectRaw('SUM(views) as view_count')
140-
->whereColumn('viewable_id', '=', $this->getTable() . '.id')
141-
->where('viewable_type', '=', $this->getMorphClass())->take(1);
141+
->whereColumn('viewable_id', '=', 'entities.id')
142+
->whereColumn('viewable_type', '=', 'entities.type')
143+
->take(1);
142144

143145
$query->addSelect(['view_count' => $viewCountQuery]);
144146
}
@@ -194,7 +196,8 @@ public function views(): MorphMany
194196
*/
195197
public function tags(): MorphMany
196198
{
197-
return $this->morphMany(Tag::class, 'entity')->orderBy('order', 'asc');
199+
return $this->morphMany(Tag::class, 'entity')
200+
->orderBy('order', 'asc');
198201
}
199202

200203
/**
@@ -216,7 +219,7 @@ public function searchTerms(): MorphMany
216219
}
217220

218221
/**
219-
* Get this entities restrictions.
222+
* Get this entities assigned permissions.
220223
*/
221224
public function permissions(): MorphMany
222225
{
@@ -299,7 +302,7 @@ public static function getType(): string
299302
}
300303

301304
/**
302-
* Gets a limited-length version of the entities name.
305+
* Gets a limited-length version of the entity name.
303306
*/
304307
public function getShortName(int $length = 25): string
305308
{
@@ -338,7 +341,7 @@ public function getParent(): ?self
338341
{
339342
if ($this instanceof Page) {
340343
/** @var BelongsTo<Chapter|Book, Page> $builder */
341-
$builder = $this->chapter_id ? $this->chapter() : $this->book();
344+
$builder = $this->pageData->chapter_id ? $this->chapter() : $this->book();
342345
return $builder->withTrashed()->first();
343346
}
344347
if ($this instanceof Chapter) {

app/Entities/Models/EntityContainerData.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace BookStack\Entities\Models;
44

5+
use BookStack\Sorting\SortRule;
56
use BookStack\Uploads\Image;
67
use BookStack\Util\HtmlContentFilter;
78
use Exception;
89
use Illuminate\Database\Eloquent\Model;
10+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
911
use Illuminate\Database\Eloquent\Relations\HasOne;
1012

1113
/**
@@ -38,7 +40,7 @@ public function getCoverUrl(int $width = 440, int $height = 250, string|null $de
3840
}
3941

4042
try {
41-
return $this->cover->getThumb($width, $height, false) ?? $default;
43+
return $this->cover?->getThumb($width, $height, false) ?? $default;
4244
} catch (Exception $err) {
4345
return $default;
4446
}
@@ -89,4 +91,20 @@ public function setDescriptionHtml(string $html, string|null $plaintext = null):
8991
$this->description_html = $this->getDescriptionHtml();
9092
}
9193
}
94+
95+
/**
96+
* Get the Page used as a default template to be used for new items within this container.
97+
*/
98+
public function defaultTemplate(): BelongsTo
99+
{
100+
return $this->belongsTo(Page::class, 'default_template_id');
101+
}
102+
103+
/**
104+
* Get the sort rule assigned to this container, if existing.
105+
*/
106+
public function sortRule(): BelongsTo
107+
{
108+
return $this->belongsTo(SortRule::class);
109+
}
92110
}

app/Entities/Models/Page.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,15 @@ public function scopeVisible(Builder $query): Builder
5757
*/
5858
public function pageData(): HasOne
5959
{
60-
return $this->hasOne(EntityPageData::class, 'id', 'page_id');
60+
return $this->hasOne(EntityPageData::class, 'page_id', 'id');
6161
}
6262

6363
/**
6464
* Get the chapter that this page is in, If applicable.
65-
*
66-
* @return BelongsTo
6765
*/
68-
public function chapter()
66+
public function chapter(): BelongsTo
6967
{
70-
return $this->belongsTo(Chapter::class);
68+
return $this->pageData->belongsTo(Chapter::class);
7169
}
7270

7371
/**
@@ -112,10 +110,8 @@ public function allRevisions(): HasMany
112110

113111
/**
114112
* Get the attachments assigned to this page.
115-
*
116-
* @return HasMany
117113
*/
118-
public function attachments()
114+
public function attachments(): HasMany
119115
{
120116
return $this->hasMany(Attachment::class, 'uploaded_to')->orderBy('order', 'asc');
121117
}
@@ -128,8 +124,8 @@ public function getUrl(string $path = ''): string
128124
$parts = [
129125
'books',
130126
urlencode($this->book_slug ?? $this->book->slug),
131-
$this->draft ? 'draft' : 'page',
132-
$this->draft ? $this->id : urlencode($this->slug),
127+
$this->pageData->draft ? 'draft' : 'page',
128+
$this->pageData->draft ? $this->id : urlencode($this->slug),
133129
trim($path, '/'),
134130
];
135131

@@ -142,9 +138,9 @@ public function getUrl(string $path = ''): string
142138
public function forJsonDisplay(): self
143139
{
144140
$refreshed = $this->refresh()->unsetRelations()->load(['tags', 'createdBy', 'updatedBy', 'ownedBy']);
145-
$refreshed->setHidden(array_diff($refreshed->getHidden(), ['html', 'markdown']));
146-
$refreshed->setAttribute('raw_html', $refreshed->html);
147-
$refreshed->html = (new PageContent($refreshed))->render();
141+
$refreshed->setHidden(array_diff($refreshed->getHidden(), ['pageData']));
142+
$refreshed->setAttribute('raw_html', $refreshed->pageData->html);
143+
$refreshed->setAttribute('html', (new PageContent($refreshed))->render());
148144

149145
return $refreshed;
150146
}

app/Sorting/BookSorter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function runBookAutoSortForAllWithSet(SortRule $set): void
3333
*/
3434
public function runBookAutoSort(Book $book): void
3535
{
36-
$set = $book->sortRule;
36+
$set = $book->containerData->sortRule;
3737
if (!$set) {
3838
return;
3939
}

resources/views/books/parts/list-item.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<a href="{{ $book->getUrl() }}" class="book entity-list-item" data-entity-type="book" data-entity-id="{{$book->id}}">
2-
<div class="entity-list-item-image bg-book" style="background-image: url('{{ $book->getBookCover() }}')">
2+
<div class="entity-list-item-image bg-book" style="background-image: url('{{ $book->getCover() }}')">
33
@icon('book')
44
</div>
55
<div class="content">

resources/views/books/parts/sort-box.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<span>{{ $book->name }}</span>
1010
</div>
1111
<div class="flex-container-row items-center text-book">
12-
@if($book->sortRule)
13-
<span title="{{ trans('entities.books_sort_auto_sort_active', ['sortName' => $book->sortRule->name]) }}">@icon('auto-sort')</span>
12+
@if($book->containerData->sortRule)
13+
<span title="{{ trans('entities.books_sort_auto_sort_active', ['sortName' => $book->containerData->sortRule->name]) }}">@icon('auto-sort')</span>
1414
@endif
1515
</div>
1616
</h5>

resources/views/books/show.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@push('social-meta')
1010
<meta property="og:description" content="{{ Str::limit($book->description, 100, '...') }}">
1111
@if($book->containerData->cover)
12-
<meta property="og:image" content="{{ $book->getBookCover() }}">
12+
<meta property="og:image" content="{{ $book->getCover() }}">
1313
@endif
1414
@endpush
1515

0 commit comments

Comments
 (0)