forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollection.php
78 lines (67 loc) · 2.52 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
namespace Statamic\Widgets;
use Statamic\Contracts\Entries\Entry as EntryContract;
use Statamic\CP\Column;
use Statamic\Facades\Collection as CollectionAPI;
use Statamic\Facades\Scope;
use Statamic\Facades\User;
class Collection extends Widget
{
/**
* The HTML that should be shown in the widget.
*
* @return \Illuminate\View\View
*/
public function html()
{
$collection = $this->config('collection');
if (! CollectionAPI::handleExists($collection)) {
return "Error: Collection [$collection] doesn't exist.";
}
$collection = CollectionAPI::findByHandle($collection);
if (! User::current()->can('view', $collection)) {
return;
}
[$sortColumn, $sortDirection] = $this->parseSort($collection);
$blueprint = $collection->entryBlueprint();
$columns = $blueprint
->columns()
->put('status', Column::make('status')
->listable(true)
->visible(true)
->defaultVisibility(true)
->sortable(false))
->only($this->config('fields', []))
->map(fn ($column) => $column->sortable(false)->visible(true))
->values();
return view('statamic::widgets.collection', [
'collection' => $collection,
'filters' => Scope::filters('entries', [
'collection' => $collection->handle(),
]),
'title' => $this->config('title', $collection->title()),
'button' => $collection->createLabel(),
'blueprints' => $collection->entryBlueprints()->reject->hidden()->values(),
'limit' => $this->config('limit', 5),
'sortColumn' => $sortColumn,
'sortDirection' => $sortDirection,
'columns' => $columns,
'canCreate' => User::current()->can('create', [EntryContract::class, $collection]) && $collection->hasVisibleEntryBlueprint(),
]);
}
/**
* Parse sort column and direction, similar to how sorting works on collection tag.
*
* @param \Statamic\Entries\Collection $collection
* @return array
*/
protected function parseSort($collection)
{
$default = $collection->dated() ? 'date:desc' : 'title:asc';
$sort = $this->config('order_by') ?? $this->config('sort') ?? $default;
$exploded = explode(':', $sort);
$column = $exploded[0];
$direction = $exploded[1] ?? 'asc';
return [$column, $direction];
}
}