-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathDatabase.php
More file actions
147 lines (126 loc) · 3.5 KB
/
Database.php
File metadata and controls
147 lines (126 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Whitecube\NovaPage\Sources;
use \App;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Whitecube\NovaPage\Pages\Template;
class Database implements SourceInterface {
/**
* The table used to store static pages content
*
* @var string
*/
protected $table;
/**
* The model used to store static pages content
*
* @var string
*/
protected $model;
/**
* The fetched model instance
*
* @var \Illuminate\Database\Eloquent\Model
*/
protected $original;
/**
* Retrieve the source's name
*
* @return string
*/
public function getName()
{
return 'database';
}
/**
* Set the source's configuration parameters
*
* @return string
*/
public function setConfig(array $config)
{
$this->table = $config['table_name'];
$this->model = $config['model'];
}
/**
* Retrieve data from the database
*
* @param \Whitecube\NovaPage\Pages\Template $template
* @return object
*/
public function fetch(Template $template)
{
$model = $this->getOriginal($template);
if(!$model->id) {
return;
}
$attributes = $this->getParsedAttributes(
$template,
$model->attributes ? (is_array($model->attributes) ? $model->attributes : json_decode($model->attributes, true)) : []
);
return [
'title' => $model->title,
'created_at' => $model->created_at,
'updated_at' => $model->updated_at,
'attributes' => $attributes
];
}
/**
* Save template in the database
*
* @param \Whitecube\NovaPage\Pages\Template $template
* @return void
*/
public function store(Template $template)
{
$original = $this->getOriginal($template);
$original->fill([
'name' => $template->getName(),
'title' => $template->getTitle(),
'type' => $template->getType(),
'attributes' => json_encode($template->getAttributes()),
'created_at' => $template->getDate('created_at'),
'updated_at' => Carbon::now()
]);
$original->save();
}
/**
* Retrieve original StaticPage model
*
* @param \Whitecube\NovaPage\Pages\Template $template
* @return null|\Illuminate\Database\Eloquent\Model
*/
public function getOriginal(Template $template)
{
if(!$this->original) {
$instance = call_user_func($this->model . '::where', 'name', $template->getName())->first();
$this->original = $instance ?? (new $this->model);
}
return $this->original;
}
/**
* Retrieve and parse attributes array
*
* @param \Whitecube\NovaPage\Pages\Template $template
* @param array $attributes
* @return array
*/
protected function getParsedAttributes(Template $template, $attributes)
{
foreach ($attributes as $key => $value) {
if(!is_array($value) && !is_object($value)) continue;
if($template->isJsonAttribute($key)) continue;
$attributes[$key] = json_encode($value);
}
return $attributes;
}
/**
* Get the text to display in the missing value exception
*
* @return string
*/
public function getErrorLocation($type, $name)
{
return $this->getName() . ' table "' . $this->table . '". Page "' . $name . '".';
}
}