|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Models\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
trait HasIdentifiableAttribute |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Get the name of the attribute that best defines the entry, from the user perspective. |
|
12
|
|
|
* |
|
13
|
|
|
* Rephrased: In most cases a user will NOT identify an Article because its ID is "4", but |
|
14
|
|
|
* because its name is "10 Ways to Burn Fat". This method returns the column in the database |
|
15
|
|
|
* that represents what is better to show to the user as an identifier rather than the ID. |
|
16
|
|
|
* Ex: name, title, label, description etc. |
|
17
|
|
|
* |
|
18
|
|
|
* @return string The name of the column that best defines this entry from the user perspective. |
|
19
|
|
|
*/ |
|
20
|
|
|
public function identifiableAttribute() |
|
21
|
|
|
{ |
|
22
|
|
|
if (property_exists($this, 'identifiableAttribute')) { |
|
23
|
|
|
return $this->identifiableAttribute; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return static::guessIdentifiableColumnName(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the most likely column in the db table that could be used as an identifiable attribute. |
|
31
|
|
|
* |
|
32
|
|
|
* @return string The name of the column in the database that is most likely to be a good indentifying attribute. |
|
33
|
|
|
*/ |
|
34
|
|
|
private static function guessIdentifiableColumnName() |
|
35
|
|
|
{ |
|
36
|
|
|
$instance = new static(); |
|
37
|
|
|
|
|
38
|
|
|
$conn = $instance->getConnectionWithExtraTypeMappings(); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
// column listing is not available in non-sql databases. In this scenario we infer |
|
41
|
|
|
// the identifiable attribute from the model `fillable` attributes |
|
42
|
|
|
if (! in_array($conn->getConfig()['driver'], CRUD::getSqlDriverList())) { |
|
|
|
|
|
|
43
|
|
|
return $instance->inferIdentifiableAttributeFromModelFillable(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$table = $instance->getTableWithPrefix(); |
|
|
|
|
|
|
47
|
|
|
$columns = $conn->getDoctrineSchemaManager()->listTableColumns($table); |
|
48
|
|
|
$indexes = $conn->getDoctrineSchemaManager()->listTableIndexes($table); |
|
49
|
|
|
$columnsNames = array_keys($columns); |
|
50
|
|
|
|
|
51
|
|
|
// if any of the sensibleDefaultNames column exists |
|
52
|
|
|
// that's probably a good choice |
|
53
|
|
|
foreach ($instance->getSensibleDefaultNames() as $defaultName) { |
|
54
|
|
|
if (in_array($defaultName, $columnsNames)) { |
|
55
|
|
|
return $defaultName; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// get indexed columns in database table |
|
60
|
|
|
$indexedColumns = []; |
|
61
|
|
|
foreach ($indexes as $index) { |
|
62
|
|
|
$indexColumns = $index->getColumns(); |
|
63
|
|
|
foreach ($indexColumns as $ic) { |
|
64
|
|
|
array_push($indexedColumns, $ic); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// if none of the sensible defaults exists |
|
69
|
|
|
// we get the first column from database |
|
70
|
|
|
// that is NOT indexed (usually primary, foreign keys) |
|
71
|
|
|
foreach ($columns as $columnName => $columnProperties) { |
|
72
|
|
|
if (! in_array($columnName, $indexedColumns)) { |
|
73
|
|
|
|
|
74
|
|
|
//check for convention "field<_id>" in case developer didn't add foreign key constraints. |
|
75
|
|
|
if (strpos($columnName, '_id') !== false) { |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $columnName; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// in case everything fails we just return the first column in database |
|
84
|
|
|
return Arr::first($columnsNames); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Infer the identifiable attribute from model fillable when getting the columns listing is not available. |
|
89
|
|
|
* |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
|
public function inferIdentifiableAttributeFromModelFillable() |
|
93
|
|
|
{ |
|
94
|
|
|
$fillableFields = $this->getFillable(); |
|
|
|
|
|
|
95
|
|
|
if (! empty($fillableFields)) { |
|
96
|
|
|
$matchedAttributeNames = array_intersect($this->getSensibleDefaultNames(), $fillableFields); |
|
97
|
|
|
if (! empty($matchedAttributeNames)) { |
|
98
|
|
|
return reset($matchedAttributeNames); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return reset($fillableFields); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
abort(500, 'Impossible to determine the identifiable attribute. Add it manually to your model or in your field definition.'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns a list of sensible default names to be shown to endusers. |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getSensibleDefaultNames() |
|
113
|
|
|
{ |
|
114
|
|
|
return ['name', 'title', 'description', 'label']; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|