1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Adds fluent syntax to Backpack CRUD Fields. |
7
|
|
|
* |
8
|
|
|
* In addition to the existing: |
9
|
|
|
* - CRUD::addField(['name' => 'price', 'type' => 'number']); |
10
|
|
|
* |
11
|
|
|
* Developers can also do: |
12
|
|
|
* - CRUD::field('price')->type('number'); |
13
|
|
|
* |
14
|
|
|
* And if the developer uses CrudField as Field in their CrudController: |
15
|
|
|
* - Field::name('price')->type('number'); |
16
|
|
|
*/ |
17
|
|
|
class CrudField |
18
|
|
|
{ |
19
|
|
|
protected $attributes; |
20
|
|
|
|
21
|
|
|
public function __construct($name) |
22
|
|
|
{ |
23
|
|
|
$field = $this->crud()->firstFieldWhere('name', $name); |
24
|
|
|
|
25
|
|
|
// if field exists |
26
|
|
|
if ((bool) $field) { |
27
|
|
|
// use all existing attributes |
28
|
|
|
$this->setAllAttributeValues($field); |
|
|
|
|
29
|
|
|
} else { |
30
|
|
|
// it means we're creating the field now, |
31
|
|
|
// so at the very least set the name attribute |
32
|
|
|
$this->setAttributeValue('name', $name); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $this->save(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function crud() |
39
|
|
|
{ |
40
|
|
|
return app()->make('crud'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a CrudField object with the parameter as its name. |
45
|
|
|
* |
46
|
|
|
* @param string $name Name of the column in the db, or model attribute. |
47
|
|
|
* @return CrudPanel |
48
|
|
|
*/ |
49
|
|
|
public static function name($name) |
50
|
|
|
{ |
51
|
|
|
return new static($name); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Remove the current field from the current operation. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function remove() |
60
|
|
|
{ |
61
|
|
|
$this->crud()->removeField($this->attributes['name']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Remove an attribute from the current field definition array. |
66
|
|
|
* |
67
|
|
|
* @param string $attribute Name of the attribute being removed. |
68
|
|
|
* @return CrudField |
69
|
|
|
*/ |
70
|
|
|
public function forget($attribute) |
71
|
|
|
{ |
72
|
|
|
$this->crud()->removeFieldAttribute($this->attributes['name'], $attribute); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Move the current field after another field. |
79
|
|
|
* |
80
|
|
|
* @param string $destinationField Name of the destination field. |
81
|
|
|
* @return CrudField |
82
|
|
|
*/ |
83
|
|
|
public function after($destinationField) |
84
|
|
|
{ |
85
|
|
|
$this->crud()->removeField($this->attributes['name']); |
86
|
|
|
$this->crud()->addField($this->attributes)->afterField($destinationField); |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Move the current field before another field. |
93
|
|
|
* |
94
|
|
|
* @param string $destinationField Name of the destination field. |
95
|
|
|
* @return CrudField |
96
|
|
|
*/ |
97
|
|
|
public function before($destinationField) |
98
|
|
|
{ |
99
|
|
|
$this->crud()->removeField($this->attributes['name']); |
100
|
|
|
$this->crud()->addField($this->attributes)->beforeField($destinationField); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Make the current field the first one in the fields list. |
107
|
|
|
* |
108
|
|
|
* @return CrudPanel |
109
|
|
|
*/ |
110
|
|
|
public function makeFirst() |
111
|
|
|
{ |
112
|
|
|
$this->crud()->removeField($this->attributes['name']); |
113
|
|
|
$this->crud()->addField($this->attributes)->makeFirstField(); |
114
|
|
|
|
115
|
|
|
return $this; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Make the current field the last one in the fields list. |
120
|
|
|
* |
121
|
|
|
* @return CrudPanel |
122
|
|
|
*/ |
123
|
|
|
public function makeLast() |
124
|
|
|
{ |
125
|
|
|
$this->crud()->removeField($this->attributes['name']); |
126
|
|
|
$this->crud()->addField($this->attributes); |
127
|
|
|
|
128
|
|
|
return $this; |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// ------------------- |
132
|
|
|
// CONVENIENCE METHODS |
133
|
|
|
// ------------------- |
134
|
|
|
// These methods don't do exactly what advertised by their name. |
135
|
|
|
// They exist because the original syntax was too long. |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the wrapper width at this many number of columns. |
139
|
|
|
* For example, to set a field wrapper to span across 6 columns, you can do both: |
140
|
|
|
* ->wrapper(['class' => 'form-group col-md-6']) |
141
|
|
|
* ->size(6). |
142
|
|
|
* |
143
|
|
|
* @param int $numberOfColumns How many columns should this field span across (1-12)? |
144
|
|
|
* @return CrudField |
145
|
|
|
*/ |
146
|
|
|
public function size($numberOfColumns) |
147
|
|
|
{ |
148
|
|
|
$this->attributes['wrapper']['class'] = 'form-group col-md-'.$numberOfColumns; |
149
|
|
|
|
150
|
|
|
return $this->save(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// --------------- |
154
|
|
|
// PRIVATE METHODS |
155
|
|
|
// --------------- |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set the value for a certain attribute on the CrudField object. |
159
|
|
|
* |
160
|
|
|
* @param string $attribute Name of the attribute. |
161
|
|
|
* @param string $value Value of that attribute. |
162
|
|
|
*/ |
163
|
|
|
private function setAttributeValue($attribute, $value) |
164
|
|
|
{ |
165
|
|
|
$this->attributes[$attribute] = $value; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Replace all field attributes on the CrudField object |
170
|
|
|
* with the given array of attribute-value pairs. |
171
|
|
|
* |
172
|
|
|
* @param array $array Array of attributes and their values. |
173
|
|
|
*/ |
174
|
|
|
private function setAllAttributeValues($array) |
175
|
|
|
{ |
176
|
|
|
$this->attributes = $array; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Update the global CrudPanel object with the current field attributes. |
181
|
|
|
* |
182
|
|
|
* @return CrudField |
183
|
|
|
*/ |
184
|
|
|
private function save() |
185
|
|
|
{ |
186
|
|
|
$key = $this->attributes['name']; |
187
|
|
|
|
188
|
|
|
if ($this->crud()->hasFieldWhere('name', $key)) { |
189
|
|
|
$this->crud()->modifyField($key, $this->attributes); |
190
|
|
|
} else { |
191
|
|
|
$this->crud()->addField($this->attributes); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// ----------------- |
198
|
|
|
// DEBUGGING METHODS |
199
|
|
|
// ----------------- |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Dump the current object to the screen, |
203
|
|
|
* so that the developer can see its contents. |
204
|
|
|
* |
205
|
|
|
* @return CrudField |
206
|
|
|
*/ |
207
|
|
|
public function dump() |
208
|
|
|
{ |
209
|
|
|
dump($this); |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Dump and die. Duumps the current object to the screen, |
216
|
|
|
* so that the developer can see its contents, then stops |
217
|
|
|
* the execution. |
218
|
|
|
* |
219
|
|
|
* @return CrudField |
220
|
|
|
*/ |
221
|
|
|
public function dd() |
222
|
|
|
{ |
223
|
|
|
dd($this); |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// ------------- |
229
|
|
|
// MAGIC METHODS |
230
|
|
|
// ------------- |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* If a developer calls a method that doesn't exist, assume they want: |
234
|
|
|
* - the CrudField object to have an attribute with that value; |
235
|
|
|
* - that field be updated inside the global CrudPanel object;. |
236
|
|
|
* |
237
|
|
|
* Eg: type('number') will set the "type" attribute to "number" |
238
|
|
|
* |
239
|
|
|
* @param string $method The method being called that doesn't exist. |
240
|
|
|
* @param array $parameters The arguments when that method was called. |
241
|
|
|
* |
242
|
|
|
* @return CrudField |
243
|
|
|
*/ |
244
|
|
|
public function __call($method, $parameters) |
245
|
|
|
{ |
246
|
|
|
$this->setAttributeValue($method, $parameters[0]); |
247
|
|
|
|
248
|
|
|
return $this->save(); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|