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
|
|
|
protected $baseViewPath; |
21
|
|
|
protected $viewKey; |
22
|
|
|
|
23
|
9 |
|
public function __construct($items = [], string $viewKey) |
24
|
|
|
{ |
25
|
9 |
|
$this->viewKey = $viewKey; |
26
|
|
|
|
27
|
9 |
|
if (!isset($this->baseViewPath)) { |
28
|
9 |
|
$this->baseViewPath = config('thinktomorrow.chief.base-view-paths.sets', 'sets'); |
29
|
|
|
} |
30
|
|
|
|
31
|
9 |
|
$this->constructWithSnippets(); |
32
|
|
|
|
33
|
9 |
|
parent::__construct($items); |
34
|
9 |
|
} |
35
|
|
|
|
36
|
2 |
|
public static function fromReference(SetReference $setReference): Set |
37
|
|
|
{ |
38
|
2 |
|
return $setReference->toSet(); |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
public function renderView(): string |
42
|
|
|
{ |
43
|
6 |
|
if ($result = $this->baseRenderView()) { |
|
|
|
|
44
|
|
|
return $result; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// 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
|
6 |
|
return ($this->viewParent && $item instanceof ViewableContract) |
51
|
6 |
|
? $item->setViewParent($this->viewParent)->renderView() |
52
|
6 |
|
: ($item->content ?? ''); |
53
|
6 |
|
})->implode(''); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Override the collection map function to include the key |
58
|
|
|
* |
59
|
|
|
* @param callable $callback |
60
|
|
|
* @return static |
61
|
|
|
*/ |
62
|
6 |
|
public function map(callable $callback) |
63
|
|
|
{ |
64
|
6 |
|
$keys = array_keys($this->items); |
65
|
|
|
|
66
|
6 |
|
$items = array_map($callback, $this->items, $keys); |
67
|
|
|
|
68
|
6 |
|
return new static(array_combine($keys, $items), $this->viewKey); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Paginate the collection with a simple navigation (prev and next) |
73
|
|
|
* |
74
|
|
|
* @param int $perPage |
75
|
|
|
* @param null $currentPage |
|
|
|
|
76
|
|
|
* @return Paginator |
77
|
|
|
*/ |
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
|
|
|
* @param null $currentPage |
|
|
|
|
93
|
|
|
* @return Paginator |
94
|
|
|
*/ |
95
|
|
|
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
|
|
|
|