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