1
|
|
|
<?php namespace Anomaly\PreferencesModule\Preference\Form; |
2
|
|
|
|
3
|
|
|
use Anomaly\PreferencesModule\Preference\Contract\PreferenceRepositoryInterface; |
4
|
|
|
use Illuminate\Config\Repository; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class PreferenceFormFields |
9
|
|
|
* |
10
|
|
|
* @link http://pyrocms.com/ |
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
12
|
|
|
* @author Ryan Thompson <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class PreferenceFormFields |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The config repository. |
19
|
|
|
* |
20
|
|
|
* @var Repository |
21
|
|
|
*/ |
22
|
|
|
protected $config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new PreferenceFormFields instance. |
26
|
|
|
* |
27
|
|
|
* @param Repository $config |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Repository $config) |
30
|
|
|
{ |
31
|
|
|
$this->config = $config; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return the form fields. |
36
|
|
|
* |
37
|
|
|
* @param PreferenceFormBuilder $builder |
38
|
|
|
*/ |
39
|
|
|
public function handle(PreferenceFormBuilder $builder, PreferenceRepositoryInterface $preferences) |
40
|
|
|
{ |
41
|
|
|
$namespace = $builder->getEntry() . '::'; |
42
|
|
|
|
43
|
|
|
/* |
44
|
|
|
* Get the fields from the config system. Sections are |
45
|
|
|
* optionally defined the same way. |
46
|
|
|
*/ |
47
|
|
|
if (!$fields = $this->config->get($namespace . 'preferences/preferences')) { |
48
|
|
|
$fields = $fields = $this->config->get($namespace . 'preferences', []); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($sections = $this->config->get($namespace . 'preferences/sections')) { |
52
|
|
|
$builder->setSections($sections); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/* |
56
|
|
|
* Finish each field. |
57
|
|
|
*/ |
58
|
|
|
foreach ($fields as $slug => &$field) { |
59
|
|
|
|
60
|
|
|
/* |
61
|
|
|
* Force an array. This is done later |
62
|
|
|
* too in normalization but we need it now |
63
|
|
|
* because we are normalizing / guessing our |
64
|
|
|
* own parameters somewhat. |
65
|
|
|
*/ |
66
|
|
|
if (is_string($field)) { |
67
|
|
|
$field = [ |
68
|
|
|
'type' => $field, |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Make sure we have a config property. |
73
|
|
|
$field['config'] = array_get($field, 'config', []); |
74
|
|
|
|
75
|
|
|
// Default the label. |
76
|
|
|
$field['label'] = array_get( |
77
|
|
|
$field, |
78
|
|
|
'label', |
79
|
|
|
$namespace . 'preference.' . $slug . '.label' |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
// Default the placeholder. |
83
|
|
|
$field['config']['placeholder'] = array_get( |
84
|
|
|
$field['config'], |
85
|
|
|
'placeholder', |
86
|
|
|
$namespace . 'preference.' . $slug . '.placeholder' |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
// Default the instructions. |
90
|
|
|
$field['instructions'] = array_get( |
91
|
|
|
$field, |
92
|
|
|
'instructions', |
93
|
|
|
$namespace . 'preference.' . $slug . '.instructions' |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
// Get the value defaulting to the default value. |
97
|
|
|
|
98
|
|
|
if ($preference = $preferences->get($namespace . $slug)) { |
99
|
|
|
$field['value'] = $preference->getValue(); |
100
|
|
|
} else { |
101
|
|
|
$field['value'] = array_get($field['config'], 'default_value'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/* |
105
|
|
|
* Disable the field if it |
106
|
|
|
* has a set env value. |
107
|
|
|
*/ |
108
|
|
|
if (isset($field['env']) && isset($field['bind']) && env($field['env']) !== null) { |
109
|
|
|
$field['disabled'] = true; |
110
|
|
|
$field['warning'] = 'module::message.env_locked'; |
111
|
|
|
$field['value'] = $this->config->get($field['bind']); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$builder->setFields($fields); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|