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
|
|
|
return $relation; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retrieve the models primary field for display purposes. |
49
|
|
|
* |
50
|
|
|
* @param $item Model to retrieve primary field of. |
51
|
|
|
* @param null|array $config CrudApi Configuration. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function getPrimaryField($item, $config = null) |
56
|
|
|
{ |
57
|
|
|
/* If config is not overridden then load crudapi config */ |
58
|
|
|
if ($config === null) { |
59
|
|
|
$config = config('crudapi'); |
60
|
|
|
} |
61
|
|
|
if (!isset($config['models']['fields']['default'])) { |
62
|
|
|
$defaultField = 'name'; |
63
|
|
|
} else { |
64
|
|
|
$defaultField = $config['models']['fields']['default']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/* Get the items Class */ |
68
|
|
|
$class = get_class($item); |
69
|
|
|
|
70
|
|
|
$stripped_class = str_replace($this->crudApi->namespace, '', $class); |
71
|
|
|
// if class starts with a \ remove it. |
72
|
|
|
if (substr($stripped_class, 0, 1) == '\\') { |
73
|
|
|
$stripped_class = substr($stripped_class, 1); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$primaryFields = $config['models']['fields']['primary']; |
77
|
|
|
|
78
|
|
|
if (array_key_exists($stripped_class, $primaryFields)) { |
79
|
|
|
return $primaryFields[$stripped_class]; |
80
|
|
|
} else { |
81
|
|
|
//return the default |
82
|
|
|
return $defaultField; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Render fields into appropriate format for an item creation form. |
88
|
|
|
* |
89
|
|
|
* @param $fields |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function formCreate($fields) |
94
|
|
|
{ |
95
|
|
|
$output = ''; |
96
|
|
|
|
97
|
|
View Code Duplication |
foreach ($fields as $f) { |
|
|
|
|
98
|
|
|
$ucF = ucfirst($f); |
99
|
|
|
|
100
|
|
|
$input_attr = [ |
101
|
|
|
'class' => 'form-control', |
102
|
|
|
'id' => 'createItem'.$f, |
103
|
|
|
'name' => $f, |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$output .= '<fieldset class="form-group">'; |
107
|
|
|
|
108
|
|
|
$output .= '<label for="'.$input_attr['id'].'">'.$ucF.'</label>'; |
109
|
|
|
|
110
|
|
|
if ($this->isIdField($f)) { |
111
|
|
|
$input_attr['type'] = 'select'; |
112
|
|
|
|
113
|
|
|
$output .= '<select '; |
114
|
|
|
foreach ($input_attr as $attr => $value) { |
115
|
|
|
$output .= "{$attr}='{$value}'"; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$relation = $this->crudApi->getRelatedModel($f); |
119
|
|
|
$output .= '>'; |
120
|
|
|
|
121
|
|
|
$output .= $this->crudApi->getRelatedOptions($relation); |
122
|
|
|
|
123
|
|
|
$output .= '</select>'; |
124
|
|
|
} else { |
125
|
|
|
$input_attr['type'] = 'text'; |
126
|
|
|
|
127
|
|
|
$output .= '<input '; |
128
|
|
|
foreach ($input_attr as $attr => $value) { |
129
|
|
|
$output .= "{$attr}='{$value}'"; |
130
|
|
|
} |
131
|
|
|
$output .= '>'; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$output .= '</fieldset>'; |
135
|
|
|
} |
136
|
|
|
return $output; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Return fields as table headings. |
141
|
|
|
* |
142
|
|
|
* @param $fields |
143
|
|
|
* |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function tableHeadings($fields) |
147
|
|
|
{ |
148
|
|
|
$output = ''; |
149
|
|
|
foreach ($fields as $f) { |
150
|
|
|
$output .= '<th>'.ucfirst($f).'</th>'; |
151
|
|
|
} |
152
|
|
|
return $output; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function tableContent($fields, $instance) |
156
|
|
|
{ |
157
|
|
|
$output = ''; |
158
|
|
|
foreach ($fields as $f) { |
159
|
|
|
if ($this->fieldHelper->isIdField($f)) { |
|
|
|
|
160
|
|
|
$display = $this->crudApi->getRelatedDisplay($f); |
161
|
|
|
$output .= '<td>' . $display . '</td>'; |
162
|
|
|
} else { |
163
|
|
|
$output .= '<td>' . $instance->$f . '</td>'; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
return $output; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.