BookStack/app/Entities/Models/Chapter.php

59 lines
1.2 KiB
PHP
Raw Normal View History

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