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