22
33namespace BookStack \Entities \Models ;
44
5- use BookStack \Sorting \SortRule ;
6- use BookStack \Uploads \Image ;
7- use Exception ;
85use Illuminate \Database \Eloquent \Builder ;
96use 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}
0 commit comments