Projet

Général

Profil

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

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

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
abstract class FeedsConfigurable {
20

    
21
  // Holds the actual configuration information.
22
  protected $config;
23

    
24
  // A unique identifier for the configuration.
25
  protected $id;
26

    
27
  /*
28
  CTools export type of this object.
29

    
30
  @todo Should live in FeedsImporter. Not all child classes
31
  of FeedsConfigurable are exportable. Same goes for $disabled.
32

    
33
  Export type can be one of
34
  FEEDS_EXPORT_NONE - the configurable only exists in memory
35
  EXPORT_IN_DATABASE - the configurable is defined in the database.
36
  EXPORT_IN_CODE - the configurable is defined in code.
37
  EXPORT_IN_CODE | EXPORT_IN_DATABASE - the configurable is defined in code, but
38
                                        overridden in the database.*/
39
  protected $export_type;
40

    
41
  /**
42
   * CTools export enabled status of this object.
43
   */
44
  protected $disabled;
45

    
46
  /**
47
   * Instantiates a FeedsConfigurable object.
48
   *
49
   * Don't use directly, use feeds_importer() or feeds_plugin() instead.
50
   *
51
   * @see feeds_importer()
52
   * @see feeds_plugin()
53
   */
54
  public static function instance($class, $id) {
55
    if (!strlen($id)) {
56
      throw new InvalidArgumentException(t('Empty configuration identifier.'));
57
    }
58
    static $instances = array();
59
    if (!isset($instances[$class][$id])) {
60
      $instances[$class][$id] = new $class($id);
61
    }
62
    return $instances[$class][$id];
63
  }
64

    
65
  /**
66
   * Constructor, set id and load default configuration.
67
   */
68
  protected function __construct($id) {
69
    // Set this object's id.
70
    $this->id = $id;
71
    // Per default we assume that a Feeds object is not saved to
72
    // database nor is it exported to code.
73
    $this->export_type = FEEDS_EXPORT_NONE;
74
    // Make sure configuration is populated.
75
    $this->config = $this->configDefaults();
76
    $this->disabled = FALSE;
77
  }
78

    
79
  /**
80
   * Override magic method __isset(). This is needed due to overriding __get().
81
   */
82
  public function __isset($name) {
83
    return isset($this->$name) ? TRUE : FALSE;
84
  }
85

    
86
  /**
87
   * Determine whether this object is persistent and enabled. I. e. it is
88
   * defined either in code or in the database and it is enabled.
89
   */
90
  public function existing() {
91
    if ($this->export_type == FEEDS_EXPORT_NONE) {
92
      throw new FeedsNotExistingException(t('Object is not persistent.'));
93
    }
94
    if ($this->disabled) {
95
      throw new FeedsNotExistingException(t('Object is disabled.'));
96
    }
97
    return $this;
98
  }
99

    
100
  /**
101
   * Save a configuration. Concrete extending classes must implement a save
102
   * operation.
103
   */
104
  public abstract function save();
105

    
106
  /**
107
   * Copy a configuration.
108
   */
109
  public function copy(FeedsConfigurable $configurable) {
110
    $this->setConfig($configurable->config);
111
  }
112

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

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

    
139
  /**
140
   * Override magic method __get(). Make sure that $this->config goes through
141
   * getConfig().
142
   */
143
  public function __get($name) {
144
    if ($name == 'config') {
145
      return $this->getConfig();
146
    }
147
    return isset($this->$name) ? $this->$name : NULL;
148
  }
149

    
150
  /**
151
   * Implements getConfig().
152
   *
153
   * Return configuration array, ensure that all default values are present.
154
   */
155
  public function getConfig() {
156
    $defaults = $this->configDefaults();
157
    return $this->config + $defaults;
158
  }
159

    
160
  /**
161
   * Return default configuration.
162
   *
163
   * @todo rename to getConfigDefaults().
164
   *
165
   * @return
166
   *   Array where keys are the variable names of the configuration elements and
167
   *   values are their default values.
168
   */
169
  public function configDefaults() {
170
    return array();
171
  }
172

    
173
  /**
174
   * Returns whether or not the configurable has a config form.
175
   *
176
   * @return bool
177
   *   True if the configurable has a config form, and false if not.
178
   */
179
  public function hasConfigForm() {
180
    $form_state = array();
181
    return (bool) $this->configForm($form_state);
182
  }
183

    
184
  /**
185
   * Return configuration form for this object. The keys of the configuration
186
   * form must match the keys of the array returned by configDefaults().
187
   *
188
   * @return
189
   *   FormAPI style form definition.
190
   */
191
  public function configForm(&$form_state) {
192
    return array();
193
  }
194

    
195
  /**
196
   * Validation handler for configForm().
197
   *
198
   * Set errors with form_set_error().
199
   *
200
   * @param $values
201
   *   An array that contains the values entered by the user through configForm.
202
   */
203
  public function configFormValidate(&$values) {
204
  }
205

    
206
  /**
207
   *  Submission handler for configForm().
208
   *
209
   *  @param $values
210
   */
211
  public function configFormSubmit(&$values) {
212
    $this->addConfig($values);
213
    $this->save();
214
    drupal_set_message(t('Your changes have been saved.'));
215
    feeds_cache_clear(FALSE);
216
  }
217

    
218
  /**
219
   * Returns an array of required modules.
220
   *
221
   * @return array
222
   *   The modules that this configurable requires.
223
   */
224
  public function dependencies() {
225
    return array();
226
  }
227
}
228

    
229
/**
230
 * Config form wrapper. Use to render the configuration form of
231
 * a FeedsConfigurable object.
232
 *
233
 * @param $configurable
234
 *   FeedsConfigurable object.
235
 * @param $form_method
236
 *   The form method that should be rendered.
237
 *
238
 * @return
239
 *   Config form array if available. NULL otherwise.
240
 */
