1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Traits; |
4
|
|
|
|
5
|
|
|
trait FieldsPrivateMethods |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Move the most recently added field before or after the given target field. Default is before. |
9
|
|
|
* |
10
|
|
|
* @param array $fields The form fields. |
11
|
|
|
* @param string $targetFieldName The target field name. |
12
|
|
|
* @param bool $before If true, the field will be moved before the target field, otherwise it will be moved after it. |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
|
|
private function moveField($fields, $targetFieldName, $before = true) |
16
|
|
|
{ |
17
|
|
|
if (array_key_exists($targetFieldName, $fields)) { |
18
|
|
|
$targetFieldPosition = $before ? array_search($targetFieldName, array_keys($fields)) |
19
|
|
|
: array_search($targetFieldName, array_keys($fields)) + 1; |
20
|
|
|
|
21
|
|
|
if ($targetFieldPosition >= (count($fields) - 1)) { |
22
|
|
|
// target field name is same as element |
23
|
|
|
return $fields; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$element = array_pop($fields); |
27
|
|
|
$beginningArrayPart = array_slice($fields, 0, $targetFieldPosition, true); |
|
|
|
|
28
|
|
|
$endingArrayPart = array_slice($fields, $targetFieldPosition, null, true); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$fields = array_merge($beginningArrayPart, [$element['name'] => $element], $endingArrayPart); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $fields; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Apply the given order to the fields and return the new array. |
38
|
|
|
* |
39
|
|
|
* @param array $fields The fields array. |
40
|
|
|
* @param array $order The desired field order array. |
41
|
|
|
* @return array The ordered fields array. |
42
|
|
|
*/ |
43
|
|
|
private function applyOrderToFields($fields, $order) |
44
|
|
|
{ |
45
|
|
|
$orderedFields = []; |
46
|
|
|
foreach ($order as $fieldName) { |
47
|
|
|
if (array_key_exists($fieldName, $fields)) { |
48
|
|
|
$orderedFields[$fieldName] = $fields[$fieldName]; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (empty($orderedFields)) { |
53
|
|
|
return $fields; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$remaining = array_diff_key($fields, $orderedFields); |
57
|
|
|
|
58
|
|
|
return array_merge($orderedFields, $remaining); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Apply the given callback to the form fields. |
63
|
|
|
* |
64
|
|
|
* @param callable $callback The callback function to run for the given form fields. |
65
|
|
|
*/ |
66
|
|
|
private function transformFields(callable $callback) |
67
|
|
|
{ |
68
|
|
|
$this->setOperationSetting('fields', $callback($this->getCleanStateFields())); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the fields as they are stored inside operation setting, not running the |
73
|
|
|
* presentation callbacks like converting the `dot.names` into `dot[names]` for html for example. |
74
|
|
|
*/ |
75
|
|
|
private function getCleanStateFields() |
76
|
|
|
{ |
77
|
|
|
return $this->getOperationSetting('fields') ?? []; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|