|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anavel\Crud\Abstractor\Eloquent\Relation; |
|
4
|
|
|
|
|
5
|
|
|
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationCompatibility; |
|
6
|
|
|
use Anavel\Crud\Contracts\Abstractor\Field; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Http\Request; |
|
9
|
|
|
|
|
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
|
1 |
|
$this->readConfig('edit'); |
|
48
|
|
|
|
|
49
|
1 |
|
if (!empty($columns)) { |
|
50
|
|
|
//Add field for model deletion |
|
51
|
|
|
$config = [ |
|
52
|
1 |
|
'name' => '__delete', |
|
53
|
1 |
|
'presentation' => 'Delete', |
|
54
|
1 |
|
'form_type' => 'checkbox', |
|
55
|
1 |
|
'no_validate' => true, |
|
56
|
1 |
|
'validation' => null, |
|
57
|
1 |
|
'functions' => null, |
|
58
|
1 |
|
]; |
|
59
|
|
|
|
|
60
|
|
|
/** @var Field $field */ |
|
61
|
1 |
|
$field = $this->fieldFactory |
|
62
|
1 |
|
->setConfig($config) |
|
63
|
1 |
|
->get(); |
|
64
|
1 |
|
$fields[$arrayKey]['__delete'] = $field; |
|
65
|
|
|
|
|
66
|
1 |
|
foreach ($columns as $columnName => $column) { |
|
67
|
1 |
|
if (in_array($columnName, $readOnly, true)) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
$formType = null; |
|
72
|
|
|
|
|
73
|
|
|
$config = [ |
|
74
|
1 |
|
'name' => $columnName, |
|
75
|
1 |
|
'presentation' => $this->name.' '.ucfirst(transcrud($columnName)), |
|
76
|
1 |
|
'form_type' => $formType, |
|
77
|
1 |
|
'no_validate' => true, |
|
78
|
1 |
|
'validation' => null, |
|
79
|
1 |
|
'functions' => null, |
|
80
|
1 |
|
]; |
|
81
|
|
|
|
|
82
|
1 |
|
$config = $this->setConfig($config, $columnName); |
|
83
|
|
|
|
|
84
|
|
|
/** @var Field $field */ |
|
85
|
1 |
|
$field = $this->fieldFactory |
|
86
|
1 |
|
->setColumn($column) |
|
87
|
1 |
|
->setConfig($config) |
|
88
|
1 |
|
->get(); |
|
89
|
|
|
|
|
90
|
1 |
|
if (!empty($result->id)) { |
|
91
|
1 |
|
$field->setValue($result->getAttribute($columnName)); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$fields[$arrayKey][$columnName] = $field; |
|
95
|
1 |
|
} |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
1 |
|
$fields = $this->addSecondaryRelationFields($fields); |
|
99
|
|
|
|
|
100
|
1 |
|
return $fields; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param array|null $relationArray |
|
105
|
|
|
* |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function persist(array $relationArray = null, Request $request) |
|
109
|
|
|
{ |
|
110
|
2 |
|
if (!empty($relationArray)) { |
|
111
|
2 |
|
$currentRelation = $this->eloquentRelation->getResults(); |
|
112
|
2 |
|
if (!empty($currentRelation)) { |
|
113
|
1 |
|
$relationModel = $currentRelation; |
|
114
|
1 |
|
} else { |
|
115
|
1 |
|
$relationModel = $this->eloquentRelation->getRelated()->newInstance(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
$this->setKeys($relationModel); |
|
119
|
|
|
|
|
120
|
2 |
|
$shouldBeSkipped = true; |
|
121
|
2 |
|
foreach ($relationArray as $fieldKey => $fieldValue) { |
|
122
|
|
|
// This field can only come from existing models |
|
123
|
2 |
|
if ($fieldKey === '__delete') { |
|
124
|
|
|
$relationModel->delete(); |
|
125
|
|
|
$shouldBeSkipped = true; |
|
126
|
|
|
break; |
|
127
|
|
|
} |
|
128
|
2 |
|
if ($shouldBeSkipped) { |
|
129
|
2 |
|
$shouldBeSkipped = ($shouldBeSkipped === ($fieldValue === '')); |
|
130
|
2 |
|
} |
|
131
|
2 |
|
$relationModel->setAttribute($fieldKey, $fieldValue); |
|
132
|
2 |
|
} |
|
133
|
|
|
|
|
134
|
2 |
|
if (!$shouldBeSkipped) { |
|
135
|
2 |
|
$relationModel->save(); |
|
136
|
2 |
|
} |
|
137
|
2 |
|
} |
|
138
|
2 |
|
} |
|
139
|
|
|
|
|
140
|
2 |
|
protected function setKeys(Model $relationModel) |
|
141
|
|
|
{ |
|
142
|
2 |
|
$relationModel->setAttribute($this->eloquentRelation->getForeignKey(), $this->relatedModel->id); |
|
143
|
2 |
|
$relationModel->setAttribute($this->eloquentRelation->getPlainMorphType(), |
|
144
|
2 |
|
$this->eloquentRelation->getMorphClass()); |
|
145
|
2 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getDisplayType() |
|
151
|
|
|
{ |
|
152
|
|
|
return self::DISPLAY_TYPE_TAB; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|