Skip to content

Commit c60052b

Browse files
committed
Entities: Started logic change to new structure
Updated base entity class, and worked through BaseRepo. Need to go through other repos next. Removed a couple of redundant interfaces as part of this since we can move the logic onto the shared ContainerData model as needed.
1 parent c767dde commit c60052b

37 files changed

+166
-182
lines changed

app/Entities/Controllers/BookApiController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ protected function forJsonDisplay(Book $book): Book
122122
$book = clone $book;
123123
$book->unsetRelations()->refresh();
124124

125-
$book->load(['tags', 'cover']);
126-
$book->makeVisible('description_html')
127-
->setAttribute('description_html', $book->descriptionHtml());
125+
$book->load(['tags']);
126+
$book->makeVisible(['cover', 'description_html'])
127+
->setAttribute('description_html', $book->containerData->getDescriptionHtml())
128+
->setAttribute('cover', $book->containerData->cover);
128129

129130
return $book;
130131
}

app/Entities/Controllers/BookshelfApiController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ protected function forJsonDisplay(Bookshelf $shelf): Bookshelf
116116
$shelf = clone $shelf;
117117
$shelf->unsetRelations()->refresh();
118118

119-
$shelf->load(['tags', 'cover']);
120-
$shelf->makeVisible('description_html')
121-
->setAttribute('description_html', $shelf->descriptionHtml());
119+
$shelf->load(['tags']);
120+
$shelf->makeVisible(['cover', 'description_html'])
121+
->setAttribute('description_html', $shelf->containerData->getDescriptionHtml())
122+
->setAttribute('cover', $shelf->containerData->cover);
122123

123124
return $shelf;
124125
}

app/Entities/Controllers/ChapterApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected function forJsonDisplay(Chapter $chapter): Chapter
144144

145145
$chapter->load(['tags']);
146146
$chapter->makeVisible('description_html');
147-
$chapter->setAttribute('description_html', $chapter->descriptionHtml());
147+
$chapter->setAttribute('description_html', $chapter->containerData->getDescriptionHtml());
148148

149149
/** @var Book $book */
150150
$book = $chapter->book()->first();

