-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathEntry.php
216 lines (167 loc) · 6.52 KB
/
Entry.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace Statamic\Eloquent\Entries;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Statamic\Contracts\Entries\Entry as EntryContract;
use Statamic\Entries\Entry as FileEntry;
use Statamic\Facades\Blink;
use Statamic\Facades\Entry as EntryFacade;
class Entry extends FileEntry
{
protected $model;
public static function fromModel(Model $model)
{
$data = isset($model->data['__localized_fields']) ? collect($model->data)->only($model->data['__localized_fields']) : $model->data;
foreach ((new self)->getDataColumnMappings($model) as $key) {
$data[$key] = $model->$key;
}
$entry = (new static)
->origin($model->origin_id)
->locale($model->site)
->slug($model->slug)
->collection($model->collection)
->data($data)
->blueprint($model->blueprint ?? $model->data['blueprint'] ?? null)
->template($model->data['template'] ?? null)
->published($model->published)
->model($model);
if ($model->date && $entry->collection()->dated()) {
$entry->date($model->date);
}
if (config('statamic.system.track_last_update')) {
if ($updatedAt = $model->updated_at ?? $model->created_at) {
$entry->set('updated_at', $updatedAt->timestamp);
}
}
return $entry->syncOriginal();
}
public function toModel()
{
return self::makeModelFromContract($this);
}
public static function makeModelFromContract(EntryContract $source)
{
$class = app('statamic.eloquent.entries.model');
$data = $source->data()
->merge(method_exists($source, 'computedData') ? $source->computedData() : []);
$date = $source->hasDate() ? $source->date() : null;
$origin = $source->origin();
if ($source->hasOrigin()) {
if ($blueprint = $source->blueprint()) {
$localizedBlueprintFields = $blueprint
->fields()
->localizable()
->all()
->map
->handle()
->all();
$originData = $origin->data();
// remove any fields in entry data that are marked as localized but value is present, and does not match origin
$localizedFields = [];
foreach ($localizedBlueprintFields as $blueprintField) {
if ($data->has($blueprintField)) {
if ($data->get($blueprintField) === $originData->get($blueprintField)) {
$data->forget($blueprintField);
} else {
$localizedFields[] = $blueprintField;
}
}
}
$data = $originData->merge($data);
$data->put('__localized_fields', $localizedFields);
if (! in_array('date', $localizedFields)) {
$date = $origin->hasDate() ? $origin->date() : null;
}
}
}
if ($parent = $source->parent()) {
$data->put('parent', (string) $parent->id);
}
$dataMappings = (new self)->getDataColumnMappings(new $class);
$attributes = [];
if ($id = $source->id()) {
$attributes['id'] = $id;
// Ensure that when calling $source->uri() that it doesn't use the cached value.
Blink::store('entry-uris')->forget($source->id());
}
// disable the uri cache so any slug updates give us the latest slug
$source->structure()?->in($source->locale())->disableUriCache();
$attributes = [
...$attributes,
'origin_id' => $origin?->id(),
'site' => $source->locale(),
'slug' => $source->slug(),
'uri' => $source->uri() ?? $source->routableUri(),
'date' => $date,
'collection' => $source->collectionHandle(),
'blueprint' => $source->blueprint ?? $source->blueprint()->handle(),
'data' => $data->except(array_merge(EntryQueryBuilder::COLUMNS, $dataMappings)),
'published' => $source->published(),
'updated_at' => $source->lastModified(),
'order' => $source->order(),
];
if ($template = $source->get('template', $source->template)) {
$attributes['data']->put('template', $template);
}
$attributes['data'] = $attributes['data']->filter(fn ($v) => $v !== null);
foreach ($dataMappings as $key) {
$attributes[$key] = $data->get($key);
}
return $class::findOrNew($id)->fill($attributes);
}
public function model($model = null)
{
if (func_num_args() === 0) {
return $this->model;
}
$this->model = $model;
if (! is_null($model)) {
$this->id($model->id);
}
return $this;
}
public function fileLastModified()
{
return $this->model?->updated_at ?? Carbon::now();
}
public function lastModified()
{
return $this->fileLastModified();
}
public function origin($origin = null)
{
if (func_num_args() > 0) {
$this->origin = $origin;
if ($this->model) {
$this->model->origin_id = $origin instanceof EntryContract ? $origin->id() : $origin;
}
return $this;
}
if ($this->origin) {
if (! $this->origin instanceof EntryContract) {
$this->origin = EntryFacade::find($this->origin);
}
return $this->origin;
}
if (! $this->model?->origin_id) {
return;
}
return EntryFacade::find($this->model->origin_id);
}
public function makeLocalization($site)
{
$this->localizations = null;
return parent::makeLocalization($site)
->data($this->data());
}
public function getDataColumnMappings(Model $model)
{
if (! config('statamic.eloquent-driver.entries.map_data_to_columns', false)) {
return [];
}
return Blink::once("eloquent-schema-{$model->getTable()}", function () use ($model) {
$schema = $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
return collect($schema)->reject(fn ($value) => in_array($value, EntryQueryBuilder::COLUMNS))->all();
});
}
}