2015-07-12 15:01:42 -04:00
|
|
|
<?php
|
|
|
|
|
2015-09-10 14:31:09 -04:00
|
|
|
namespace BookStack;
|
2015-07-12 15:01:42 -04:00
|
|
|
|
2015-08-16 09:51:45 -04:00
|
|
|
class Book extends Entity
|
2015-07-12 15:01:42 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
protected $fillable = ['name', 'description'];
|
|
|
|
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return '/books/' . $this->slug;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEditUrl()
|
|
|
|
{
|
|
|
|
return $this->getUrl() . '/edit';
|
|
|
|
}
|
|
|
|
|
2015-07-12 16:31:15 -04:00
|
|
|
public function pages()
|
|
|
|
{
|
2015-09-10 14:31:09 -04:00
|
|
|
return $this->hasMany('BookStack\Page');
|
2015-07-12 16:31:15 -04:00
|
|
|
}
|
|
|
|
|
2015-07-27 15:17:08 -04:00
|
|
|
public function chapters()
|
|
|
|
{
|
2015-09-10 14:31:09 -04:00
|
|
|
return $this->hasMany('BookStack\Chapter');
|
2015-07-27 15:17:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function children()
|
|
|
|
{
|
2015-07-30 17:27:35 -04:00
|
|
|
$pages = $this->pages()->where('chapter_id', '=', 0)->get();
|
2015-07-27 15:17:08 -04:00
|
|
|
$chapters = $this->chapters()->get();
|
2015-07-30 18:18:48 -04:00
|
|
|
foreach($chapters as $chapter) {
|
|
|
|
$pages->push($chapter);
|
|
|
|
}
|
|
|
|
return $pages->sortBy('priority');
|
2015-07-27 15:17:08 -04:00
|
|
|
}
|
|
|
|
|
2015-08-31 15:11:44 -04:00
|
|
|
public function getExcerpt($length = 100)
|
|
|
|
{
|
|
|
|
return strlen($this->description) > $length ? substr($this->description, 0, $length-3) . '...' : $this->description;
|
|
|
|
}
|
|
|
|
|
2015-07-12 15:01:42 -04:00
|
|
|
}
|