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