|
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
|
|
|
$existingBlock = $this->pageBlocks()->where('type', $blockType)->where('label', $pageBlockLabel)->first(); |
|
55
|
|
|
if (!$existingBlock) { |
|
56
|
|
|
$blockClass = '\\Chadanuk\MiniCms\Blocks\\' . ucfirst($blockType) . 'Block'; |
|
57
|
|
|
$block = $blockClass::create($content, $pageBlockLabel, $this->id); |
|
58
|
|
|
$this->blocks()->attach($block->blockId, ['label' => $pageBlockLabel]); |
|
59
|
|
|
} else { |
|
60
|
|
|
$block = BlockFactory::create(Block::find($existingBlock->blockId), BlockContent::find($existingBlock->blockContentId), $pageBlockLabel, $this->id); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $block; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function blocks() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->belongsToMany(Block::class, 'pages_blocks'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function blockContents() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->hasMany(BlockContent::class); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getViewPath() |
|
77
|
|
|
{ |
|
78
|
|
|
if (View::exists('mini-cms.templates.' . $this->slug)) { |
|
79
|
|
|
return 'mini-cms.templates.' . $this->slug; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (View::exists('mini-cms::templates.' . $this->slug)) { |
|
83
|
|
|
return 'mini-cms::templates.' . $this->slug; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (View::exists('mini-cms.templates.default')) { |
|
87
|
|
|
return 'mini-cms.templates.default'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return 'mini-cms::templates.default'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function fetchBlocks() |
|
94
|
|
|
{ |
|
95
|
|
|
$viewPath = $this->getViewPath(); |
|
96
|
|
|
$fullPath = View::getFinder()->find($viewPath); |
|
97
|
|
|
|
|
98
|
|
|
$templateContents = file_get_contents($fullPath); |
|
99
|
|
|
|
|
100
|
|
|
preg_match_all('/\@minicms\(\'(.*)\', \'(.*)\'\)/', $templateContents, $blocks); |
|
101
|
|
|
|
|
102
|
|
|
foreach ($blocks[1] as $key => $type) { |
|
103
|
|
|
$this->addBlock($type, $blocks[2][$key], null); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $this->blocks()->get(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function pageBlocks() |
|
110
|
|
|
{ |
|
111
|
|
|
$blockData = collect([]); |
|
112
|
|
|
$this->blocks()->withPivot('label')->get()->each(function ($block) use ($blockData) { |
|
113
|
|
|
$blockContents = BlockContent::where([ |
|
114
|
|
|
'page_id' => $this->id, |
|
115
|
|
|
'block_id' => $block->id, |
|
116
|
|
|
])->get(); |
|
117
|
|
|
|
|
118
|
|
|
$blockContents->each(function ($blockContent) use ($blockData, $block) { |
|
119
|
|
|
$blockData->push(['blockContent' => $blockContent, 'block' => $block]); |
|
120
|
|
|
}); |
|
121
|
|
|
}); |
|
122
|
|
|
|
|
123
|
|
|
return $blockData->map(function ($blockDataItem) { |
|
124
|
|
|
return BlockFactory::create($blockDataItem['block'], $blockDataItem['blockContent'], $blockDataItem['block']->pivot->label, $this->id); |
|
125
|
|
|
}); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function updateBlocks(array $blockData = []) |
|
129
|
|
|
{ |
|
130
|
|
|
collect($blockData)->each(function ($blockContent, $blockContentId) { |
|
131
|
|
|
|
|
132
|
|
|
$this->blockContents()->where([ |
|
133
|
|
|
'id' => $blockContentId, |
|
134
|
|
|
])->update(['content' => $blockContent]); |
|
135
|
|
|
}); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|