1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Repositories\Behaviors; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
|
9
|
|
|
trait HandleRevisions |
10
|
|
|
{ |
11
|
2 |
|
public function hydrateHandleRevisions($object, $fields) |
12
|
|
|
{ |
13
|
|
|
// HandleRepeaters trait => getRepeaters |
14
|
2 |
|
foreach($this->getRepeaters() as $repeater) { |
|
|
|
|
15
|
|
|
$this->hydrateRepeater($object, $fields, $repeater['relation'], $repeater['model']); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
// HandleBrowers trait => getBrowsers |
19
|
2 |
|
foreach($this->getBrowsers() as $browser) { |
|
|
|
|
20
|
|
|
$this->hydrateBrowser($object, $fields, $browser['relation'], $browser['positionAttribute'], $browser['model']); |
21
|
|
|
} |
22
|
|
|
|
23
|
2 |
|
return $object; |
24
|
|
|
} |
25
|
|
|
|
26
|
17 |
|
public function beforeSaveHandleRevisions($object, $fields) |
27
|
|
|
{ |
28
|
17 |
|
$lastRevisionPayload = json_decode($object->revisions->first()->payload ?? "{}", true); |
29
|
|
|
|
30
|
17 |
|
if ($fields != $lastRevisionPayload) { |
31
|
17 |
|
$object->revisions()->create([ |
32
|
17 |
|
'payload' => json_encode($fields), |
33
|
17 |
|
'user_id' => Auth::guard('twill_users')->user()->id ?? null, |
34
|
|
|
]); |
35
|
|
|
} |
36
|
|
|
|
37
|
17 |
|
return $fields; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function preview($id, $fields) |
41
|
|
|
{ |
42
|
1 |
|
$object = $this->model->findOrFail($id); |
43
|
|
|
|
44
|
1 |
|
return $this->hydrateObject($object, $fields); |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
protected function hydrateObject($object, $fields) |
48
|
|
|
{ |
49
|
2 |
|
$fields = $this->prepareFieldsBeforeSave($object, $fields); |
|
|
|
|
50
|
|
|
|
51
|
2 |
|
$object->fill(Arr::except($fields, $this->getReservedFields())); |
|
|
|
|
52
|
|
|
|
53
|
2 |
|
$object = $this->hydrate($object, $fields); |
|
|
|
|
54
|
|
|
|
55
|
2 |
|
return $object; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
public function previewForRevision($id, $revisionId) |
59
|
|
|
{ |
60
|
1 |
|
$object = $this->model->findOrFail($id); |
61
|
|
|
|
62
|
1 |
|
$fields = json_decode($object->revisions->where('id', $revisionId)->first()->payload, true); |
63
|
|
|
|
64
|
1 |
|
$hydratedObject = $this->hydrateObject($this->model->newInstance(), $fields); |
65
|
1 |
|
$hydratedObject->id = $id; |
66
|
|
|
|
67
|
1 |
|
return $hydratedObject; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function hydrateMultiSelect($object, $fields, $relationship, $model = null, $customHydratedRelationship = null) |
71
|
|
|
{ |
72
|
|
|
$fieldsHasElements = isset($fields[$relationship]) && !empty($fields[$relationship]); |
73
|
|
|
$relatedElements = $fieldsHasElements ? $fields[$relationship] : []; |
74
|
|
|
|
75
|
|
|
$relationRepository = $this->getModelRepository($relationship, $model); |
|
|
|
|
76
|
|
|
$relatedElementsCollection = Collection::make(); |
77
|
|
|
|
78
|
|
|
foreach ($relatedElements as $relatedElement) { |
79
|
|
|
$newRelatedElement = $relationRepository->getById($relatedElement); |
80
|
|
|
$relatedElementsCollection->push($newRelatedElement); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$object->setRelation($customHydratedRelationship ?? $relationship, $relatedElementsCollection); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function hydrateBrowser($object, $fields, $relationship, $positionAttribute = 'position', $model = null) |
87
|
|
|
{ |
88
|
|
|
return $this->hydrateOrderedBelongsTomany($object, $fields, $relationship, $positionAttribute, $model); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function hydrateOrderedBelongsTomany($object, $fields, $relationship, $positionAttribute = 'position', $model = null) |
92
|
|
|
{ |
93
|
|
|
$fieldsHasElements = isset($fields['browsers'][$relationship]) && !empty($fields['browsers'][$relationship]); |
94
|
|
|
$relatedElements = $fieldsHasElements ? $fields['browsers'][$relationship] : []; |
95
|
|
|
|
96
|
|
|
$relationRepository = $this->getModelRepository($relationship, $model); |
97
|
|
|
$relatedElementsCollection = Collection::make(); |
98
|
|
|
$position = 1; |
99
|
|
|
|
100
|
|
|
foreach ($relatedElements as $relatedElement) { |
101
|
|
|
$newRelatedElement = $relationRepository->getById($relatedElement['id']); |
102
|
|
|
$pivot = $newRelatedElement->newPivot($object, [$positionAttribute => $position++], $object->$relationship()->getTable(), true); |
103
|
|
|
$newRelatedElement->setRelation('pivot', $pivot); |
104
|
|
|
$relatedElementsCollection->push($newRelatedElement); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$object->setRelation($relationship, $relatedElementsCollection); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function hydrateRepeater($object, $fields, $relationship, $model) |
111
|
|
|
{ |
112
|
|
|
$relationFields = $fields['repeaters'][$relationship] ?? []; |
113
|
|
|
|
114
|
|
|
$relationRepository = $this->getModelRepository($relationship, $model); |
115
|
|
|
|
116
|
|
|
$repeaterCollection = Collection::make(); |
117
|
|
|
|
118
|
|
|
foreach ($relationFields as $index => $relationField) { |
119
|
|
|
$relationField['position'] = $index + 1; |
120
|
|
|
$relationField[$this->model->getForeignKey()] = $object->id; |
121
|
|
|
unset($relationField['id']); |
122
|
|
|
|
123
|
|
|
$newRepeater = $relationRepository->createForPreview($relationField); |
124
|
|
|
|
125
|
|
|
$repeaterCollection->push($newRepeater); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$object->setRelation($relationship, $repeaterCollection); |
129
|
|
|
} |
130
|
|
|
|
131
|
5 |
|
public function getCountForMine() |
132
|
|
|
{ |
133
|
5 |
|
$query = $this->model->newQuery(); |
134
|
5 |
|
return $this->filter($query, $this->countScope)->mine()->count(); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
5 |
|
public function getCountByStatusSlugHandleRevisions($slug) |
138
|
|
|
{ |
139
|
5 |
|
if ($slug === 'mine') { |
140
|
5 |
|
return $this->getCountForMine(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return false; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|