BookStack/app/Entities/Models/Chapter.php

59 lines
1.2 KiB
PHP
Raw Normal View History

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