BookStack/app/Entities/Models/Chapter.php

62 lines
1.4 KiB
PHP
Raw Normal View History

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