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