66use App \Model \Table \CategoriesTable ;
77use App \Model \Table \PackagesTable ;
88use App \Utility \CakephpVersion ;
9+ use App \Utility \FilterSelection ;
910use Cake \Http \Exception \BadRequestException ;
1011use Interact \Attribute \Action ;
1112use Interact \Attribute \Pagination ;
@@ -48,6 +49,24 @@ class CatalogueIsland extends Island
4849 #[State(writable: true , url: 'php_slugs ' , history: true )]
4950 public array $ phpSlugs = [];
5051
52+ /**
53+ * @var list<string>
54+ */
55+ #[State(writable: true , url: 'exclude_cakephp_slugs ' , history: true )]
56+ public array $ excludedCakephpSlugs = [];
57+
58+ /**
59+ * @var list<string>
60+ */
61+ #[State(writable: true , url: 'exclude_php_slugs ' , history: true )]
62+ public array $ excludedPhpSlugs = [];
63+
64+ /**
65+ * @var list<string>
66+ */
67+ #[State(writable: true , url: 'exclude_categories ' , history: true )]
68+ public array $ excludedCategories = [];
69+
5170 #[Pagination(
5271 settings: [
5372 'limit ' => 21 ,
@@ -75,40 +94,68 @@ public function initialize(): void
7594 */
7695 public function updated (UpdatedPaths $ paths ): void
7796 {
78- if ($ paths ->hasAny ('search ' , 'cakephpSlugs ' , 'phpSlugs ' , 'category ' , 'awesomeOnly ' , 'featuredOnly ' ) && !$ paths ->has ('paging.page ' )) {
97+ $ filtersChanged = $ paths ->hasAny (
98+ 'search ' ,
99+ 'cakephpSlugs ' ,
100+ 'phpSlugs ' ,
101+ 'excludedCakephpSlugs ' ,
102+ 'excludedPhpSlugs ' ,
103+ 'excludedCategories ' ,
104+ 'category ' ,
105+ 'awesomeOnly ' ,
106+ 'featuredOnly ' ,
107+ );
108+ if ($ filtersChanged && !$ paths ->has ('paging.page ' )) {
79109 $ this ->paging ->page = 1 ;
80110 }
81111 }
82112
83113 /**
84- * Toggle a version selected from the directory filters .
114+ * Cycle a version through neutral, included and excluded .
85115 */
86116 #[Action]
87117 public function toggleVersion (string $ group , string $ slug ): void
88118 {
89- if (!in_array ($ group , ['cakephp_slugs ' , 'php_slugs ' ], true )) {
90- throw new BadRequestException ('Unsupported version group. ' );
91- }
92- $ property = $ group === 'cakephp_slugs ' ? 'cakephpSlugs ' : 'phpSlugs ' ;
93- $ values = $ this ->{$ property };
94- $ values = in_array ($ slug , $ values , true )
95- ? array_values (array_diff ($ values , [$ slug ]))
96- : [...$ values , $ slug ];
97- $ this ->{$ property } = $ values ;
119+ [$ property , $ excludedProperty ] = $ this ->versionProperties ($ group );
120+ $ selection = FilterSelection ::cycle ($ this ->{$ property }, $ this ->{$ excludedProperty }, $ slug );
121+ $ this ->{$ property } = $ selection ['included ' ];
122+ $ this ->{$ excludedProperty } = $ selection ['excluded ' ];
123+ $ this ->paging ->page = 1 ;
124+ }
125+
126+ /**
127+ * Clear both inclusion and exclusion for one version family.
128+ */
129+ #[Action]
130+ public function clearVersions (string $ group ): void
131+ {
132+ [$ property , $ excludedProperty ] = $ this ->versionProperties ($ group );
133+ $ this ->{$ property } = [];
134+ $ this ->{$ excludedProperty } = [];
98135 $ this ->paging ->page = 1 ;
99136 }
100137
138+ /**
139+ * @return array{string, string}
140+ */
141+ private function versionProperties (string $ group ): array
142+ {
143+ return match ($ group ) {
144+ 'cakephp_slugs ' => ['cakephpSlugs ' , 'excludedCakephpSlugs ' ],
145+ 'php_slugs ' => ['phpSlugs ' , 'excludedPhpSlugs ' ],
146+ default => throw new BadRequestException ('Unsupported version group. ' ),
147+ };
148+ }
149+
101150 /**
102151 * Select a compatibility version from plugin details without losing other filters.
103152 */
104153 #[Action]
105154 public function selectVersion (string $ group , string $ slug ): void
106155 {
107- if (!in_array ($ group , ['cakephp_slugs ' , 'php_slugs ' ], true )) {
108- throw new BadRequestException ('Unsupported version group. ' );
109- }
110- $ property = $ group === 'cakephp_slugs ' ? 'cakephpSlugs ' : 'phpSlugs ' ;
156+ [$ property , $ excludedProperty ] = $ this ->versionProperties ($ group );
111157 $ this ->{$ property } = [$ slug ];
158+ $ this ->{$ excludedProperty } = array_values (array_diff ($ this ->{$ excludedProperty }, [$ slug ]));
112159 $ this ->paging ->page = 1 ;
113160 }
114161
@@ -119,6 +166,19 @@ public function selectVersion(string $group, string $slug): void
119166 public function selectCategory (string $ slug ): void
120167 {
121168 $ this ->category = $ slug ;
169+ $ this ->excludedCategories = $ slug === '' ? [] : array_values (array_diff ($ this ->excludedCategories , [$ slug ]));
170+ $ this ->paging ->page = 1 ;
171+ }
172+
173+ /**
174+ * Keep one included category while allowing multiple exclusions.
175+ */
176+ #[Action]
177+ public function toggleCategory (string $ slug ): void
178+ {
179+ $ selection = FilterSelection ::cycle ($ this ->category !== '' ? [$ this ->category ] : [], $ this ->excludedCategories , $ slug , multiple: false );
180+ $ this ->category = $ selection ['included ' ][0 ] ?? '' ;
181+ $ this ->excludedCategories = $ selection ['excluded ' ];
122182 $ this ->paging ->page = 1 ;
123183 }
124184
@@ -151,6 +211,9 @@ public function clearFilters(): void
151211 $ this ->search = '' ;
152212 $ this ->cakephpSlugs = [];
153213 $ this ->phpSlugs = [];
214+ $ this ->excludedCakephpSlugs = [];
215+ $ this ->excludedPhpSlugs = [];
216+ $ this ->excludedCategories = [];
154217 $ this ->category = '' ;
155218 $ this ->awesomeOnly = false ;
156219 $ this ->featuredOnly = false ;
@@ -168,24 +231,32 @@ public function viewData(): array
168231 throw new BadRequestException ('Search query is too long. ' );
169232 }
170233 $ options = $ this ->packages ->filterOptions ();
234+ $ this ->excludedCakephpSlugs = $ this ->acceptedSlugs ($ this ->excludedCakephpSlugs , $ options ['cakephp ' ]);
235+ $ this ->excludedPhpSlugs = $ this ->acceptedSlugs ($ this ->excludedPhpSlugs , $ options ['php ' ]);
236+ $ this ->cakephpSlugs = array_values (array_diff ($ this ->acceptedSlugs ($ this ->cakephpSlugs , $ options ['cakephp ' ]), $ this ->excludedCakephpSlugs ));
237+ $ this ->phpSlugs = array_values (array_diff ($ this ->acceptedSlugs ($ this ->phpSlugs , $ options ['php ' ]), $ this ->excludedPhpSlugs ));
171238 $ includeDevelopment = array_any ($ this ->cakephpSlugs , static fn (mixed $ slug ): bool => is_string ($ slug ) && CakephpVersion ::isDevelopmentSlug ($ slug ));
172239 $ categoryOptions = $ this ->fetchTable (CategoriesTable::class)->unhydratedFind ('catalogueOptions ' , namespace: $ this ->namespace , includeDevelopment: $ includeDevelopment )->all ()->toArray ();
173- if (!array_key_exists ($ this ->category , $ categoryOptions )) {
240+ $ this ->excludedCategories = $ this ->acceptedSlugs ($ this ->excludedCategories , $ categoryOptions );
241+ if (!array_key_exists ($ this ->category , $ categoryOptions ) || in_array ($ this ->category , $ this ->excludedCategories , true )) {
174242 $ this ->category = '' ;
175243 }
176- $ this ->cakephpSlugs = array_values (array_intersect (array_filter ($ this ->cakephpSlugs , is_string (...)), array_keys ($ options ['cakephp ' ])));
177- $ this ->phpSlugs = array_values (array_intersect (array_filter ($ this ->phpSlugs , is_string (...)), array_keys ($ options ['php ' ])));
178244 $ query = [
179245 'search ' => $ this ->search ,
180246 'cakephp_slugs ' => $ this ->cakephpSlugs ,
181247 'php_slugs ' => $ this ->phpSlugs ,
248+ 'exclude_cakephp_slugs ' => $ this ->excludedCakephpSlugs ,
249+ 'exclude_php_slugs ' => $ this ->excludedPhpSlugs ,
250+ 'exclude_categories ' => $ this ->excludedCategories ,
182251 'category ' => $ this ->category ,
183252 'awesome ' => $ this ->awesomeOnly ,
184253 'featured ' => $ this ->featuredOnly ,
185254 'sort ' => $ this ->paging ->sort ,
186255 'direction ' => $ this ->paging ->direction ,
187256 ];
188- $ hasFilters = trim ($ this ->search ) !== '' || $ this ->cakephpSlugs !== [] || $ this ->phpSlugs !== [] || $ this ->category !== '' || $ this ->awesomeOnly || $ this ->featuredOnly ;
257+ $ hasFilters = trim ($ this ->search ) !== '' || $ this ->cakephpSlugs !== [] || $ this ->phpSlugs !== []
258+ || $ this ->excludedCakephpSlugs !== [] || $ this ->excludedPhpSlugs !== [] || $ this ->excludedCategories !== []
259+ || $ this ->category !== '' || $ this ->awesomeOnly || $ this ->featuredOnly ;
189260 $ packages = $ this ->paginate ($ this ->packages ->find ('catalogue ' , filters: $ query , namespace: $ this ->namespace ));
190261
191262 return [
@@ -203,4 +274,14 @@ public function viewData(): array
203274 ],
204275 ];
205276 }
277+
278+ /**
279+ * @param array<mixed> $values Untrusted URL or client selections.
280+ * @param array<array-key, mixed> $options Available choices.
281+ * @return list<string>
282+ */
283+ private function acceptedSlugs (array $ values , array $ options ): array
284+ {
285+ return array_values (array_unique (array_intersect (array_filter ($ values , is_string (...)), array_keys ($ options ))));
286+ }
206287}
0 commit comments