2222use MonsieurBiz \SyliusMediaManagerPlugin \Repository \FileRepositoryInterface ;
2323use MonsieurBiz \SyliusMediaManagerPlugin \Resolver \FilePathResolverInterface ;
2424use MonsieurBiz \SyliusMediaManagerPlugin \Validator \FileValidatorInterface ;
25+ use Symfony \Component \DependencyInjection \Attribute \Autowire ;
2526use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
2627use Symfony \UX \LiveComponent \Attribute \LiveAction ;
2728use Symfony \UX \LiveComponent \Attribute \LiveArg ;
@@ -49,6 +50,15 @@ final class FileListManager
4950 #[LiveProp(updateFromParent: true )]
5051 public ?string $ relativeRootDirectoryPath = null ;
5152
53+ #[LiveProp]
54+ public int $ currentPage = 1 ;
55+
56+ #[LiveProp]
57+ public int $ itemsPerPage = 20 ;
58+
59+ #[LiveProp]
60+ public int $ totalItems = 0 ;
61+
5262 #[LiveProp]
5363 /**
5464 * @var File[]
@@ -59,7 +69,10 @@ public function __construct(
5969 private readonly FilePathResolverInterface $ filePathResolver ,
6070 private readonly FileRepositoryInterface $ fileRepository ,
6171 private readonly FileValidatorInterface $ fileValidator ,
72+ #[Autowire(param: 'monsieurbiz_sylius_media_manager.pagination.items_per_page ' )]
73+ int $ defaultItemsPerPage = 20 ,
6274 ) {
75+ $ this ->itemsPerPage = $ defaultItemsPerPage ;
6376 }
6477
6578 public function __invoke (): void
@@ -70,9 +83,12 @@ public function __invoke(): void
7083
7184 try {
7285 $ relativeDirectoryPath = $ this ->filePathResolver ->getRelativeFilePath ($ this ->absoluteDirectoryPath );
86+ $ this ->totalItems = $ this ->fileRepository ->countFromPath ($ this ->absoluteDirectoryPath );
7387 $ this ->fileList = $ this ->fileRepository ->findAllFromPath (
7488 $ this ->absoluteDirectoryPath ,
75- $ relativeDirectoryPath !== $ this ->relativeRootDirectoryPath
89+ $ relativeDirectoryPath !== $ this ->relativeRootDirectoryPath ,
90+ $ this ->currentPage ,
91+ $ this ->itemsPerPage ,
7692 );
7793 } catch (CannotReadFolderException ) {
7894 $ this ->emit ('displayError ' , [
@@ -95,6 +111,7 @@ public function setCurrentDirectory(#[LiveArg] string $absoluteDirectoryPath): v
95111 'absoluteDirectoryPath ' => $ absoluteDirectoryPath ,
96112 ], 'MediaManager:SelectionModal ' );
97113 $ this ->loaded = false ;
114+ $ this ->currentPage = 1 ; // Reset to first page when changing directory
98115 }
99116
100117 /**
@@ -150,4 +167,80 @@ public function deleteFile(#[LiveArg] string $fileName): void
150167 'openModalAfterClose ' => self ::SELECTION_MODAL_NAME ,
151168 ], 'MediaManager:ConfirmationModal ' );
152169 }
170+
171+ #[LiveAction]
172+ public function onFileDeleted (): void
173+ {
174+ // Refresh the file list and check if we need to go to previous page
175+ $ this ->loaded = false ;
176+
177+ // If current page becomes empty (except for first page), go to previous page
178+ $ totalPages = $ this ->getTotalPages ();
179+ if ($ this ->currentPage > 1 && $ this ->currentPage > $ totalPages ) {
180+ $ this ->currentPage = max (1 , $ totalPages );
181+ }
182+ }
183+
184+ #[LiveAction]
185+ public function changeItemsPerPage (#[LiveArg] int $ itemsPerPage ): void
186+ {
187+ $ this ->itemsPerPage = $ itemsPerPage ;
188+ $ this ->currentPage = 1 ;
189+ $ this ->loaded = false ;
190+ $ this ->__invoke ();
191+ }
192+
193+ #[LiveAction]
194+ public function goToPage (#[LiveArg] int $ page ): void
195+ {
196+ $ totalPages = $ this ->getTotalPages ();
197+ if ($ page < 1 ) {
198+ $ page = 1 ;
199+ } elseif ($ page > $ totalPages ) {
200+ $ page = $ totalPages ;
201+ }
202+
203+ $ this ->currentPage = $ page ;
204+ $ this ->loaded = false ;
205+ }
206+
207+ #[LiveAction]
208+ public function previousPage (): void
209+ {
210+ if ($ this ->currentPage > 1 ) {
211+ --$ this ->currentPage ;
212+ $ this ->loaded = false ;
213+ $ this ->__invoke ();
214+ }
215+ }
216+
217+ #[LiveAction]
218+ public function nextPage (): void
219+ {
220+ if ($ this ->currentPage < $ this ->getTotalPages ()) {
221+ ++$ this ->currentPage ;
222+ $ this ->loaded = false ;
223+ $ this ->__invoke ();
224+ }
225+ }
226+
227+ public function getTotalPages (): int
228+ {
229+ return (int ) ceil ($ this ->totalItems / $ this ->itemsPerPage );
230+ }
231+
232+ public function hasMultiplePages (): bool
233+ {
234+ return $ this ->getTotalPages () > 1 ;
235+ }
236+
237+ public function hasPreviousPage (): bool
238+ {
239+ return $ this ->currentPage > 1 ;
240+ }
241+
242+ public function hasNextPage (): bool
243+ {
244+ return $ this ->currentPage < $ this ->getTotalPages ();
245+ }
153246}
0 commit comments