66use BookStack \Auth \Permissions \PermissionService ;
77use BookStack \Auth \User ;
88use BookStack \Entities \Book ;
9+ use BookStack \Entities \BookChild ;
910use BookStack \Entities \Bookshelf ;
1011use BookStack \Entities \Chapter ;
1112use BookStack \Entities \Entity ;
1617use BookStack \Exceptions \NotifyException ;
1718use BookStack \Uploads \AttachmentService ;
1819use DOMDocument ;
19- use DOMNode ;
2020use DOMXPath ;
2121use Illuminate \Contracts \Pagination \LengthAwarePaginator ;
2222use Illuminate \Database \Eloquent \Builder ;
@@ -465,25 +465,6 @@ public function findSuitableSlug($type, $name, $currentId = false, $bookId = fal
465465 return $ slug ;
466466 }
467467
468- /**
469- * Check if a slug already exists in the database.
470- * @param string $type
471- * @param string $slug
472- * @param bool|integer $currentId
473- * @param bool|integer $bookId
474- * @return bool
475- */
476- protected function slugExists ($ type , $ slug , $ currentId = false , $ bookId = false )
477- {
478- $ query = $ this ->entityProvider ->get ($ type )->where ('slug ' , '= ' , $ slug );
479- if (strtolower ($ type ) === 'page ' || strtolower ($ type ) === 'chapter ' ) {
480- $ query = $ query ->where ('book_id ' , '= ' , $ bookId );
481- }
482- if ($ currentId ) {
483- $ query = $ query ->where ('id ' , '!= ' , $ currentId );
484- }
485- return $ query ->count () > 0 ;
486- }
487468
488469 /**
489470 * Updates entity restrictions from a request
@@ -501,14 +482,14 @@ public function updateEntityPermissionsFromRequest(Request $request, Entity $ent
501482 foreach ($ restrictions as $ action => $ value ) {
502483 $ entity ->permissions ()->create ([
503484 'role_id ' => $ roleId ,
504- 'action ' => strtolower ($ action )
485+ 'action ' => strtolower ($ action ),
505486 ]);
506487 }
507488 }
508489 }
509490
510491 $ entity ->save ();
511- $ this -> permissionService -> buildJointPermissionsForEntity ( $ entity );
492+ $ entity -> rebuildPermissions ( );
512493 }
513494
514495
@@ -519,54 +500,50 @@ public function updateEntityPermissionsFromRequest(Request $request, Entity $ent
519500 * @param array $input
520501 * @param Book|null $book
521502 * @return Entity
522- * @throws Throwable
523503 */
524504 public function createFromInput (string $ type , array $ input = [], Book $ book = null )
525505 {
526506 $ entityModel = $ this ->entityProvider ->get ($ type )->newInstance ($ input );
527- $ entityModel ->slug = $ this ->findSuitableSlug ($ type , $ entityModel ->name , false , $ book ? $ book ->id : false );
528507 $ entityModel ->created_by = user ()->id ;
529508 $ entityModel ->updated_by = user ()->id ;
530509
531510 if ($ book ) {
532511 $ entityModel ->book_id = $ book ->id ;
533512 }
534513
514+ $ entityModel ->refreshSlug ();
535515 $ entityModel ->save ();
536516
537517 if (isset ($ input ['tags ' ])) {
538518 $ this ->tagRepo ->saveTagsToEntity ($ entityModel , $ input ['tags ' ]);
539519 }
540520
541- $ this -> permissionService -> buildJointPermissionsForEntity ( $ entityModel );
521+ $ entityModel -> rebuildPermissions ( );
542522 $ this ->searchService ->indexEntity ($ entityModel );
543523 return $ entityModel ;
544524 }
545525
546526 /**
547527 * Update entity details from request input.
548- * Used for books and chapters
549- * @param string $type
550- * @param Entity $entityModel
551- * @param array $input
552- * @return Entity
553- * @throws Throwable
528+ * Used for books and chapters.
529+ * TODO: Remove type param
554530 */
555- public function updateFromInput (string $ type , Entity $ entityModel , array $ input = [])
531+ public function updateFromInput (string $ type , Entity $ entityModel , array $ input = []): Entity
556532 {
557- if ($ entityModel ->name !== $ input ['name ' ]) {
558- $ entityModel ->slug = $ this ->findSuitableSlug ($ type , $ input ['name ' ], $ entityModel ->id );
559- }
560-
561533 $ entityModel ->fill ($ input );
562534 $ entityModel ->updated_by = user ()->id ;
535+
536+ if ($ entityModel ->isDirty ('name ' )) {
537+ $ entityModel ->refreshSlug ();
538+ }
539+
563540 $ entityModel ->save ();
564541
565542 if (isset ($ input ['tags ' ])) {
566543 $ this ->tagRepo ->saveTagsToEntity ($ entityModel , $ input ['tags ' ]);
567544 }
568545
569- $ this -> permissionService -> buildJointPermissionsForEntity ( $ entityModel );
546+ $ entityModel -> rebuildPermissions ( );
570547 $ this ->searchService ->indexEntity ($ entityModel );
571548 return $ entityModel ;
572549 }
@@ -595,62 +572,24 @@ public function updateShelfBooks(Bookshelf $shelf, string $books)
595572
596573 /**
597574 * Change the book that an entity belongs to.
598- * @param string $type
599- * @param integer $newBookId
600- * @param Entity $entity
601- * @param bool $rebuildPermissions
602- * @return Entity
603575 */
604- public function changeBook ($ type , $ newBookId , Entity $ entity , $ rebuildPermissions = false )
576+ public function changeBook (BookChild $ bookChild , int $ newBookId ): Entity
605577 {
606- $ entity ->book_id = $ newBookId ;
578+ $ bookChild ->book_id = $ newBookId ;
579+ $ bookChild ->refreshSlug ();
580+ $ bookChild ->save ();
581+
607582 // Update related activity
608- foreach ($ entity ->activity as $ activity ) {
609- $ activity ->book_id = $ newBookId ;
610- $ activity ->save ();
611- }
612- $ entity ->slug = $ this ->findSuitableSlug ($ type , $ entity ->name , $ entity ->id , $ newBookId );
613- $ entity ->save ();
583+ $ bookChild ->activity ()->update (['book_id ' => $ newBookId ]);
614584
615585 // Update all child pages if a chapter
616- if (strtolower ( $ type ) === 'chapter ' ) {
617- foreach ($ entity ->pages as $ page ) {
618- $ this ->changeBook (' page ' , $ newBookId, $ page , false );
586+ if ($ bookChild -> isA ( 'chapter ' ) ) {
587+ foreach ($ bookChild ->pages as $ page ) {
588+ $ this ->changeBook ($ page , $ newBookId );
619589 }
620590 }
621591
622- // Update permissions if applicable
623- if ($ rebuildPermissions ) {
624- $ entity ->load ('book ' );
625- $ this ->permissionService ->buildJointPermissionsForEntity ($ entity ->book );
626- }
627-
628- return $ entity ;
629- }
630-
631- /**
632- * Alias method to update the book jointPermissions in the PermissionService.
633- * @param Book $book
634- */
635- public function buildJointPermissionsForBook (Book $ book )
636- {
637- $ this ->permissionService ->buildJointPermissionsForEntity ($ book );
638- }
639-
640- /**
641- * Format a name as a url slug.
642- * @param $name
643- * @return string
644- */
645- protected function nameToSlug ($ name )
646- {
647- $ slug = preg_replace ('/[\+\/ \\\?\@\}\{\.\,\=\[\]\#\&\!\* \'\;\:\$\%]/ ' , '' , mb_strtolower ($ name ));
648- $ slug = preg_replace ('/\s{2,}/ ' , ' ' , $ slug );
649- $ slug = str_replace (' ' , '- ' , $ slug );
650- if ($ slug === "" ) {
651- $ slug = substr (md5 (rand (1 , 500 )), 0 , 5 );
652- }
653- return $ slug ;
592+ return $ bookChild ;
654593 }
655594
656595 /**
@@ -885,6 +824,7 @@ public function copyBookshelfPermissions(Bookshelf $bookshelf)
885824 $ shelfBooks = $ bookshelf ->books ()->get ();
886825 $ updatedBookCount = 0 ;
887826
827+ /** @var Book $book */
888828 foreach ($ shelfBooks as $ book ) {
889829 if (!userCan ('restrictions-manage ' , $ book )) {
890830 continue ;
@@ -893,7 +833,7 @@ public function copyBookshelfPermissions(Bookshelf $bookshelf)
893833 $ book ->restricted = $ bookshelf ->restricted ;
894834 $ book ->permissions ()->createMany ($ shelfPermissions );
895835 $ book ->save ();
896- $ this -> permissionService -> buildJointPermissionsForEntity ( $ book );
836+ $ book -> rebuildPermissions ( );
897837 $ updatedBookCount ++;
898838 }
899839
0 commit comments