Projet

Général

Profil

Paste
Télécharger (11 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / includes / rules.event.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains event handler interface and base classes.
6
 */
7

    
8
/**
9
 * Interface for handling rules events.
10
 *
11
 * Configurable events (i.e. events making use of settings) have a custom
12
 * event suffix, which gets appended to the base event name. The configured
13
 * event name of, e.g. the event for viewing an article node, would be
14
 * node_view--article, whereas "node_view" is the base event name and "article"
15
 * the event suffix as returned from
16
 * RulesEventHandlerInterface::getEventNameSuffix(). The event suffix is
17
 * generated based upon the event settings and must map to this settings, i.e.
18
 * each set of event settings must always generate the same suffix.
19
 * For a configurable event to be invoked, rules_invoke_event() has to be called
20
 * with the configured event name, e.g.
21
 * @code
22
 * rules_invoke_event('node_view--' . $node->type, $node, $view_mode);
23
 * @endcode
24
 * If the event settings are optional, both events have to be invoked whereas
25
 * usually the more general event is invoked last. E.g.:
26
 * @code
27
 * rules_invoke_event('node_view--' . $node->type, $node, $view_mode);
28
 * rules_invoke_event('node_view', $node, $view_mode);
29
 * @endcode
30
 *
31
 * Rules event handlers have to be declared using the 'class' key in
32
 * hook_rules_event_info(), or may be discovered automatically, see
33
 * rules_discover_plugins() for details.
34
 *
35
 * @see RulesEventHandlerBase
36
 * @see RulesEventDefaultHandler
37
 */
38
interface RulesEventHandlerInterface {
39

    
40
  /**
41
   * Constructs the event handler.
42
   *
43
   * @param string $event_name
44
   *   The base event string.
45
   * @param array $info
46
   *   The event info of the given event.
47
   */
48
  public function __construct($event_name, $info);
49

    
50
  /**
51
   * Sets the event settings.
52
   *
53
   * @param array $settings
54
   *   An array of settings to set.
55
   *
56
   * @return RulesEventHandlerInterface
57
   *   The handler itself for chaining.
58
   */
59
  public function setSettings(array $settings);
60

    
61
  /**
62
   * Gets the event settings.
63
   *
64
   * @return array
65
   *   The array of settings.
66
   */
67
  public function getSettings();
68

    
69
  /**
70
   * Returns an array of default settings.
71
   *
72
   * @return array
73
   *   The array of default settings.
74
   */
75
  public function getDefaults();
76

    
77
  /**
78
   * Returns a user-facing summary of the settings.
79
   *
80
   * @return string
81
   *   The summary in HTML, i.e. properly escaped or filtered.
82
   */
83
  public function summary();
84

    
85
  /**
86
   * Builds the event settings form.
87
   *
88
   * @param array $form_state
89
   *   An associative array containing the current state of the form.
90
   *
91
   * @return array
92
   *   The form structure.
93
   */
94
  public function buildForm(array &$form_state);
95

    
96
  /**
97
   * Validate the event settings independent from a form submission.
98
   *
99
   * @throws RulesIntegrityException
100
   *   In case of validation errors, RulesIntegrityExceptions are thrown.
101
   */
102
  public function validate();
103

    
104
  /**
105
   * Extract the form values and update the event settings.
106
   *
107
   * @param array $form
108
   *   An associative array containing the structure of the form.
109
   * @param array $form_state
110
   *   An associative array containing the current state of the form.
111
   */
112
  public function extractFormValues(array &$form, array &$form_state);
113

    
114
  /**
115
   * Returns the suffix to be added to the base event named based upon settings.
116
   *
117
   * If event settings are used, the event name Rules uses for the configured
118
   * event is {EVENT_NAME}--{SUFFIX}.
119
   *
120
   * @return string
121
   *   The suffix string. Return an empty string for not appending a suffix.
122
   */
123
  public function getEventNameSuffix();
124

    
125
  /**
126
   * Returns info about the variables provided by this event.
127
   *
128
   * @return array
129
   *   An array of provided variables, keyed by variable names and with the
130
   *   variable info array as value.
131
   */
132
  public function availableVariables();
133

    
134
  /**
135
   * Returns the base name of the event the event handler belongs to.
136
   *
137
   * @return string
138
   *   The name of the event the event handler belongs to.
139
   */
140
  public function getEventName();
141

    
142
  /**
143
   * Returns the info array of the event the event handler belongs to.
144
   *
145
   * @return string
146
   *   The info array of the event the event handler belongs to.
147
   */
148
  public function getEventInfo();
149

    
150
}
151

    
152
/**
153
 * Interface for event dispatchers.
154
 */
155
interface RulesEventDispatcherInterface extends RulesEventHandlerInterface {
156

    
157
  /**
158
   * Starts the event watcher.
159
   */
160
  public function startWatching();
161

    
162
  /**
163
   * Stops the event watcher.
164
   */
165
  public function stopWatching();
166

    
167
  /**
168
   * Returns whether the event dispatcher is currently active.
169
   *
170
   * @return bool
171
   *   TRUE if the event dispatcher is currently active, FALSE otherwise.
172
   */
173
  public function isWatching();
174

    
175
}
176

    
177
/**
178
 * Base class for event handler.
179
 */
180
abstract class RulesEventHandlerBase implements RulesEventHandlerInterface {
181

    
182
  /**
183
   * The event name.
184
   *
185
   * @var string
186
   */
187
  protected $eventName;
188

    
189
  /**
190
   * The event info.
191
   *
192
   * @var array
193
   */
194
  protected $eventInfo;
195

    
196
  /**
197
   * The event settings.
198
   *
199
   * @var array
200
   */
201
  protected $settings = array();
202

    
203
  /**
204
   * Implements RulesEventHandlerInterface::__construct().
205
   */
206
  public function __construct($event_name, $info) {
207
    $this->eventName = $event_name;
208
    $this->eventInfo = $info;
209
    $this->settings = $this->getDefaults();
210
  }
211

    
212
  /**
213
   * Implements RulesEventHandlerInterface::getSettings().
214
   */
215
  public function getSettings() {
216
    return $this->settings;
217
  }
218

    
219
  /**
220
   * Implements RulesEventHandlerInterface::setSettings().
221
   */
222
  public function setSettings(array $settings) {
223
    $this->settings = $settings + $this->getDefaults();
224
    return $this;
225
  }
226

    
227
  /**
228
   * Implements RulesEventHandlerInterface::validate().
229
   */
230
  public function validate() {
231
    // Nothing to check by default.
232
  }
233

    
234
  /**
235
   * Implements RulesEventHandlerInterface::extractFormValues().
236
   */
237
  public function extractFormValues(array &$form, array &$form_state) {
238
    foreach ($this->getDefaults() as $key => $setting) {
239
      $this->settings[$key] = isset($form_state['values'][$key]) ? $form_state['values'][$key] : $setting;
240
    }
241
  }
242

    
243
  /**
244
   * Implements RulesEventHandlerInterface::availableVariables().
245
   */
246
  public function availableVariables() {
247
    return isset($this->eventInfo['variables']) ? $this->eventInfo['variables'] : array();
248
  }
249

    
250
  /**
251
   * Implements RulesEventHandlerInterface::getEventName().
252
   */
253
  public function getEventName() {
254
    return $this->eventName;
255
  }
256

    
257
  /**
258
   * Implements RulesEventHandlerInterface::getEventInfo().
259
   */
260
  public function getEventInfo() {
261
    return $this->eventInfo;
262
  }
263

    
264
}
265

    
266
/**
267
 * A handler for events having no settings. This is the default handler.
268
 */
269
class RulesEventDefaultHandler extends RulesEventHandlerBase {
270

    
271
  /**
272
   * Implements RulesEventHandlerInterface::buildForm().
273
   */
274
  public function buildForm(array &$form_state) {
275
    return array();
276
  }
277

    
278
  /**
279
   * Implements RulesEventHandlerInterface::getConfiguredEventName().
280
   */
281
  public function getEventNameSuffix() {
282
    return '';
283
  }
284

    
285
  /**
286
   * Implements RulesEventHandlerInterface::summary().
287
   */
288
  public function summary() {
289
    return check_plain($this->eventInfo['label']);
290
  }
291

    
292
  /**
293
   * Implements RulesEventHandlerInterface::getDefaults().
294
   */
295
  public function getDefaults() {
296
    return array();
297
  }
298

    
299
  /**
300
   * Implements RulesEventHandlerInterface::getSettings().
301
   */
302
  public function getSettings() {
303
    return NULL;
304
  }
305

    
306
}
307

    
308
/**
309
 * Exposes the bundle of an entity as event setting.
310
 */
311
class RulesEventHandlerEntityBundle extends RulesEventHandlerBase {
312

    
313
  protected $entityType;
314
  protected $entityInfo;
315
  protected $bundleKey;
316

    
317
  /**
318
   * Implements RulesEventHandlerInterface::__construct().
319
   */
320
  public function __construct($event_name, $info) {
321
    parent::__construct($event_name, $info);
322
    // Cut off the suffix, e.g. remove 'view' from node_view.
323
    $this->entityType = implode('_', explode('_', $event_name, -1));
324
    $this->entityInfo = entity_get_info($this->entityType);
325
    if (!$this->entityInfo) {
326
      throw new InvalidArgumentException('Unsupported event name passed.');
327
    }
328
  }
329

    
330
  /**
331
   * Implements RulesEventHandlerInterface::summary().
332
   */
333
  public function summary() {
334
    $bundle = &$this->settings['bundle'];
335
    $bundle_label = isset($this->entityInfo['bundles'][$bundle]['label']) ? $this->entityInfo['bundles'][$bundle]['label'] : $bundle;
336
    $suffix = isset($bundle) ? ' ' . t('of @bundle-key %name', array('@bundle-key' => $this->getBundlePropertyLabel(), '%name' => $bundle_label)) : '';
337
    return check_plain($this->eventInfo['label']) . $suffix;
338
  }
339

    
340
  /**
341
   * Implements RulesEventHandlerInterface::buildForm().
342
   */
343
  public function buildForm(array &$form_state) {
344
    $form['bundle'] = array(
345
      '#type' => 'select',
346
      '#title' => t('Restrict by @bundle', array('@bundle' => $this->getBundlePropertyLabel())),
347
      '#description' => t('If you need to filter for multiple values, either add multiple events or use the "Entity is of bundle" condition instead.'),
348
      '#default_value' => $this->settings['bundle'],
349
      '#empty_value' => '',
350
      '#options' => array(),
351
    );
352
    foreach ($this->entityInfo['bundles'] as $name => $bundle_info) {
353
      $form['bundle']['#options'][$name] = $bundle_info['label'];
354
    }
355
    return $form;
356
  }
357

    
358
  /**
359
   * Returns the label to use for the bundle property.
360
   *
361
   * @return string
362
   *   The label to use for the bundle property.
363
   */
364
  protected function getBundlePropertyLabel() {
365
    return $this->entityInfo['entity keys']['bundle'];
366
  }
367

    
368
  /**
369
   * Implements RulesEventHandlerInterface::extractFormValues().
370
   */
371
  public function extractFormValues(array &$form, array &$form_state) {
372
    $this->settings['bundle'] = !empty($form_state['values']['bundle']) ? $form_state['values']['bundle'] : NULL;
373
  }
374

    
375
  /**
376
   * Implements RulesEventHandlerInterface::validate().
377
   */
378
  public function validate() {
379
    if ($this->settings['bundle'] && empty($this->entityInfo['bundles'][$this->settings['bundle']])) {
380
      throw new RulesIntegrityException(t('The @bundle %bundle of %entity_type is not known.',
381
        array(
382
          '%bundle' => $this->settings['bundle'],
383
          '%entity_type' => $this->entityInfo['label'],
384
          '@bundle' => $this->getBundlePropertyLabel(),
385
        )), array(NULL, 'bundle'));
386
    }
387
  }
388

    
389
  /**
390
   * Implements RulesEventHandlerInterface::getConfiguredEventName().
391
   */
392
  public function getEventNameSuffix() {
393
    return $this->settings['bundle'];
394
  }
395

    
396
  /**
397
   * Implements RulesEventHandlerInterface::getDefaults().
398
   */
399
  public function getDefaults() {
400
    return array(
401
      'bundle' => NULL,
402
    );
403
  }
404

    
405
  /**
406
   * Implements RulesEventHandlerInterface::availableVariables().
407
   */
408
  public function availableVariables() {
409
    $variables = $this->eventInfo['variables'];
410
    if ($this->settings['bundle']) {
411
      // Add the bundle to all variables of the entity type.
412
      foreach ($variables as $name => $variable_info) {
413
        if ($variable_info['type'] == $this->entityType) {
414
          $variables[$name]['bundle'] = $this->settings['bundle'];
415
        }
416
      }
417
    }
418
    return $variables;
419
  }
420

    
421
}