@@ -36,21 +36,33 @@ public function getAttachmentFileSize(Attachment $attachment): int
3636 *
3737 * @throws FileUploadException
3838 */
39- public function saveNewUpload (UploadedFile $ uploadedFile , int $ pageId ): Attachment
39+ public function saveNewUpload (UploadedFile $ uploadedFile , int $ entityId , string $ entityType = ' page ' ): Attachment
4040 {
4141 $ attachmentName = $ uploadedFile ->getClientOriginalName ();
4242 $ attachmentPath = $ this ->putFileInStorage ($ uploadedFile );
43- $ largestExistingOrder = Attachment::query ()->where ('uploaded_to ' , '= ' , $ pageId )->max ('order ' );
43+
44+ // Determine the model class
45+ $ modelClass = $ entityType === 'book '
46+ ? 'BookStack \\Entities \\Models \\Book '
47+ : 'BookStack \\Entities \\Models \\Page ' ;
48+
49+ // Get max order for the entity using polymorphic relationship
50+ $ largestExistingOrder = Attachment::query ()
51+ ->where ('attachable_type ' , $ modelClass )
52+ ->where ('attachable_id ' , $ entityId )
53+ ->max ('order ' ) ?? 0 ;
4454
4555 /** @var Attachment $attachment */
4656 $ attachment = Attachment::query ()->forceCreate ([
47- 'name ' => $ attachmentName ,
48- 'path ' => $ attachmentPath ,
49- 'extension ' => $ uploadedFile ->getClientOriginalExtension (),
50- 'uploaded_to ' => $ pageId ,
51- 'created_by ' => user ()->id ,
52- 'updated_by ' => user ()->id ,
53- 'order ' => $ largestExistingOrder + 1 ,
57+ 'name ' => $ attachmentName ,
58+ 'path ' => $ attachmentPath ,
59+ 'extension ' => $ uploadedFile ->getClientOriginalExtension (),
60+ 'uploaded_to ' => $ entityType === 'page ' ? $ entityId : null , // Backward compatibility
61+ 'attachable_type ' => $ modelClass ,
62+ 'attachable_id ' => $ entityId ,
63+ 'created_by ' => user ()->id ,
64+ 'updated_by ' => user ()->id ,
65+ 'order ' => $ largestExistingOrder + 1 ,
5466 ]);
5567
5668 return $ attachment ;
@@ -83,19 +95,30 @@ public function saveUpdatedUpload(UploadedFile $uploadedFile, Attachment $attach
8395 /**
8496 * Save a new File attachment from a given link and name.
8597 */
86- public function saveNewFromLink (string $ name , string $ link , int $ page_id ): Attachment
98+ public function saveNewFromLink (string $ name , string $ link , int $ entityId , string $ entityType = ' page ' ): Attachment
8799 {
88- $ largestExistingOrder = Attachment::where ('uploaded_to ' , '= ' , $ page_id )->max ('order ' );
100+ // Determine the model class
101+ $ modelClass = $ entityType === 'book '
102+ ? 'BookStack \\Entities \\Models \\Book '
103+ : 'BookStack \\Entities \\Models \\Page ' ;
104+
105+ // Get max order for the entity using polymorphic relationship
106+ $ largestExistingOrder = Attachment::query ()
107+ ->where ('attachable_type ' , $ modelClass )
108+ ->where ('attachable_id ' , $ entityId )
109+ ->max ('order ' ) ?? 0 ;
89110
90111 return Attachment::forceCreate ([
91- 'name ' => $ name ,
92- 'path ' => $ link ,
93- 'external ' => true ,
94- 'extension ' => '' ,
95- 'uploaded_to ' => $ page_id ,
96- 'created_by ' => user ()->id ,
97- 'updated_by ' => user ()->id ,
98- 'order ' => $ largestExistingOrder + 1 ,
112+ 'name ' => $ name ,
113+ 'path ' => $ link ,
114+ 'external ' => true ,
115+ 'extension ' => '' ,
116+ 'uploaded_to ' => $ entityType === 'page ' ? $ entityId : null , // Backward compatibility
117+ 'attachable_type ' => $ modelClass ,
118+ 'attachable_id ' => $ entityId ,
119+ 'created_by ' => user ()->id ,
120+ 'updated_by ' => user ()->id ,
121+ 'order ' => $ largestExistingOrder + 1 ,
99122 ]);
100123 }
101124
@@ -104,8 +127,26 @@ public function saveNewFromLink(string $name, string $link, int $page_id): Attac
104127 */
105128 public function updateFileOrderWithinPage (array $ attachmentOrder , string $ pageId )
106129 {
130+ $ modelClass = 'BookStack \\Entities \\Models \\Page ' ;
107131 foreach ($ attachmentOrder as $ index => $ attachmentId ) {
108- Attachment::query ()->where ('uploaded_to ' , '= ' , $ pageId )
132+ Attachment::query ()
133+ ->where ('attachable_type ' , $ modelClass )
134+ ->where ('attachable_id ' , '= ' , $ pageId )
135+ ->where ('id ' , '= ' , $ attachmentId )
136+ ->update (['order ' => $ index ]);
137+ }
138+ }
139+
140+ /**
141+ * Updates the ordering for a listing of attached files for a book.
142+ */
143+ public function updateFileOrderWithinBook (array $ attachmentOrder , string $ bookId )
144+ {
145+ $ modelClass = 'BookStack \\Entities \\Models \\Book ' ;
146+ foreach ($ attachmentOrder as $ index => $ attachmentId ) {
147+ Attachment::query ()
148+ ->where ('attachable_type ' , $ modelClass )
149+ ->where ('attachable_id ' , '= ' , $ bookId )
109150 ->where ('id ' , '= ' , $ attachmentId )
110151 ->update (['order ' => $ index ]);
111152 }
0 commit comments