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