Projet

Général

Profil

Paste
Télécharger (8,98 ko) Statistiques
| Branche: | Révision:

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

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. Captures configuration handling, form
16
 * handling and distinguishes between in-memory configuration and persistent
17
 * configuration.
18
 *
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
 */
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
   * Instantiates a FeedsConfigurable object.
52
   *
53
   * Don't use directly, use feeds_importer() or feeds_plugin() instead.
54
   *
55
   * @see feeds_importer()
56
   * @see feeds_plugin()
57
   */
58
  public static function instance($class, $id) {
59
    if (!strlen($id)) {
60
      throw new InvalidArgumentException(t('Empty configuration identifier.'));
61
    }
62

    
63
    $instances = &drupal_static(__METHOD__, array());
64

    
65
    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
  /**
93
   * Determine whether this object is persistent and enabled. I. e. it is
94
   * defined either in code or in the database and it is enabled.
95
   */
96
  public function existing() {
97
    if ($this->export_type == FEEDS_EXPORT_NONE) {
98
      throw new FeedsNotExistingException(t('Object is not persistent.'));
99
    }
100
    if ($this->disabled) {
101
      throw new FeedsNotExistingException(t('Object is disabled.'));
102
    }
103
    return $this;
104
  }
105

    
106
  /**
107
   * Save a configuration. Concrete extending classes must implement a save
108
   * operation.
109
   */
110
  public abstract function save();
111

    
112
  /**
113
   * Copy a configuration.
114
   */
115
  public function copy(FeedsConfigurable $configurable) {
116
    $this->setConfig($configurable->config);
117
  }
118

    
119
  /**
120
   * Set configuration.
121
   *
122
   * @param $config
123
   *   Array containing configuration information. Config array will be filtered
124
   *   by the keys returned by configDefaults() and populated with default
125
   *   values that are not included in $config.
126
   */
127
  public function setConfig($config) {
128
    $defaults = $this->configDefaults();
129
    $this->config = array_intersect_key($config, $defaults) + $defaults;
130
  }
131

    
132
  /**
133
   * Similar to setConfig but adds to existing configuration.
134
   *
135
   * @param $config
136
   *   Array containing configuration information. Will be filtered by the keys
137
   *   returned by configDefaults().
138
   */
139
  public function addConfig($config) {
140
    $this->config = is_array($this->config) ? array_merge($this->config, $config) : $config;
141
    $default_keys = $this->configDefaults();
142
    $this->config = array_intersect_key($this->config, $default_keys);
143
  }
144

    
145
  /**
146
   * Overrides magic method __get().
147
   *
148
   * - Makes sure that external reads of FeedsConfigurable::config go through
149
   *   ::getConfig();
150
   * - Makes private and protected properties from this class and protected
151
   *   properties from child classes publicly readable.
152
   * - Prevents warnings when accessing non-existent properties.
153
   */
154
  public function __get($name) {
155
    if ($name == 'config') {
156
      return $this->getConfig();
157
    }
158
    return isset($this->$name) ? $this->$name : NULL;
159
  }
160

    
161
  /**
162
   * Implements getConfig().
163
   *
164
   * Return configuration array, ensure that all default values are present.
165
   */
166
  public function getConfig() {
167
    $defaults = $this->configDefaults();
168
    return $this->config + $defaults;
169
  }
170

    
171
  /**
172
   * Return default configuration.
173
   *
174
   * @todo rename to getConfigDefaults().
175
   *
176
   * @return
177
   *   Array where keys are the variable names of the configuration elements and
178
   *   values are their default values.
179
   */
180
  public function configDefaults() {
181
    return array();
182
  }
183

    
184
  /**
185
   * Returns whether or not the configurable has a config form.
186
   *
187
   * @return bool
188
   *   True if the configurable has a config form, and false if not.
189
   */
190
  public function hasConfigForm() {
191
    $form_state = array();
192
    return (bool) $this->configForm($form_state);
193
  }
194

    
195
  /**
196
   * Return configuration form for this object. The keys of the configuration
197
   * form must match the keys of the array returned by configDefaults().
198
   *
199
   * @return
200
   *   FormAPI style form definition.
201
   */
202
  public function configForm(&$form_state) {
203
    return array();
204
  }
205

    
206
  /**
207
   * Validation handler for configForm().
208
   *
209
   * Set errors with form_set_error().
210
   *
211
   * @param $values
212
   *   An array that contains the values entered by the user through configForm.
213
   */
214
  public function configFormValidate(&$values) {
215
  }
216

    
217
  /**
218
   *  Submission handler for configForm().
219
   *
220
   *  @param $values
221
   */
222
  public function configFormSubmit(&$values) {
223
    $this->addConfig($values);
224
    $this->save();
225
    drupal_set_message(t('Your changes have been saved.'));
226
    feeds_cache_clear(FALSE);
227
  }
228

    
229
  /**
230
   * Returns an array of required modules.
231
   *
232
   * @return array
233
   *   The modules that this configurable requires.
234
   */
235
  public function dependencies() {
236
    return array();
237
  }
238
}
239

    
240
/**
241
 * Config form wrapper. Use to render the configuration form of
242
 * a FeedsConfigurable object.
243
 *
244
 * @param $configurable
245
 *   FeedsConfigurable object.
246
 * @param $form_method
247
 *   The form method that should be rendered.
248
 *
249
 * @return
250
 *   Config form array if available. NULL otherwise.
251
 */
