|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
|
4
|
|
|
|
|
5
|
|
|
trait FakeFields |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Update the request input array to something that can be passed to the model's create or update function. |
|
9
|
|
|
* The resulting array will only include the fields that are stored in the database and their values, |
|
10
|
|
|
* plus the '_token' and 'redirect_after_save' variables. |
|
11
|
|
|
* |
|
12
|
|
|
* @param array $requestInput The request input. |
|
13
|
|
|
* @param string $form The CRUD form. Can be 'create' or 'update' . Default is 'create'. |
|
14
|
|
|
* @param int|bool $id The CRUD entry id in the case of the 'update' form. |
|
15
|
|
|
* |
|
16
|
|
|
* @see \Illuminate\Http\Request::all() For an example on how to get the request input. |
|
17
|
|
|
* |
|
18
|
|
|
* @return array The updated request input. |
|
19
|
|
|
*/ |
|
20
|
11 |
|
public function compactFakeFields($requestInput, $form = 'create', $id = false) |
|
21
|
|
|
{ |
|
22
|
|
|
// get the right fields according to the form type (create/update) |
|
23
|
11 |
|
$fields = $this->getFields($form, $id); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
9 |
|
$compactedFakeFields = []; |
|
26
|
9 |
|
foreach ($fields as $field) { |
|
27
|
|
|
// compact fake fields |
|
28
|
7 |
|
if (isset($field['fake']) && $field['fake'] == true && array_key_exists($field['name'], $requestInput)) { |
|
29
|
2 |
|
$fakeFieldKey = isset($field['store_in']) ? $field['store_in'] : 'extras'; |
|
30
|
2 |
|
$this->addCompactedField($requestInput, $field['name'], $fakeFieldKey); |
|
31
|
|
|
|
|
32
|
2 |
|
if (! in_array($fakeFieldKey, $compactedFakeFields)) { |
|
33
|
7 |
|
$compactedFakeFields[] = $fakeFieldKey; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// json_encode all fake_value columns if applicable in the database, so they can be properly stored and interpreted |
|
|
|
|
|
|
39
|
9 |
|
foreach ($compactedFakeFields as $value) { |
|
40
|
2 |
|
if (! (property_exists($this->model, 'translatable') && in_array($value, $this->model->getTranslatableAttributes(), true)) && $this->model->shouldEncodeFake($value)) { |
|
|
|
|
|
|
41
|
2 |
|
$requestInput[$value] = json_encode($requestInput[$value]); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// if there are no fake fields defined, return the original request input |
|
46
|
9 |
|
return $requestInput; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Compact a fake field in the request input array. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $requestInput The request input. |
|
53
|
|
|
* @param string $fakeFieldName The fake field name. |
|
54
|
|
|
* @param string $fakeFieldKey The fake field key. |
|
55
|
|
|
*/ |
|
56
|
2 |
|
private function addCompactedField(&$requestInput, $fakeFieldName, $fakeFieldKey) |
|
57
|
|
|
{ |
|
58
|
2 |
|
$fakeField = $requestInput[$fakeFieldName]; |
|
59
|
2 |
|
array_pull($requestInput, $fakeFieldName); // remove the fake field from the request |
|
60
|
|
|
|
|
61
|
2 |
|
$requestInput[$fakeFieldKey][$fakeFieldName] = $fakeField; |
|
62
|
2 |
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
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.