|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
|
|
8
|
|
|
trait ColumnsProtectedMethods |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Add a column to the current operation, using the Setting API. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $column Column definition array. |
|
14
|
|
|
*/ |
|
15
|
|
|
protected function addColumnToOperationSettings($column) |
|
16
|
|
|
{ |
|
17
|
|
|
$allColumns = $this->columns(); |
|
|
|
|
|
|
18
|
|
|
$allColumns = Arr::add($allColumns, $column['key'], $column); |
|
19
|
|
|
|
|
20
|
|
|
$this->setOperationSetting('columns', $allColumns); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* If a column priority has not been defined, provide a default one. |
|
25
|
|
|
* |
|
26
|
|
|
* @param array $column Column definition array. |
|
27
|
|
|
* @return array Proper array defining the column. |
|
28
|
|
|
*/ |
|
29
|
|
|
protected function makeSureColumnHasPriority($column) |
|
30
|
|
|
{ |
|
31
|
|
|
$columns_count = $this->countColumnsWithoutActions(); |
|
|
|
|
|
|
32
|
|
|
$assumed_priority = $columns_count ? $columns_count : 0; |
|
33
|
|
|
|
|
34
|
|
|
$column['priority'] = $column['priority'] ?? $assumed_priority; |
|
35
|
|
|
|
|
36
|
|
|
return $column; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* If the field definition array is actually a string, it means the programmer was lazy |
|
41
|
|
|
* and has only passed the name of the column. Turn that into a proper array. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $column Column definition array. |
|
44
|
|
|
* @return array Proper array defining the column. |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function makeSureColumnHasName($column) |
|
47
|
|
|
{ |
|
48
|
|
|
if (is_string($column)) { |
|
|
|
|
|
|
49
|
|
|
$column = ['name' => $column]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (is_array($column) && ! isset($column['name'])) { |
|
53
|
|
|
$column['name'] = 'anonymous_column_'.Str::random(5); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $column; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* If a column array is missing the "label" attribute, an ugly error would be show. |
|
61
|
|
|
* So we add the field Name as a label - it's better than nothing. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $column Column definition array. |
|
64
|
|
|
* @return array Proper array defining the column. |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function makeSureColumnHasLabel($column) |
|
67
|
|
|
{ |
|
68
|
|
|
if (! isset($column['label'])) { |
|
69
|
|
|
$column['label'] = mb_ucfirst($this->makeLabel($column['name'])); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $column; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* If a column definition is missing the type, set a default. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $column Column definition array. |
|
79
|
|
|
* @return array Column definition array with type. |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function makeSureColumnHasType($column) |
|
82
|
|
|
{ |
|
83
|
|
|
// if it's got a method on the model with the same name |
|
84
|
|
|
// then it should be a relationship |
|
85
|
|
|
if (! isset($column['type']) && method_exists($this->model, $column['name'])) { |
|
86
|
|
|
$column['type'] = 'relationship'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (! isset($column['type'])) { |
|
90
|
|
|
$column['type'] = 'text'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $column; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* If a column definition is missing the key, set the default. |
|
98
|
|
|
* The key is used when storing all columns using the Settings API, |
|
99
|
|
|
* it is used as the "key" of the associative array that holds all columns. |
|
100
|
|
|
* |
|
101
|
|
|
* @param array $column Column definition array. |
|
102
|
|
|
* @return array Column definition array with key. |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function makeSureColumnHasKey($column) |
|
105
|
|
|
{ |
|
106
|
|
|
if (! isset($column['key'])) { |
|
107
|
|
|
$column['key'] = str_replace('.', '__', $column['name']); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $column; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* If a column definition is missing the wrapper element, set the default (empty). |
|
115
|
|
|
* The wrapper is the HTML element that wrappes around the column text. |
|
116
|
|
|
* By defining this array a developer can wrap the text into an anchor (link), |
|
117
|
|
|
* span, div or whatever they want. |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $column Column definition array. |
|
120
|
|
|
* @return array Column definition array with wrapper. |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function makeSureColumnHasWrapper($column) |
|
123
|
|
|
{ |
|
124
|
|
|
if (! isset($column['wrapper'])) { |
|
125
|
|
|
$column['wrapper'] = []; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $column; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* If an entity has been defined for the column, but no model, |
|
133
|
|
|
* determine the model from that relationship. |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $column Column definition array. |
|
136
|
|
|
* @return array Column definition array with model. |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function makeSureColumnHasModel($column) |
|
139
|
|
|
{ |
|
140
|
|
|
// if this is a relation type field and no corresponding model was specified, |
|
141
|
|
|
// get it from the relation method defined in the main model |
|
142
|
|
|
if (isset($column['entity']) && ! isset($column['model'])) { |
|
143
|
|
|
$column['model'] = $this->getRelationModel($column['entity']); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $column; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Move the most recently added column before or after the given target column. Default is before. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string|array $targetColumn The target column name or array. |
|
153
|
|
|
* @param bool $before If true, the column will be moved before the target column, otherwise it will be moved after it. |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function moveColumn($targetColumn, $before = true) |
|
156
|
|
|
{ |
|
157
|
|
|
// TODO: this and the moveField method from the Fields trait should be refactored into a single method and moved |
|
158
|
|
|
// into a common class |
|
159
|
|
|
$targetColumnName = is_array($targetColumn) ? $targetColumn['name'] : $targetColumn; |
|
160
|
|
|
$columnsArray = $this->columns(); |
|
161
|
|
|
|
|
162
|
|
|
if (array_key_exists($targetColumnName, $columnsArray)) { |
|
163
|
|
|
$targetColumnPosition = $before ? array_search($targetColumnName, array_keys($columnsArray)) : |
|
164
|
|
|
array_search($targetColumnName, array_keys($columnsArray)) + 1; |
|
165
|
|
|
|
|
166
|
|
|
$element = array_pop($columnsArray); |
|
167
|
|
|
$beginningPart = array_slice($columnsArray, 0, $targetColumnPosition, true); |
|
|
|
|
|
|
168
|
|
|
$endingArrayPart = array_slice($columnsArray, $targetColumnPosition, null, true); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
$columnsArray = array_merge($beginningPart, [$element['name'] => $element], $endingArrayPart); |
|
171
|
|
|
$this->setOperationSetting('columns', $columnsArray); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Check if the column exists in the database, as a DB column. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $table |
|
179
|
|
|
* @param string $name |
|
180
|
|
|
* |
|
181
|
|
|
* @return bool |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function hasDatabaseColumn($table, $name) |
|
184
|
|
|
{ |
|
185
|
|
|
static $cache = []; |
|
186
|
|
|
|
|
187
|
|
|
if ($this->driverIsMongoDb()) { |
|
|
|
|
|
|
188
|
|
|
return true; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if (isset($cache[$table])) { |
|
192
|
|
|
$columns = $cache[$table]; |
|
193
|
|
|
} else { |
|
194
|
|
|
$columns = $cache[$table] = $this->getSchema()->getColumnListing($table); |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return in_array($name, $columns); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|