@@ -60,17 +60,25 @@ public function indexAction(): void
60
60
$ q ->filter (Filter::equal ('cluster_uuid ' , Uuid::fromString ($ clusterUuid )->getBytes ()));
61
61
}
62
62
63
+ if ($ this ->getFavorable ()) {
64
+ $ favoriteToggle = $ this ->createFavoriteToggle ($ q );
65
+ }
66
+
63
67
$ limitControl = $ this ->createLimitControl ();
64
- $ sortControl = $ this ->createSortControl ($ q , $ this ->getSortColumns ());
68
+ $ sortControl = $ this ->createSortControl (
69
+ $ q ,
70
+ $ this ->getSortColumns ()
71
+ + ($ favoriteToggleActive ? ['favorite.priority desc ' => 'Custom Order ' ] : [])
72
+ );
65
73
$ paginationControl = $ this ->createPaginationControl ($ q );
66
-
67
74
$ viewModeSwitcher = $ this ->createViewModeSwitcher ($ paginationControl , $ limitControl );
68
75
69
76
$ searchBar = $ this ->createSearchBar ($ q , [
70
77
$ limitControl ->getLimitParam (),
71
78
$ sortControl ->getSortParam (),
72
79
$ viewModeSwitcher ->getViewModeParam (),
73
- 'columns '
80
+ (isset ($ favoriteToggle ) ? $ favoriteToggle ->getFavoriteParam () : '' ),
81
+ 'columns ' ,
74
82
]);
75
83
76
84
if ($ searchBar ->hasBeenSent () && ! $ searchBar ->isValid ()) {
@@ -92,6 +100,9 @@ public function indexAction(): void
92
100
$ this ->addControl ($ sortControl );
93
101
$ this ->addControl ($ limitControl );
94
102
$ this ->addControl ($ viewModeSwitcher );
103
+ if ($ this ->getFavorable ()) {
104
+ $ this ->addControl ($ favoriteToggle );
105
+ }
95
106
$ this ->addControl ($ searchBar );
96
107
97
108
$ contentClass = $ this ->getContentClass ();
@@ -110,6 +121,8 @@ abstract protected function getContentClass(): string;
110
121
111
122
abstract protected function getPermission (): string ;
112
123
124
+ abstract protected function getFavorable (): bool ;
125
+
113
126
protected function getIgnoredViewModes (): array
114
127
{
115
128
return [];
@@ -285,4 +298,52 @@ function (ViewModeSwitcher $viewModeSwitcher) use (
285
298
286
299
return $ viewModeSwitcher ;
287
300
}
301
+
302
+ /**
303
+ * Create and return the FavoriteToggle
304
+ *
305
+ * This automatically shifts the favorite URL parameter from {@link $params}.
306
+ *
307
+ * @param Query $query
308
+ *
309
+ * @return FavoriteToggle
310
+ */
311
+ public function createFavoriteToggle (
312
+ Query $ query
313
+ ): FavoriteToggle {
314
+ $ favoriteToggle = new FavoriteToggle ();
315
+ $ defaultFavoriteParam = $ favoriteToggle ->getFavoriteParam ();
316
+ $ favoriteParam = $ this ->params ->shift ($ defaultFavoriteParam );
317
+ $ favoriteToggle ->populate ([
318
+ $ defaultFavoriteParam => $ favoriteParam
319
+ ]);
320
+
321
+ $ favoriteToggle ->on (FavoriteToggle::ON_SUCCESS , function (FavoriteToggle $ favoriteToggle ) use (
322
+ $ query ,
323
+ $ defaultFavoriteParam
324
+ ) {
325
+ $ favoriteParam = $ favoriteToggle ->getValue ($ defaultFavoriteParam );
326
+
327
+ $ requestUrl = Url::fromRequest ();
328
+
329
+ // Redirect if favorite param has changed to update the URL
330
+ if (isset ($ favoriteParam ) && $ requestUrl ->getParam ($ defaultFavoriteParam ) !== $ favoriteParam ) {
331
+ $ requestUrl ->setParam ($ defaultFavoriteParam , $ favoriteParam );
332
+ if (
333
+ $ favoriteParam === 'n '
334
+ && $ requestUrl ->getParam (SortControl::DEFAULT_SORT_PARAM ) === 'favorite.priority desc '
335
+ ) {
336
+ $ requestUrl ->remove (SortControl::DEFAULT_SORT_PARAM );
337
+ }
338
+
339
+ $ this ->redirectNow ($ requestUrl );
340
+ }
341
+ })->handleRequest ($ this ->getServerRequest ());
342
+
343
+ if ($ favoriteToggle ->getValue ($ defaultFavoriteParam ) === 'y ' ) {
344
+ $ query ->with ('favorite ' )->filter (Filter::equal ('favorite.username ' , Auth::getInstance ()->getUser ()->getUsername ()));
345
+ }
346
+
347
+ return $ favoriteToggle ;
348
+ }
288
349
}
0 commit comments