Skip to content

Commit fe47c01

Browse files
committed
Abstract
1 parent f718e21 commit fe47c01

File tree

4 files changed

+52
-61
lines changed

4 files changed

+52
-61
lines changed

src/Fieldtypes/Entries.php

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Statamic\Facades\Search;
1717
use Statamic\Facades\Site;
1818
use Statamic\Facades\User;
19+
use Statamic\Http\Controllers\CP\Collections\QueriesAuthorEntries;
1920
use Statamic\Http\Resources\CP\Entries\EntriesFieldtypeEntries;
2021
use Statamic\Http\Resources\CP\Entries\EntriesFieldtypeEntry as EntryResource;
2122
use Statamic\Query\OrderedQueryBuilder;
@@ -31,7 +32,8 @@
3132

3233
class Entries extends Relationship
3334
{
34-
use QueriesFilters;
35+
use QueriesAuthorEntries,
36+
QueriesFilters;
3537

3638
protected $categories = ['relationship'];
3739
protected $keywords = ['entry'];
@@ -147,28 +149,7 @@ public function getIndexItems($request)
147149
collect($this->getConfiguredCollections())
148150
->map(fn ($handle) => Collection::findByHandle($handle))
149151
->filter(fn ($collection) => User::current()->cant('view-other-authors-entries', [EntryContract::class, $collection]))
150-
->each(function ($collection) use ($query) {
151-
$blueprints = $collection->entryBlueprints();
152-
153-
$blueprintsWithAuthor = $blueprints
154-
->filter(fn ($blueprint) => $blueprint->hasField('author'))
155-
->map->handle()->all();
156-
157-
$blueprintsWithoutAuthor = $blueprints
158-
->diff($blueprintsWithAuthor)
159-
->map->handle()->all();
160-
161-
$query
162-
->whereNotIn('collection', [$collection->handle()])
163-
->orWhere(fn ($query) => $query
164-
->where(fn ($query) => $query
165-
->whereIn('blueprint', $blueprintsWithAuthor)
166-
->whereIn('author', [User::current()->id()])
167-
->orWhereJsonContains('author', User::current()->id())
168-
)
169-
->orWhereIn('blueprint', $blueprintsWithoutAuthor)
170-
);
171-
});
152+
->each(fn ($collection) => $this->queryAuthorEntries($query, $collection));
172153

173154
$this->activeFilterBadges = $this->queryFilters($query, $filters, $this->getSelectionFilterContext());
174155

src/Http/Controllers/CP/Collections/CollectionsController.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
class CollectionsController extends CpController
2626
{
27+
use QueriesAuthorEntries;
28+
2729
public function index(Request $request)
2830
{
2931
$this->authorize('index', CollectionContract::class, __('You are not authorized to view collections.'));
@@ -57,26 +59,10 @@ private function collections()
5759
})->map(function ($collection) {
5860
$entriesCount = $collection->queryEntries()
5961
->where('site', Site::selected())
60-
->when(User::current()->cant('view-other-authors-entries', [EntryContract::class, $collection]), function ($query) use ($collection) {
61-
$blueprints = $collection->entryBlueprints();
62-
63-
$blueprintsWithAuthor = $blueprints
64-
->filter(fn ($blueprint) => $blueprint->hasField('author'))
65-
->map->handle()->all();
66-
67-
$blueprintsWithoutAuthor = $blueprints
68-
->diff($blueprintsWithAuthor)
69-
->map->handle()->all();
70-
71-
$query->where(fn ($query) => $query
72-
->where(fn ($query) => $query
73-
->whereIn('blueprint', $blueprintsWithAuthor)
74-
->whereIn('author', [User::current()->id()])
75-
->orWhereJsonContains('author', User::current()->id())
76-
)
77-
->orWhereIn('blueprint', $blueprintsWithoutAuthor)
78-
);
79-
})
62+
->when(
63+
User::current()->cant('view-other-authors-entries', [EntryContract::class, $collection]),
64+
fn ($query) => $this->queryAuthorEntries($query, $collection)
65+
)
8066
->count();
8167

8268
return [

src/Http/Controllers/CP/Collections/EntriesController.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
class EntriesController extends CpController
2626
{
2727
use ExtractsFromEntryFields,
28+
QueriesAuthorEntries,
2829
QueriesFilters;
2930

3031
public function index(FilteredRequest $request, $collection)
@@ -85,24 +86,7 @@ protected function indexQuery($collection)
8586
}
8687

8788
if (User::current()->cant('view-other-authors-entries', [EntryContract::class, $collection])) {
88-
$blueprints = $collection->entryBlueprints();
89-
90-
$blueprintsWithAuthor = $blueprints
91-
->filter(fn ($blueprint) => $blueprint->hasField('author'))
92-
->map->handle()->all();
93-
94-
$blueprintsWithoutAuthor = $blueprints
95-
->diff($blueprintsWithAuthor)
96-
->map->handle()->all();
97-
98-
$query->where(fn ($query) => $query
99-
->where(fn ($query) => $query
100-
->whereIn('blueprint', $blueprintsWithAuthor)
101-
->whereIn('author', [User::current()->id()])
102-
->orWhereJsonContains('author', User::current()->id())
103-
)
104-
->orWhereIn('blueprint', $blueprintsWithoutAuthor)
105-
);
89+
$this->queryAuthorEntries($query, $collection);
10690
}
10791

10892
return $query;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Statamic\Http\Controllers\CP\Collections;
4+
5+
use Statamic\Contracts\Entries\Collection;
6+
use Statamic\Contracts\Query\Builder;
7+
use Statamic\Facades\User;
8+
use Statamic\Fields\Blueprint;
9+
use Statamic\Support\FileCollection;
10+
11+
trait QueriesAuthorEntries
12+
{
13+
protected function queryAuthorEntries(Builder $query, Collection $collection): void
14+
{
15+
$query
16+
->where(fn ($query) => $query
17+
->whereNotIn('collection', [$collection->handle()]) // Needed for entries fieldtypes configured for multiple collections
18+
->orWhere(fn ($query) => $query
19+
->whereIn('blueprint', $this->blueprintsWithAuthor($collection->entryBlueprints()))
20+
->whereIn('author', [User::current()->id()])
21+
->orWhereJsonContains('author', User::current()->id())
22+
)
23+
->orWhereIn('blueprint', $this->blueprintsWithoutAuthor($collection->entryBlueprints()))
24+
);
25+
}
26+
27+
protected function blueprintsWithAuthor(FileCollection $blueprints): array
28+
{
29+
return $blueprints
30+
->filter(fn (Blueprint $blueprint) => $blueprint->hasField('author'))
31+
->map->handle()->all();
32+
}
33+
34+
protected function blueprintsWithoutAuthor(FileCollection $blueprints): array
35+
{
36+
return $blueprints
37+
->filter(fn (Blueprint $blueprint) => ! $blueprint->hasField('author'))
38+
->map->handle()->all();
39+
}
40+
}

0 commit comments

Comments
 (0)