2018-10-13 06:27:55 -04:00
|
|
|
<?php namespace BookStack\Entities\Repos;
|
2016-02-20 07:37:06 -05:00
|
|
|
|
2018-09-25 11:58:03 -04:00
|
|
|
use BookStack\Actions\TagRepo;
|
|
|
|
use BookStack\Actions\ViewService;
|
|
|
|
use BookStack\Auth\Permissions\PermissionService;
|
2018-09-25 13:00:40 -04:00
|
|
|
use BookStack\Auth\User;
|
2018-10-13 06:27:55 -04:00
|
|
|
use BookStack\Entities\Book;
|
|
|
|
use BookStack\Entities\Bookshelf;
|
|
|
|
use BookStack\Entities\Chapter;
|
|
|
|
use BookStack\Entities\Entity;
|
|
|
|
use BookStack\Entities\EntityProvider;
|
|
|
|
use BookStack\Entities\Page;
|
|
|
|
use BookStack\Entities\SearchService;
|
2017-01-01 11:05:44 -05:00
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2017-10-15 14:14:46 -04:00
|
|
|
use BookStack\Exceptions\NotifyException;
|
2018-09-25 07:30:50 -04:00
|
|
|
use BookStack\Uploads\AttachmentService;
|
2017-01-02 06:07:27 -05:00
|
|
|
use DOMDocument;
|
2018-12-07 13:33:32 -05:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2018-09-25 13:00:40 -04:00
|
|
|
use Illuminate\Http\Request;
|
2016-08-26 15:20:58 -04:00
|
|
|
use Illuminate\Support\Collection;
|
2016-02-20 07:37:06 -05:00
|
|
|
|
|
|
|
class EntityRepo
|
|
|
|
{
|
2017-01-02 06:07:27 -05:00
|
|
|
|
2017-01-01 11:05:44 -05:00
|
|
|
/**
|
2018-09-25 13:00:40 -04:00
|
|
|
* @var EntityProvider
|
2017-01-01 11:05:44 -05:00
|
|
|
*/
|
2018-09-25 13:00:40 -04:00
|
|
|
protected $entityProvider;
|
2017-01-01 11:05:44 -05:00
|
|
|
|
2016-03-05 14:00:26 -05:00
|
|
|
/**
|
2016-05-01 16:20:50 -04:00
|
|
|
* @var PermissionService
|
2016-03-05 14:00:26 -05:00
|
|
|
*/
|
2016-05-01 16:20:50 -04:00
|
|
|
protected $permissionService;
|
2016-02-20 07:37:06 -05:00
|
|
|
|
2017-01-01 11:05:44 -05:00
|
|
|
/**
|
|
|
|
* @var ViewService
|
|
|
|
*/
|
|
|
|
protected $viewService;
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
/**
|
2018-09-25 13:00:40 -04:00
|
|
|
* @var TagRepo
|
2017-01-02 06:07:27 -05:00
|
|
|
*/
|
|
|
|
protected $tagRepo;
|
|
|
|
|
2016-05-15 08:41:18 -04:00
|
|
|
/**
|
2017-03-19 08:48:44 -04:00
|
|
|
* @var SearchService
|
2016-05-15 08:41:18 -04:00
|
|
|
*/
|
2017-03-19 08:48:44 -04:00
|
|
|
protected $searchService;
|
2016-05-15 08:41:18 -04:00
|
|
|
|
2016-02-20 07:37:06 -05:00
|
|
|
/**
|
2017-03-19 08:48:44 -04:00
|
|
|
* EntityRepo constructor.
|
2018-09-25 13:00:40 -04:00
|
|
|
* @param EntityProvider $entityProvider
|
2017-01-02 06:07:27 -05:00
|
|
|
* @param ViewService $viewService
|
|
|
|
* @param PermissionService $permissionService
|
2018-09-25 13:00:40 -04:00
|
|
|
* @param TagRepo $tagRepo
|
2017-03-19 08:48:44 -04:00
|
|
|
* @param SearchService $searchService
|
2016-02-20 07:37:06 -05:00
|
|
|
*/
|
2017-01-02 06:07:27 -05:00
|
|
|
public function __construct(
|
2018-09-25 13:00:40 -04:00
|
|
|
EntityProvider $entityProvider,
|
2018-01-28 11:58:52 -05:00
|
|
|
ViewService $viewService,
|
|
|
|
PermissionService $permissionService,
|
|
|
|
TagRepo $tagRepo,
|
|
|
|
SearchService $searchService
|
|
|
|
) {
|
2018-09-25 13:00:40 -04:00
|
|
|
$this->entityProvider = $entityProvider;
|
2017-01-02 06:07:27 -05:00
|
|
|
$this->viewService = $viewService;
|
|
|
|
$this->permissionService = $permissionService;
|
|
|
|
$this->tagRepo = $tagRepo;
|
2017-03-19 08:48:44 -04:00
|
|
|
$this->searchService = $searchService;
|
2016-02-20 07:37:06 -05:00
|
|
|
}
|
|
|
|
|
2017-01-01 11:05:44 -05:00
|
|
|
/**
|
|
|
|
* Base query for searching entities via permission system
|
|
|
|
* @param string $type
|
|
|
|
* @param bool $allowDrafts
|
2018-09-25 13:00:40 -04:00
|
|
|
* @param string $permission
|
2017-01-01 11:05:44 -05:00
|
|
|
* @return \Illuminate\Database\Query\Builder
|
|
|
|
*/
|
2017-12-31 06:14:46 -05:00
|
|
|
protected function entityQuery($type, $allowDrafts = false, $permission = 'view')
|
2017-01-01 11:05:44 -05:00
|
|
|
{
|
2018-09-25 13:00:40 -04:00
|
|
|
$q = $this->permissionService->enforceEntityRestrictions($type, $this->entityProvider->get($type), $permission);
|
2017-01-01 11:05:44 -05:00
|
|
|
if (strtolower($type) === 'page' && !$allowDrafts) {
|
|
|
|
$q = $q->where('draft', '=', false);
|
2016-03-06 08:17:46 -05:00
|
|
|
}
|
2017-01-01 11:05:44 -05:00
|
|
|
return $q;
|
2016-02-20 07:37:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-01 11:05:44 -05:00
|
|
|
* Check if an entity with the given id exists.
|
|
|
|
* @param $type
|
|
|
|
* @param $id
|
|
|
|
* @return bool
|
2016-02-20 07:37:06 -05:00
|
|
|
*/
|
2017-01-01 11:05:44 -05:00
|
|
|
public function exists($type, $id)
|
2016-02-20 07:37:06 -05:00
|
|
|
{
|
2017-01-01 11:05:44 -05:00
|
|
|
return $this->entityQuery($type)->where('id', '=', $id)->exists();
|
2016-02-20 07:37:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-01 11:05:44 -05:00
|
|
|
* Get an entity by ID
|
|
|
|
* @param string $type
|
|
|
|
* @param integer $id
|
|
|
|
* @param bool $allowDrafts
|
2017-08-28 08:55:39 -04:00
|
|
|
* @param bool $ignorePermissions
|
2018-09-25 07:30:50 -04:00
|
|
|
* @return \BookStack\Entities\Entity
|
2017-01-01 11:05:44 -05:00
|
|
|
*/
|
2017-08-28 08:55:39 -04:00
|
|
|
public function getById($type, $id, $allowDrafts = false, $ignorePermissions = false)
|
2017-01-01 11:05:44 -05:00
|
|
|
{
|
2018-09-25 13:00:40 -04:00
|
|
|
$query = $this->entityQuery($type, $allowDrafts);
|
|
|
|
|
2017-08-28 08:55:39 -04:00
|
|
|
if ($ignorePermissions) {
|
2018-09-25 13:00:40 -04:00
|
|
|
$query = $this->entityProvider->get($type)->newQuery();
|
2017-08-28 08:55:39 -04:00
|
|
|
}
|
2018-09-25 13:00:40 -04:00
|
|
|
|
|
|
|
return $query->find($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param []int $ids
|
|
|
|
* @param bool $allowDrafts
|
|
|
|
* @param bool $ignorePermissions
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|Collection
|
|
|
|
*/
|
|
|
|
public function getManyById($type, $ids, $allowDrafts = false, $ignorePermissions = false)
|
|
|
|
{
|
|
|
|
$query = $this->entityQuery($type, $allowDrafts);
|
|
|
|
|
|
|
|
if ($ignorePermissions) {
|
|
|
|
$query = $this->entityProvider->get($type)->newQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query->whereIn('id', $ids)->get();
|
2017-01-01 11:05:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an entity by its url slug.
|
|
|
|
* @param string $type
|
|
|
|
* @param string $slug
|
|
|
|
* @param string|bool $bookSlug
|
2018-09-25 07:30:50 -04:00
|
|
|
* @return \BookStack\Entities\Entity
|
2017-01-01 11:05:44 -05:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function getBySlug($type, $slug, $bookSlug = false)
|
|
|
|
{
|
|
|
|
$q = $this->entityQuery($type)->where('slug', '=', $slug);
|
2017-01-01 16:21:11 -05:00
|
|
|
|
2017-01-01 11:05:44 -05:00
|
|
|
if (strtolower($type) === 'chapter' || strtolower($type) === 'page') {
|
2018-01-28 11:58:52 -05:00
|
|
|
$q = $q->where('book_id', '=', function ($query) use ($bookSlug) {
|
2017-01-01 11:05:44 -05:00
|
|
|
$query->select('id')
|
2018-09-25 13:00:40 -04:00
|
|
|
->from($this->entityProvider->book->getTable())
|
2017-01-01 11:05:44 -05:00
|
|
|
->where('slug', '=', $bookSlug)->limit(1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
$entity = $q->first();
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($entity === null) {
|
|
|
|
throw new NotFoundException(trans('errors.' . strtolower($type) . '_not_found'));
|
|
|
|
}
|
2017-01-01 11:05:44 -05:00
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
|
2017-01-01 11:05:44 -05:00
|
|
|
/**
|
2017-12-31 06:14:46 -05:00
|
|
|
* Get all entities of a type with the given permission, limited by count unless count is false.
|
2017-01-01 11:05:44 -05:00
|
|
|
* @param string $type
|
|
|
|
* @param integer|bool $count
|
2017-12-31 06:14:46 -05:00
|
|
|
* @param string $permission
|
2017-01-01 11:05:44 -05:00
|
|
|
* @return Collection
|
|
|
|
*/
|
2017-12-31 06:14:46 -05:00
|
|
|
public function getAll($type, $count = 20, $permission = 'view')
|
2017-01-01 11:05:44 -05:00
|
|
|
{
|
2017-12-31 06:14:46 -05:00
|
|
|
$q = $this->entityQuery($type, false, $permission)->orderBy('name', 'asc');
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($count !== false) {
|
|
|
|
$q = $q->take($count);
|
|
|
|
}
|
2017-01-01 11:05:44 -05:00
|
|
|
return $q->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all entities in a paginated format
|
|
|
|
* @param $type
|
|
|
|
* @param int $count
|
2018-12-07 13:33:32 -05:00
|
|
|
* @param string $sort
|
|
|
|
* @param string $order
|
2019-02-16 12:13:01 -05:00
|
|
|
* @param null|callable $queryAddition
|
2017-01-01 11:05:44 -05:00
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
|
|
|
*/
|
2019-02-16 12:13:01 -05:00
|
|
|
public function getAllPaginated($type, int $count = 10, string $sort = 'name', string $order = 'asc', $queryAddition = null)
|
2017-01-01 11:05:44 -05:00
|
|
|
{
|
2018-12-07 13:33:32 -05:00
|
|
|
$query = $this->entityQuery($type);
|
|
|
|
$query = $this->addSortToQuery($query, $sort, $order);
|
2019-02-16 12:13:01 -05:00
|
|
|
if ($queryAddition) {
|
|
|
|
$queryAddition($query);
|
|
|
|
}
|
2018-12-07 13:33:32 -05:00
|
|
|
return $query->paginate($count);
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:13:01 -05:00
|
|
|
/**
|
|
|
|
* Add sorting operations to an entity query.
|
|
|
|
* @param Builder $query
|
|
|
|
* @param string $sort
|
|
|
|
* @param string $order
|
|
|
|
* @return Builder
|
|
|
|
*/
|
2018-12-07 13:33:32 -05:00
|
|
|
protected function addSortToQuery(Builder $query, string $sort = 'name', string $order = 'asc')
|
|
|
|
{
|
|
|
|
$order = ($order === 'asc') ? 'asc' : 'desc';
|
|
|
|
$propertySorts = ['name', 'created_at', 'updated_at'];
|
|
|
|
|
|
|
|
if (in_array($sort, $propertySorts)) {
|
|
|
|
return $query->orderBy($sort, $order);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
2017-01-01 11:05:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the most recently created entities of the given type.
|
|
|
|
* @param string $type
|
2016-03-06 08:17:46 -05:00
|
|
|
* @param int $count
|
|
|
|
* @param int $page
|
2017-01-01 11:05:44 -05:00
|
|
|
* @param bool|callable $additionalQuery
|
2017-04-16 05:47:44 -04:00
|
|
|
* @return Collection
|
2016-02-20 07:37:06 -05:00
|
|
|
*/
|
2017-01-01 11:05:44 -05:00
|
|
|
public function getRecentlyCreated($type, $count = 20, $page = 0, $additionalQuery = false)
|
2016-02-20 07:37:06 -05:00
|
|
|
{
|
2018-09-25 13:00:40 -04:00
|
|
|
$query = $this->permissionService->enforceEntityRestrictions($type, $this->entityProvider->get($type))
|
2017-01-01 11:05:44 -05:00
|
|
|
->orderBy('created_at', 'desc');
|
2018-01-28 11:58:52 -05:00
|
|
|
if (strtolower($type) === 'page') {
|
|
|
|
$query = $query->where('draft', '=', false);
|
|
|
|
}
|
2016-03-06 08:17:46 -05:00
|
|
|
if ($additionalQuery !== false && is_callable($additionalQuery)) {
|
|
|
|
$additionalQuery($query);
|
|
|
|
}
|
2017-01-01 11:05:44 -05:00
|
|
|
return $query->skip($page * $count)->take($count)->get();
|
2016-03-06 08:17:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-01 11:05:44 -05:00
|
|
|
* Get the most recently updated entities of the given type.
|
|
|
|
* @param string $type
|
2016-03-06 08:17:46 -05:00
|
|
|
* @param int $count
|
|
|
|
* @param int $page
|
2017-01-01 11:05:44 -05:00
|
|
|
* @param bool|callable $additionalQuery
|
2017-04-16 05:47:44 -04:00
|
|
|
* @return Collection
|
2016-03-06 08:17:46 -05:00
|
|
|
*/
|
2017-01-01 11:05:44 -05:00
|
|
|
public function getRecentlyUpdated($type, $count = 20, $page = 0, $additionalQuery = false)
|
2016-03-06 08:17:46 -05:00
|
|
|
{
|
2018-09-25 13:00:40 -04:00
|
|
|
$query = $this->permissionService->enforceEntityRestrictions($type, $this->entityProvider->get($type))
|
2017-01-01 11:05:44 -05:00
|
|
|
->orderBy('updated_at', 'desc');
|
2018-01-28 11:58:52 -05:00
|
|
|
if (strtolower($type) === 'page') {
|
|
|
|
$query = $query->where('draft', '=', false);
|
|
|
|
}
|
2016-03-06 08:17:46 -05:00
|
|
|
if ($additionalQuery !== false && is_callable($additionalQuery)) {
|
|
|
|
$additionalQuery($query);
|
|
|
|
}
|
|
|
|
return $query->skip($page * $count)->take($count)->get();
|
2016-02-20 07:37:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-01 11:05:44 -05:00
|
|
|
* Get the most recently viewed entities.
|
|
|
|
* @param string|bool $type
|
|
|
|
* @param int $count
|
|
|
|
* @param int $page
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getRecentlyViewed($type, $count = 10, $page = 0)
|
|
|
|
{
|
2018-09-25 13:00:40 -04:00
|
|
|
$filter = is_bool($type) ? false : $this->entityProvider->get($type);
|
2017-01-01 11:05:44 -05:00
|
|
|
return $this->viewService->getUserRecentlyViewed($count, $page, $filter);
|
|
|
|
}
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
/**
|
|
|
|
* Get the latest pages added to the system with pagination.
|
|
|
|
* @param string $type
|
|
|
|
* @param int $count
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getRecentlyCreatedPaginated($type, $count = 20)
|
|
|
|
{
|
|
|
|
return $this->entityQuery($type)->orderBy('created_at', 'desc')->paginate($count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the latest pages added to the system with pagination.
|
|
|
|
* @param string $type
|
|
|
|
* @param int $count
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getRecentlyUpdatedPaginated($type, $count = 20)
|
|
|
|
{
|
|
|
|
return $this->entityQuery($type)->orderBy('updated_at', 'desc')->paginate($count);
|
|
|
|
}
|
|
|
|
|
2017-01-01 11:05:44 -05:00
|
|
|
/**
|
|
|
|
* Get the most popular entities base on all views.
|
2019-03-30 12:54:15 -04:00
|
|
|
* @param string $type
|
2017-01-01 11:05:44 -05:00
|
|
|
* @param int $count
|
2016-02-20 07:37:06 -05:00
|
|
|
* @param int $page
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-03-30 12:54:15 -04:00
|
|
|
public function getPopular(string $type, int $count = 10, int $page = 0)
|
2016-02-20 07:37:06 -05:00
|
|
|
{
|
2019-03-30 12:54:15 -04:00
|
|
|
return $this->viewService->getPopular($count, $page, $type);
|
2016-03-05 14:00:26 -05:00
|
|
|
}
|
|
|
|
|
2016-03-13 08:04:08 -04:00
|
|
|
/**
|
|
|
|
* Get draft pages owned by the current user.
|
|
|
|
* @param int $count
|
|
|
|
* @param int $page
|
2018-09-25 13:00:40 -04:00
|
|
|
* @return Collection
|
2016-03-13 08:04:08 -04:00
|
|
|
*/
|
|
|
|
public function getUserDraftPages($count = 20, $page = 0)
|
|
|
|
{
|
2018-09-25 13:00:40 -04:00
|
|
|
return $this->entityProvider->page->where('draft', '=', true)
|
2016-09-29 07:43:46 -04:00
|
|
|
->where('created_by', '=', user()->id)
|
2016-03-13 08:04:08 -04:00
|
|
|
->orderBy('updated_at', 'desc')
|
|
|
|
->skip($count * $page)->take($count)->get();
|
|
|
|
}
|
|
|
|
|
2018-09-25 13:00:40 -04:00
|
|
|
/**
|
|
|
|
* Get the number of entities the given user has created.
|
|
|
|
* @param string $type
|
|
|
|
* @param User $user
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getUserTotalCreated(string $type, User $user)
|
|
|
|
{
|
|
|
|
return $this->entityProvider->get($type)
|
|
|
|
->where('created_by', '=', $user->id)->count();
|
|
|
|
}
|
|
|
|
|
2018-09-16 14:34:09 -04:00
|
|
|
/**
|
|
|
|
* Get the child items for a chapter sorted by priority but
|
|
|
|
* with draft items floated to the top.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Bookshelf $bookshelf
|
2018-09-16 14:34:09 -04:00
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[]
|
|
|
|
*/
|
|
|
|
public function getBookshelfChildren(Bookshelf $bookshelf)
|
|
|
|
{
|
|
|
|
return $this->permissionService->enforceEntityRestrictions('book', $bookshelf->books())->get();
|
|
|
|
}
|
|
|
|
|
2019-02-24 10:57:35 -05:00
|
|
|
/**
|
|
|
|
* Get the direct children of a book.
|
|
|
|
* @param Book $book
|
|
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
|
|
*/
|
|
|
|
public function getBookDirectChildren(Book $book)
|
|
|
|
{
|
|
|
|
$pages = $this->permissionService->enforceEntityRestrictions('page', $book->directPages())->get();
|
|
|
|
$chapters = $this->permissionService->enforceEntityRestrictions('chapters', $book->chapters())->get();
|
|
|
|
return collect()->concat($pages)->concat($chapters)->sortBy('priority')->sortByDesc('draft');
|
|
|
|
}
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
/**
|
|
|
|
* Get all child objects of a book.
|
|
|
|
* Returns a sorted collection of Pages and Chapters.
|
|
|
|
* Loads the book slug onto child elements to prevent access database access for getting the slug.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Book $book
|
2017-01-02 06:07:27 -05:00
|
|
|
* @param bool $filterDrafts
|
2017-02-26 08:26:51 -05:00
|
|
|
* @param bool $renderPages
|
2017-01-02 06:07:27 -05:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-02-26 08:26:51 -05:00
|
|
|
public function getBookChildren(Book $book, $filterDrafts = false, $renderPages = false)
|
2017-01-02 06:07:27 -05:00
|
|
|
{
|
2017-02-26 08:26:51 -05:00
|
|
|
$q = $this->permissionService->bookChildrenQuery($book->id, $filterDrafts, $renderPages)->get();
|
2017-01-02 06:07:27 -05:00
|
|
|
$entities = [];
|
|
|
|
$parents = [];
|
|
|
|
$tree = [];
|
|
|
|
|
|
|
|
foreach ($q as $index => $rawEntity) {
|
2018-09-25 13:00:40 -04:00
|
|
|
if ($rawEntity->entity_type === $this->entityProvider->page->getMorphClass()) {
|
|
|
|
$entities[$index] = $this->entityProvider->page->newFromBuilder($rawEntity);
|
2017-02-26 08:26:51 -05:00
|
|
|
if ($renderPages) {
|
2017-04-16 05:47:44 -04:00
|
|
|
$entities[$index]->html = $rawEntity->html;
|
2017-02-26 08:26:51 -05:00
|
|
|
$entities[$index]->html = $this->renderPage($entities[$index]);
|
|
|
|
};
|
2018-09-25 13:00:40 -04:00
|
|
|
} else if ($rawEntity->entity_type === $this->entityProvider->chapter->getMorphClass()) {
|
|
|
|
$entities[$index] = $this->entityProvider->chapter->newFromBuilder($rawEntity);
|
2017-01-02 06:07:27 -05:00
|
|
|
$key = $entities[$index]->entity_type . ':' . $entities[$index]->id;
|
|
|
|
$parents[$key] = $entities[$index];
|
|
|
|
$parents[$key]->setAttribute('pages', collect());
|
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($entities[$index]->chapter_id === 0 || $entities[$index]->chapter_id === '0') {
|
|
|
|
$tree[] = $entities[$index];
|
|
|
|
}
|
2017-01-02 06:07:27 -05:00
|
|
|
$entities[$index]->book = $book;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($entities as $entity) {
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($entity->chapter_id === 0 || $entity->chapter_id === '0') {
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-25 13:00:40 -04:00
|
|
|
$parentKey = $this->entityProvider->chapter->getMorphClass() . ':' . $entity->chapter_id;
|
2017-04-22 08:39:34 -04:00
|
|
|
if (!isset($parents[$parentKey])) {
|
|
|
|
$tree[] = $entity;
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-02 06:07:27 -05:00
|
|
|
$chapter = $parents[$parentKey];
|
|
|
|
$chapter->pages->push($entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return collect($tree);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the child items for a chapter sorted by priority but
|
|
|
|
* with draft items floated to the top.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Chapter $chapter
|
2017-04-16 05:47:44 -04:00
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[]
|
2017-01-02 06:07:27 -05:00
|
|
|
*/
|
|
|
|
public function getChapterChildren(Chapter $chapter)
|
|
|
|
{
|
|
|
|
return $this->permissionService->enforceEntityRestrictions('page', $chapter->pages())
|
|
|
|
->orderBy('draft', 'DESC')->orderBy('priority', 'ASC')->get();
|
|
|
|
}
|
|
|
|
|
2017-01-01 11:57:47 -05:00
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
/**
|
|
|
|
* Get the next sequential priority for a new child element in the given book.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Book $book
|
2017-01-02 06:07:27 -05:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getNewBookPriority(Book $book)
|
|
|
|
{
|
|
|
|
$lastElem = $this->getBookChildren($book)->pop();
|
|
|
|
return $lastElem ? $lastElem->priority + 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a new priority for a new page to be added to the given chapter.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Chapter $chapter
|
2017-01-02 06:07:27 -05:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getNewChapterPriority(Chapter $chapter)
|
|
|
|
{
|
|
|
|
$lastPage = $chapter->pages('DESC')->first();
|
|
|
|
return $lastPage !== null ? $lastPage->priority + 1 : 0;
|
|
|
|
}
|
|
|
|
|
2017-01-01 11:57:47 -05:00
|
|
|
/**
|
|
|
|
* Find a suitable slug for an entity.
|
|
|
|
* @param string $type
|
|
|
|
* @param string $name
|
|
|
|
* @param bool|integer $currentId
|
|
|
|
* @param bool|integer $bookId Only pass if type is not a book
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function findSuitableSlug($type, $name, $currentId = false, $bookId = false)
|
|
|
|
{
|
|
|
|
$slug = $this->nameToSlug($name);
|
|
|
|
while ($this->slugExists($type, $slug, $currentId, $bookId)) {
|
|
|
|
$slug .= '-' . substr(md5(rand(1, 500)), 0, 3);
|
|
|
|
}
|
|
|
|
return $slug;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a slug already exists in the database.
|
|
|
|
* @param string $type
|
|
|
|
* @param string $slug
|
|
|
|
* @param bool|integer $currentId
|
|
|
|
* @param bool|integer $bookId
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function slugExists($type, $slug, $currentId = false, $bookId = false)
|
|
|
|
{
|
2018-09-25 13:00:40 -04:00
|
|
|
$query = $this->entityProvider->get($type)->where('slug', '=', $slug);
|
2017-01-01 11:57:47 -05:00
|
|
|
if (strtolower($type) === 'page' || strtolower($type) === 'chapter') {
|
|
|
|
$query = $query->where('book_id', '=', $bookId);
|
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($currentId) {
|
|
|
|
$query = $query->where('id', '!=', $currentId);
|
|
|
|
}
|
2017-01-01 11:57:47 -05:00
|
|
|
return $query->count() > 0;
|
|
|
|
}
|
|
|
|
|
2016-03-05 14:00:26 -05:00
|
|
|
/**
|
|
|
|
* Updates entity restrictions from a request
|
2018-09-25 13:00:40 -04:00
|
|
|
* @param Request $request
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Entity $entity
|
2018-09-25 13:00:40 -04:00
|
|
|
* @throws \Throwable
|
2016-03-05 14:00:26 -05:00
|
|
|
*/
|
2018-09-25 13:00:40 -04:00
|
|
|
public function updateEntityPermissionsFromRequest(Request $request, Entity $entity)
|
2016-03-05 14:00:26 -05:00
|
|
|
{
|
2017-11-19 10:56:06 -05:00
|
|
|
$entity->restricted = $request->get('restricted', '') === 'true';
|
2016-05-01 16:20:50 -04:00
|
|
|
$entity->permissions()->delete();
|
2017-11-19 10:56:06 -05:00
|
|
|
|
|
|
|
if ($request->filled('restrictions')) {
|
2016-03-05 14:00:26 -05:00
|
|
|
foreach ($request->get('restrictions') as $roleId => $restrictions) {
|
|
|
|
foreach ($restrictions as $action => $value) {
|
2016-05-01 16:20:50 -04:00
|
|
|
$entity->permissions()->create([
|
2016-03-05 14:00:26 -05:00
|
|
|
'role_id' => $roleId,
|
2016-03-06 08:17:46 -05:00
|
|
|
'action' => strtolower($action)
|
2016-03-05 14:00:26 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-19 10:56:06 -05:00
|
|
|
|
2016-03-05 14:00:26 -05:00
|
|
|
$entity->save();
|
2016-05-01 16:20:50 -04:00
|
|
|
$this->permissionService->buildJointPermissionsForEntity($entity);
|
2016-03-05 14:00:26 -05:00
|
|
|
}
|
|
|
|
|
2016-05-15 08:41:18 -04:00
|
|
|
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
/**
|
|
|
|
* Create a new entity from request input.
|
|
|
|
* Used for books and chapters.
|
|
|
|
* @param string $type
|
|
|
|
* @param array $input
|
|
|
|
* @param bool|Book $book
|
2018-09-25 07:30:50 -04:00
|
|
|
* @return \BookStack\Entities\Entity
|
2017-01-02 06:07:27 -05:00
|
|
|
*/
|
|
|
|
public function createFromInput($type, $input = [], $book = false)
|
|
|
|
{
|
|
|
|
$isChapter = strtolower($type) === 'chapter';
|
2018-09-25 13:00:40 -04:00
|
|
|
$entityModel = $this->entityProvider->get($type)->newInstance($input);
|
2018-03-30 09:09:51 -04:00
|
|
|
$entityModel->slug = $this->findSuitableSlug($type, $entityModel->name, false, $isChapter ? $book->id : false);
|
|
|
|
$entityModel->created_by = user()->id;
|
|
|
|
$entityModel->updated_by = user()->id;
|
|
|
|
$isChapter ? $book->chapters()->save($entityModel) : $entityModel->save();
|
|
|
|
|
|
|
|
if (isset($input['tags'])) {
|
|
|
|
$this->tagRepo->saveTagsToEntity($entityModel, $input['tags']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->permissionService->buildJointPermissionsForEntity($entityModel);
|
|
|
|
$this->searchService->indexEntity($entityModel);
|
|
|
|
return $entityModel;
|
2017-01-02 06:07:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update entity details from request input.
|
2017-04-16 05:47:44 -04:00
|
|
|
* Used for books and chapters
|
2017-01-02 06:07:27 -05:00
|
|
|
* @param string $type
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Entity $entityModel
|
2017-01-02 06:07:27 -05:00
|
|
|
* @param array $input
|
2018-09-25 07:30:50 -04:00
|
|
|
* @return \BookStack\Entities\Entity
|
2017-01-02 06:07:27 -05:00
|
|
|
*/
|
|
|
|
public function updateFromInput($type, Entity $entityModel, $input = [])
|
|
|
|
{
|
|
|
|
if ($entityModel->name !== $input['name']) {
|
|
|
|
$entityModel->slug = $this->findSuitableSlug($type, $input['name'], $entityModel->id);
|
|
|
|
}
|
|
|
|
$entityModel->fill($input);
|
|
|
|
$entityModel->updated_by = user()->id;
|
|
|
|
$entityModel->save();
|
2018-03-30 09:09:51 -04:00
|
|
|
|
|
|
|
if (isset($input['tags'])) {
|
|
|
|
$this->tagRepo->saveTagsToEntity($entityModel, $input['tags']);
|
|
|
|
}
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
$this->permissionService->buildJointPermissionsForEntity($entityModel);
|
2017-03-19 08:48:44 -04:00
|
|
|
$this->searchService->indexEntity($entityModel);
|
2017-01-02 06:07:27 -05:00
|
|
|
return $entityModel;
|
|
|
|
}
|
|
|
|
|
2018-08-27 09:18:09 -04:00
|
|
|
/**
|
|
|
|
* Sync the books assigned to a shelf from a comma-separated list
|
|
|
|
* of book IDs.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Bookshelf $shelf
|
2018-08-27 09:18:09 -04:00
|
|
|
* @param string $books
|
|
|
|
*/
|
|
|
|
public function updateShelfBooks(Bookshelf $shelf, string $books)
|
|
|
|
{
|
|
|
|
$ids = explode(',', $books);
|
|
|
|
|
2018-09-16 14:34:09 -04:00
|
|
|
// Check books exist and match ordering
|
2018-08-27 09:18:09 -04:00
|
|
|
$bookIds = $this->entityQuery('book')->whereIn('id', $ids)->get(['id'])->pluck('id');
|
2018-09-16 14:34:09 -04:00
|
|
|
$syncData = [];
|
|
|
|
foreach ($ids as $index => $id) {
|
|
|
|
if ($bookIds->contains($id)) {
|
|
|
|
$syncData[$id] = ['order' => $index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$shelf->books()->sync($syncData);
|
2018-08-27 09:18:09 -04:00
|
|
|
}
|
|
|
|
|
2019-04-15 15:43:25 -04:00
|
|
|
/**
|
|
|
|
* Append a Book to a BookShelf.
|
|
|
|
* @param Bookshelf $shelf
|
|
|
|
* @param Book $book
|
|
|
|
*/
|
|
|
|
public function appendBookToShelf(Bookshelf $shelf, Book $book)
|
|
|
|
{
|
|
|
|
if ($shelf->contains($book)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$maxOrder = $shelf->books()->max('order');
|
|
|
|
$shelf->books()->attach($book->id, ['order' => $maxOrder + 1]);
|
|
|
|
}
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
/**
|
|
|
|
* Change the book that an entity belongs to.
|
|
|
|
* @param string $type
|
|
|
|
* @param integer $newBookId
|
|
|
|
* @param Entity $entity
|
|
|
|
* @param bool $rebuildPermissions
|
2018-09-25 07:30:50 -04:00
|
|
|
* @return \BookStack\Entities\Entity
|
2017-01-02 06:07:27 -05:00
|
|
|
*/
|
|
|
|
public function changeBook($type, $newBookId, Entity $entity, $rebuildPermissions = false)
|
|
|
|
{
|
|
|
|
$entity->book_id = $newBookId;
|
|
|
|
// Update related activity
|
|
|
|
foreach ($entity->activity as $activity) {
|
|
|
|
$activity->book_id = $newBookId;
|
|
|
|
$activity->save();
|
|
|
|
}
|
|
|
|
$entity->slug = $this->findSuitableSlug($type, $entity->name, $entity->id, $newBookId);
|
|
|
|
$entity->save();
|
|
|
|
|
|
|
|
// Update all child pages if a chapter
|
|
|
|
if (strtolower($type) === 'chapter') {
|
|
|
|
foreach ($entity->pages as $page) {
|
|
|
|
$this->changeBook('page', $newBookId, $page, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update permissions if applicable
|
|
|
|
if ($rebuildPermissions) {
|
|
|
|
$entity->load('book');
|
|
|
|
$this->permissionService->buildJointPermissionsForEntity($entity->book);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $entity;
|
|
|
|
}
|
|
|
|
|
2016-08-26 15:20:58 -04:00
|
|
|
/**
|
|
|
|
* Alias method to update the book jointPermissions in the PermissionService.
|
2017-04-29 17:01:43 -04:00
|
|
|
* @param Book $book
|
2016-08-26 15:20:58 -04:00
|
|
|
*/
|
2017-04-29 17:01:43 -04:00
|
|
|
public function buildJointPermissionsForBook(Book $book)
|
2016-08-26 15:20:58 -04:00
|
|
|
{
|
2017-04-29 17:01:43 -04:00
|
|
|
$this->permissionService->buildJointPermissionsForEntity($book);
|
2016-08-26 15:20:58 -04:00
|
|
|
}
|
|
|
|
|
2016-11-12 12:16:52 -05:00
|
|
|
/**
|
|
|
|
* Format a name as a url slug.
|
|
|
|
* @param $name
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function nameToSlug($name)
|
|
|
|
{
|
2017-11-11 11:27:29 -05:00
|
|
|
$slug = preg_replace('/[\+\/\\\?\@\}\{\.\,\=\[\]\#\&\!\*\'\;\:\$\%]/', '', mb_strtolower($name));
|
|
|
|
$slug = preg_replace('/\s{2,}/', ' ', $slug);
|
|
|
|
$slug = str_replace(' ', '-', $slug);
|
2018-01-28 11:58:52 -05:00
|
|
|
if ($slug === "") {
|
|
|
|
$slug = substr(md5(rand(1, 500)), 0, 5);
|
|
|
|
}
|
2016-11-12 12:16:52 -05:00
|
|
|
return $slug;
|
|
|
|
}
|
|
|
|
|
2017-01-16 16:24:48 -05:00
|
|
|
/**
|
2019-01-05 12:18:40 -05:00
|
|
|
* Render the page for viewing
|
2017-01-16 16:24:48 -05:00
|
|
|
* @param Page $page
|
2019-01-05 12:18:40 -05:00
|
|
|
* @param bool $blankIncludes
|
|
|
|
* @return string
|
2017-01-16 16:24:48 -05:00
|
|
|
*/
|
2019-01-05 12:18:40 -05:00
|
|
|
public function renderPage(Page $page, bool $blankIncludes = false) : string
|
2017-01-16 16:24:48 -05:00
|
|
|
{
|
2017-01-21 08:53:00 -05:00
|
|
|
$content = $page->html;
|
2019-01-05 12:18:40 -05:00
|
|
|
|
2018-03-17 11:51:40 -04:00
|
|
|
if (!config('app.allow_content_scripts')) {
|
|
|
|
$content = $this->escapeScripts($content);
|
|
|
|
}
|
|
|
|
|
2019-01-05 12:18:40 -05:00
|
|
|
if ($blankIncludes) {
|
|
|
|
$content = $this->blankPageIncludes($content);
|
|
|
|
} else {
|
|
|
|
$content = $this->parsePageIncludes($content);
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|
2017-01-21 08:53:00 -05:00
|
|
|
|
2019-01-05 12:18:40 -05:00
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove any page include tags within the given HTML.
|
|
|
|
* @param string $html
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function blankPageIncludes(string $html) : string
|
|
|
|
{
|
|
|
|
return preg_replace("/{{@\s?([0-9].*?)}}/", '', $html);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse any include tags "{{@<page_id>#section}}" to be part of the page.
|
|
|
|
* @param string $html
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
|
|
|
protected function parsePageIncludes(string $html) : string
|
|
|
|
{
|
|
|
|
$matches = [];
|
|
|
|
preg_match_all("/{{@\s?([0-9].*?)}}/", $html, $matches);
|
|
|
|
|
2017-12-30 10:50:33 -05:00
|
|
|
$topLevelTags = ['table', 'ul', 'ol'];
|
2017-01-21 08:53:00 -05:00
|
|
|
foreach ($matches[1] as $index => $includeId) {
|
|
|
|
$splitInclude = explode('#', $includeId, 2);
|
|
|
|
$pageId = intval($splitInclude[0]);
|
2018-01-28 11:58:52 -05:00
|
|
|
if (is_nan($pageId)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-21 08:53:00 -05:00
|
|
|
|
2019-01-05 12:18:40 -05:00
|
|
|
$matchedPage = $this->getById('page', $pageId);
|
2017-09-02 11:06:03 -04:00
|
|
|
if ($matchedPage === null) {
|
2019-01-05 12:18:40 -05:00
|
|
|
$html = str_replace($matches[0][$index], '', $html);
|
2017-01-21 08:53:00 -05:00
|
|
|
continue;
|
2017-01-16 16:24:48 -05:00
|
|
|
}
|
|
|
|
|
2017-01-21 08:53:00 -05:00
|
|
|
if (count($splitInclude) === 1) {
|
2019-01-05 12:18:40 -05:00
|
|
|
$html = str_replace($matches[0][$index], $matchedPage->html, $html);
|
2017-01-21 08:53:00 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$doc = new DOMDocument();
|
2019-04-15 16:20:32 -04:00
|
|
|
libxml_use_internal_errors(true);
|
2017-09-02 11:06:03 -04:00
|
|
|
$doc->loadHTML(mb_convert_encoding('<body>'.$matchedPage->html.'</body>', 'HTML-ENTITIES', 'UTF-8'));
|
2017-01-21 08:53:00 -05:00
|
|
|
$matchingElem = $doc->getElementById($splitInclude[1]);
|
|
|
|
if ($matchingElem === null) {
|
2019-01-05 12:18:40 -05:00
|
|
|
$html = str_replace($matches[0][$index], '', $html);
|
2017-01-21 08:53:00 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$innerContent = '';
|
2017-12-30 10:50:33 -05:00
|
|
|
$isTopLevel = in_array(strtolower($matchingElem->nodeName), $topLevelTags);
|
|
|
|
if ($isTopLevel) {
|
|
|
|
$innerContent .= $doc->saveHTML($matchingElem);
|
|
|
|
} else {
|
|
|
|
foreach ($matchingElem->childNodes as $childNode) {
|
|
|
|
$innerContent .= $doc->saveHTML($childNode);
|
|
|
|
}
|
2017-01-21 08:53:00 -05:00
|
|
|
}
|
2019-04-15 16:20:32 -04:00
|
|
|
libxml_clear_errors();
|
2019-01-05 12:18:40 -05:00
|
|
|
$html = str_replace($matches[0][$index], trim($innerContent), $html);
|
2017-01-16 16:24:48 -05:00
|
|
|
}
|
|
|
|
|
2019-01-05 12:18:40 -05:00
|
|
|
return $html;
|
2017-01-16 16:24:48 -05:00
|
|
|
}
|
|
|
|
|
2018-03-17 11:51:40 -04:00
|
|
|
/**
|
|
|
|
* Escape script tags within HTML content.
|
|
|
|
* @param string $html
|
2019-01-05 12:18:40 -05:00
|
|
|
* @return string
|
2018-03-17 11:51:40 -04:00
|
|
|
*/
|
2019-01-05 12:18:40 -05:00
|
|
|
protected function escapeScripts(string $html) : string
|
2018-03-17 11:51:40 -04:00
|
|
|
{
|
|
|
|
$scriptSearchRegex = '/<script.*?>.*?<\/script>/ms';
|
|
|
|
$matches = [];
|
|
|
|
preg_match_all($scriptSearchRegex, $html, $matches);
|
|
|
|
|
|
|
|
foreach ($matches[0] as $match) {
|
|
|
|
$html = str_replace($match, htmlentities($match), $html);
|
|
|
|
}
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
/**
|
|
|
|
* Search for image usage within page content.
|
|
|
|
* @param $imageString
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function searchForImage($imageString)
|
|
|
|
{
|
|
|
|
$pages = $this->entityQuery('page')->where('html', 'like', '%' . $imageString . '%')->get();
|
|
|
|
foreach ($pages as $page) {
|
|
|
|
$page->url = $page->getUrl();
|
|
|
|
$page->html = '';
|
|
|
|
$page->text = '';
|
|
|
|
}
|
|
|
|
return count($pages) > 0 ? $pages : false;
|
|
|
|
}
|
|
|
|
|
2018-09-16 14:34:09 -04:00
|
|
|
/**
|
|
|
|
* Destroy a bookshelf instance
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Bookshelf $shelf
|
2018-09-16 14:34:09 -04:00
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
public function destroyBookshelf(Bookshelf $shelf)
|
|
|
|
{
|
|
|
|
$this->destroyEntityCommonRelations($shelf);
|
|
|
|
$shelf->delete();
|
|
|
|
}
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
/**
|
|
|
|
* Destroy the provided book and all its child entities.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Book $book
|
2018-08-27 09:18:09 -04:00
|
|
|
* @throws NotifyException
|
|
|
|
* @throws \Throwable
|
2017-01-02 06:07:27 -05:00
|
|
|
*/
|
|
|
|
public function destroyBook(Book $book)
|
|
|
|
{
|
|
|
|
foreach ($book->pages as $page) {
|
|
|
|
$this->destroyPage($page);
|
|
|
|
}
|
|
|
|
foreach ($book->chapters as $chapter) {
|
|
|
|
$this->destroyChapter($chapter);
|
|
|
|
}
|
2018-09-16 14:34:09 -04:00
|
|
|
$this->destroyEntityCommonRelations($book);
|
2017-01-02 06:07:27 -05:00
|
|
|
$book->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a chapter and its relations.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Chapter $chapter
|
2018-08-27 09:18:09 -04:00
|
|
|
* @throws \Throwable
|
2017-01-02 06:07:27 -05:00
|
|
|
*/
|
|
|
|
public function destroyChapter(Chapter $chapter)
|
|
|
|
{
|
|
|
|
if (count($chapter->pages) > 0) {
|
|
|
|
foreach ($chapter->pages as $page) {
|
|
|
|
$page->chapter_id = 0;
|
|
|
|
$page->save();
|
|
|
|
}
|
|
|
|
}
|
2018-09-16 14:34:09 -04:00
|
|
|
$this->destroyEntityCommonRelations($chapter);
|
2017-01-02 06:07:27 -05:00
|
|
|
$chapter->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a given page along with its dependencies.
|
|
|
|
* @param Page $page
|
2017-10-15 14:14:46 -04:00
|
|
|
* @throws NotifyException
|
2018-08-27 09:18:09 -04:00
|
|
|
* @throws \Throwable
|
2017-01-02 06:07:27 -05:00
|
|
|
*/
|
|
|
|
public function destroyPage(Page $page)
|
|
|
|
{
|
2017-10-15 14:14:46 -04:00
|
|
|
// Check if set as custom homepage
|
|
|
|
$customHome = setting('app-homepage', '0:');
|
|
|
|
if (intval($page->id) === intval(explode(':', $customHome)[0])) {
|
|
|
|
throw new NotifyException(trans('errors.page_custom_home_deletion'), $page->getUrl());
|
|
|
|
}
|
|
|
|
|
2018-09-20 10:27:30 -04:00
|
|
|
$this->destroyEntityCommonRelations($page);
|
|
|
|
|
2017-01-02 06:07:27 -05:00
|
|
|
// Delete Attached Files
|
|
|
|
$attachmentService = app(AttachmentService::class);
|
|
|
|
foreach ($page->attachments as $attachment) {
|
|
|
|
$attachmentService->deleteFile($attachment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$page->delete();
|
|
|
|
}
|
2018-09-16 14:34:09 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy or handle the common relations connected to an entity.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Entity $entity
|
2018-09-16 14:34:09 -04:00
|
|
|
* @throws \Throwable
|
|
|
|
*/
|
|
|
|
protected function destroyEntityCommonRelations(Entity $entity)
|
|
|
|
{
|
|
|
|
\Activity::removeEntity($entity);
|
|
|
|
$entity->views()->delete();
|
|
|
|
$entity->permissions()->delete();
|
|
|
|
$entity->tags()->delete();
|
|
|
|
$entity->comments()->delete();
|
|
|
|
$this->permissionService->deleteJointPermissionsForEntity($entity);
|
|
|
|
$this->searchService->deleteEntityTerms($entity);
|
|
|
|
}
|
2018-09-20 14:16:11 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the permissions of a bookshelf to all child books.
|
|
|
|
* Returns the number of books that had permissions updated.
|
2018-09-25 07:30:50 -04:00
|
|
|
* @param \BookStack\Entities\Bookshelf $bookshelf
|
2018-09-20 14:16:11 -04:00
|
|
|
* @return int
|
2018-09-21 10:15:16 -04:00
|
|
|
* @throws \Throwable
|
2018-09-20 14:16:11 -04:00
|
|
|
*/
|
|
|
|
public function copyBookshelfPermissions(Bookshelf $bookshelf)
|
|
|
|
{
|
|
|
|
$shelfPermissions = $bookshelf->permissions()->get(['role_id', 'action'])->toArray();
|
|
|
|
$shelfBooks = $bookshelf->books()->get();
|
|
|
|
$updatedBookCount = 0;
|
|
|
|
|
|
|
|
foreach ($shelfBooks as $book) {
|
2018-09-21 13:48:47 -04:00
|
|
|
if (!userCan('restrictions-manage', $book)) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-09-20 14:16:11 -04:00
|
|
|
$book->permissions()->delete();
|
|
|
|
$book->restricted = $bookshelf->restricted;
|
|
|
|
$book->permissions()->createMany($shelfPermissions);
|
|
|
|
$book->save();
|
|
|
|
$this->permissionService->buildJointPermissionsForEntity($book);
|
|
|
|
$updatedBookCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $updatedBookCount;
|
|
|
|
}
|
2016-05-15 08:41:18 -04:00
|
|
|
}
|