Projet

Général

Profil

Paste
Télécharger (9,65 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / includes / FeedsConfigurable.inc @ ec2b0e7b

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