*
* @property int $book_id
* @property int $priority
+ * @property string $book_slug
* @property Book $book
*
* @method Builder whereSlugs(string $bookSlug, string $childSlug)
*/
abstract class BookChild extends Entity
{
+
+ protected static function boot()
+ {
+ parent::boot();
+
+ // Load book slugs onto these models by default during query-time
+ static::addGlobalScope('book_slug', function(Builder $builder) {
+ $builder->addSelect(['book_slug' => function($builder) {
+ $builder->select('slug')
+ ->from('books')
+ ->whereColumn('books.id', '=', 'book_id');
+ }]);
+ });
+ }
/**
- * Scope a query to find items where the the child has the given childSlug
+ * Scope a query to find items where the child has the given childSlug
* where its parent has the bookSlug.
*/
public function scopeWhereSlugs(Builder $query, string $bookSlug, string $childSlug)
{
$parts = [
'books',
- urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
+ urlencode($this->book_slug ?? $this->book->slug),
'chapter',
urlencode($this->slug),
trim($path, '/'),
*/
class Page extends BookChild
{
- protected $fillable = ['name', 'priority', 'markdown'];
+ public static $listAttributes = ['name', 'id', 'slug', 'book_id', 'text', 'created_at', 'updated_at'];
+ public static $contentAttributes = ['name', 'id', 'slug', 'book_id', 'html', 'text', 'created_at', 'updated_at'];
- protected $simpleAttributes = ['name', 'id', 'slug'];
+ protected $fillable = ['name', 'priority', 'markdown'];
public $textField = 'text';
return parent::scopeVisible($query);
}
- /**
- * Converts this page into a simplified array.
- *
- * @return mixed
- */
- public function toSimpleArray()
- {
- $array = array_intersect_key($this->toArray(), array_flip($this->simpleAttributes));
- $array['url'] = $this->getUrl();
-
- return $array;
- }
-
/**
* Get the chapter that this page is in, If applicable.
*
{
$parts = [
'books',
- urlencode($this->getAttribute('bookSlug') ?? $this->book->slug),
+ urlencode($this->book_slug ?? $this->book->slug),
$this->draft ? 'draft' : 'page',
$this->draft ? $this->id : urlencode($this->slug),
trim($path, '/'),
/**
* Get the visible pages within this book.
*/
- protected function getPages(bool $showDrafts = false): Collection
+ protected function getPages(bool $showDrafts = false, bool $getPageContent = false): Collection
{
- $query = Page::visible()->where('book_id', '=', $this->book->id);
+ $query = Page::visible()
+ ->select($getPageContent ? Page::$contentAttributes : Page::$listAttributes)
+ ->where('book_id', '=', $this->book->id);
if (!$showDrafts) {
$query->where('draft', '=', false);
use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Repos\BookshelfRepo;
use BookStack\Entities\Tools\PageContent;
-use Views;
class HomeController extends Controller
{
->where('draft', false)
->orderBy('updated_at', 'desc')
->take($favourites->count() > 0 ? 6 : 12)
+ ->select(Page::$listAttributes)
->get();
$homepageOptions = ['default', 'books', 'bookshelves', 'page'];