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;
|
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;
|
2018-06-24 13:38:19 +01:00
|
|
|
|
2025-10-18 13:14:30 +01:00
|
|
|
/**
|
|
|
|
|
* @property string $description
|
|
|
|
|
* @property string $description_html
|
|
|
|
|
*/
|
|
|
|
|
class Bookshelf extends Entity implements HasDescriptionInterface, HasCoverInterface
|
2018-06-24 13:38:19 +01:00
|
|
|
{
|
2021-10-30 21:29:59 +01:00
|
|
|
use HasFactory;
|
2025-10-18 13:14:30 +01:00
|
|
|
use ContainerTrait;
|
2018-06-24 13:38:19 +01:00
|
|
|
|
2023-12-18 16:23:40 +00:00
|
|
|
public float $searchFactor = 1.2;
|
2018-06-24 13:38:19 +01:00
|
|
|
|
2025-10-18 13:14:30 +01:00
|
|
|
protected $hidden = ['image_id', 'deleted_at', 'description_html', 'priority', 'default_template_id', 'sort_rule_id', 'entity_id', 'entity_type', 'chapter_id', 'book_id'];
|
|
|
|
|
protected $fillable = ['name'];
|
2020-04-10 15:19:18 +01:00
|
|
|
|
2018-08-27 14:18:09 +01:00
|
|
|
/**
|
|
|
|
|
* Get the books in this shelf.
|
2025-10-18 13:14:30 +01:00
|
|
|
* Should not be used directly since it does not take into account permissions.
|
2018-08-27 14:18:09 +01:00
|
|
|
*/
|
2025-10-18 13:14:30 +01:00
|
|
|
public function books(): BelongsToMany
|
2018-08-27 14:18:09 +01:00
|
|
|
{
|
2019-04-15 20:43:25 +01:00
|
|
|
return $this->belongsToMany(Book::class, 'bookshelves_books', 'bookshelf_id', 'book_id')
|
2025-10-18 13:14:30 +01:00
|
|
|
->select(['entities.*', 'entity_container_data.*'])
|
2019-04-15 20:43:25 +01:00
|
|
|
->withPivot('order')
|
|
|
|
|
->orderBy('order', 'asc');
|
2018-08-27 14:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
2019-10-05 12:55:01 +01:00
|
|
|
/**
|
|
|
|
|
* Related books that are visible to the current user.
|
|
|
|
|
*/
|
|
|
|
|
public function visibleBooks(): BelongsToMany
|
|
|
|
|
{
|
2021-11-22 23:33:55 +00:00
|
|
|
return $this->books()->scopes('visible');
|
2019-10-05 12:55:01 +01:00
|
|
|
}
|
|
|
|
|
|
2018-06-24 13:38:19 +01:00
|
|
|
/**
|
|
|
|
|
* Get the url for this bookshelf.
|
|
|
|
|
*/
|
2020-11-22 01:20:38 +00:00
|
|
|
public function getUrl(string $path = ''): string
|
2018-06-24 13:38:19 +01:00
|
|
|
{
|
2020-11-22 01:20:38 +00:00
|
|
|
return url('/shelves/' . implode('/', [urlencode($this->slug), trim($path, '/')]));
|
2018-06-24 13:38:19 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-07 18:28:11 +01:00
|
|
|
/**
|
|
|
|
|
* Check if this shelf contains the given book.
|
|
|
|
|
*/
|
2019-10-05 12:55:01 +01:00
|
|
|
public function contains(Book $book): bool
|
2019-04-07 18:28:11 +01:00
|
|
|
{
|
|
|
|
|
return $this->books()->where('id', '=', $book->id)->count() > 0;
|
|
|
|
|
}
|
2019-09-19 18:20:09 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add a book to the end of this shelf.
|
|
|
|
|
*/
|
2025-10-18 13:14:30 +01:00
|
|
|
public function appendBook(Book $book): void
|
2019-09-19 18:20:09 +01:00
|
|
|
{
|
2019-10-05 12:55:01 +01:00
|
|
|
if ($this->contains($book)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-09-19 18:20:09 +01:00
|
|
|
|
2019-10-05 12:55:01 +01:00
|
|
|
$maxOrder = $this->books()->max('order');
|
|
|
|
|
$this->books()->attach($book->id, ['order' => $maxOrder + 1]);
|
2019-09-19 18:20:09 +01:00
|
|
|
}
|
2022-10-09 16:36:03 +01:00
|
|
|
|
2025-10-18 13:14:30 +01:00
|
|
|
public function coverInfo(): EntityCover
|
2022-10-09 16:36:03 +01:00
|
|
|
{
|
2025-10-18 13:14:30 +01:00
|
|
|
return new EntityCover($this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function cover(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Image::class, 'image_id');
|
2022-10-09 16:36:03 +01:00
|
|
|
}
|
2018-06-24 13:38:19 +01:00
|
|
|
}
|