BookStack/app/Entities/Models/Chapter.php

62 lines
1.4 KiB
PHP
Raw Normal View History

<?php namespace BookStack\Entities\Models;
2015-07-27 15:17:08 -04:00
use Illuminate\Support\Collection;
/**
* Class Chapter
* @property Collection<Page> $pages
*/
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.
* @param string $dir
* @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));
}
/**
* Check if this chapter has any child pages.
* @return bool
*/
public function hasChildren()
{
return count($this->pages) > 0;
}
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
}