1
|
|
|
<?php namespace Mascame\Artificer\Fields; |
2
|
|
|
|
3
|
|
|
use Mascame\Artificer\Artificer; |
4
|
|
|
use Mascame\Artificer\Fields\Types\Relations\Relation; |
5
|
|
|
use Mascame\Artificer\Model\Model; |
6
|
|
|
use \Illuminate\Support\Str as Str; |
7
|
|
|
use Mascame\Artificer\Fields\FieldWrapper; |
8
|
|
|
use Mascame\Formality\Field\Field; |
9
|
|
|
|
10
|
|
|
class FieldFactory extends \Mascame\Formality\Factory\Factory |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public $fieldClass; |
14
|
|
|
public $fields; |
15
|
|
|
public $relatedFields = null; |
16
|
|
|
public $custom_fields = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Model |
20
|
|
|
*/ |
21
|
|
|
public $modelObject; |
22
|
|
|
public $data; |
23
|
|
|
|
24
|
|
|
public $namespace = '\Mascame\Artificer\Fields\Types\\'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param $data |
28
|
|
|
* @return mixed |
29
|
|
|
*/ |
30
|
|
|
public function makeFields() |
31
|
|
|
{ |
32
|
|
|
$fields = parent::makeFields(); |
33
|
|
|
|
34
|
|
|
foreach($fields as $key => $field) { |
35
|
|
|
/** |
36
|
|
|
* @var $field Field |
37
|
|
|
*/ |
38
|
|
|
$field->setOptions([ |
39
|
|
|
'attributes' => [ |
40
|
|
|
'class' => 'form-control' |
41
|
|
|
] |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
if (is_a($field, \Mascame\Artificer\Fields\Types\Relations\Relation::class)) { |
45
|
|
|
$field = $this->completeRelation($field); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$fields[$key] = new FieldWrapper($field); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $fields; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $field Relation |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function completeRelation($field) { |
59
|
|
|
$relationship = $field->getOption('relationship', []); |
60
|
|
|
|
61
|
|
|
$completedRelation = [ |
62
|
|
|
"method" => $field->guessRelatedMethod(), |
63
|
|
|
"type" => $field->getType(), |
64
|
|
|
"model" => $field->guessModel(), |
65
|
|
|
"show" => function($value) { |
66
|
|
|
if (! is_array($value) && method_exists($value, 'toArray')) { |
67
|
|
|
|
68
|
|
|
// Avoids cryptic errors |
69
|
|
|
try { |
70
|
|
|
$value = $value->toArray(); |
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
var_dump($e->getMessage()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Jump to next column avoiding 'id' |
77
|
|
|
return array_values(array_slice($value, 1, 1))[0]; |
78
|
|
|
} |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
// user config takes preference |
82
|
|
|
$field->setOptions(['relationship' => array_merge($completedRelation, $relationship)]); |
83
|
|
|
|
84
|
|
|
return $field; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param $name |
89
|
|
|
* @return bool|mixed |
90
|
|
|
*/ |
91
|
|
|
// protected function getTypeFromConfig($name) { |
92
|
|
|
// if (FieldOption::has('type', $name) || FieldOption::has('relationship.type', $name)) { |
93
|
|
|
// return (FieldOption::has('type', $name)) ? |
94
|
|
|
// FieldOption::get('type', $name) : |
95
|
|
|
// FieldOption::get('relationship.type', $name); |
96
|
|
|
// } |
97
|
|
|
// |
98
|
|
|
// return false; |
99
|
|
|
// } |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $name |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
protected function isRelation($name) |
106
|
|
|
{ |
107
|
|
|
return Str::contains($this->fieldClass, '\\Relations\\') || in_array($name, $this->relatedFields); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array|null |
112
|
|
|
*/ |
113
|
|
|
public function getRelated() |
114
|
|
|
{ |
115
|
|
|
if ($this->relatedFields) return $this->relatedFields; |
116
|
|
|
|
117
|
|
|
if (null == $fields = FieldOption::all()) { |
118
|
|
|
return $this->relatedFields = []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/* |
122
|
|
|
* We compare columns with config array to determine if there are new fields |
123
|
|
|
*/ |
124
|
|
|
$this->relatedFields = array_diff(array_keys($fields), $this->modelObject->columns); |
125
|
|
|
|
126
|
|
|
return $this->relatedFields; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
protected function withRelated() |
133
|
|
|
{ |
134
|
|
|
$related = $this->getRelated(); |
135
|
|
|
|
136
|
|
|
if ( ! empty($related)) { |
137
|
|
|
foreach ($related as $field) { |
138
|
|
|
$this->modelObject->columns[] = $field; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->modelObject->columns; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
// protected function withCustomFields() |
149
|
|
|
// { |
150
|
|
|
// if (isset($this->modelObject->options['fields'])) { |
151
|
|
|
// |
152
|
|
|
// foreach ($this->modelObject->options['fields'] as $name => $data) { |
153
|
|
|
// if ( ! in_array($name, $this->modelObject->columns)) { |
154
|
|
|
// $this->modelObject->columns[] = $name; |
155
|
|
|
// } |
156
|
|
|
// } |
157
|
|
|
// |
158
|
|
|
// } |
159
|
|
|
// |
160
|
|
|
// return $this->modelObject->columns; |
161
|
|
|
// } |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $field |
165
|
|
|
* @return null |
166
|
|
|
*/ |
167
|
|
|
public function fieldValue($field) |
168
|
|
|
{ |
169
|
|
|
return (isset($this->data->$field)) ? $this->data->$field : null; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} |