|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\HTMLEditor; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Config; |
|
6
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
7
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A PHP version of TinyMCE's configuration, to allow various parameters to be configured on a site or section basis |
|
11
|
|
|
* |
|
12
|
|
|
* There can be multiple HTMLEditorConfig's, which should always be created / accessed using HTMLEditorConfig::get. |
|
13
|
|
|
* You can then set the currently active config using set_active. |
|
14
|
|
|
* The order of precendence for which config is used is (lowest to highest): |
|
15
|
|
|
* |
|
16
|
|
|
* - default_config config setting |
|
17
|
|
|
* - Active config assigned |
|
18
|
|
|
* - Config name assigned to HTMLEditorField |
|
19
|
|
|
* - Config instance assigned to HTMLEditorField |
|
20
|
|
|
* |
|
21
|
|
|
* Typically global config changes should set the active config. |
|
22
|
|
|
* |
|
23
|
|
|
* The default config class can be changed via dependency injection to replace HTMLEditorConfig. |
|
24
|
|
|
* |
|
25
|
|
|
* @author "Hamish Friedlander" <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class HTMLEditorConfig |
|
28
|
|
|
{ |
|
29
|
|
|
use Configurable; |
|
30
|
|
|
use Injectable; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Array of registered configurations |
|
34
|
|
|
* |
|
35
|
|
|
* @var HTMLEditorConfig[] |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $configs = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Identifier key of current config. This will match an array key in $configs. |
|
41
|
|
|
* If left blank, will fall back to value of default_config set via config. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
protected static $current = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Name of default config. This will be ignored if $current is assigned a value. |
|
49
|
|
|
* |
|
50
|
|
|
* @config |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private static $default_config = 'default'; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* List of themes defined for the frontend |
|
57
|
|
|
* |
|
58
|
|
|
* @config |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
private static $user_themes = []; |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* List of the current themes set for this config |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected static $current_themes = null; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the HTMLEditorConfig object for the given identifier. This is a correct way to get an HTMLEditorConfig |
|
72
|
|
|
* instance - do not call 'new' |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $identifier The identifier for the config set. If omitted, the active config is returned. |
|
75
|
|
|
* @return static The configuration object. |
|
76
|
|
|
* This will be created if it does not yet exist for that identifier |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function get($identifier = null) |
|
79
|
|
|
{ |
|
80
|
|
|
if (!$identifier) { |
|
81
|
|
|
return static::get_active(); |
|
82
|
|
|
} |
|
83
|
|
|
// Create new instance if unconfigured |
|
84
|
|
|
if (!isset(self::$configs[$identifier])) { |
|
85
|
|
|
self::$configs[$identifier] = static::create(); |
|
86
|
|
|
} |
|
87
|
|
|
return self::$configs[$identifier]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Assign a new config, or clear existing, for the given identifier |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $identifier A specific identifier |
|
94
|
|
|
* @param HTMLEditorConfig $config Config to set, or null to clear |
|
95
|
|
|
* @return HTMLEditorConfig The assigned config |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function set_config($identifier, HTMLEditorConfig $config = null) |
|
98
|
|
|
{ |
|
99
|
|
|
if ($config) { |
|
100
|
|
|
self::$configs[$identifier] = $config; |
|
101
|
|
|
} else { |
|
102
|
|
|
unset(self::$configs[$identifier]); |
|
103
|
|
|
} |
|
104
|
|
|
return $config; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Gets the current themes, if it is not set this will fallback to config |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function getThemes() |
|
112
|
|
|
{ |
|
113
|
|
|
if (isset(static::$current_themes)) { |
|
114
|
|
|
return static::$current_themes; |
|
115
|
|
|
} |
|
116
|
|
|
return Config::inst()->get(static::class, 'user_themes'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Sets the current theme |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $themes |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function setThemes($themes) |
|
125
|
|
|
{ |
|
126
|
|
|
static::$current_themes = $themes; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Set the currently active configuration object. Note that the existing active |
|
131
|
|
|
* config will not be renamed to the new identifier. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $identifier The identifier for the config set |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function set_active_identifier($identifier) |
|
136
|
|
|
{ |
|
137
|
|
|
self::$current = $identifier; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Get the currently active configuration identifier. Will fall back to default_config |
|
142
|
|
|
* if unassigned. |
|
143
|
|
|
* |
|
144
|
|
|
* @return string The active configuration identifier |
|
145
|
|
|
*/ |
|
146
|
|
|
public static function get_active_identifier() |
|
147
|
|
|
{ |
|
148
|
|
|
$identifier = self::$current ?: static::config()->get('default_config'); |
|
149
|
|
|
return $identifier; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get the currently active configuration object |
|
154
|
|
|
* |
|
155
|
|
|
* @return HTMLEditorConfig The active configuration object |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function get_active() |
|
158
|
|
|
{ |
|
159
|
|
|
$identifier = self::get_active_identifier(); |
|
160
|
|
|
return self::get($identifier); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Assigns the currently active config an explicit instance |
|
165
|
|
|
* |
|
166
|
|
|
* @param HTMLEditorConfig $config |
|
167
|
|
|
* @return HTMLEditorConfig The given config |
|
168
|
|
|
*/ |
|
169
|
|
|
public static function set_active(HTMLEditorConfig $config) |
|
170
|
|
|
{ |
|
171
|
|
|
$identifier = static::get_active_identifier(); |
|
172
|
|
|
return static::set_config($identifier, $config); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Get the available configurations as a map of friendly_name to |
|
177
|
|
|
* configuration name. |
|
178
|
|
|
* |
|
179
|
|
|
* @return array |
|
180
|
|
|
*/ |
|
181
|
|
|
public static function get_available_configs_map() |
|
182
|
|
|
{ |
|
183
|
|
|
$configs = array(); |
|
184
|
|
|
|
|
185
|
|
|
foreach (self::$configs as $identifier => $config) { |
|
186
|
|
|
$configs[$identifier] = $config->getOption('friendly_name'); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return $configs; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Get the current value of an option |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $key The key of the option to get |
|
196
|
|
|
* @return mixed The value of the specified option |
|
197
|
|
|
*/ |
|
198
|
|
|
abstract public function getOption($key); |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Set the value of one option |
|
202
|
|
|
* @param string $key The key of the option to set |
|
203
|
|
|
* @param mixed $value The value of the option to set |
|
204
|
|
|
* @return $this |
|
205
|
|
|
*/ |
|
206
|
|
|
abstract public function setOption($key, $value); |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Set multiple options. This does not merge recursively, but only at the top level. |
|
210
|
|
|
* |
|
211
|
|
|
* @param array $options The options to set, as keys and values of the array |
|
212
|
|
|
* @return $this |
|
213
|
|
|
*/ |
|
214
|
|
|
abstract public function setOptions($options); |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Associative array of data-attributes to apply to the underlying text-area |
|
218
|
|
|
* |
|
219
|
|
|
* @return array |
|
220
|
|
|
*/ |
|
221
|
|
|
abstract public function getAttributes(); |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Initialise the editor on the client side |
|
225
|
|
|
*/ |
|
226
|
|
|
abstract public function init(); |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Provide additional schema data for the field this object configures |
|
230
|
|
|
* |
|
231
|
|
|
* @return array |
|
232
|
|
|
*/ |
|
233
|
|
|
public function getConfigSchemaData() |
|
234
|
|
|
{ |
|
235
|
|
|
return [ |
|
236
|
|
|
'attributes' => $this->getAttributes(), |
|
237
|
|
|
'editorjs' => null, |
|
238
|
|
|
]; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|