BookStack/app/Entities/Models/Chapter.php

62 lines
1.4 KiB
PHP
Raw Normal View History

2021-06-26 23:23:15 +08:00
<?php
namespace BookStack\Entities\Models;
2015-07-28 03:17:08 +08:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2021-11-06 08:32:01 +08:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
/**
2021-06-26 23:23:15 +08:00
* Class Chapter.
*
* @property Collection<Page> $pages
2021-11-07 06:00:33 +08:00
* @property string $description
*/
class Chapter extends BookChild
2015-07-28 03:17:08 +08:00
{
use HasFactory;
public $searchFactor = 1.2;
2015-07-28 03:17:08 +08:00
protected $fillable = ['name', 'description', 'priority', 'book_id'];
protected $hidden = ['restricted', 'pivot', 'deleted_at'];
2015-07-28 03:17:08 +08:00
/**
* Get the pages that this chapter contains.
*
* @return HasMany<Page>
*/
2021-11-06 08:32:01 +08:00
public function pages(string $dir = 'ASC'): HasMany
2015-07-28 03:17:08 +08:00
{
return $this->hasMany(Page::class)->orderBy('priority', $dir);
2015-07-28 03:17:08 +08:00
}
/**
* Get the url of this chapter.
*/
2021-11-06 08:32:01 +08:00
public function getUrl(string $path = ''): string
2015-07-28 03:17:08 +08:00
{
$parts = [
'books',
urlencode($this->book_slug ?? $this->book->slug),
'chapter',
urlencode($this->slug),
trim($path, '/'),
];
return url('/' . implode('/', $parts));
}
2019-03-17 23:07:03 +08:00
/**
* Get the visible pages in this chapter.
2019-03-17 23:07:03 +08:00
*/
public function getVisiblePages(): Collection
2019-03-17 23:07:03 +08:00
{
return $this->pages()
->scopes('visible')
->orderBy('draft', 'desc')
->orderBy('priority', 'asc')
->get();
2019-03-17 23:07:03 +08:00
}
2015-07-28 03:17:08 +08:00
}