1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Taskforcedev\CrudApi\Helpers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Field. |
7
|
|
|
*/ |
8
|
|
|
class Field |
9
|
|
|
{ |
10
|
|
|
public $crudApi; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Field constructor. |
14
|
|
|
* |
15
|
|
|
* @param CrudApi $crudApi |
16
|
|
|
*/ |
17
|
|
|
public function __construct(CrudApi $crudApi) |
18
|
|
|
{ |
19
|
|
|
$this->crudApi = $crudApi; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Determine if the field is an id field. |
24
|
|
|
* |
25
|
|
|
* @param $field |
26
|
|
|
* |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
public function isIdField($field) |
30
|
|
|
{ |
31
|
|
|
return strpos($field, '_id') === false ? false : true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Parse a relation field name into the relation name. |
36
|
|
|
* |
37
|
|
|
* @param string $field Field name |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getRelatedField($field) |
42
|
|
|
{ |
43
|
|
|
$relation = str_replace('_id', '', $field); |
44
|
|
|
|
45
|
|
|
return $relation; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Retrieve the models primary field for display purposes. |
50
|
|
|
* |
51
|
|
|
* @param $item Model to retrieve primary field of |
52
|
|
|
* @param null|array $config CrudApi Configuration |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getPrimaryField($item, $config = null) |
57
|
|
|
{ |
58
|
|
|
/* If config is not overridden then load crudapi config */ |
59
|
|
|
if ($config === null) { |
60
|
|
|
$config = config('crudapi'); |
61
|
|
|
} |
62
|
|
|
if (!isset($config['models']['fields']['default'])) { |
63
|
|
|
$defaultField = 'name'; |
64
|
|
|
} else { |
65
|
|
|
$defaultField = $config['models']['fields']['default']; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* Get the items Class */ |
69
|
|
|
$class = get_class($item); |
70
|
|
|
|
71
|
|
|
$stripped_class = str_replace($this->crudApi->namespace, '', $class); |
72
|
|
|
// if class starts with a \ remove it. |
73
|
|
|
if (substr($stripped_class, 0, 1) == '\\') { |
74
|
|
|
$stripped_class = substr($stripped_class, 1); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$primaryFields = $config['models']['fields']['primary']; |
78
|
|
|
|
79
|
|
|
if (array_key_exists($stripped_class, $primaryFields)) { |
80
|
|
|
return $primaryFields[$stripped_class]; |
81
|
|
|
} else { |
82
|
|
|
//return the default |
83
|
|
|
return $defaultField; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Display the primary field. |
89
|
|
|
* |
90
|
|
|
* @param $item |
91
|
|
|
* @param null $config |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function displayPrimaryField($item, $config = null) |
96
|
|
|
{ |
97
|
|
|
$field = $this->getPrimaryField($item, $config); |
98
|
|
|
|
99
|
|
|
return $item->$field; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Render fields into appropriate format for an item creation form. |
104
|
|
|
* |
105
|
|
|
* @param $fields |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function formCreate($fields) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$output = ''; |
112
|
|
|
|
113
|
|
|
foreach ($fields as $f) { |
114
|
|
|
$ucF = ucfirst($f); |
115
|
|
|
|
116
|
|
|
$input_attr = [ |
117
|
|
|
'class' => 'form-control', |
118
|
|
|
'id' => 'createItem'.$f, |
119
|
|
|
'name' => $f, |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
$label = $this->humanReadableField($ucF); |
123
|
|
|
|
124
|
|
|
$output .= '<fieldset class="form-group">'; |
125
|
|
|
|
126
|
|
|
$output .= '<label for="'.$input_attr['id'].'">'.$label.'</label>'; |
127
|
|
|
|
128
|
|
|
if ($this->isIdField($f)) { |
129
|
|
|
$input_attr['type'] = 'select'; |
130
|
|
|
|
131
|
|
|
$output .= '<select '; |
132
|
|
|
foreach ($input_attr as $attr => $value) { |
133
|
|
|
$output .= "{$attr}='{$value}'"; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$relation = $this->crudApi->getRelatedModel($f); |
137
|
|
|
$output .= '>'; |
138
|
|
|
|
139
|
|
|
$output .= $this->crudApi->getRelatedOptions($relation); |
140
|
|
|
|
141
|
|
|
$output .= '</select>'; |
142
|
|
|
} else { |
143
|
|
|
$input_attr['type'] = 'text'; |
144
|
|
|
|
145
|
|
|
$output .= '<input '; |
146
|
|
|
foreach ($input_attr as $attr => $value) { |
147
|
|
|
$output .= "{$attr}='{$value}'"; |
148
|
|
|
} |
149
|
|
|
$output .= '>'; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$output .= '</fieldset>'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $output; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Render fields into appropriate format for an edit form. |
160
|
|
|
* |
161
|
|
|
* @param array $fields Fields to render. |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function formEdit($fields) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$output = ''; |
168
|
|
|
foreach ($fields as $f) { |
169
|
|
|
$ucF = ucfirst($f); |
170
|
|
|
|
171
|
|
|
$input_attr = [ |
172
|
|
|
'class' => 'form-control', |
173
|
|
|
'id' => 'editItem'.$ucF, |
174
|
|
|
'name' => $f, |
175
|
|
|
]; |
176
|
|
|
|
177
|
|
|
$label = $this->humanReadableField($ucF); |
178
|
|
|
|
179
|
|
|
$output .= '<fieldset class="form-group">'; |
180
|
|
|
|
181
|
|
|
$output .= '<label for="'.$input_attr['id'].'">'.$label.'</label>'; |
182
|
|
|
|
183
|
|
|
if ($this->isIdField($f)) { |
184
|
|
|
$input_attr['type'] = 'select'; |
185
|
|
|
|
186
|
|
|
$output .= '<select '; |
187
|
|
|
foreach ($input_attr as $attr => $value) { |
188
|
|
|
$output .= "{$attr}='{$value}'"; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$relation = $this->crudApi->getRelatedModel($f); |
192
|
|
|
$output .= '>'; |
193
|
|
|
|
194
|
|
|
$output .= $this->crudApi->getRelatedOptions($relation); |
195
|
|
|
$output .= '</select>'; |
196
|
|
|
} else { |
197
|
|
|
$input_attr['type'] = 'text'; |
198
|
|
|
|
199
|
|
|
$output .= '<input '; |
200
|
|
|
foreach ($input_attr as $attr => $value) { |
201
|
|
|
$output .= "{$attr}='{$value}'"; |
202
|
|
|
} |
203
|
|
|
$output .= '>'; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$output .= '</fieldset>'; |
207
|
|
|
} |
208
|
|
|
return $output; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Return fields as table headings. |
213
|
|
|
* |
214
|
|
|
* @param array $fields Fields to display. |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
|
|
public function tableHeadings($fields) |
219
|
|
|
{ |
220
|
|
|
$output = ''; |
221
|
|
|
foreach ($fields as $f) { |
222
|
|
|
$field = $this->humanReadableField($f); |
223
|
|
|
$output .= '<th>'.$field.'</th>'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $output; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Output the instances fields into table content format. |
231
|
|
|
* |
232
|
|
|
* @param array $fields |
233
|
|
|
* @param $instance |
234
|
|
|
* |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
public function tableContent($fields, $instance) |
238
|
|
|
{ |
239
|
|
|
$output = ''; |
240
|
|
|
foreach ($fields as $f) { |
241
|
|
|
if ($this->isIdField($f)) { |
242
|
|
|
$related = $this->getRelatedField($f); |
243
|
|
|
$relation = $instance->$related; |
244
|
|
|
$field = $this->getPrimaryField($relation); |
245
|
|
|
if (strpos($field, ',')) { |
246
|
|
|
$subfields = explode(',', $field); |
247
|
|
|
$display = ''; |
248
|
|
|
foreach ($subfields as $sf) { |
249
|
|
|
$display .= $relation->$sf.' '; |
250
|
|
|
} |
251
|
|
|
} else { |
252
|
|
|
$display = $relation->$field; |
253
|
|
|
} |
254
|
|
|
$output .= '<td>'.$display.'</td>'; |
255
|
|
|
} else { |
256
|
|
|
$output .= '<td>'.$instance->$f.'</td>'; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $output; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Convert a field internal name into human readable name. |
265
|
|
|
* |
266
|
|
|
* Example: |
267
|
|
|
* 'Organisation_id' becomes 'Organisation Id'. |
268
|
|
|
* |
269
|
|
|
* @param string $field Field to convert. |
270
|
|
|
* |
271
|
|
|
* @return mixed|string |
272
|
|
|
*/ |
273
|
|
|
public function humanReadableField($field) |
274
|
|
|
{ |
275
|
|
|
$field = str_replace('_id', ' Id', $field); |
276
|
|
|
$field = ucwords($field); |
277
|
|
|
return $field; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|
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.