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