|
1
|
|
|
<?php |
|
2
|
|
|
namespace Anavel\Crud\Abstractor\Eloquent\Relation; |
|
3
|
|
|
|
|
4
|
|
|
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationCompatibility; |
|
5
|
|
|
use Anavel\Crud\Contracts\Abstractor\Field; |
|
6
|
|
|
use App; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
class Translation extends Relation |
|
10
|
|
|
{ |
|
11
|
|
|
use CheckRelationCompatibility; |
|
12
|
|
|
|
|
13
|
|
|
protected $langs = []; |
|
14
|
|
|
|
|
15
|
|
|
protected $compatibleEloquentRelations = array( |
|
16
|
|
|
'Illuminate\Database\Eloquent\Relations\HasMany' |
|
17
|
|
|
); |
|
18
|
|
|
|
|
19
|
5 |
|
public function setup() |
|
20
|
|
|
{ |
|
21
|
5 |
|
if (empty($this->langs)) { |
|
22
|
5 |
|
$this->langs = config('anavel.translation_languages'); |
|
|
|
|
|
|
23
|
5 |
|
} |
|
24
|
|
|
|
|
25
|
5 |
|
$this->checkRelationCompatibility(); |
|
26
|
5 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function getEditFields($arrayKey = null) |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var \ANavallaSuiza\Laravel\Database\Contracts\Dbal\AbstractionLayer $dbal */ |
|
34
|
1 |
|
$dbal = $this->modelManager->getAbstractionLayer(get_class($this->eloquentRelation->getRelated())); |
|
35
|
|
|
|
|
36
|
1 |
|
$columns = $dbal->getTableColumns(); |
|
37
|
|
|
|
|
38
|
1 |
|
$results = $this->eloquentRelation->getResults(); |
|
39
|
|
|
|
|
40
|
1 |
|
$results = $results->keyBy('locale'); |
|
41
|
|
|
|
|
42
|
1 |
|
$this->readConfig('edit'); |
|
43
|
|
|
|
|
44
|
1 |
|
if (empty($arrayKey)) { |
|
45
|
1 |
|
$arrayKey = $this->name; |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
$translationFields = []; |
|
49
|
1 |
|
if (! empty($columns)) { |
|
50
|
1 |
|
foreach ($this->langs as $key => $lang) { |
|
51
|
1 |
|
$tempFields = []; |
|
52
|
1 |
|
foreach ($columns as $columnName => $column) { |
|
53
|
1 |
|
if ($columnName === $this->eloquentRelation->getPlainForeignKey()) { |
|
54
|
1 |
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
if ($columnName === $this->eloquentRelation->getParent()->getKeyName()) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
$formType = null; |
|
62
|
|
|
|
|
63
|
1 |
|
if ($columnName === 'locale') { |
|
64
|
1 |
|
$formType = 'hidden'; |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
$config = [ |
|
68
|
1 |
|
'name' => $columnName, |
|
69
|
1 |
|
'presentation' => ucfirst(transcrud($columnName)) . ' [' . $lang . ']', |
|
70
|
1 |
|
'form_type' => $formType, |
|
71
|
1 |
|
'no_validate' => true, |
|
72
|
1 |
|
'validation' => null, |
|
73
|
|
|
'functions' => null |
|
74
|
1 |
|
]; |
|
75
|
|
|
|
|
76
|
1 |
|
$config = $this->setConfig($config, $columnName); |
|
77
|
|
|
|
|
78
|
|
|
/** @var Field $field */ |
|
79
|
1 |
|
$field = $this->fieldFactory |
|
80
|
1 |
|
->setColumn($column) |
|
81
|
1 |
|
->setConfig($config) |
|
82
|
1 |
|
->get(); |
|
83
|
|
|
|
|
84
|
1 |
|
if ($columnName === 'locale') { |
|
85
|
1 |
|
$field->setValue($lang); |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
if ($results->has($lang)) { |
|
89
|
|
|
$item = $results->get($lang); |
|
90
|
|
|
|
|
91
|
|
|
$field->setValue($item->getAttribute($columnName)); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$tempFields[] = $field; |
|
95
|
1 |
|
} |
|
96
|
1 |
|
$translationFields[$arrayKey][$lang] = $tempFields; |
|
97
|
1 |
|
} |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return $translationFields; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param array|null $relationArray |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
2 |
|
public function persist(array $relationArray = null, Request $request) |
|
108
|
|
|
{ |
|
109
|
2 |
|
if (! empty($relationArray)) { |
|
110
|
2 |
|
$currentTranslations = $this->eloquentRelation->getResults(); |
|
111
|
2 |
|
$currentTranslations = $currentTranslations->keyBy('locale'); |
|
112
|
|
|
|
|
113
|
2 |
|
foreach ($relationArray as $translation) { |
|
114
|
2 |
|
$isEmpty = true; |
|
115
|
|
|
|
|
116
|
2 |
|
foreach ($translation as $fieldKey => $fieldValue) { |
|
117
|
2 |
|
if ($isEmpty && $fieldKey != 'locale') { |
|
118
|
2 |
|
$isEmpty = ($isEmpty === ($fieldValue === '')); |
|
119
|
2 |
|
} |
|
120
|
2 |
|
} |
|
121
|
|
|
|
|
122
|
2 |
|
if ($currentTranslations->has($translation['locale'])) { |
|
123
|
1 |
|
$translationModel = $currentTranslations->get($translation['locale']); |
|
124
|
1 |
|
if ($isEmpty) { |
|
125
|
1 |
|
$translationModel->delete(); |
|
126
|
1 |
|
continue; |
|
127
|
|
|
} |
|
128
|
1 |
|
} else { |
|
129
|
1 |
|
$translationModel = $this->eloquentRelation->getRelated()->newInstance(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
if ($isEmpty === false) { |
|
133
|
2 |
|
$translationModel->setAttribute($this->eloquentRelation->getForeignKey(), $this->relatedModel->id); |
|
134
|
|
|
|
|
135
|
2 |
|
foreach ($translation as $fieldKey => $fieldValue) { |
|
136
|
2 |
|
$translationModel->setAttribute($fieldKey, $fieldValue); |
|
137
|
2 |
|
} |
|
138
|
2 |
|
} |
|
139
|
2 |
|
$translationModel->save(); |
|
140
|
2 |
|
} |
|
141
|
2 |
|
} |
|
142
|
2 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getDisplayType() |
|
148
|
|
|
{ |
|
149
|
|
|
return self::DISPLAY_TYPE_INLINE; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..