2021-06-26 15:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BookStack\Entities\Models;
|
2018-09-25 12:30:50 +01:00
|
|
|
|
2025-10-18 13:14:30 +01:00
|
|
|
use BookStack\Entities\Tools\EntityCover;
|
|
|
|
|
use BookStack\Entities\Tools\EntityDefaultTemplate;
|
2025-02-11 14:36:25 +00:00
|
|
|
use BookStack\Sorting\SortRule;
|
2018-09-25 12:30:50 +01:00
|
|
|
use BookStack\Uploads\Image;
|
2021-10-30 21:29:59 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2019-10-05 12:55:01 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
|
use Illuminate\Support\Collection;
|
2015-07-12 20:01:42 +01:00
|
|
|
|
2019-09-19 18:03:17 +01:00
|
|
|
/**
|
2021-06-26 15:23:15 +00:00
|
|
|
* Class Book.
|
|
|
|
|
*
|
2021-09-18 21:21:44 +01:00
|
|
|
* @property string $description
|
2025-10-18 13:14:30 +01:00
|
|
|
* @property string $description_html
|
2021-09-18 21:21:44 +01:00
|
|
|
* @property int $image_id
|
2023-12-12 12:14:00 +00:00
|
|
|
* @property ?int $default_template_id
|
2025-02-11 14:36:25 +00:00
|
|
|
* @property ?int $sort_rule_id
|
2021-09-13 22:54:21 +01:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection $chapters
|
|
|
|
|
* @property \Illuminate\Database\Eloquent\Collection $pages
|
|
|
|
|
* @property \Illuminate\Database\Eloquent\Collection $directPages
|
2022-09-28 14:14:51 +01:00
|
|
|
* @property \Illuminate\Database\Eloquent\Collection $shelves
|
2025-10-18 13:14:30 +01:00
|
|
|
* @property ?SortRule $sortRule
|
2019-09-19 18:03:17 +01:00
|
|
|
*/
|
2025-10-18 13:14:30 +01:00
|
|
|
class Book extends Entity implements HasDescriptionInterface, HasCoverInterface, HasDefaultTemplateInterface
|
2015-07-12 20:01:42 +01:00
|
|
|
{
|
2021-10-30 21:29:59 +01:00
|
|
|
use HasFactory;
|
2025-10-18 13:14:30 +01:00
|
|
|
use ContainerTrait;
|
2021-10-30 21:29:59 +01:00
|
|
|
|
2023-12-18 16:23:40 +00:00
|
|
|
public float $searchFactor = 1.2;
|
2015-07-12 20:01:42 +01:00
|
|
|
|
2025-10-18 13:14:30 +01:00
|
|
|
protected $hidden = ['pivot', 'deleted_at', 'description_html', 'entity_id', 'entity_type', 'chapter_id', 'book_id', 'priority'];
|
2023-12-17 15:02:15 +00:00
|
|
|
protected $fillable = ['name'];
|
2015-07-12 20:01:42 +01:00
|
|
|
|
2016-05-01 21:20:50 +01:00
|
|
|
/**
|
|
|
|
|
* Get the url for this book.
|
|
|
|
|
*/
|
2020-11-22 01:20:38 +00:00
|
|
|
public function getUrl(string $path = ''): string
|
2015-07-12 20:01:42 +01:00
|
|
|
{
|
2020-11-22 01:20:38 +00:00
|
|
|
return url('/books/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
|
2015-07-12 20:01:42 +01:00
|
|
|
}
|
2017-12-06 17:32:29 +00:00
|
|
|
|
2016-05-01 21:20:50 +01:00
|
|
|
/**
|
|
|
|
|
* Get all pages within this book.
|
2025-09-02 16:02:52 +01:00
|
|
|
* @return HasMany<Page, $this>
|
2016-05-01 21:20:50 +01:00
|
|
|
*/
|
2021-11-22 23:33:55 +00:00
|
|
|
public function pages(): HasMany
|
2015-07-12 21:31:15 +01:00
|
|
|
{
|
2016-05-01 21:20:50 +01:00
|
|
|
return $this->hasMany(Page::class);
|
2015-07-12 21:31:15 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-24 15:57:35 +00:00
|
|
|
/**
|
|
|
|
|
* Get the direct child pages of this book.
|
|
|
|
|
*/
|
2021-11-22 23:33:55 +00:00
|
|
|
public function directPages(): HasMany
|
2019-02-24 15:57:35 +00:00
|
|
|
{
|
2025-10-18 13:14:30 +01:00
|
|
|
return $this->pages()->whereNull('chapter_id');
|
2019-02-24 15:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-01 21:20:50 +01:00
|
|
|
/**
|
|
|
|
|
* Get all chapters within this book.
|
2025-09-02 16:02:52 +01:00
|
|
|
* @return HasMany<Chapter, $this>
|
2016-05-01 21:20:50 +01:00
|
|
|
*/
|
2021-11-22 23:33:55 +00:00
|
|
|
public function chapters(): HasMany
|
2015-07-27 20:17:08 +01:00
|
|
|
{
|
2025-10-18 13:14:30 +01:00
|
|
|
return $this->hasMany(Chapter::class)
|
|
|
|
|
->where('type', '=', 'chapter');
|
2015-07-27 20:17:08 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-20 15:27:30 +01:00
|
|
|
/**
|
|
|
|
|
* Get the shelves this book is contained within.
|
|
|
|
|
*/
|
2021-11-22 23:33:55 +00:00
|
|
|
public function shelves(): BelongsToMany
|
2018-09-20 15:27:30 +01:00
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Bookshelf::class, 'bookshelves_books', 'book_id', 'bookshelf_id');
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-01 21:20:50 +01:00
|
|
|
/**
|
2019-10-05 12:55:01 +01:00
|
|
|
* Get the direct child items within this book.
|
2016-05-01 21:20:50 +01:00
|
|
|
*/
|
2024-02-07 16:37:36 +00:00
|
|
|
public function getDirectVisibleChildren(): Collection
|
2015-08-31 20:11:44 +01:00
|
|
|
{
|
2021-11-22 23:33:55 +00:00
|
|
|
$pages = $this->directPages()->scopes('visible')->get();
|
|
|
|
|
$chapters = $this->chapters()->scopes('visible')->get();
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2020-02-15 19:09:33 +00:00
|
|
|
return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
|
2015-08-31 20:11:44 +01:00
|
|
|
}
|
2025-10-18 13:14:30 +01:00
|
|
|
|
|
|
|
|
public function defaultTemplate(): EntityDefaultTemplate
|
|
|
|
|
{
|
|
|
|
|
return new EntityDefaultTemplate($this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function cover(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Image::class, 'image_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function coverInfo(): EntityCover
|
|
|
|
|
{
|
|
|
|
|
return new EntityCover($this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the sort rule assigned to this container, if existing.
|
|
|
|
|
*/
|
|
|
|
|
public function sortRule(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(SortRule::class);
|
|
|
|
|
}
|
2015-07-12 20:01:42 +01:00
|
|
|
}
|