Projet

Général

Profil

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

root / drupal7 / sites / all / modules / rules / includes / rules.event.inc @ 76e2e7c3

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
   */
74
  public function getDefaults();
75

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

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

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

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

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

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

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

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

    
150
/**
151
 * Interface for event dispatchers.
152
 */
153
interface RulesEventDispatcherInterface extends RulesEventHandlerInterface {
154

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

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

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

    
174
/**
175
 * Base class for event handler.
176
 */
177
abstract class RulesEventHandlerBase implements RulesEventHandlerInterface {
178

    
179
  /**
180
   * The event name.
181
   *
182
   * @var string
183
   */
184
  protected $eventName;
185

    
186
  /**
187
   * The event info.
188
   *
189
   * @var array
190
   */
191
  protected $eventInfo;
192

    
193
  /**
194
   * The event settings.
195
   *
196
   * @var array
197
   */
198
  protected $settings = array();
199

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

    
209
  /**
210
   * Implements RulesEventHandlerInterface::getSettings()
211
   */
212
  public function getSettings() {
213
    return $this->settings;
214
  }
215

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

    
224
  /**
225
   * Implements RulesEventHandlerInterface::validate()
226
   */
227
  public function validate() {
228
    // Nothing to check by default.
229
  }
230

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

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

    
247
  /**
248
   * Implements RulesEventHandlerInterface::getEventName()
249
   */
250
  public function getEventName() {
251
    return $this->eventName;
252
  }
253

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

    
262
/**
263
 * A handler for events having no settings. This is the default handler.
264
 */
265
class RulesEventDefaultHandler extends RulesEventHandlerBase  {
266

    
267
  /**
268
   * Implements RulesEventHandlerInterface::buildForm()
269
   */
270
  public function buildForm(array &$form_state) {
271
    return array();
272
  }
273

    
274
  /**
275
   * Implements RulesEventHandlerInterface::getConfiguredEventName()
276
   */
277
  public function getEventNameSuffix() {
278
    return '';
279
  }
280

    
281
  /**
282
   * Implements RulesEventHandlerInterface::summary()
283
   */
284
  public function summary() {
285
    return check_plain($this->eventInfo['label']);
286
  }
287

    
288
  /**
289
   * Implements RulesEventHandlerInterface::getDefaults()
290
   */
291
  public function getDefaults() {
292
    return array();
293
  }
294

    
295
  /**
296
   * Implements RulesEventHandlerInterface::getSettings()
297
   */
298
  public function getSettings() {
299
    return NULL;
300
  }
301
}
302

    
303
/**
304
 * Exposes the bundle of an entity as event setting.
305
 */
306
class RulesEventHandlerEntityBundle extends RulesEventHandlerBase {
307

    
308
  protected $entityType, $entityInfo, $bundleKey;
309

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

    
323
  /**
324
   * Implements RulesEventHandlerInterface::summary()
325
   */
326
  public function summary() {
327
    $bundle = &$this->settings['bundle'];
328
    $bundle_label = isset($this->entityInfo['bundles'][$bundle]['label']) ? $this->entityInfo['bundles'][$bundle]['label'] : $bundle;
329
    $suffix = isset($bundle) ? ' ' . t('of @bundle-key %name', array('@bundle-key' => $this->getBundlePropertyLabel(), '%name' => $bundle_label)) : '';
330
    return check_plain($this->eventInfo['label']) . $suffix;
331
  }
332

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

    
350
  /**
351
   * Returns the label to use for the bundle property.
352
   *
353
   * @return string
354
   */
355
  protected function getBundlePropertyLabel() {
356
    return $this->entityInfo['entity keys']['bundle'];
357
  }
358

    
359
  /**
360
   * Implements RulesEventHandlerInterface::extractFormValues()
361
   */
362
  public function extractFormValues(array &$form, array &$form_state) {
363
    $this->settings['bundle'] = !empty($form_state['values']['bundle']) ? $form_state['values']['bundle'] : NULL;
364
  }
365

    
366
  /**
367
   * Implements RulesEventHandlerInterface::validate()
368
   */
369
  public function validate() {
370
    if ($this->settings['bundle'] && empty($this->entityInfo['bundles'][$this->settings['bundle']])) {
371
      throw new RulesIntegrityException(t('The @bundle %bundle of %entity_type is not known.',
372
        array(
373
          '%bundle' => $this->settings['bundle'],
374
          '%entity_type' => $this->entityInfo['label'],
375
          '@bundle' => $this->getBundlePropertyLabel(),
376
      )), array(NULL, 'bundle'));
377
    }
378
  }
379

    
380
  /**
381
   * Implements RulesEventHandlerInterface::getConfiguredEventName()
382
   */
383
  public function getEventNameSuffix() {
384
    return $this->settings['bundle'];
385
  }
386

    
387
  /**
388
   * Implements RulesEventHandlerInterface::getDefaults()
389
   */
390
  public function getDefaults() {
391
    return array(
392
      'bundle' => NULL,
393
    );
394
  }
395

    
396
  /**
397
   * Implements RulesEventHandlerInterface::availableVariables()
398
   */
399
  public function availableVariables() {
400
    $variables = $this->eventInfo['variables'];
401
    if ($this->settings['bundle']) {
402
      // Add the bundle to all variables of the entity type.
403
      foreach ($variables as $name => $variable_info) {
404
        if ($variable_info['type'] == $this->entityType) {
405
          $variables[$name]['bundle'] = $this->settings['bundle'];
406
        }
407
      }
408
    }
409
    return $variables;
410
  }
411
}