app/Entities/Models/Book.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@
2626
* @property ?Page $defaultTemplate
2727
* @property ?SortRule $sortRule
2828
*/
29-
class Book extends Entity implements CoverImageInterface, HtmlDescriptionInterface
29+
class Book extends Entity
3030
{
3131
use HasFactory;
32-
use HtmlDescriptionTrait;
3332

3433
public float $searchFactor = 1.2;
3534

36-
protected $fillable = ['name'];
3735
protected $hidden = ['pivot', 'image_id', 'deleted_at', 'description_html'];
3836

3937
/**
@@ -50,33 +48,17 @@ public function getUrl(string $path = ''): string
5048
public function getBookCover(int $width = 440, int $height = 250): string
5149
{
5250
$default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
53-
if (!$this->image_id || !$this->cover) {
51+
if (!$this->containerData->image_id || !$this->containerData->cover) {
5452
return $default;
5553
}
5654

5755
try {
58-
return $this->cover->getThumb($width, $height, false) ?? $default;
56+
return $this->containerData->cover->getThumb($width, $height, false) ?? $default;
5957
} catch (Exception $err) {
6058
return $default;
6159
}
6260
}
6361

64-
/**
65-
* Get the cover image of the book.
66-
*/
67-
public function cover(): BelongsTo
68-
{
69-
return $this->belongsTo(Image::class, 'image_id');
70-
}
71-
72-
/**
73-
* Get the type of the image model that is used when storing a cover image.
74-
*/
75-
public function coverImageTypeKey(): string
76-
{
77-
return 'cover_book';
78-
}
79-
8062
/**
8163
* Get the Page that is used as default template for newly created pages within this Book.
8264
*/

app/Entities/Models/Bookshelf.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,21 @@
22

33
namespace BookStack\Entities\Models;
44

5-
use BookStack\Uploads\Image;
65
use Exception;
76
use Illuminate\Database\Eloquent\Factories\HasFactory;
8-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
97
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
108

11-
class Bookshelf extends Entity implements CoverImageInterface, HtmlDescriptionInterface
9+
class Bookshelf extends Entity
1210
{
1311
use HasFactory;
14-
use HtmlDescriptionTrait;
1512

1613
public float $searchFactor = 1.2;
1714

18-
protected $fillable = ['name', 'description', 'image_id'];
19-
2015
protected $hidden = ['image_id', 'deleted_at', 'description_html'];
2116

2217
/**
2318
* Get the books in this shelf.
24-
* Should not be used directly since does not take into account permissions.
19+
* Should not be used directly since it does not take into account permissions.
2520
*/
2621
public function books(): BelongsToMany
2722
{
@@ -53,34 +48,17 @@ public function getBookCover(int $width = 440, int $height = 250): string
5348
{
5449
// TODO - Make generic, focused on books right now, Perhaps set-up a better image
5550
$default = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
56-
if (!$this->image_id || !$this->cover) {
51+
if (!$this->containerData->image_id || !$this->containerData->cover) {
5752
return $default;
5853
}
5954

6055
try {
61-
return $this->cover->getThumb($width, $height, false) ?? $default;
56+
return $this->containerData->cover->getThumb($width, $height, false) ?? $default;
6257
} catch (Exception $err) {
6358
return $default;
6459
}
6560
}
6661

67-
/**
68-
* Get the cover image of the shelf.
69-
* @return BelongsTo<Image, $this>
70-
*/
71-
public function cover(): BelongsTo
72-
{
73-
return $this->belongsTo(Image::class, 'image_id');
74-
}
75-
76-
/**
77-
* Get the type of the image model that is used when storing a cover image.
78-
*/
79-
public function coverImageTypeKey(): string
80-
{
81-
return 'cover_bookshelf';
82-
}
83-
8462
/**
8563
* Check if this shelf contains the given book.
8664
*/
@@ -92,7 +70,7 @@ public function contains(Book $book): bool
9270
/**
9371
* Add a book to the end of this shelf.
9472
*/
95-
public function appendBook(Book $book)
73+
public function appendBook(Book $book): void
9674
{
9775
if ($this->contains($book)) {
9876
return;

app/Entities/Models/Chapter.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
* @property ?int $default_template_id
1515
* @property ?Page $defaultTemplate
1616
*/
17-
class Chapter extends BookChild implements HtmlDescriptionInterface
17+
class Chapter extends BookChild
1818
{
1919
use HasFactory;
20-
use HtmlDescriptionTrait;
2120

2221
public float $searchFactor = 1.2;
23-
24-
protected $fillable = ['name', 'description', 'priority'];
2522
protected $hidden = ['pivot', 'deleted_at', 'description_html'];
2623

2724
/**

app/Entities/Models/CoverImageInterface.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

app/Entities/Models/Entity.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Illuminate\Database\Eloquent\Builder;
2929
use Illuminate\Database\Eloquent\Collection;
3030
use Illuminate\Database\Eloquent\Relations\BelongsTo;
31+
use Illuminate\Database\Eloquent\Relations\HasOne;
3132
use Illuminate\Database\Eloquent\Relations\MorphMany;
3233
use Illuminate\Database\Eloquent\SoftDeletes;
3334

@@ -47,6 +48,8 @@
4748
* @property int $owned_by
4849
* @property Collection $tags
4950
*
51+
* @property ?EntityContainerData $containerData
52+
*
5053
* @method static Entity|Builder visible()
5154
* @method static Builder withLastView()
5255
* @method static Builder withViewCount()
@@ -90,6 +93,22 @@ protected static function booted(): void
9093
static::addGlobalScope('entity', new EntityScope());
9194
}
9295

96+
public function shouldHaveContainerData(): bool
97+
{
98+
return $this instanceof Bookshelf ||
99+
$this instanceof Book ||
100+
$this instanceof Chapter;
101+
}
102+
103+
/**
104+
* Get the container-specific data for this page.
105+
*/
106+
public function containerData(): HasOne
107+
{
108+
return $this->hasOne(EntityContainerData::class, 'id', 'entity_id')
109+
->where('entity_type', '=', $this->getMorphClass());
110+
}
111+
93112
/**
94113
* Get the entities that are visible to the current user.
95114
*/

app/Entities/Models/HtmlDescriptionTrait.php renamed to app/Entities/Models/EntityContainerData.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@
22

33
namespace BookStack\Entities\Models;
44

5+
use BookStack\Uploads\Image;
56
use BookStack\Util\HtmlContentFilter;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Relations\HasOne;
69

710
/**
811
* @property string $description
912
* @property string $description_html
13+
* @property ?int $default_template_id
14+
* @property ?int $image_id
15+
* @property ?int $sort_rule_id
1016
*/
11-
trait HtmlDescriptionTrait
17+
class EntityContainerData extends Model
1218
{
19+
public $timestamps = false;
20+
21+
/**
22+
* Relation for the cover image for this entity.
23+
* @return HasOne<Image, $this>
24+
*/
25+
public function cover(): HasOne
26+
{
27+
return $this->hasOne(Image::class, 'image_id');
28+
}
29+
30+
/**
31+
* Get the description as a cleaned/handled HTML string.
32+
*/
1333
public function descriptionHtml(bool $raw = false): string
1434
{
1535
$html = $this->description_html ?: '<p>' . nl2br(e($this->description)) . '</p>';
@@ -20,6 +40,10 @@ public function descriptionHtml(bool $raw = false): string
2040
return HtmlContentFilter::removeScriptsFromHtmlString($html);
2141
}
2242

43+
/**
44+
* Update the description from HTML code.
45+
* Optionally takes plaintext to use for the model also.
46+
*/
2347
public function setDescriptionHtml(string $html, string|null $plaintext = null): void
2448
{
2549
$this->description_html = $html;
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 Illuminate\Database\Eloquent\Model;
6+
7+
class EntityPageData extends Model
8+
{
9+
public $timestamps = false;
10+
}

0 commit comments

Comments
 (0)