241
function feeds_get_form($configurable, $form_method) {
242
  if (method_exists($configurable, $form_method)) {
243
    return drupal_get_form(get_class($configurable) . '_feeds_form', $configurable, $form_method);
244
  }
245
}
246

    
247
/**
248
 * Config form callback. Don't call directly, but use
249
 * feeds_get_form($configurable, 'method') instead.
250
 *
251
 * @param
252
 *   FormAPI $form_state.
253
 * @param
254
 *   FeedsConfigurable object.
255
 * @param
256
 *   The object to perform the save() operation on.
257
 * @param $form_method
258
 *   The $form_method that should be rendered.
259
 */
260
function feeds_form($form, &$form_state, $configurable, $form_method) {
261
  $form = $configurable->$form_method($form_state);
262
  $form['#configurable'] = $configurable;
263
  $form['#feeds_form_method'] = $form_method;
264
  $form['#validate'] = array('feeds_form_validate');
265
  $form['#submit'] = array('feeds_form_submit');
266
  $form['submit'] = array(
267
    '#type' => 'submit',
268
    '#value' => t('Save'),
269
    '#weight' => 100,
270
  );
271
  return $form;
272
}
273

    
274
/**
275
 * Validation handler for feeds_form().
276
 */
277
function feeds_form_validate($form, &$form_state) {
278
  _feeds_form_helper($form, $form_state, 'Validate');
279
}
280

    
281
/**
282
 * Submit handler for feeds_form().
283
 */
284
function feeds_form_submit($form, &$form_state) {
285
  _feeds_form_helper($form, $form_state, 'Submit');
286
}
287

    
288
/**
289
 * Helper for Feeds validate and submit callbacks.
290
 *
291
 * @todo This is all terrible. Remove.
292
 */
293
function _feeds_form_helper($form, &$form_state, $action) {
294
  $method = $form['#feeds_form_method'] . $action;
295
  $class = get_class($form['#configurable']);
296
  $id = $form['#configurable']->id;
297

    
298
  // Re-initialize the configurable object. Using feeds_importer() and
299
  // feeds_plugin() will ensure that we're using the same instance. We can't
300
  // reuse the previous form instance because feeds_importer() is used to save.
301
  // This will re-initialize all of the plugins anyway, causing some tricky
302
  // saving issues in certain cases.
303
  // See http://drupal.org/node/1672880.
304
  if ($class == variable_get('feeds_importer_class', 'FeedsImporter')) {
305
    $form['#configurable'] = feeds_importer($id);
306
  }
307
  else {
308
    $importer = feeds_importer($id);
309
    $plugin_key = $importer->config[$form['#configurable']->pluginType()]['plugin_key'];
310
    $form['#configurable'] = feeds_plugin($plugin_key, $id);
311
  }
312

    
313
  if (method_exists($form['#configurable'], $method)) {
314
    $form['#configurable']->$method($form_state['values']);
315
  }
316
}