|
1
|
|
|
<?php namespace Backpack\CRUD; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
5
|
|
|
use DB; |
|
6
|
|
|
use Lang; |
|
7
|
|
|
|
|
8
|
|
|
trait CrudTrait { |
|
9
|
|
|
|
|
10
|
|
|
/* |
|
11
|
|
|
|-------------------------------------------------------------------------- |
|
12
|
|
|
| Methods for ENUM and SELECT crud fields. |
|
13
|
|
|
|-------------------------------------------------------------------------- |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
public static function getPossibleEnumValues($field_name){ |
|
17
|
|
|
$instance = new static; // create an instance of the model to be able to get the table name |
|
18
|
|
|
$type = DB::select( DB::raw('SHOW COLUMNS FROM '.$instance->getTable().' WHERE Field = "'.$field_name.'"') )[0]->Type; |
|
|
|
|
|
|
19
|
|
|
preg_match('/^enum\((.*)\)$/', $type, $matches); |
|
20
|
|
|
$enum = array(); |
|
21
|
|
|
$exploded = explode(',', $matches[1]); |
|
22
|
|
|
foreach($exploded as $value){ |
|
23
|
|
|
$v = trim( $value, "'" ); |
|
24
|
|
|
$enum[] = $v; |
|
25
|
|
|
} |
|
26
|
|
|
return $enum; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function isColumnNullable($column_name) { |
|
30
|
|
|
$instance = new static; // create an instance of the model to be able to get the table name |
|
31
|
|
|
$answer = DB::select(DB::raw("SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='".$instance->getTable()."' AND COLUMN_NAME='".$column_name."' AND table_schema='".env('DB_DATABASE')."'"))[0]; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
return ($answer->IS_NULLABLE == 'YES' ? true : false); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/* |
|
38
|
|
|
|-------------------------------------------------------------------------- |
|
39
|
|
|
| Methods for Fake Fields functionality (used in PageManager). |
|
40
|
|
|
|-------------------------------------------------------------------------- |
|
41
|
|
|
*/ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Add fake fields as regular attributes, even though they are stored as JSON. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $columns - the database columns that contain the JSONs |
|
47
|
|
|
*/ |
|
48
|
|
|
public function addFakes($columns = ['extras']) { |
|
49
|
|
|
foreach ($columns as $key => $column) { |
|
50
|
|
|
|
|
51
|
|
|
$column_contents = $this->{$column}; |
|
52
|
|
|
|
|
53
|
|
|
if (!is_object($this->{$column})) |
|
54
|
|
|
{ |
|
55
|
|
|
$column_contents = json_decode($this->{$column}); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (count($column_contents)) |
|
59
|
|
|
{ |
|
60
|
|
|
foreach ($column_contents as $fake_field_name => $fake_field_value) { |
|
61
|
|
|
$this->setAttribute($fake_field_name, $fake_field_value); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Return the entity with fake fields as attributes. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $columns - the database columns that contain the JSONs |
|
71
|
|
|
* @return CrudTrait |
|
|
|
|
|
|
72
|
|
|
*/ |
|
73
|
|
|
public function withFakes($columns = []) |
|
74
|
|
|
{ |
|
75
|
|
|
$model = '\\'.get_class($this); |
|
76
|
|
|
|
|
77
|
|
|
if (!count($columns)) { |
|
78
|
|
|
if (property_exists($model, 'fakeColumns')) { |
|
79
|
|
|
$columns = $this->fakeColumns; |
|
|
|
|
|
|
80
|
|
|
} else |
|
81
|
|
|
{ |
|
82
|
|
|
$columns = ['extras']; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->addFakes($columns); |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/* |
|
92
|
|
|
|-------------------------------------------------------------------------- |
|
93
|
|
|
| Translation Methods |
|
94
|
|
|
|-------------------------------------------------------------------------- |
|
95
|
|
|
*/ |
|
96
|
|
|
|
|
97
|
|
|
public function translations() |
|
98
|
|
|
{ |
|
99
|
|
|
$model = '\\'.get_class($this); |
|
100
|
|
|
|
|
101
|
|
|
if (isset($this->translatable)) |
|
102
|
|
|
{ |
|
103
|
|
|
return $model::where('translation_of', $this->id)->get(); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return collect(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// get translations plus current item, plus original |
|
110
|
|
|
public function allTranslations() |
|
111
|
|
|
{ |
|
112
|
|
|
$model = '\\'.get_class($this); |
|
113
|
|
|
|
|
114
|
|
|
// the translations |
|
115
|
|
|
$translations = $this->translations(); |
|
116
|
|
|
|
|
117
|
|
|
$all_translations = new Collection(); |
|
118
|
|
|
// the original |
|
119
|
|
|
if ($this->translation_of) { |
|
120
|
|
|
$original = $model::find($this->translation_of); |
|
|
|
|
|
|
121
|
|
|
$all_translations = $all_translations->push($original); |
|
122
|
|
|
|
|
123
|
|
|
// get all translations from the original |
|
124
|
|
|
$translationsOfOriginal = $model::where('translation_of', $original->id)->get(); |
|
125
|
|
|
foreach($translationsOfOriginal as $translation) { |
|
126
|
|
|
$all_translations->push($translation); |
|
127
|
|
|
} |
|
128
|
|
|
} else { |
|
129
|
|
|
// prepend the translation to be first in the list of translations |
|
130
|
|
|
$all_translations = $translations->prepend($this); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $all_translations; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function translation($translation_lang = false) |
|
137
|
|
|
{ |
|
138
|
|
|
if ($translation_lang == false) { |
|
|
|
|
|
|
139
|
|
|
$translation_lang = Lang::locale(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$model = '\\'.get_class($this); |
|
143
|
|
|
if (isset($this->translatable)) |
|
144
|
|
|
{ |
|
145
|
|
|
return $model::where('translation_of', $this->id)->where('translation_lang', $translation_lang)->first(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function translationLanguages() |
|
152
|
|
|
{ |
|
153
|
|
|
$model = '\\'.get_class($this); |
|
|
|
|
|
|
154
|
|
|
$translations = $this->translations(); |
|
155
|
|
|
|
|
156
|
|
|
$translated_in = []; |
|
157
|
|
|
|
|
158
|
|
|
if ($translations->count()) |
|
159
|
|
|
{ |
|
160
|
|
|
foreach ($translations as $key => $translation) { |
|
161
|
|
|
$translated_in[] = $translation->language; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return collect($translated_in); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function language() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->belongsTo('\Dick\TranslationManager\Models\Language', 'translation_lang', 'abbr'); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Overwriting the Eloquent save() method, to set a default translation language, if necessary. |
|
175
|
|
|
*/ |
|
176
|
|
|
public function save(array $options = []) |
|
177
|
|
|
{ |
|
178
|
|
|
if (isset($this->translatable)) |
|
179
|
|
|
{ |
|
180
|
|
|
// set a default language (the one the user is currently using) |
|
181
|
|
|
if (!(isset($this->translation_lang)) || $this->translation_lang == '') |
|
182
|
|
|
{ |
|
183
|
|
|
$this->translation_lang = \Lang::locale(); |
|
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// TODO: if some untranslatable attributes are empty, but its parent's are filled, copy them |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
parent::save($options); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
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.