1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Thinktomorrow\Chief\Concerns\Viewable; |
6
|
|
|
|
7
|
|
|
use Thinktomorrow\Chief\Modules\Module; |
8
|
|
|
use Thinktomorrow\Chief\Pages\Page; |
9
|
|
|
use Thinktomorrow\Chief\Relations\ActsAsParent; |
10
|
|
|
use Thinktomorrow\Chief\Sets\Set; |
11
|
|
|
use Thinktomorrow\Chief\Management\ManagedModel; |
12
|
|
|
|
13
|
|
|
trait Viewable |
14
|
|
|
{ |
15
|
|
|
protected $viewParent; |
16
|
|
|
|
17
|
11 |
|
public function renderView(): string |
18
|
|
|
{ |
19
|
11 |
|
$renderedView = $this->renderRawView(); |
20
|
|
|
|
21
|
|
|
// In case of snippets, we inject these after the initial view rendering |
22
|
11 |
|
if (method_exists($this, 'shouldParseWithSnippets') && $this->shouldParseWithSnippets($renderedView)) { |
23
|
3 |
|
$renderedView = $this->parseWithSnippets($renderedView); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
11 |
|
return $renderedView; |
27
|
|
|
} |
28
|
|
|
|
29
|
11 |
|
private function renderRawView(): string |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
11 |
|
return view($this->viewPath(), $this->viewData())->render(); |
|
|
|
|
33
|
11 |
|
} catch (NotFoundView $e) { |
34
|
11 |
|
if (config('thinktomorrow.chief.strict')) { |
35
|
|
|
throw $e; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// If no view has been created for this model, we try once again to fetch the content value if any. This will silently fail |
40
|
|
|
// if no content value is present. We consider the 'content' attribute to be a default for our copy. |
41
|
11 |
|
return isset($this->content) ? (string)$this->content : ''; |
42
|
|
|
} |
43
|
|
|
|
44
|
12 |
|
public function setViewParent(ActsAsParent $parent): ViewableContract |
45
|
|
|
{ |
46
|
12 |
|
$this->viewParent = $parent; |
47
|
|
|
|
48
|
12 |
|
return $this; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* This is the model's view identifier. This key is used to determine the full view |
53
|
|
|
* path of the model. By default this is based on the morphKey value of the model. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
* @throws NotFoundViewKey |
57
|
|
|
*/ |
58
|
11 |
|
public function viewKey(): string |
59
|
|
|
{ |
60
|
11 |
|
if (property_exists($this, 'viewKey') && isset($this->viewKey)) { |
61
|
6 |
|
return $this->viewKey; |
62
|
|
|
} |
63
|
|
|
|
64
|
10 |
|
if ($this instanceof ManagedModel) { |
65
|
10 |
|
return $this->managedModelKey(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (config('thinktomorrow.chief.strict')) { |
69
|
|
|
throw new NotFoundViewKey('Missing view key. Please add a [viewKey] property to ' . get_class($this)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return ''; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Group identifier for a page set. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
* @throws NotFoundViewKey |
80
|
|
|
*/ |
81
|
|
|
public function setKey(): string |
82
|
|
|
{ |
83
|
11 |
|
return $this->viewKey(); |
84
|
|
|
} |
85
|
11 |
|
|
86
|
|
|
/** |
87
|
|
|
* This is the full path reference for this model's view file. This is relative to your main view folder |
88
|
|
|
* e.g. key 'articles.show'. A sensible default is set and determined based on the viewKey value. |
89
|
|
|
* But you are free to override this and change it to another value to fit your own logic. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
* @throws NotFoundView |
93
|
|
|
*/ |
94
|
|
|
protected function viewPath(): string |
95
|
|
|
{ |
96
|
|
|
return ViewPath::make($this, $this->viewParent, $this->baseViewPath ?? null)->get(); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private function viewData(): array |
103
|
|
|
{ |
104
|
|
|
$viewData = [ |
105
|
|
|
'model' => $this, |
106
|
|
|
'parent' => $this->viewParent, |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
/** @deprecated since 0.3 in favor of generic 'model' variable */ |
110
|
|
|
if ($this instanceof Page) { |
111
|
|
|
$viewData['page'] = $this; |
112
|
|
|
} |
113
|
|
|
if ($this instanceof Module) { |
114
|
|
|
$viewData['module'] = $this; |
115
|
|
|
} |
116
|
|
|
if ($this instanceof Set) { |
117
|
|
|
$viewData['collection'] = $this; |
118
|
|
|
$viewData['pages'] = $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $viewData; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|