1
|
|
|
<?php namespace Anomaly\Streams\Platform\Model; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\FieldType\FieldType; |
4
|
|
|
use Anomaly\Streams\Platform\Ui\Form\Contract\FormRepositoryInterface; |
5
|
|
|
use Anomaly\Streams\Platform\Ui\Form\FormBuilder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class EloquentFormRepository |
9
|
|
|
* |
10
|
|
|
* @link http://pyrocms.com/ |
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class EloquentFormRepository implements FormRepositoryInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The form model. |
19
|
|
|
* |
20
|
|
|
* @var EloquentModel |
21
|
|
|
*/ |
22
|
|
|
protected $model; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new EloquentFormRepositoryInterface instance. |
26
|
|
|
* |
27
|
|
|
* @param EloquentModel $model |
28
|
|
|
*/ |
29
|
|
|
public function __construct(EloquentModel $model) |
30
|
|
|
{ |
31
|
|
|
$this->model = $model; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Find an entry. |
36
|
|
|
* |
37
|
|
|
* @param $id |
38
|
|
|
* @return EloquentModel |
39
|
|
|
*/ |
40
|
|
|
public function findOrNew($id) |
41
|
|
|
{ |
42
|
|
|
return $this->model->findOrNew($id); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Save the form. |
47
|
|
|
* |
48
|
|
|
* @param FormBuilder $builder |
49
|
|
|
*/ |
50
|
|
|
public function save(FormBuilder $builder) |
51
|
|
|
{ |
52
|
|
|
$entry = $builder->getFormEntry(); |
53
|
|
|
|
54
|
|
|
$data = $this->prepareValueData($builder); |
55
|
|
|
|
56
|
|
|
$entry->unguard(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$builder->fire('querying', compact('builder')); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Update OR create the entry. |
62
|
|
|
* Keep this as is or we will |
63
|
|
|
* have issues with post relations |
64
|
|
|
* in following observer logic. |
65
|
|
|
*/ |
66
|
|
|
if ($entry->getId()) { |
67
|
|
|
$entry->update($data); |
|
|
|
|
68
|
|
|
} else { |
69
|
|
|
$entry = $entry->create($data); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$entry->reguard(); |
73
|
|
|
|
74
|
|
|
$builder->setFormEntry($entry); |
75
|
|
|
|
76
|
|
|
$this->processSelfHandlingFields($builder); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Prepare the value data for update / create. |
81
|
|
|
* |
82
|
|
|
* @param FormBuilder $builder |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function prepareValueData(FormBuilder $builder) |
86
|
|
|
{ |
87
|
|
|
$form = $builder->getForm(); |
88
|
|
|
|
89
|
|
|
$entry = $form->getEntry(); |
90
|
|
|
$fields = $form->getFields(); |
91
|
|
|
|
92
|
|
|
$allowed = $fields->allowed(); |
93
|
|
|
$disabled = $fields->disabled(); |
94
|
|
|
|
95
|
|
|
/* |
96
|
|
|
* Set initial data from the |
97
|
|
|
* entry, minus undesired data. |
98
|
|
|
*/ |
99
|
|
|
$data = array_diff_key( |
100
|
|
|
$entry->getUnguardedAttributes(), |
|
|
|
|
101
|
|
|
array_merge( |
102
|
|
|
['id', 'created_at', 'created_by_id', 'updated_at', 'updated_by_id'], |
103
|
|
|
array_flip($disabled->fieldSlugs()) |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Save default translation input. |
109
|
|
|
* |
110
|
|
|
* @var FieldType $field |
111
|
|
|
*/ |
112
|
|
|
foreach ($allowed->notTranslatable() as $field) { |
113
|
|
|
if (!$field->getLocale()) { |
114
|
|
|
array_set($data, str_replace('__', '.', $field->getField()), $form->getValue($field->getInputName())); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/* |
119
|
|
|
* Loop through available translations |
120
|
|
|
* and save translated input. |
121
|
|
|
* |
122
|
|
|
* @var FieldType $field |
123
|
|
|
*/ |
124
|
|
|
if ($entry->getTranslationModelName()) { |
|
|
|
|
125
|
|
|
foreach (config('streams::locales.enabled') as $locale) { |
126
|
|
|
foreach ($allowed->translatable() as $field) { |
127
|
|
|
if ($field->getLocale() == $locale) { |
128
|
|
|
array_set( |
129
|
|
|
$data, |
130
|
|
|
$locale . '.' . $field->getField(), |
131
|
|
|
$form->getValue($field->getInputName()) |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $data; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Process fields that handle themselves. |
143
|
|
|
* |
144
|
|
|
* @param FormBuilder $builder |
145
|
|
|
*/ |
146
|
|
|
protected function processSelfHandlingFields(FormBuilder $builder) |
147
|
|
|
{ |
148
|
|
|
$form = $builder->getForm(); |
149
|
|
|
|
150
|
|
|
$entry = $form->getEntry(); |
151
|
|
|
$fields = $form->getFields(); |
152
|
|
|
|
153
|
|
|
$fields = $fields->selfHandling(); |
154
|
|
|
|
155
|
|
|
/* @var FieldType $field */ |
156
|
|
|
foreach ($fields as $field) { |
157
|
|
|
app()->call([$field->setEntry($entry), 'handle'], compact('builder')); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: