2020-11-22 08:17:45 +08:00
|
|
|
<?php namespace BookStack\Entities\Models;
|
2015-07-28 03:17:08 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
use Illuminate\Support\Collection;
|
2019-09-20 07:18:28 +08:00
|
|
|
|
2019-10-05 19:55:01 +08:00
|
|
|
/**
|
|
|
|
* Class Chapter
|
|
|
|
* @property Collection<Page> $pages
|
|
|
|
*/
|
2019-09-20 07:18:28 +08:00
|
|
|
class Chapter extends BookChild
|
2015-07-28 03:17:08 +08:00
|
|
|
{
|
2018-03-25 02:46:31 +08:00
|
|
|
public $searchFactor = 1.3;
|
|
|
|
|
2015-07-28 03:17:08 +08:00
|
|
|
protected $fillable = ['name', 'description', 'priority', 'book_id'];
|
2020-11-28 23:21:54 +08:00
|
|
|
protected $hidden = ['restricted', 'pivot', 'deleted_at'];
|
2015-07-28 03:17:08 +08:00
|
|
|
|
2016-05-02 04:20:50 +08:00
|
|
|
/**
|
|
|
|
* Get the pages that this chapter contains.
|
2017-01-02 19:07:27 +08:00
|
|
|
* @param string $dir
|
2016-05-02 04:20:50 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-01-02 19:07:27 +08:00
|
|
|
public function pages($dir = 'ASC')
|
2015-07-28 03:17:08 +08:00
|
|
|
{
|
2017-01-02 19:07:27 +08:00
|
|
|
return $this->hasMany(Page::class)->orderBy('priority', $dir);
|
2015-07-28 03:17:08 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 04:20:50 +08:00
|
|
|
/**
|
|
|
|
* Get the url of this chapter.
|
|
|
|
*/
|
2020-11-22 09:20:38 +08:00
|
|
|
public function getUrl($path = ''): string
|
2015-07-28 03:17:08 +08:00
|
|
|
{
|
2020-11-22 09:20:38 +08:00
|
|
|
$parts = [
|
|
|
|
'books',
|
|
|
|
urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
|
|
|
|
'chapter',
|
|
|
|
urlencode($this->slug),
|
|
|
|
trim($path, '/'),
|
|
|
|
];
|
|
|
|
|
|
|
|
return url('/' . implode('/', $parts));
|
2015-07-31 05:27:35 +08:00
|
|
|
}
|
|
|
|
|
2019-03-17 23:07:03 +08:00
|
|
|
/**
|
2019-10-05 19:55:01 +08:00
|
|
|
* Get the visible pages in this chapter.
|
2019-03-17 23:07:03 +08:00
|
|
|
*/
|
2019-10-05 19:55:01 +08:00
|
|
|
public function getVisiblePages(): Collection
|
2019-03-17 23:07:03 +08:00
|
|
|
{
|
2019-10-05 19:55:01 +08:00
|
|
|
return $this->pages()->visible()
|
|
|
|
->orderBy('draft', 'desc')
|
|
|
|
->orderBy('priority', 'asc')
|
|
|
|
->get();
|
2019-03-17 23:07:03 +08:00
|
|
|
}
|
2015-07-28 03:17:08 +08:00
|
|
|
}
|