|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Thinktomorrow\Chief\Sets; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Thinktomorrow\Chief\Concerns\Viewable\Viewable; |
|
9
|
|
|
use Thinktomorrow\Chief\Snippets\WithSnippets; |
|
10
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
|
11
|
|
|
use Thinktomorrow\Chief\Concerns\Viewable\ViewableContract; |
|
12
|
|
|
|
|
13
|
|
|
class Set extends Collection implements ViewableContract |
|
14
|
|
|
{ |
|
15
|
|
|
use WithSnippets, |
|
|
|
|
|
|
16
|
|
|
Viewable{ |
|
17
|
|
|
renderView as baseRenderView; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
9 |
|
protected $baseViewPath; |
|
21
|
|
|
protected $viewKey; |
|
22
|
9 |
|
|
|
23
|
|
|
public function __construct($items = [], string $viewKey) |
|
24
|
9 |
|
{ |
|
25
|
|
|
$this->viewKey = $viewKey; |
|
26
|
9 |
|
|
|
27
|
9 |
|
if (!isset($this->baseViewPath)) { |
|
28
|
|
|
$this->baseViewPath = config('thinktomorrow.chief.base-view-paths.sets', 'sets'); |
|
29
|
6 |
|
} |
|
30
|
|
|
|
|
31
|
6 |
|
$this->constructWithSnippets(); |
|
32
|
|
|
|
|
33
|
|
|
parent::__construct($items); |
|
34
|
2 |
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
public static function fromReference(SetReference $setReference): Set |
|
37
|
|
|
{ |
|
38
|
|
|
return $setReference->toSet(); |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
6 |
|
public function renderView(): string |
|
42
|
|
|
{ |
|
43
|
6 |
|
if ($result = $this->baseRenderView()) { |
|
|
|
|
|
|
44
|
|
|
return $result; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
6 |
|
// If no view has been created for this page collection, we try once again to fetch the content value if any. This will silently fail |
|
48
|
|
|
// if no content value is present. We don't consider the 'content' attribute to be a default as we do for module. |
|
49
|
|
|
return $this->map(function ($item) { |
|
50
|
|
|
return ($this->viewParent && $item instanceof ViewableContract) |
|
51
|
|
|
? $item->setViewParent($this->viewParent)->renderView() |
|
52
|
|
|
: ($item->content ?? ''); |
|
53
|
|
|
})->implode(''); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
6 |
|
* Override the collection map function to include the key |
|
58
|
|
|
* |
|
59
|
|
|
* @param callable $callback |
|
60
|
6 |
|
* @return static |
|
61
|
6 |
|
*/ |
|
62
|
6 |
|
public function map(callable $callback) |
|
63
|
6 |
|
{ |
|
64
|
|
|
$keys = array_keys($this->items); |
|
65
|
|
|
|
|
66
|
|
|
$items = array_map($callback, $this->items, $keys); |
|
67
|
|
|
|
|
68
|
6 |
|
return new static(array_combine($keys, $items), $this->viewKey); |
|
69
|
6 |
|
} |
|
70
|
6 |
|
|
|
71
|
6 |
|
/** |
|
72
|
6 |
|
* Paginate the collection with a simple navigation (prev and next) |
|
73
|
|
|
* |
|
74
|
|
|
* @param int $perPage |
|
75
|
6 |
|
* @param null $currentPage |
|
|
|
|
|
|
76
|
6 |
|
* @return Paginator |
|
77
|
6 |
|
*/ |
|
78
|
|
|
public function simplePaginate($perPage = 12, $currentPage = null): Paginator |
|
79
|
|
|
{ |
|
80
|
|
|
$currentPage = $currentPage ?? request()->get('page', 1); |
|
81
|
|
|
$path = request()->path(); |
|
82
|
|
|
$items = array_slice($this->all(), ($currentPage - 1) * $perPage); |
|
83
|
|
|
|
|
84
|
|
|
return (new \Illuminate\Pagination\Paginator($items, $perPage, $currentPage))->setPath($path); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Paginate the collection with a length aware pagination result which allows |
|
89
|
|
|
* to navigate to the first, last or any specific page |
|
90
|
|
|
* |
|
91
|
|
|
* @param int $perPage |
|
92
|
6 |
|
* @param null $currentPage |
|
|
|
|
|
|
93
|
6 |
|
* @return Paginator |
|
94
|
6 |
|
*/ |
|
95
|
6 |
|
public function paginate($perPage = 12, $currentPage = null): Paginator |
|
96
|
|
|
{ |
|
97
|
|
|
$currentPage = $currentPage ?? request()->get('page', 1); |
|
98
|
|
|
$path = request()->path(); |
|
99
|
|
|
$items = array_slice($this->all(), ($currentPage - 1) * $perPage, $perPage); |
|
100
|
|
|
|
|
101
|
|
|
return (new \Illuminate\Pagination\LengthAwarePaginator($items, $this->count(), $perPage, $currentPage))->setPath($path); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|