1
|
|
|
<?php |
2
|
|
|
namespace Anavel\Crud\Abstractor\Eloquent\Relation; |
3
|
|
|
|
4
|
|
|
use Anavel\Crud\Abstractor\ConfigurationReader; |
5
|
|
|
use Anavel\Crud\Abstractor\Eloquent\Relation\Traits\CheckRelationConfig; |
6
|
|
|
use Anavel\Crud\Abstractor\Eloquent\Traits\ModelFields; |
7
|
|
|
use Anavel\Crud\Contracts\Abstractor\Model as ModelAbstractor; |
8
|
|
|
use Anavel\Crud\Contracts\Abstractor\Relation as RelationAbstractorContract; |
9
|
|
|
use ANavallaSuiza\Laravel\Database\Contracts\Manager\ModelManager; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation; |
12
|
|
|
use Anavel\Crud\Contracts\Abstractor\FieldFactory as FieldFactoryContract; |
13
|
|
|
use Illuminate\Support\Collection; |
14
|
|
|
|
15
|
|
|
abstract class Relation implements RelationAbstractorContract |
16
|
|
|
{ |
17
|
|
|
use CheckRelationConfig; |
18
|
|
|
use ConfigurationReader; |
19
|
|
|
use ModelFields; |
20
|
|
|
|
21
|
|
|
const DISPLAY_TYPE_TAB = 'tab'; |
22
|
|
|
const DISPLAY_TYPE_INLINE = 'inline'; |
23
|
|
|
|
24
|
|
|
protected $name; |
25
|
|
|
protected $slug; |
26
|
|
|
protected $presentation; |
27
|
|
|
protected $type; |
28
|
|
|
/** |
29
|
|
|
* @var Model |
30
|
|
|
*/ |
31
|
|
|
protected $relatedModel; |
32
|
|
|
/** |
33
|
|
|
* @var EloquentRelation |
34
|
|
|
*/ |
35
|
|
|
protected $eloquentRelation; |
36
|
|
|
/** |
37
|
|
|
* @var FieldFactoryContract |
38
|
|
|
*/ |
39
|
|
|
protected $fieldFactory; |
40
|
|
|
protected $modelManager; |
41
|
|
|
/** @var ModelAbstractor */ |
42
|
|
|
protected $modelAbstractor; |
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $config; |
47
|
|
|
|
48
|
35 |
|
public function __construct(array $config, ModelManager $modelManager, Model $model, EloquentRelation $eloquentRelation, FieldFactoryContract $fieldFactory) |
49
|
|
|
{ |
50
|
35 |
|
$this->checkNameConfig($config); |
51
|
35 |
|
$this->name = $config['name']; |
52
|
35 |
|
$this->slug = $config['name']; |
53
|
35 |
|
$this->relatedModel = $model; |
54
|
35 |
|
$this->eloquentRelation = $eloquentRelation; |
55
|
35 |
|
$this->fieldFactory = $fieldFactory; |
56
|
|
|
|
57
|
35 |
|
$this->modelManager = $modelManager; |
58
|
|
|
|
59
|
35 |
|
$this->config = $config; |
60
|
35 |
|
$this->setup(); |
61
|
|
|
|
62
|
32 |
|
$relatedModelClassName = get_class($this->eloquentRelation->getRelated()); |
63
|
32 |
|
$relatedmodelRelationsConfig = array(); |
64
|
|
|
|
65
|
1 |
|
foreach (config('anavel-crud.models') as $modelConfig) { |
66
|
|
|
if (is_array($modelConfig) && array_key_exists('model', $modelConfig) && $relatedModelClassName == $modelConfig['model']) { |
67
|
1 |
|
if (array_key_exists('relations', $modelConfig)) { |
68
|
|
|
$relatedmodelRelationsConfig['relations'] = $modelConfig['relations']; |
69
|
1 |
|
} |
70
|
|
|
} |
71
|
1 |
|
} |
72
|
1 |
|
|
73
|
1 |
|
$this->modelAbstractor = \App::make('Anavel\Crud\Contracts\Abstractor\ModelFactory')->getByClassName(get_class($this->eloquentRelation->getRelated()), $relatedmodelRelationsConfig); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function addSecondaryRelationFields(array $fields) |
77
|
|
|
{ |
78
|
|
|
foreach ($this->modelAbstractor->getRelations() as $relationKey => $relation) { |
79
|
|
|
/** @var RelationAbstractorContract $relation */ |
80
|
|
|
foreach ($relation->getEditFields($relationKey) as $editGroupName => $editGroup) { |
81
|
|
|
$fields[$this->name][$editGroupName] = $editGroup; |
82
|
|
|
}; |
83
|
|
|
} |
84
|
|
|
return $fields; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getName() |
88
|
|
|
{ |
89
|
|
|
return $this->name; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
/** |
93
|
|
|
* return null|string |
94
|
4 |
|
*/ |
95
|
|
|
public function getDisplay() |
96
|
|
|
{ |
97
|
|
|
if (! empty($this->config['display'])) { |
98
|
4 |
|
return $this->config['display']; |
99
|
4 |
|
} |
100
|
4 |
|
return null; |
101
|
|
|
} |
102
|
4 |
|
|
103
|
|
View Code Duplication |
public function getPresentation() |
|
|
|
|
104
|
|
|
{ |
105
|
3 |
|
if ($this->presentation) { |
106
|
|
|
return transcrud($this->presentation); |
107
|
3 |
|
} |
108
|
|
|
|
109
|
|
|
$nameWithSpaces = str_replace('_', ' ', $this->name); |
110
|
|
|
$namePieces = explode(' ', $nameWithSpaces); |
111
|
|
|
$namePieces = array_filter(array_map('trim', $namePieces)); |
112
|
|
|
|
113
|
|
|
return transcrud(ucfirst(implode(' ', $namePieces))); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getType() |
117
|
|
|
{ |
118
|
4 |
|
return get_class($this); |
119
|
|
|
} |
120
|
4 |
|
|
121
|
|
|
public function getModelAbstractor() |
122
|
|
|
{ |
123
|
|
|
return $this->modelAbstractor; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return Collection |
128
|
|
|
*/ |
129
|
|
|
public function getSecondaryRelations() |
130
|
|
|
{ |
131
|
|
|
return $this->modelAbstractor->getRelations(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param Model $relatedModel |
136
|
|
|
* @return Relation |
137
|
|
|
*/ |
138
|
|
|
public function setRelatedModel(Model $relatedModel) |
139
|
|
|
{ |
140
|
|
|
$this->relatedModel = $relatedModel; |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.