BookStack/app/Entities/Models/Chapter.php

53 lines
1.2 KiB
PHP
Raw Normal View History

<?php namespace BookStack\Entities\Models;
2015-07-28 03:17:08 +08:00
use Illuminate\Support\Collection;
/**
* Class Chapter
* @property Collection<Page> $pages
*/
class Chapter extends BookChild
2015-07-28 03:17:08 +08:00
{
public $searchFactor = 1.3;
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.
* @param string $dir
* @return mixed
*/
public function pages($dir = 'ASC')
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.
*/
public function getUrl($path = ''): string
2015-07-28 03:17:08 +08:00
{
$parts = [
'books',
urlencode($this->getAttribute('bookSlug') ?? $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()->visible()
->orderBy('draft', 'desc')
->orderBy('priority', 'asc')
->get();
2019-03-17 23:07:03 +08:00
}
2015-07-28 03:17:08 +08:00
}