Projet

Général

Profil

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

root / htmltest / sites / all / modules / feeds / includes / FeedsConfigurable.inc @ a5572547

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
   * Instantiate a FeedsConfigurable object.
48
   *
49
   * Don't use directly, use feeds_importer() or feeds_plugin()
50
   * instead.
51
   */
52
  public static function instance($class, $id) {
53
    // This is useful at least as long as we're developing.
54
    if (empty($id)) {
55
      throw new Exception(t('Empty configuration identifier.'));
56
    }
57
    static $instances = array();
58
    if (!isset($instances[$class][$id])) {
59
      $instances[$class][$id] = new $class($id);
60
    }
61
    return $instances[$class][$id];
62
  }
63

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

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

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

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

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

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

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

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

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

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

    
172
  /**
173
   * Return configuration form for this object. The keys of the configuration
174
   * form must match the keys of the array returned by configDefaults().
175
   *
176
   * @return
177
   *   FormAPI style form definition.
178
   */
179
  public function configForm(&$form_state) {
180
    return array();
181
  }
182

    
183
  /**
184
   * Validation handler for configForm().
185
   *
186
   * Set errors with form_set_error().
187
   *
188
   * @param $values
189
   *   An array that contains the values entered by the user through configForm.
190
   */
191
  public function configFormValidate(&$values) {
192
  }
193

    
194
  /**
195
   *  Submission handler for configForm().
196
   *
197
   *  @param $values
198
   */
199
  public function configFormSubmit(&$values) {
200
    $this->addConfig($values);
201
    $this->save();
202
    drupal_set_message(t('Your changes have been saved.'));
203
    feeds_cache_clear(FALSE);
204
  }
205
}
206

    
207
/**
208
 * Config form wrapper. Use to render the configuration form of
209
 * a FeedsConfigurable object.
210
 *
211
 * @param $configurable
212
 *   FeedsConfigurable object.
213
 * @param $form_method
214
 *   The form method that should be rendered.
215
 *
216
 * @return
217
 *   Config form array if available. NULL otherwise.
218
 */
219
function feeds_get_form($configurable, $form_method) {
220
  if (method_exists($configurable, $form_method)) {
221
    return drupal_get_form(get_class($configurable) . '_feeds_form', $configurable, $form_method);
222
  }
223
}
224

    
225
/**
226
 * Config form callback. Don't call directly, but use
227
 * feeds_get_form($configurable, 'method') instead.
228
 *
229
 * @param
230
 *   FormAPI $form_state.
231
 * @param
232
 *   FeedsConfigurable object.
233
 * @param
234
 *   The object to perform the save() operation on.
235
 * @param $form_method
236
 *   The $form_method that should be rendered.
237
 */
238
function feeds_form($form, &$form_state, $configurable, $form_method) {
239
  $form = $configurable->$form_method($form_state);
240
  $form['#configurable'] = $configurable;
241
  $form['#feeds_form_method'] = $form_method;
242
  $form['#validate'] = array('feeds_form_validate');
243
  $form['#submit'] = array('feeds_form_submit');
244
  $form['submit'] = array(
245
    '#type' => 'submit',
246
    '#value' => t('Save'),
247
    '#weight' => 100,
248
  );
249
  return $form;
250
}
251

    
252
/**
253
 * Validation handler for feeds_form().
254
 */
255
function feeds_form_validate($form, &$form_state) {
256
  _feeds_form_helper($form, $form_state, 'Validate');
257
}
258

    
259
/**
260
 * Submit handler for feeds_form().
261
 */
262
function feeds_form_submit($form, &$form_state) {
263
  _feeds_form_helper($form, $form_state, 'Submit');
264
}
265

    
266
/**
267
 * Helper for Feeds validate and submit callbacks.
268
 *
269
 * @todo This is all terrible. Remove.
270
 */
271
function _feeds_form_helper($form, &$form_state, $action) {
272
  $method = $form['#feeds_form_method'] . $action;
273
  $class = get_class($form['#configurable']);
274
  $id = $form['#configurable']->id;
275

    
276
  // Re-initialize the configurable object. Using feeds_importer() and
277
  // feeds_plugin() will ensure that we're using the same instance. We can't
278
  // reuse the previous form instance because feeds_importer() is used to save.
279
  // This will re-initialize all of the plugins anyway, causing some tricky
280
  // saving issues in certain cases.
281
  // See http://drupal.org/node/1672880.
282
  if ($class == variable_get('feeds_importer_class', 'FeedsImporter')) {
283
    $form['#configurable'] = feeds_importer($id);
284
  }
285
  else {
286
    $importer = feeds_importer($id);
287
    $plugin_key = $importer->config[$form['#configurable']->pluginType()]['plugin_key'];
288
    $form['#configurable'] = feeds_plugin($plugin_key, $id);
289
  }
290

    
291
  if (method_exists($form['#configurable'], $method)) {
292
    $form['#configurable']->$method($form_state['values']);
293
  }
294
}