BookStack/app/Entities/Chapter.php

75 lines
1.8 KiB
PHP
Raw Normal View History

<?php namespace BookStack\Entities;
2015-07-28 03:17:08 +08:00
use Illuminate\Support\Collection;
/**
* Class Chapter
* @property Collection<Page> $pages
* @package BookStack\Entities
*/
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'];
2020-05-23 07:28:41 +08:00
protected $hidden = ['restricted', 'pivot'];
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.
* @param string|bool $path
* @return string
*/
public function getUrl($path = false)
2015-07-28 03:17:08 +08:00
{
$bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug;
$fullPath = '/books/' . urlencode($bookSlug) . '/chapter/' . urlencode($this->slug);
if ($path !== false) {
$fullPath .= '/' . trim($path, '/');
}
return url($fullPath);
2015-07-28 03:17:08 +08:00
}
/**
* Get an excerpt of this chapter's description to the specified length or less.
* @param int $length
* @return string
*/
2019-03-17 23:07:03 +08:00
public function getExcerpt(int $length = 100)
{
2019-03-17 23:07:03 +08:00
$description = $this->text ?? $this->description;
return mb_strlen($description) > $length ? mb_substr($description, 0, $length-3) . '...' : $description;
}
/**
* Check if this chapter has any child pages.
* @return bool
*/
public function hasChildren()
{
return count($this->pages) > 0;
}
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
}