1 | <?php |
||
10 | class MiniCrudSingle extends Relation |
||
11 | { |
||
12 | use CheckRelationCompatibility; |
||
13 | |||
14 | protected $compatibleEloquentRelations = [ |
||
15 | 'Illuminate\Database\Eloquent\Relations\MorphOne', |
||
16 | ]; |
||
17 | |||
18 | 5 | public function setup() |
|
19 | { |
||
20 | 5 | $this->checkRelationCompatibility(); |
|
21 | 4 | } |
|
22 | |||
23 | /** |
||
24 | * @return array |
||
25 | */ |
||
26 | 1 | public function getEditFields($arrayKey = null) |
|
27 | { |
||
28 | 1 | if (empty($arrayKey)) { |
|
29 | 1 | $arrayKey = $this->name; |
|
30 | 1 | } |
|
31 | |||
32 | 1 | $fields = []; |
|
33 | |||
34 | 1 | $columns = $this->modelAbstractor->getColumns('edit'); |
|
35 | |||
36 | /** @var Model $result */ |
||
37 | 1 | $result = $this->eloquentRelation->getResults(); |
|
38 | |||
39 | $readOnly = [ |
||
40 | 1 | Model::CREATED_AT, |
|
41 | 1 | Model::UPDATED_AT, |
|
42 | 1 | $this->eloquentRelation->getPlainForeignKey(), |
|
43 | 1 | $this->eloquentRelation->getPlainMorphType(), |
|
44 | 1 | $this->eloquentRelation->getParent()->getKeyName(), |
|
45 | 1 | ]; |
|
46 | |||
47 | |||
48 | 1 | $this->readConfig('edit'); |
|
49 | |||
50 | 1 | if (!empty($columns)) { |
|
51 | //Add field for model deletion |
||
52 | $config = [ |
||
53 | 1 | 'name' => '__delete', |
|
54 | 1 | 'presentation' => 'Delete', |
|
55 | 1 | 'form_type' => 'checkbox', |
|
56 | 1 | 'no_validate' => true, |
|
57 | 1 | 'validation' => null, |
|
58 | 1 | 'functions' => null, |
|
59 | 1 | ]; |
|
60 | |||
61 | /** @var Field $field */ |
||
62 | 1 | $field = $this->fieldFactory |
|
63 | 1 | ->setConfig($config) |
|
64 | 1 | ->get(); |
|
65 | 1 | $fields[$arrayKey]['__delete'] = $field; |
|
66 | |||
67 | 1 | foreach ($columns as $columnName => $column) { |
|
68 | 1 | if (in_array($columnName, $readOnly, true)) { |
|
69 | continue; |
||
70 | } |
||
71 | |||
72 | 1 | $formType = null; |
|
73 | |||
74 | $config = [ |
||
75 | 1 | 'name' => $columnName, |
|
76 | 1 | 'presentation' => $this->name.' '.ucfirst(transcrud($columnName)), |
|
77 | 1 | 'form_type' => $formType, |
|
78 | 1 | 'no_validate' => true, |
|
79 | 1 | 'validation' => null, |
|
80 | 1 | 'functions' => null, |
|
81 | 1 | ]; |
|
82 | |||
83 | 1 | $config = $this->setConfig($config, $columnName); |
|
84 | |||
85 | /** @var Field $field */ |
||
86 | 1 | $field = $this->fieldFactory |
|
87 | 1 | ->setColumn($column) |
|
88 | 1 | ->setConfig($config) |
|
89 | 1 | ->get(); |
|
90 | |||
91 | 1 | if (!empty($result->id)) { |
|
92 | 1 | $field->setValue($result->getAttribute($columnName)); |
|
93 | 1 | } |
|
94 | |||
95 | 1 | $fields[$arrayKey][$columnName] = $field; |
|
96 | 1 | } |
|
97 | 1 | } |
|
98 | |||
99 | 1 | $fields = $this->addSecondaryRelationFields($fields); |
|
100 | |||
101 | |||
102 | 1 | return $fields; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param array|null $relationArray |
||
107 | * |
||
108 | * @return mixed |
||
109 | */ |
||
110 | 2 | public function persist(array $relationArray = null, Request $request) |
|
111 | { |
||
112 | 2 | if (!empty($relationArray)) { |
|
113 | 2 | $currentRelation = $this->eloquentRelation->getResults(); |
|
114 | 2 | if (!empty($currentRelation)) { |
|
115 | 1 | $relationModel = $currentRelation; |
|
116 | 1 | } else { |
|
117 | 1 | $relationModel = $this->eloquentRelation->getRelated()->newInstance(); |
|
118 | } |
||
119 | |||
120 | 2 | $this->setKeys($relationModel); |
|
121 | |||
122 | 2 | $shouldBeSkipped = true; |
|
123 | 2 | foreach ($relationArray as $fieldKey => $fieldValue) { |
|
124 | // This field can only come from existing models |
||
125 | 2 | if ($fieldKey === '__delete') { |
|
126 | $relationModel->delete(); |
||
127 | $shouldBeSkipped = true; |
||
128 | break; |
||
129 | } |
||
130 | 2 | if ($shouldBeSkipped) { |
|
131 | 2 | $shouldBeSkipped = ($shouldBeSkipped === ($fieldValue === '')); |
|
132 | 2 | } |
|
133 | 2 | $relationModel->setAttribute($fieldKey, $fieldValue); |
|
134 | 2 | } |
|
135 | |||
136 | 2 | if (!$shouldBeSkipped) { |
|
137 | 2 | $relationModel->save(); |
|
138 | 2 | } |
|
139 | 2 | } |
|
140 | 2 | } |
|
141 | |||
142 | 2 | protected function setKeys(Model $relationModel) |
|
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | public function getDisplayType() |
||
156 | } |
||
157 |