Skip to content

Commit

Permalink
[5.x] Collection Actions (#10471)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
edalzell and jasonvarga authored Aug 13, 2024
1 parent 302c69d commit 953412a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 15 deletions.
27 changes: 24 additions & 3 deletions resources/js/components/collections/Listing.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<data-list ref="dataList" :columns="columns" :rows="rows">
<data-list ref="dataList" :columns="columns" :rows="items">
<div class="card overflow-hidden p-0" slot-scope="{ filteredRows: rows }">
<data-list-table :rows="rows">
<template slot="cell-title" slot-scope="{ row: collection }">
Expand All @@ -12,6 +12,12 @@
<dropdown-item v-if="collection.editable" :text="__('Edit Collection')" :redirect="collection.edit_url" />
<dropdown-item v-if="collection.blueprint_editable" :text="__('Edit Blueprints')" :redirect="collection.blueprints_url" />
<dropdown-item v-if="collection.editable" :text="__('Scaffold Views')" :redirect="collection.scaffold_url" />
<data-list-inline-actions
:item="collection.id"
:url="collection.actions_url"
:actions="collection.actions"
@completed="actionCompleted"
></data-list-inline-actions>
<dropdown-item
v-if="collection.deleteable"
:text="__('Delete Collection')"
Expand Down Expand Up @@ -45,8 +51,23 @@ export default {
data() {
return {
rows: this.initialRows,
columns: this.initialColumns
initializedRequest: false,
items: this.initialRows,
requestUrl: cp_url(`collections`),
}
},
methods: {
request() {
// If we have initial data, we don't need to perform a request.
// Subsequent requests, like after performing actions, we do want to perform a request.
if (! this.initializedRequest) {
this.loading = false;
this.initializedRequest = true;
return;
}
Listing.methods.request.call(this);
}
}
Expand Down
9 changes: 8 additions & 1 deletion resources/js/components/collections/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<h1 class="flex-1" v-text="__(title)" />

<dropdown-list class="rtl:ml-2 ltr:mr-2" v-if="!!this.$scopedSlots.twirldown">
<slot name="twirldown" />
<slot name="twirldown" :actionCompleted="actionCompleted" />
</dropdown-list>

<div class="btn-group rtl:ml-4 ltr:mr-4" v-if="canUseStructureTree && !treeIsDirty">
Expand Down Expand Up @@ -168,9 +168,12 @@ import PageTree from '../structures/PageTree.vue';
import DeleteEntryConfirmation from './DeleteEntryConfirmation.vue';
import DeleteLocalizationConfirmation from './DeleteLocalizationConfirmation.vue';
import SiteSelector from '../SiteSelector.vue';
import HasActions from '../publish/HasActions';
export default {
mixins: [HasActions],
components: {
PageTree,
DeleteEntryConfirmation,
Expand Down Expand Up @@ -358,6 +361,10 @@ export default {
editPage(page, vm, store, $event) {
const url = page.edit_url;
$event.metaKey ? window.open(url) : window.location = url;
},
afterActionSuccessfullyCompleted(response) {
if (!response.redirect) window.location.reload();
}
}
Expand Down
9 changes: 8 additions & 1 deletion resources/views/collections/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
|| auth()->user()->can('delete', $collection)
|| auth()->user()->can('configure fields')
)
<template #twirldown>
<template #twirldown="{ actionCompleted }">
@can('edit', $collection)
<dropdown-item :text="__('Edit Collection')" redirect="{{ $collection->editUrl() }}"></dropdown-item>
@endcan
Expand All @@ -46,6 +46,12 @@
@can('edit', $collection)
<dropdown-item :text="__('Scaffold Views')" redirect="{{ cp_route('collections.scaffold', $collection->handle()) }}"></dropdown-item>
@endcan
<data-list-inline-actions
item="{{ $collection->handle() }}"
url="{{ cp_route('collections.actions.run', ['collection' => $collection->handle()]) }}"
:actions="{{ $actions }}"
@completed="actionCompleted"
></data-list-inline-actions>
@can('delete', $collection)
<dropdown-item :text="__('Delete Collection')" class="warning" @click="$refs.deleter.confirm()">
<resource-deleter
Expand All @@ -56,6 +62,7 @@
></resource-deleter>
</dropdown-item>
@endcan

</template>
@endif
</collection-view>
Expand Down
2 changes: 2 additions & 0 deletions routes/cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Statamic\Http\Controllers\CP\Auth\LoginController;
use Statamic\Http\Controllers\CP\Auth\ResetPasswordController;
use Statamic\Http\Controllers\CP\Auth\UnauthorizedController;
use Statamic\Http\Controllers\CP\Collections\CollectionActionController;
use Statamic\Http\Controllers\CP\Collections\CollectionBlueprintsController;
use Statamic\Http\Controllers\CP\Collections\CollectionsController;
use Statamic\Http\Controllers\CP\Collections\CollectionTreeController;
Expand Down Expand Up @@ -147,6 +148,7 @@

Route::get('collections/{collection}/tree', [CollectionTreeController::class, 'index'])->name('collections.tree.index');
Route::patch('collections/{collection}/tree', [CollectionTreeController::class, 'update'])->name('collections.tree.update');
Route::post('collections/{collection}/actions', [CollectionActionController::class, 'run'])->name('collections.actions.run');

Route::group(['prefix' => 'collections/{collection}/entries'], function () {
Route::get('/', [EntriesController::class, 'index'])->name('collections.entries.index');
Expand Down
14 changes: 14 additions & 0 deletions src/Http/Controllers/CP/Collections/CollectionActionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Statamic\Http\Controllers\CP\Collections;

use Statamic\Facades\Collection;
use Statamic\Http\Controllers\CP\ActionController;

class CollectionActionController extends ActionController
{
protected function getSelectedItems($items, $context)
{
return $items->map(fn ($item) => Collection::find($item));
}
}
38 changes: 28 additions & 10 deletions src/Http/Controllers/CP/Collections/CollectionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Statamic\Contracts\Entries\Entry as EntryContract;
use Statamic\CP\Column;
use Statamic\Exceptions\SiteNotFoundException;
use Statamic\Facades\Action;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Collection;
use Statamic\Facades\Scope;
Expand All @@ -21,11 +22,33 @@

class CollectionsController extends CpController
{
public function index()
public function index(Request $request)
{
$this->authorize('index', CollectionContract::class, __('You are not authorized to view collections.'));

$collections = Collection::all()->filter(function ($collection) {
$columns = [
Column::make('title')->label(__('Title')),
Column::make('entries')->label(__('Entries'))->numeric(true),
];

if ($request->wantsJson()) {
return [
'data' => $this->collections(),
'meta' => [
'columns' => $columns,
],
];
}

return view('statamic::collections.index', [
'collections' => $this->collections(),
'columns' => $columns,
]);
}

private function collections()
{
return Collection::all()->filter(function ($collection) {
return User::current()->can('configure collections')
|| User::current()->can('view', $collection)
&& $collection->sites()->contains(Site::selected()->handle());
Expand All @@ -44,16 +67,10 @@ public function index()
'editable' => User::current()->can('edit', $collection),
'blueprint_editable' => User::current()->can('configure fields'),
'available_in_selected_site' => $collection->sites()->contains(Site::selected()->handle()),
'actions' => Action::for($collection),
'actions_url' => cp_route('collections.actions.run', ['collection' => $collection->handle()]),
];
})->values();

return view('statamic::collections.index', [
'collections' => $collections,
'columns' => [
Column::make('title')->label(__('Title')),
Column::make('entries')->label(__('Entries'))->numeric(true),
],
]);
}

public function show(Request $request, $collection)
Expand Down Expand Up @@ -104,6 +121,7 @@ public function show(Request $request, $collection)
->all(),
'canCreate' => User::current()->can('create', [EntryContract::class, $collection]) && $collection->hasVisibleEntryBlueprint(),
'canChangeLocalizationDeleteBehavior' => count($authorizedSites) > 1 && (count($authorizedSites) == $collection->sites()->count()),
'actions' => Action::for($collection),
];

if ($collection->queryEntries()->count() === 0) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Feature/Collections/ViewCollectionListingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function it_shows_a_list_of_collections()
'editable' => true,
'blueprint_editable' => true,
'available_in_selected_site' => true,
'actions' => collect(),
'actions_url' => 'http://localhost/cp/collections/foo/actions',
],
[
'id' => 'bar',
Expand All @@ -59,6 +61,8 @@ public function it_shows_a_list_of_collections()
'editable' => true,
'blueprint_editable' => true,
'available_in_selected_site' => true,
'actions' => collect(),
'actions_url' => 'http://localhost/cp/collections/bar/actions',
],
]))
->assertDontSee('no-results');
Expand Down

0 comments on commit 953412a

Please sign in to comment.