|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD; |
|
4
|
|
|
|
|
5
|
|
|
use DB; |
|
6
|
|
|
use Illuminate\Support\Facades\Config; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
|
|
9
|
|
|
trait CrudTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/* |
|
12
|
|
|
|-------------------------------------------------------------------------- |
|
13
|
|
|
| Methods for ENUM and SELECT crud fields. |
|
14
|
|
|
|-------------------------------------------------------------------------- |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
public static function getPossibleEnumValues($field_name) |
|
18
|
|
|
{ |
|
19
|
|
|
$instance = new static(); // create an instance of the model to be able to get the table name |
|
20
|
|
|
$type = DB::select(DB::raw('SHOW COLUMNS FROM `'.Config::get('database.connections.'.env('DB_CONNECTION').'.prefix').$instance->getTable().'` WHERE Field = "'.$field_name.'"'))[0]->Type; |
|
|
|
|
|
|
21
|
|
|
preg_match('/^enum\((.*)\)$/', $type, $matches); |
|
22
|
|
|
$enum = []; |
|
23
|
|
|
foreach (explode(',', $matches[1]) as $value) { |
|
24
|
|
|
$enum[] = trim($value, "'"); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return $enum; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public static function getEnumValuesAsAssociativeArray($field_name) |
|
31
|
|
|
{ |
|
32
|
|
|
$instance = new static(); |
|
33
|
|
|
$enum_values = $instance->getPossibleEnumValues($field_name); |
|
34
|
|
|
|
|
35
|
|
|
$array = array_flip($enum_values); |
|
36
|
|
|
|
|
37
|
|
|
foreach ($array as $key => $value) { |
|
38
|
|
|
$array[$key] = $key; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $array; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function isColumnNullable($column_name) |
|
45
|
|
|
{ |
|
46
|
|
|
// create an instance of the model to be able to get the table name |
|
47
|
|
|
$instance = new static(); |
|
48
|
|
|
|
|
49
|
|
|
$conn = DB::connection($instance->getConnectionName()); |
|
|
|
|
|
|
50
|
|
|
$table = Config::get('database.connections.'.env('DB_CONNECTION').'.prefix').$instance->getTable(); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
// register the enum column type, because Doctrine doesn't support it |
|
53
|
|
|
$conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); |
|
54
|
|
|
|
|
55
|
|
|
return ! $conn->getDoctrineColumn($table, $column_name)->getNotnull(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/* |
|
59
|
|
|
|-------------------------------------------------------------------------- |
|
60
|
|
|
| Methods for Fake Fields functionality (used in PageManager). |
|
61
|
|
|
|-------------------------------------------------------------------------- |
|
62
|
|
|
*/ |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Add fake fields as regular attributes, even though they are stored as JSON. |
|
66
|
|
|
* |
|
67
|
|
|
* @param array $columns - the database columns that contain the JSONs |
|
68
|
|
|
*/ |
|
69
|
|
|
public function addFakes($columns = ['extras']) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($columns as $key => $column) { |
|
72
|
|
|
$column_contents = $this->{$column}; |
|
73
|
|
|
|
|
74
|
|
|
if (! is_object($this->{$column})) { |
|
75
|
|
|
$column_contents = json_decode($this->{$column}); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (count($column_contents)) { |
|
79
|
|
|
foreach ($column_contents as $fake_field_name => $fake_field_value) { |
|
80
|
|
|
$this->setAttribute($fake_field_name, $fake_field_value); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Return the entity with fake fields as attributes. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $columns - the database columns that contain the JSONs |
|
90
|
|
|
* |
|
91
|
|
|
* @return Model |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
|
|
public function withFakes($columns = []) |
|
94
|
|
|
{ |
|
95
|
|
|
$model = '\\'.get_class($this); |
|
96
|
|
|
|
|
97
|
|
|
if (! count($columns)) { |
|
98
|
|
|
$columns = (property_exists($model, 'fakeColumns')) ? $this->fakeColumns : ['extras']; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->addFakes($columns); |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/* |
|
107
|
|
|
|-------------------------------------------------------------------------- |
|
108
|
|
|
| Methods for storing uploaded files (used in CRUD). |
|
109
|
|
|
|-------------------------------------------------------------------------- |
|
110
|
|
|
*/ |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Handle file upload and DB storage for a file: |
|
114
|
|
|
* - on CREATE |
|
115
|
|
|
* - stores the file at the destination path |
|
116
|
|
|
* - generates a name |
|
117
|
|
|
* - stores the full path in the DB; |
|
118
|
|
|
* - on UPDATE |
|
119
|
|
|
* - if the value is null, deletes the file and sets null in the DB |
|
120
|
|
|
* - if the value is different, stores the different file and updates DB value. |
|
121
|
|
|
* |
|
122
|
|
|
* @param [type] $value Value for that column sent from the input. |
|
|
|
|
|
|
123
|
|
|
* @param [type] $attribute_name Model attribute name (and column in the db). |
|
|
|
|
|
|
124
|
|
|
* @param [type] $disk Filesystem disk used to store files. |
|
|
|
|
|
|
125
|
|
|
* @param [type] $destination_path Path in disk where to store the files. |
|
|
|
|
|
|
126
|
|
|
*/ |
|
127
|
|
|
public function uploadFileToDisk($value, $attribute_name, $disk, $destination_path) |
|
128
|
|
|
{ |
|
129
|
|
|
$request = \Request::instance(); |
|
130
|
|
|
|
|
131
|
|
|
// if a new file is uploaded, delete the file from the disk |
|
132
|
|
|
if ($request->hasFile($attribute_name) && |
|
133
|
|
|
$this->{$attribute_name} && |
|
134
|
|
|
$this->{$attribute_name} != null) { |
|
135
|
|
|
\Storage::disk($disk)->delete($this->{$attribute_name}); |
|
136
|
|
|
$this->attributes[$attribute_name] = null; |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
// if the file input is empty, delete the file from the disk |
|
140
|
|
|
if (is_null($value) && $this->{$attribute_name} != null) { |
|
141
|
|
|
\Storage::disk($disk)->delete($this->{$attribute_name}); |
|
142
|
|
|
$this->attributes[$attribute_name] = null; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// if a new file is uploaded, store it on disk and its filename in the database |
|
146
|
|
View Code Duplication |
if ($request->hasFile($attribute_name) && $request->file($attribute_name)->isValid()) { |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
// 1. Generate a new file name |
|
149
|
|
|
$file = $request->file($attribute_name); |
|
150
|
|
|
$new_file_name = md5($file->getClientOriginalName().time()).'.'.$file->getClientOriginalExtension(); |
|
151
|
|
|
|
|
152
|
|
|
// 2. Move the new file to the correct path |
|
153
|
|
|
$file_path = $file->storeAs($destination_path, $new_file_name, $disk); |
|
154
|
|
|
|
|
155
|
|
|
// 3. Save the complete path to the database |
|
156
|
|
|
$this->attributes[$attribute_name] = $file_path; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Handle multiple file upload and DB storage: |
|
162
|
|
|
* - if files are sent |
|
163
|
|
|
* - stores the files at the destination path |
|
164
|
|
|
* - generates random names |
|
165
|
|
|
* - stores the full path in the DB, as JSON array; |
|
166
|
|
|
* - if a hidden input is sent to clear one or more files |
|
167
|
|
|
* - deletes the file |
|
168
|
|
|
* - removes that file from the DB. |
|
169
|
|
|
* |
|
170
|
|
|
* @param [type] $value Value for that column sent from the input. |
|
|
|
|
|
|
171
|
|
|
* @param [type] $attribute_name Model attribute name (and column in the db). |
|
|
|
|
|
|
172
|
|
|
* @param [type] $disk Filesystem disk used to store files. |
|
|
|
|
|
|
173
|
|
|
* @param [type] $destination_path Path in disk where to store the files. |
|
|
|
|
|
|
174
|
|
|
*/ |
|
175
|
|
|
public function uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path) |
|
|
|
|
|
|
176
|
|
|
{ |
|
177
|
|
|
$request = \Request::instance(); |
|
178
|
|
|
$attribute_value = (array) $this->{$attribute_name}; |
|
179
|
|
|
$files_to_clear = $request->get('clear_'.$attribute_name); |
|
180
|
|
|
|
|
181
|
|
|
// if a file has been marked for removal, |
|
182
|
|
|
// delete it from the disk and from the db |
|
183
|
|
|
if ($files_to_clear) { |
|
184
|
|
|
$attribute_value = (array) $this->{$attribute_name}; |
|
185
|
|
|
foreach ($files_to_clear as $key => $filename) { |
|
186
|
|
|
\Storage::disk($disk)->delete($filename); |
|
187
|
|
|
$attribute_value = array_where($attribute_value, function ($value, $key) use ($filename) { |
|
|
|
|
|
|
188
|
|
|
return $value != $filename; |
|
189
|
|
|
}); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
// if a new file is uploaded, store it on disk and its filename in the database |
|
194
|
|
View Code Duplication |
if ($request->hasFile($attribute_name)) { |
|
|
|
|
|
|
195
|
|
|
foreach ($request->file($attribute_name) as $file) { |
|
|
|
|
|
|
196
|
|
|
if ($file->isValid()) { |
|
197
|
|
|
// 1. Generate a new file name |
|
198
|
|
|
$new_file_name = md5($file->getClientOriginalName().time()).'.'.$file->getClientOriginalExtension(); |
|
199
|
|
|
|
|
200
|
|
|
// 2. Move the new file to the correct path |
|
201
|
|
|
$file_path = $file->storeAs($destination_path, $new_file_name, $disk); |
|
202
|
|
|
|
|
203
|
|
|
// 3. Add the public path to the database |
|
204
|
|
|
$attribute_value[] = $file_path; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
$this->attributes[$attribute_name] = json_encode($attribute_value); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/* |
|
213
|
|
|
|-------------------------------------------------------------------------- |
|
214
|
|
|
| Methods for working with translatable models. |
|
215
|
|
|
|-------------------------------------------------------------------------- |
|
216
|
|
|
*/ |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Get the attributes that were casted in the model. |
|
220
|
|
|
* Used for translations because Spatie/Laravel-Translatable |
|
221
|
|
|
* overwrites the getCasts() method. |
|
222
|
|
|
* |
|
223
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
224
|
|
|
*/ |
|
225
|
|
|
public function getCastedAttributes() : array |
|
226
|
|
|
{ |
|
227
|
|
|
return parent::getCasts(); |
|
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Check if a model is translatable. |
|
232
|
|
|
* All translation adaptors must have the translationEnabledForModel() method. |
|
233
|
|
|
* |
|
234
|
|
|
* @return bool |
|
235
|
|
|
*/ |
|
236
|
|
|
public function translationEnabled() |
|
237
|
|
|
{ |
|
238
|
|
|
if (method_exists($this, 'translationEnabledForModel')) { |
|
239
|
|
|
return $this->translationEnabledForModel(); |
|
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return false; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.