252
function feeds_get_form($configurable, $form_method) {
253
  if (method_exists($configurable, $form_method)) {
254
    return drupal_get_form(get_class($configurable) . '_feeds_form', $configurable, $form_method);
255
  }
256
}
257

    
258
/**
259
 * Config form callback. Don't call directly, but use
260
 * feeds_get_form($configurable, 'method') instead.
261
 *
262
 * @param
263
 *   FormAPI $form_state.
264
 * @param
265
 *   FeedsConfigurable object.
266
 * @param
267
 *   The object to perform the save() operation on.
268
 * @param $form_method
269
 *   The $form_method that should be rendered.
270
 */
271
function feeds_form($form, &$form_state, $configurable, $form_method) {
272
  $form = $configurable->$form_method($form_state);
273
  $form['#configurable'] = $configurable;
274
  $form['#feeds_form_method'] = $form_method;
275
  $form['#validate'] = array('feeds_form_validate');
276
  $form['#submit'] = array('feeds_form_submit');
277
  $form['submit'] = array(
278
    '#type' => 'submit',
279
    '#value' => t('Save'),
280
    '#weight' => 100,
281
  );
282
  return $form;
283
}
284

    
285
/**
286
 * Validation handler for feeds_form().
287
 */
288
function feeds_form_validate($form, &$form_state) {
289
  _feeds_form_helper($form, $form_state, 'Validate');
290
}
291

    
292
/**
293
 * Submit handler for feeds_form().
294
 */
295
function feeds_form_submit($form, &$form_state) {
296
  _feeds_form_helper($form, $form_state, 'Submit');
297
}
298

    
299
/**
300
 * Helper for Feeds validate and submit callbacks.
301
 *
302
 * @todo This is all terrible. Remove.
303
 */
304
function _feeds_form_helper($form, &$form_state, $action) {
305
  $method = $form['#feeds_form_method'] . $action;
306
  $class = get_class($form['#configurable']);
307
  $id = $form['#configurable']->id;
308

    
309
  // Re-initialize the configurable object. Using feeds_importer() and
310
  // feeds_plugin() will ensure that we're using the same instance. We can't
311
  // reuse the previous form instance because feeds_importer() is used to save.
312
  // This will re-initialize all of the plugins anyway, causing some tricky
313
  // saving issues in certain cases.
314
  // See http://drupal.org/node/1672880.
315
  if ($class == variable_get('feeds_importer_class', 'FeedsImporter')) {
316
    $form['#configurable'] = feeds_importer($id);
317
  }
318
  else {
319
    $importer = feeds_importer($id);
320
    $plugin_key = $importer->config[$form['#configurable']->pluginType()]['plugin_key'];
321
    $form['#configurable'] = feeds_plugin($plugin_key, $id);
322
  }
323

    
324
  if (method_exists($form['#configurable'], $method)) {
325
    $form['#configurable']->$method($form_state['values']);
326
  }
327
}