Skip to content

Commit 2e727ad

Browse files
committed
Allow excluding versions and categories from the catalogue
1 parent 88633b4 commit 2e727ad

15 files changed

Lines changed: 504 additions & 56 deletions

File tree

‎resources/css/style.css‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ body::before {
333333
@apply border-dashed;
334334
}
335335
.version-pill {
336-
@apply h-auto min-h-6 shrink-0 rounded-full border-base-300 bg-transparent px-1.75 py-0.5 text-xs leading-tight
336+
@apply relative h-auto min-h-6 shrink-0 rounded-full border-base-300 bg-transparent px-1.75 py-0.5 text-xs leading-tight
337337
font-medium whitespace-nowrap text-muted no-underline hover:border-primary hover:text-primary
338338
focus-visible:outline-2 focus-visible:outline-offset-2 aria-pressed:border-primary aria-pressed:bg-primary
339339
aria-pressed:text-primary-content forced-colors:aria-pressed:outline-2
@@ -371,11 +371,17 @@ body::before {
371371
& input { @apply w-full min-h-10 rounded-lg border-control bg-base-100 px-3 py-2 text-[0.8125rem]; }
372372
}
373373
.category-choice {
374-
@apply flex min-h-12 min-w-0 items-center justify-between gap-2 rounded-lg border border-base-300 p-2 text-left
374+
@apply relative flex min-h-12 min-w-0 items-center justify-between gap-2 rounded-lg border border-base-300 p-2 text-left
375375
text-[0.8125rem] text-base-content no-underline hover:border-primary hover:bg-row-hover
376376
aria-[current=true]:border-primary aria-[current=true]:bg-primary/8 aria-[current=true]:text-primary md:px-3
377377
md:py-2.5;
378378
}
379+
.version-pill[data-filter-state="exclude"], .category-choice[data-filter-state="exclude"] {
380+
@apply border-dashed border-warning bg-warning/10 text-warning hover:border-warning hover:bg-warning/15 hover:text-warning;
381+
}
382+
.filter-excluded-marker {
383+
@apply absolute -top-1 -right-1 grid size-3.5 place-items-center rounded-full bg-warning text-xs leading-none font-bold text-warning-content;
384+
}
379385
.catalogue-summary {
380386
@apply flex min-w-0 flex-wrap items-center gap-2.5;
381387
& h2 { @apply text-base leading-[1.6] font-[650]; }

‎resources/js/app.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const register = (Alpine) => {
8181
if (control.hasAttribute('data-clear-catalogue')) return this.$server.call('clearFilters')
8282
if (control.hasAttribute('data-category-slug')) {
8383
this.closeCategories(control.classList.contains('selected-category'))
84-
return this.$server.call('selectCategory', control.dataset.categorySlug)
84+
return this.$server.call(control.hasAttribute('data-category-cycle') ? 'toggleCategory' : 'selectCategory', control.dataset.categorySlug)
8585
}
8686
if (control.hasAttribute('data-toggle-featured')) return this.$server.call('toggleFeatured')
8787
if (control.hasAttribute('data-toggle-awesome')) return this.$server.call('toggleAwesome')
@@ -91,10 +91,9 @@ const register = (Alpine) => {
9191
selectVersion(event) {
9292
return this.$server.call('selectVersion', event.detail.group, event.detail.slug)
9393
},
94-
clearVersions(property, event) {
94+
clearVersions(group, event) {
9595
event.target.closest('details').querySelector('summary').focus({ preventScroll: true })
96-
this.$server.state[property] = []
97-
return this.$server.sync()
96+
return this.$server.call('clearVersions', group)
9897
},
9998
selectCategory(event) {
10099
this.closeCategories()
@@ -212,7 +211,8 @@ const refreshDocument = () => {
212211
// Error and private-page restrictions belong to the server and remain intact.
213212
if (!canonical || robots?.content === 'noindex,nofollow') return
214213
const params = new URL(window.location.href).searchParams
215-
const filtered = JSON.parse(canonical.dataset.filterQueryKeys).some(key => params.has(key))
214+
const filterKeys = new Set(JSON.parse(canonical.dataset.filterQueryKeys))
215+
const filtered = Array.from(params.keys()).some(key => filterKeys.has(key.replace(/\[.*$/, '')))
216216
if (filtered && !robots) {
217217
robots = document.createElement('meta')
218218
robots.name = 'robots'

‎src/Island/CatalogueIsland.php‎

Lines changed: 100 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Model\Table\CategoriesTable;
77
use App\Model\Table\PackagesTable;
88
use App\Utility\CakephpVersion;
9+
use App\Utility\FilterSelection;
910
use Cake\Http\Exception\BadRequestException;
1011
use Interact\Attribute\Action;
1112
use 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
}

‎src/Model/Filter/PackagesCollection.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,29 @@ public function initialize(): void
2525

2626
$this->addTaggedSlugFilter('cakephp_slugs');
2727
$this->addTaggedSlugFilter('php_slugs');
28+
$this->addTaggedSlugFilter('exclude_cakephp_slugs', exclude: true);
29+
$this->addTaggedSlugFilter('exclude_php_slugs', exclude: true);
2830
}
2931

3032
/**
3133
* @param string $filterName
3234
* @return void
3335
*/
34-
protected function addTaggedSlugFilter(string $filterName): void
36+
protected function addTaggedSlugFilter(string $filterName, bool $exclude = false): void
3537
{
3638
$this->callback($filterName, [
37-
'callback' => function (SelectQuery $query, array $args) use ($filterName): bool {
39+
'callback' => function (SelectQuery $query, array $args) use ($filterName, $exclude): bool {
3840
$value = $args[$filterName];
3941
if (is_array($value)) {
4042
// Selection order and duplicates must not fragment the query cache.
4143
$value = array_values(array_unique($value));
4244
sort($value, SORT_STRING);
4345
}
44-
$query->find('tagged', value: $value);
46+
if ($exclude) {
47+
$query->find('withoutTags', slugs: (array)$value);
48+
} else {
49+
$query->find('tagged', value: $value);
50+
}
4551

4652
return true;
4753
},

‎src/Model/Table/PackagesTable.php‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,61 @@ public function findCatalogue(SelectQuery $query, array $filters, ?string $names
188188
}
189189
$query->where(static fn(QueryExpression $exp) => $exp->exists($membership));
190190
}
191+
if (!empty($filters['exclude_categories'])) {
192+
$query->find('withoutCategories', slugs: $filters['exclude_categories']);
193+
}
191194

192195
return $this->cachePublicQuery($query, 'catalogue', count: true);
193196
}
194197

198+
/**
199+
* Exclude any matching compatibility tag, including on packages with other supported versions.
200+
*
201+
* @template TSubject of \Cake\Datasource\EntityInterface|array
202+
* @param \Cake\ORM\Query\SelectQuery<TSubject> $query Package query.
203+
* @param list<string> $slugs Excluded tag slugs.
204+
* @return \Cake\ORM\Query\SelectQuery<TSubject>
205+
*/
206+
public function findWithoutTags(SelectQuery $query, array $slugs): SelectQuery
207+
{
208+
if ($slugs === []) {
209+
return $query;
210+
}
211+
$junction = $this->Tagged->getTarget();
212+
$alias = $junction->getAlias();
213+
$membership = $junction->find()->select([$alias . '.fk_id'])
214+
->innerJoin(['ExcludedTag' => $this->Tags->getTarget()->getTable()], ['ExcludedTag.id = ' . $alias . '.tag_id'])
215+
->where([$alias . '.fk_model' => $this->getAlias(), 'ExcludedTag.slug IN' => $slugs])
216+
->where(static fn(QueryExpression $exp) => $exp->equalFields($alias . '.fk_id', 'Packages.id'));
217+
218+
return $query->where(static fn(QueryExpression $exp) => $exp->notExists($membership));
219+
}
220+
221+
/**
222+
* Exclude category membership while retaining uncategorized plugins.
223+
*
224+
* @template TSubject of \Cake\Datasource\EntityInterface|array
225+
* @param \Cake\ORM\Query\SelectQuery<TSubject> $query Package query.
226+
* @param list<string> $slugs Excluded category slugs.
227+
* @return \Cake\ORM\Query\SelectQuery<TSubject>
228+
*/
229+
public function findWithoutCategories(SelectQuery $query, array $slugs): SelectQuery
230+
{
231+
if ($slugs === []) {
232+
return $query;
233+
}
234+
$slugs = array_values(array_unique($slugs));
235+
sort($slugs, SORT_STRING);
236+
$junction = $this->Categories->junction();
237+
$alias = $junction->getAlias();
238+
$membership = $junction->find()->select([$alias . '.package_id'])
239+
->innerJoin(['ExcludedCategory' => $this->Categories->getTarget()->getTable()], ['ExcludedCategory.id = ' . $alias . '.category_id'])
240+
->where(['ExcludedCategory.slug IN' => $slugs])
241+
->where(static fn(QueryExpression $exp) => $exp->equalFields($alias . '.package_id', 'Packages.id'));
242+
243+
return $query->where(static fn(QueryExpression $exp) => $exp->notExists($membership));
244+
}
245+
195246
/**
196247
* Preserve the normal directory's published-release and download eligibility.
197248
*

‎src/Utility/FilterSelection.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Utility;
5+
6+
/**
7+
* Keep generated links and island actions on the same three-state cycle.
8+
*/
9+
final class FilterSelection
10+
{
11+
/**
12+
* @param list<string> $included Included options.
13+
* @param list<string> $excluded Excluded options.
14+
* @return array{included: list<string>, excluded: list<string>}
15+
*/
16+
public static function cycle(array $included, array $excluded, string $slug, bool $multiple = true): array
17+
{
18+
if (in_array($slug, $excluded, true)) {
19+
$excluded = array_values(array_diff($excluded, [$slug]));
20+
$included = array_values(array_diff($included, [$slug]));
21+
} elseif (in_array($slug, $included, true)) {
22+
$included = array_values(array_diff($included, [$slug]));
23+
$excluded[] = $slug;
24+
} else {
25+
$included = $multiple ? [...$included, $slug] : [$slug];
26+
}
27+
28+
return ['included' => $included, 'excluded' => $excluded];
29+
}
30+
}

‎src/View/Helper/SeoHelper.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class SeoHelper extends Helper
1616
{
1717
public const array FILTER_QUERY_KEYS = [
1818
'search', 'cakephp_slugs', 'php_slugs', 'category', 'awesome', 'featured',
19+
'exclude_cakephp_slugs', 'exclude_php_slugs', 'exclude_categories',
1920
'sort', 'direction', 'page', 'keyword', 'query', 'cakephp',
2021
];
2122

0 commit comments

Comments
 (0)