1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chuckbe\Chuckcms\Models; |
4
|
|
|
|
5
|
|
|
use Eloquent; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @property array $content |
9
|
|
|
*/ |
10
|
|
|
class Content extends Eloquent |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The attributes that are mass assignable. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $fillable = [ |
18
|
|
|
'slug', 'type', 'content', |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
protected $casts = [ |
22
|
|
|
'content' => 'array', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The attributes that should be hidden for arrays. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $hidden = ['id', 'created_at', 'updated_at']; |
31
|
|
|
|
32
|
|
|
public function getBySlug($slug) |
33
|
|
|
{ |
34
|
|
|
return $this->where('slug', $slug)->first(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getRules() |
38
|
|
|
{ |
39
|
|
|
$rules = []; |
40
|
|
|
foreach ($this->content['fields'] as $fieldKey => $fieldValue) { |
41
|
|
|
$rules[$fieldKey] = $fieldValue['validation']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $rules; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function storeEntry($input) |
48
|
|
|
{ |
49
|
|
|
$slug = $input->get('content_slug'); |
50
|
|
|
if (is_array($this->content['actions']['detail'])) { |
51
|
|
|
if (array_key_exists('url', $this->content['actions']['detail'])) { |
52
|
|
|
$url = $this->getUrlFromInput($this->content['actions']['detail']['url'], $input); |
53
|
|
|
$page = $this->content['actions']['detail']['page']; |
54
|
|
|
} else { |
55
|
|
|
$url = null; |
56
|
|
|
$page = 'default'; |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$url = null; |
60
|
|
|
$page = 'default'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$json = []; |
64
|
|
|
foreach ($this->content['fields'] as $fieldKey => $fieldValue) { |
65
|
|
|
$cleanKey = str_replace($slug.'_', '', $fieldKey); |
66
|
|
|
$json[$cleanKey] = $input->get($fieldKey); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
Repeater::updateOrCreate( |
70
|
|
|
['id' => $input->get('repeater_id')], |
71
|
|
|
['slug' => $slug, |
72
|
|
|
'url' => $url, |
73
|
|
|
'page' => $page, |
74
|
|
|
'json' => $json, |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return 'success'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function deleteById($id) |
82
|
|
|
{ |
83
|
|
|
$repeater = Repeater::where('id', $id)->first(); |
84
|
|
|
|
85
|
|
|
if ($repeater->delete()) { |
86
|
|
|
return 'success'; |
87
|
|
|
} else { |
88
|
|
|
return 'error'; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getUrlFromInput($url, $input) |
93
|
|
|
{ |
94
|
|
|
$fields = $this->getContents($url, '[', ']'); |
95
|
|
|
$finalUrl = $url; |
96
|
|
|
foreach ($fields as $field) { |
97
|
|
|
$finalUrl = str_replace('['.$field.']', $input->get($field), $finalUrl); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $finalUrl; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getContents($str, $startDelimiter, $endDelimiter) |
104
|
|
|
{ |
105
|
|
|
$contents = []; |
106
|
|
|
$startDelimiterLength = strlen($startDelimiter); |
107
|
|
|
$endDelimiterLength = strlen($endDelimiter); |
108
|
|
|
$startFrom = 0; |
109
|
|
|
while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) { |
110
|
|
|
$contentStart += $startDelimiterLength; |
111
|
|
|
$contentEnd = strpos($str, $endDelimiter, $contentStart); |
112
|
|
|
if (false === $contentEnd) { |
113
|
|
|
break; |
114
|
|
|
} |
115
|
|
|
$contents[] = substr($str, $contentStart, $contentEnd - $contentStart); |
116
|
|
|
$startFrom = $contentEnd + $endDelimiterLength; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $contents; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|