1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anavel\Crud\Abstractor\Eloquent\Relation; |
4
|
|
|
|
5
|
|
|
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationCompatibility; |
6
|
|
|
use Doctrine\DBAL\Schema\Column; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
|
10
|
|
|
class Select extends Relation |
11
|
|
|
{ |
12
|
|
|
use CheckRelationCompatibility; |
13
|
|
|
|
14
|
|
|
protected $compatibleEloquentRelations = [ |
15
|
|
|
'Illuminate\Database\Eloquent\Relations\BelongsTo', |
16
|
|
|
]; |
17
|
|
|
|
18
|
5 |
|
public function setup() |
19
|
|
|
{ |
20
|
5 |
|
$this->checkRelationCompatibility(); |
21
|
5 |
|
$this->checkDisplayConfig(); |
22
|
5 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string|null $arrayKey |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
4 |
|
public function getEditFields($arrayKey = 'main') |
30
|
|
|
{ |
31
|
|
|
/** @var \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal */ |
32
|
4 |
|
$dbal = $this->modelManager->getAbstractionLayer(get_class($this->eloquentRelation->getRelated())); |
33
|
|
|
|
34
|
4 |
|
$column = $this->getColumn($dbal); |
35
|
|
|
|
36
|
4 |
|
$repo = $this->modelManager->getRepository(get_class($this->eloquentRelation->getRelated())); |
37
|
|
|
|
38
|
4 |
|
$results = $repo->all(); |
39
|
|
|
|
40
|
4 |
|
$options = ['' => '']; |
41
|
|
|
|
42
|
4 |
|
$this->readConfig('edit'); |
43
|
|
|
|
44
|
4 |
|
foreach ($results as $result) { |
45
|
2 |
|
$options[$result->getKey()] = $this->setDisplay($result); |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
// If results are displayed using a field other than id, order by that field |
49
|
4 |
|
if (! empty($this->config['display'])) { |
50
|
4 |
|
asort($options); |
51
|
4 |
|
} |
52
|
|
|
|
53
|
4 |
|
$config = $this->getConfig(); |
54
|
|
|
|
55
|
4 |
|
$config = $this->setConfig($config, $column->getName()); |
56
|
|
|
|
57
|
4 |
|
$field = $this->fieldFactory |
58
|
4 |
|
->setColumn($column) |
59
|
4 |
|
->setConfig($config) |
60
|
4 |
|
->get(); |
61
|
|
|
|
62
|
4 |
|
$field->setOptions($options); |
63
|
|
|
|
64
|
4 |
|
$field = $this->setFieldValue($field); |
65
|
|
|
|
66
|
4 |
|
$select = $this->addToArray($arrayKey, $field); |
67
|
|
|
|
68
|
4 |
|
return $select; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
protected function addToArray($arrayKey, $field) |
72
|
|
|
{ |
73
|
2 |
|
$select = []; |
74
|
2 |
|
$select[$arrayKey][] = $field; |
75
|
|
|
|
76
|
2 |
|
return $select; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array|null $relationArray |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
|
public function persist(array $relationArray = null, Request $request) |
85
|
|
|
{ |
86
|
|
|
// |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Model $result |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
2 |
|
protected function setDisplay($result) |
95
|
|
|
{ |
96
|
2 |
|
if (is_array($this->config['display'])) { |
97
|
|
|
$displayString = ''; |
98
|
|
|
foreach ($this->config['display'] as $key => $display) { |
99
|
|
|
if ($key !== 0) { |
100
|
|
|
$displayString .= ' | '; |
101
|
|
|
} |
102
|
|
|
$displayString .= $result->getAttribute($display); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $displayString; |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
return $result->getAttribute($this->config['display']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
2 |
|
protected function getConfig() |
115
|
|
|
{ |
116
|
|
|
return [ |
117
|
2 |
|
'name' => $this->eloquentRelation->getForeignKey(), |
118
|
2 |
|
'presentation' => $this->getPresentation(), |
119
|
2 |
|
'form_type' => 'select', |
120
|
2 |
|
'validation' => null, |
121
|
2 |
|
'functions' => null, |
122
|
2 |
|
]; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
protected function setFieldValue($field) |
126
|
|
|
{ |
127
|
2 |
|
$results = $this->eloquentRelation->getResults(); |
128
|
|
|
|
129
|
2 |
|
if (!empty($results)) { |
130
|
|
|
$field->setValue($results->getKey()); |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
return $field; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal |
138
|
|
|
* |
139
|
|
|
* @return Column |
140
|
|
|
*/ |
141
|
2 |
|
protected function getColumn($dbal) |
142
|
|
|
{ |
143
|
2 |
|
return $dbal->getTableColumn($this->eloquentRelation->getOtherKey()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getDisplayType() |
150
|
|
|
{ |
151
|
|
|
return self::DISPLAY_TYPE_INLINE; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|