1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chadanuk\MiniCms; |
4
|
|
|
|
5
|
|
|
use Chadanuk\MiniCms\Blocks\Block; |
6
|
|
|
use Illuminate\Support\Facades\View; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Chadanuk\MiniCms\Blocks\BlockContent; |
9
|
|
|
use Chadanuk\MiniCms\Events\PageCreating; |
10
|
|
|
use Chadanuk\MiniCms\Blocks\BlockTypeAbstract; |
11
|
|
|
use Chadanuk\MiniCms\Blocks\BlockFactory; |
12
|
|
|
|
13
|
|
|
class Page extends Model |
14
|
|
|
{ |
15
|
|
|
protected $guarded = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The event map for the model. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $dispatchesEvents = [ |
23
|
|
|
'creating' => PageCreating::class, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the route key for the model. |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getRouteKeyName() |
32
|
|
|
{ |
33
|
|
|
return 'slug'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Retrieve the model for a bound value. |
38
|
|
|
* |
39
|
|
|
* @param mixed $value |
40
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null |
41
|
|
|
*/ |
42
|
|
|
public function resolveRouteBinding($value = 'home') |
43
|
|
|
{ |
44
|
|
|
return $this->where('slug', $value)->first() ?? abort(404); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function findBySlug(String $slug) |
48
|
|
|
{ |
49
|
|
|
return self::where('slug', $slug)->first(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function addBlock(String $blockType, String $pageBlockLabel, $content = null): BlockTypeAbstract |
53
|
|
|
{ |
54
|
|
|
$existingBlockContent = $this->blockContents()->whereHas('block', function ($query) use ($blockType, $pageBlockLabel) { |
55
|
|
|
return $query->where('type', $blockType)->where('label', $pageBlockLabel); |
56
|
|
|
})->first(); |
57
|
|
|
|
58
|
|
|
if (!$existingBlockContent) { |
59
|
|
|
|
60
|
|
|
$blockClass = '\\Chadanuk\MiniCms\Blocks\\' . ucfirst($blockType) . 'Block'; |
61
|
|
|
|
62
|
|
|
$block = $blockClass::create($content, $pageBlockLabel, $this->id); |
63
|
|
|
$this->blockContents()->save($block->blockContent); |
64
|
|
|
} else { |
65
|
|
|
|
66
|
|
|
$block = BlockFactory::create($existingBlockContent->block()->first(), $existingBlockContent, $pageBlockLabel, $this->id); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $block; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function blockContents() |
73
|
|
|
{ |
74
|
|
|
return $this->hasMany(BlockContent::class); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getViewPath() |
78
|
|
|
{ |
79
|
|
|
if (View::exists('mini-cms.templates.' . $this->slug)) { |
80
|
|
|
return 'mini-cms.templates.' . $this->slug; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (View::exists('mini-cms::templates.' . $this->slug)) { |
84
|
|
|
return 'mini-cms::templates.' . $this->slug; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (View::exists('mini-cms.templates.default')) { |
88
|
|
|
return 'mini-cms.templates.default'; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return 'mini-cms::templates.default'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function fetchBlocks() |
95
|
|
|
{ |
96
|
|
|
$viewPath = $this->getViewPath(); |
97
|
|
|
$fullPath = View::getFinder()->find($viewPath); |
98
|
|
|
|
99
|
|
|
$templateContents = file_get_contents($fullPath); |
100
|
|
|
|
101
|
|
|
preg_match_all('/\@minicms\((.*)\)/', $templateContents, $blocks); |
102
|
|
|
foreach ($blocks[1] as $key => $value) { |
103
|
|
|
$blockArgs = explode(', ', $value); |
104
|
|
|
list($type, $label) = $blockArgs; |
105
|
|
|
$pageSlug = null; |
106
|
|
|
|
107
|
|
|
if (isset($blockArgs[2])) { |
108
|
|
|
$pageSlug = $blockArgs[2]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->addBlock(trim($type, '\''), trim($label, '\''), trim($pageSlug, '\'')); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->blockContents()->get(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function pageBlocks() |
118
|
|
|
{ |
119
|
|
|
$blockData = collect([]); |
120
|
|
|
$this->blockContents()->get()->each(function ($blockContent) use ($blockData) { |
121
|
|
|
$blockData->push(['blockContent' => $blockContent, 'block' => $blockContent->block]); |
122
|
|
|
}); |
123
|
|
|
|
124
|
|
|
return $blockData->map(function ($blockDataItem) { |
125
|
|
|
|
126
|
|
|
return BlockFactory::create( |
127
|
|
|
$blockDataItem['block'], |
128
|
|
|
$blockDataItem['blockContent'], |
129
|
|
|
$blockDataItem['block']->label, |
130
|
|
|
$this->id |
131
|
|
|
); |
132
|
|
|
}); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function updateBlocks(array $blockData = []) |
136
|
|
|
{ |
137
|
|
|
collect($blockData)->each(function ($blockContent, $blockContentId) { |
138
|
|
|
|
139
|
|
|
$this->blockContents()->where([ |
140
|
|
|
'id' => $blockContentId, |
141
|
|
|
])->update(['content' => $blockContent]); |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|