1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* FeedsConfigurable and helper functions.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Used when an object does not exist in the DB or code but should.
|
10
|
*/
|
11
|
class FeedsNotExistingException extends Exception {
|
12
|
}
|
13
|
|
14
|
/**
|
15
|
* Base class for configurable classes.
|
16
|
*
|
17
|
* Captures configuration handling, form handling and distinguishes between
|
18
|
* in-memory configuration and persistent configuration.
|
19
|
*
|
20
|
* Due to the magic method __get(), protected properties from this class and
|
21
|
* from classes that extend this class will be publicly readable (but not
|
22
|
* writeable).
|
23
|
*/
|
24
|
abstract class FeedsConfigurable {
|
25
|
|
26
|
/**
|
27
|
* Holds the actual configuration information.
|
28
|
*
|
29
|
* @var array
|
30
|
*/
|
31
|
protected $config;
|
32
|
|
33
|
/**
|
34
|
* An unique identifier for the configuration.
|
35
|
*
|
36
|
* @var string
|
37
|
*/
|
38
|
protected $id;
|
39
|
|
40
|
/**
|
41
|
* CTools export type of this object.
|
42
|
*
|
43
|
* Export type can be one of:
|
44
|
* - FEEDS_EXPORT_NONE - the configurable only exists in memory.
|
45
|
* - EXPORT_IN_DATABASE - the configurable is defined in the database.
|
46
|
* - EXPORT_IN_CODE - the configurable is defined in code.
|
47
|
* - EXPORT_IN_CODE | EXPORT_IN_DATABASE - the configurable is defined in
|
48
|
* code, but overridden in the database.
|
49
|
*
|
50
|
* @var int
|
51
|
*
|
52
|
* @todo Should live in FeedsImporter. Not all child classes
|
53
|
* of FeedsConfigurable are exportable. Same goes for $disabled.
|
54
|
*/
|
55
|
protected $export_type;
|
56
|
|
57
|
/**
|
58
|
* CTools export enabled status of this object.
|
59
|
*
|
60
|
* @var bool
|
61
|
*/
|
62
|
protected $disabled;
|
63
|
|
64
|
/**
|
65
|
* Provides a statically cached instance of a FeedsConfigurable subclass.
|
66
|
*
|
67
|
* Don't use directly, use feeds_importer() or feeds_plugin() instead.
|
68
|
*
|
69
|
* @param string $class
|
70
|
* The name of a subclass of FeedsConfigurable.
|
71
|
* @param string $id
|
72
|
* The ID of this configuration object.
|
73
|
*
|
74
|
* @return FeedsConfigurable
|
75
|
* An instance of this class.
|
76
|
*
|
77
|
* @throws InvalidArgumentException
|
78
|
* When an empty configuration identifier is passed.
|
79
|
*
|
80
|
* @see feeds_importer()
|
81
|
* @see feeds_plugin()
|
82
|
*/
|
83
|
public static function instance($class, $id) {
|
84
|
// @todo Verify that $class is an existing subclass of FeedsConfigurable.
|
85
|
if (!strlen($id)) {
|
86
|
throw new InvalidArgumentException(t('Empty configuration identifier.'));
|
87
|
}
|
88
|
|
89
|
$instances = &drupal_static(__METHOD__, array());
|
90
|
|
91
|
if (!isset($instances[$class][$id])) {
|
92
|
$instances[$class][$id] = new $class($id);
|
93
|
}
|
94
|
return $instances[$class][$id];
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Constructor, set id and load default configuration.
|
99
|
*
|
100
|
* @param string $id
|
101
|
* The ID of this configuration object.
|
102
|
*/
|
103
|
protected function __construct($id) {
|
104
|
// Set this object's id.
|
105
|
$this->id = $id;
|
106
|
// Per default we assume that a Feeds object is not saved to
|
107
|
// database nor is it exported to code.
|
108
|
$this->export_type = FEEDS_EXPORT_NONE;
|
109
|
// Make sure configuration is populated.
|
110
|
$this->config = $this->configDefaults();
|
111
|
$this->disabled = FALSE;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Override magic method __isset(). This is needed due to overriding __get().
|
116
|
*/
|
117
|
public function __isset($name) {
|
118
|
return isset($this->$name) ? TRUE : FALSE;
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Determine whether this object is persistent.
|
123
|
*
|
124
|
* @return bool
|
125
|
* True if the object is persistent.
|
126
|
* False otherwise.
|
127
|
*/
|
128
|
public function doesExist() {
|
129
|
return ($this->export_type == FEEDS_EXPORT_NONE) ? FALSE : TRUE;
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Determine whether this object is enabled.
|
134
|
*
|
135
|
* @return bool
|
136
|
* True if the object is enabled.
|
137
|
* False otherwise.
|
138
|
*/
|
139
|
public function isEnabled() {
|
140
|
return $this->disabled ? FALSE : TRUE;
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Determines whether this object is persistent and enabled.
|
145
|
*
|
146
|
* This means that it exists either in code or in the database and it is
|
147
|
* enabled.
|
148
|
*
|
149
|
* @return $this
|
150
|
*
|
151
|
* @throws FeedsNotExistingException
|
152
|
* When the object is not persistent or is not enabled.
|
153
|
*/
|
154
|
public function existing() {
|
155
|
if (!$this->doesExist()) {
|
156
|
throw new FeedsNotExistingException(t('Object is not persistent.'));
|
157
|
}
|
158
|
if (!$this->isEnabled()) {
|
159
|
throw new FeedsNotExistingException(t('Object is disabled.'));
|
160
|
}
|
161
|
return $this;
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Saves a configuration.
|
166
|
*
|
167
|
* Concrete extending classes must implement a save operation.
|
168
|
*/
|
169
|
abstract public function save();
|
170
|
|
171
|
/**
|
172
|
* Copy a configuration.
|
173
|
*
|
174
|
* @param FeedsConfigurable $configurable
|
175
|
* The FeedsConfigurable instance to take the configuration from.
|
176
|
*/
|
177
|
public function copy(FeedsConfigurable $configurable) {
|
178
|
$this->setConfig($configurable->config);
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Set configuration.
|
183
|
*
|
184
|
* @param array $config
|
185
|
* Array containing configuration information. Config array will be filtered
|
186
|
* by the keys returned by configDefaults() and populated with default
|
187
|
* values that are not included in $config.
|
188
|
*/
|
189
|
public function setConfig($config) {
|
190
|
$defaults = $this->configDefaults();
|
191
|
$this->config = array_intersect_key($config, $defaults) + $defaults;
|
192
|
}
|
193
|
|
194
|
/**
|
195
|
* Similar to setConfig but adds to existing configuration.
|
196
|
*
|
197
|
* @param array $config
|
198
|
* Array containing configuration information. Will be filtered by the keys
|
199
|
* returned by configDefaults().
|
200
|
*/
|
201
|
public function addConfig($config) {
|
202
|
$this->config = is_array($this->config) ? array_merge($this->config, $config) : $config;
|
203
|
$default_keys = $this->configDefaults();
|
204
|
$this->config = array_intersect_key($this->config, $default_keys);
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Overrides magic method __get().
|
209
|
*
|
210
|
* - Makes sure that external reads of FeedsConfigurable::config go through
|
211
|
* ::getConfig();
|
212
|
* - Makes private and protected properties from this class and protected
|
213
|
* properties from child classes publicly readable.
|
214
|
* - Prevents warnings when accessing non-existent properties.
|
215
|
*/
|
216
|
public function __get($name) {
|
217
|
if ($name == 'config') {
|
218
|
return $this->getConfig();
|
219
|
}
|
220
|
return isset($this->$name) ? $this->$name : NULL;
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* Implements getConfig().
|
225
|
*
|
226
|
* Returns configuration array, ensure that all default values are present.
|
227
|
*
|
228
|
* @return array
|
229
|
* The configuration for this object.
|
230
|
*/
|
231
|
public function getConfig() {
|
232
|
$defaults = $this->configDefaults();
|
233
|
return $this->config + $defaults;
|
234
|
}
|
235
|
|
236
|
/**
|
237
|
* Return default configuration.
|
238
|
*
|
239
|
* @todo rename to getConfigDefaults().
|
240
|
*
|
241
|
* @return array
|
242
|
* Array where keys are the variable names of the configuration elements and
|
243
|
* values are their default values.
|
244
|
*/
|
245
|
public function configDefaults() {
|
246
|
return array() + module_invoke_all('feeds_config_defaults', $this);
|
247
|
}
|
248
|
|
249
|
/**
|
250
|
* Validates the configuration.
|
251
|
*
|
252
|
* @return array
|
253
|
* A list of errors.
|
254
|
*/
|
255
|
public function validateConfig() {
|
256
|
return array();
|
257
|
}
|
258
|
|
259
|
/**
|
260
|
* Returns whether or not the configurable has a config form.
|
261
|
*
|
262
|
* @return bool
|
263
|
* True if the configurable has a config form, and false if not.
|
264
|
*/
|
265
|
public function hasConfigForm() {
|
266
|
$form_state = array();
|
267
|
return (bool) $this->configForm($form_state);
|
268
|
}
|
269
|
|
270
|
/**
|
271
|
* Returns configuration form for this object.
|
272
|
*
|
273
|
* The keys of the configuration form must match the keys of the array
|
274
|
* returned by configDefaults().
|
275
|
*
|
276
|
* @param array $form_state
|
277
|
* The current state of the form.
|
278
|
*
|
279
|
* @return array
|
280
|
* FormAPI style form definition.
|
281
|
*/
|
282
|
public function configForm(&$form_state) {
|
283
|
return array();
|
284
|
}
|
285
|
|
286
|
/**
|
287
|
* Validation handler for configForm().
|
288
|
*
|
289
|
* Set errors with form_set_error().
|
290
|
*
|
291
|
* @param array $values
|
292
|
* An array that contains the values entered by the user through configForm.
|
293
|
*/
|
294
|
public function configFormValidate(&$values) {
|
295
|
}
|
296
|
|
297
|
/**
|
298
|
* Submission handler for configForm().
|
299
|
*
|
300
|
* @param array $values
|
301
|
* An array that contains the values entered by the user through configForm.
|
302
|
*/
|
303
|
public function configFormSubmit(&$values) {
|
304
|
$this->addConfig($values);
|
305
|
$this->save();
|
306
|
drupal_set_message(t('Your changes have been saved.'));
|
307
|
feeds_cache_clear(FALSE);
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* Returns an array of required modules.
|
312
|
*
|
313
|
* @return array
|
314
|
* The modules that this configurable requires.
|
315
|
*/
|
316
|
public function dependencies() {
|
317
|
return array();
|
318
|
}
|
319
|
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
* FeedsConfigurable config form wrapper.
|
324
|
*
|
325
|
* Used to render the configuration form of a FeedsConfigurable object.
|
326
|
*
|
327
|
* @param FeedsConfigurable $configurable
|
328
|
* The FeedsConfigurable instance for which a configuration form must be
|
329
|
* rendered.
|
330
|
* @param string $form_method
|
331
|
* The form method that should be rendered.
|
332
|
*
|
333
|
* @return array|null
|
334
|
* Config form array if available. NULL otherwise.
|
335
|
*/
|
336
|
function feeds_get_form(FeedsConfigurable $configurable, $form_method) {
|
337
|
if (method_exists($configurable, $form_method)) {
|
338
|
return drupal_get_form(get_class($configurable) . '_feeds_form', $configurable, $form_method);
|
339
|
}
|
340
|
}
|
341
|
|
342
|
/**
|
343
|
* Form constructor for a Feeds configuration form.
|
344
|
*
|
345
|
* Don't call directly, but use
|
346
|
* feeds_get_form($configurable, 'method') instead.
|
347
|
*
|
348
|
* @param array $form
|
349
|
* The form.
|
350
|
* @param array $form_state
|
351
|
* The current state of the form.
|
352
|
* @param FeedsConfigurable $configurable
|
353
|
* The object to perform the save() operation on.
|
354
|
* @param string $form_method
|
355
|
* The $form_method that should be rendered.
|
356
|
*
|
357
|
* @return array
|
358
|
* Form array.
|
359
|
*/
|
360
|
function feeds_form(array $form, array &$form_state, FeedsConfigurable $configurable, $form_method) {
|
361
|
$form = $configurable->$form_method($form_state);
|
362
|
$form['#configurable'] = $configurable;
|
363
|
$form['#feeds_form_method'] = $form_method;
|
364
|
$form['#validate'] = array('feeds_form_validate');
|
365
|
$form['#submit'] = array('feeds_form_submit');
|
366
|
$form['submit'] = array(
|
367
|
'#type' => 'submit',
|
368
|
'#value' => t('Save'),
|
369
|
'#weight' => 100,
|
370
|
);
|
371
|
return $form;
|
372
|
}
|
373
|
|
374
|
/**
|
375
|
* Form validation handler for feeds_form().
|
376
|
*
|
377
|
* @see feeds_form()
|
378
|
*/
|
379
|
function feeds_form_validate($form, &$form_state) {
|
380
|
_feeds_form_helper($form, $form_state, 'Validate');
|
381
|
}
|
382
|
|
383
|
/**
|
384
|
* Submit handler for feeds_form().
|
385
|
*
|
386
|
* @see feeds_form()
|
387
|
*/
|
388
|
function feeds_form_submit($form, &$form_state) {
|
389
|
_feeds_form_helper($form, $form_state, 'Submit');
|
390
|
}
|
391
|
|
392
|
/**
|
393
|
* Helper for Feeds validate and submit callbacks.
|
394
|
*
|
395
|
* @param array $form
|
396
|
* The form.
|
397
|
* @param array $form_state
|
398
|
* The current state of the form.
|
399
|
* @param string $action
|
400
|
* The action to perform on the form, for example:
|
401
|
* - Validate;
|
402
|
* - Submit.
|
403
|
*
|
404
|
* @todo This is all terrible. Remove.
|
405
|
*/
|
406
|
function _feeds_form_helper(array $form, array &$form_state, $action) {
|
407
|
$method = $form['#feeds_form_method'] . $action;
|
408
|
$class = get_class($form['#configurable']);
|
409
|
$id = $form['#configurable']->id;
|
410
|
|
411
|
// Re-initialize the configurable object. Using feeds_importer() and
|
412
|
// feeds_plugin() will ensure that we're using the same instance. We can't
|
413
|
// reuse the previous form instance because feeds_importer() is used to save.
|
414
|
// This will re-initialize all of the plugins anyway, causing some tricky
|
415
|
// saving issues in certain cases.
|
416
|
// See http://drupal.org/node/1672880.
|
417
|
if ($class == variable_get('feeds_importer_class', 'FeedsImporter')) {
|
418
|
$form['#configurable'] = feeds_importer($id);
|
419
|
}
|
420
|
else {
|
421
|
$importer = feeds_importer($id);
|
422
|
$plugin_key = $importer->config[$form['#configurable']->pluginType()]['plugin_key'];
|
423
|
$form['#configurable'] = feeds_plugin($plugin_key, $id);
|
424
|
}
|
425
|
|
426
|
if (method_exists($form['#configurable'], $method)) {
|
427
|
$form['#configurable']->$method($form_state['values']);
|
428
|
}
|
429
|
}
|