Projet

Général

Profil

Paste
Télécharger (20,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / ui / ui.data.inc @ d719f12f

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file Contains data type related forms.
5
 */
6
7
8
/**
9
 * Interface for data types providing a direct input form.
10
 */
11
interface RulesDataDirectInputFormInterface {
12
13
  /**
14
   * Constructs the direct input form.
15
   *
16
   * @return Array
17
   *  The direct input form.
18
   */
19
  public static function inputForm($name, $info, $settings, RulesPlugin $element);
20
21
  /**
22
   * Render the configured value.
23
   *
24
   * @return Array
25
   *   A renderable array.
26
   */
27
  public static function render($value);
28
29
}
30
31
/**
32
 * Interface for data UI classes providing an options list.
33
 */
34
interface RulesDataInputOptionsListInterface extends RulesDataDirectInputFormInterface {
35
36
  /**
37
   * Returns the options list for the data type.
38
   *
39
   * @param RulesPlugin $element
40
   *   The rules element to get the options for.
41
   * @param string $name
42
   *   The name of the parameter for which to get options.
43
   *
44
   * For retrieving information about the used data type and parameter, the
45
   * helper RulesDataUI::getTypeInfo() may be used as following:
46
   * @code
47
   *   list($type, $parameter_info) = RulesDataUI::getTypeInfo($element, $name);
48
   * @endcode
49
   *
50
   * @return
51
   *   An array of options as used by hook_options_list().
52
   */
53
  public static function optionsList(RulesPlugin $element, $name);
54
}
55
56
/**
57
 * Default UI related class for data types.
58
 */
59
class RulesDataUI {
60
61
  /**
62
   * Specifies the default input mode per data type.
63
   */
64
  public static function getDefaultMode() {
65
    return 'selector';
66
  }
67
68
  /**
69
   * Provides the selection form for a parameter.
70
   */
71
  public static function selectionForm($name, $info, $settings, RulesPlugin $element) {
72
    if (!isset($settings[$name . ':select'])) {
73
      $settings[$name . ':select'] = '';
74
      $vars = $element->availableVariables();
75
      // Default to variables with the same name as the parameter.
76
      if (isset($vars[$name])) {
77
        $settings[$name . ':select'] = $name;
78
      }
79
      // If there is only one match, use it by default.
80
      elseif (count($matches = RulesData::matchingDataSelector($vars, $info, '', 1, FALSE)) == 1) {
81
        $settings[$name . ':select'] = rules_array_key($matches);
82
      }
83
    }
84
    $form[$name . ':select'] = array(
85
      '#type' => 'rules_data_selection',
86
      '#title' => t('Data selector'),
87
      '#default_value' => $settings[$name . ':select'],
88
      '#required' => empty($info['optional']),
89
      '#autocomplete_path' => RulesPluginUI::path($element->root()->name, 'autocomplete' . '/' . $name),
90
      // Make the autocomplete textfield big enough so that it can display
91
      // descriptions without word wraps.
92
      '#size' => 75,
93
      '#description' => t("The data selector helps you drill down into the data available to Rules. <em>To make entity fields appear in the data selector, you may have to use the condition 'entity has field' (or 'content is of type').</em> More useful tips about data selection is available in <a href='@url'>the online documentation</a>.",
94
        array('@url' => rules_external_help('data-selection'))),
95
    );
96
97
    $cache = rules_get_cache();
98
    $form['types_help'] = array(
99
      '#theme' => 'rules_settings_help',
100
      '#heading' => t('Data types'),
101
    );
102
    if ($info['type'] == '*') {
103
      $type_labels[] = t('any');
104
    }
105
    else {
106
      $types = is_array($info['type']) ? $info['type'] : array($info['type']);
107
      $type_labels = array();
108
      foreach ($types as $type) {
109
        $type_labels[] = drupal_ucfirst(isset($cache['data_info'][$type]['label']) ? $cache['data_info'][$type]['label'] : $type);
110
      }
111
    }
112
    $form['types_help']['#text'] = format_plural(count($type_labels), 'Select data of the type %types.', 'Select data of the types %types.', array('%types' => implode(', ', $type_labels)));
113
114
    if (!empty($info['translatable'])) {
115
      if (empty($info['custom translation language'])) {
116
        $text = t('If a multilingual data source (i.e. a translatable field) is given, the argument is translated to the current interface language.');
117
      }
118
      else {
119
        $text = t('If a multilingual data source (i.e. a translatable field) is given, the argument is translated to the configured language.');
120
      }
121
      $form['translation'] = array(
122
        '#theme' => 'rules_settings_help',
123
        '#text' => $text,
124
        '#heading' => t('Translation'),
125
      );
126
    }
127
    $form['help'] = array(
128
      '#theme' => 'rules_data_selector_help',
129
      '#variables' => $element->availableVariables(),
130
      '#parameter' => $info,
131
    );
132
133
    // Add data processor.
134
    $settings += array($name . ':process' => array());
135
    $form[$name . ':process'] = array();
136
    RulesDataProcessor::attachForm($form[$name . ':process'], $settings[$name . ':process'], $info, $element->availableVariables());
137
    return $form;
138
  }
139
140
  /**
141
   * Renders the value by making use of the label if an options list is available.
142
   *
143
   * Used for data UI classes implementing the
144
   * RulesDataDirectInputFormInterface.
145
   *
146
   * In case an options list is available, the the usual render() method won't
147
   * be invoked, instead the selected entry is rendered via this method.
148
   *
149
   * @todo for Drupal 8: Refactor to avoid implementations have to care about
150
   * option lists when generating the form, but not when rendering values.
151
   */
152
  public static function renderOptionsLabel($value, $name, $info, RulesPlugin $element) {
153
    if (!empty($info['options list'])) {
154
      $element->call('loadBasicInclude');
155
      $options = entity_property_options_flatten(call_user_func($info['options list'], $element, $name));
156
      if (!is_array($value) && isset($options[$value])) {
157
        $value = $options[$value];
158
      }
159
      elseif (is_array($value)) {
160
        foreach ($value as $key => $single_value) {
161
          if (isset($options[$single_value])) {
162
            $value[$key] = $options[$single_value];
163
          }
164
        }
165
        $value = implode(', ', $value);
166
      }
167
      return array(
168
        'content' => array('#markup' => check_plain($value)),
169
        '#attributes' => array('class' => array('rules-parameter-options-entry')),
170
      );
171
    }
172
  }
173
174
  /**
175
   * Returns the data type and parameter information for the given arguments.
176
   *
177
   * This helper may be used by options list callbacks operation at data-type
178
   * level, see RulesDataInputOptionsListInterface.
179
   */
180
  public static function getTypeInfo(RulesPlugin $element, $name) {
181
    $parameters = $element->pluginParameterInfo();
182
    return array($parameters[$name]['type'], $parameters[$name]);
183
  }
184
}
185
186
/**
187
 * UI for textual data.
188
 */
189
class RulesDataUIText extends RulesDataUI implements RulesDataDirectInputFormInterface {
190
191
  public static function getDefaultMode() {
192
    return 'input';
193
  }
194
195
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
196
    if (!empty($info['options list'])) {
197
      // Make sure the .rules.inc of the providing module is included as the
198
      // options list callback may reside there.
199
      $element->call('loadBasicInclude');
200
      $form[$name] = array(
201
        '#type' => 'select',
202
        '#options' => call_user_func($info['options list'], $element, $name),
203
      );
204
    }
205
    else {
206
      $form[$name] = array(
207
        '#type' => 'textarea',
208
      );
209
      RulesDataInputEvaluator::attachForm($form, $settings, $info, $element->availableVariables());
210
    }
211
    $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
212
    $form[$name] += array(
213
      '#title' => t('Value'),
214
      '#default_value' => $settings[$name],
215
      '#required' => empty($info['optional']),
216
      '#after_build' => array('rules_ui_element_fix_empty_after_build'),
217
      '#rows' => 3,
218
    );
219
    return $form;
220
  }
221
222
  public static function render($value) {
223
    return array(
224
      'content' => array('#markup' => check_plain($value)),
225
      '#attributes' => array('class' => array('rules-parameter-text')),
226
    );
227
  }
228
}
229
230
/**
231
 * UI for text tokens.
232
 */
233
class RulesDataUITextToken extends RulesDataUIText {
234
235
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
236
    $form = parent::inputForm($name, $info, $settings, $element);
237
    if ($form[$name]['#type'] == 'textarea') {
238
      $form[$name]['#element_validate'][] = 'rules_ui_element_token_validate';
239
      $form[$name]['#description'] = t('May only contain lowercase letters, numbers, and underscores and has to start with a letter.');
240
      $form[$name]['#rows'] = 1;
241
    }
242
    return $form;
243
  }
244
}
245
246
/**
247
 * UI for formatted text.
248
 */
249
class RulesDataUITextFormatted extends RulesDataUIText {
250
251
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
252
    $form = parent::inputForm($name, $info, $settings, $element);
253
    $settings += array($name => isset($info['default value']) ? $info['default value'] : array('value' => NULL, 'format' => NULL));
254
255
    $form[$name]['#type'] = 'text_format';
256
    $form[$name]['#base_type'] = 'textarea';
257
    $form[$name]['#default_value'] = $settings[$name]['value'];
258
    $form[$name]['#format'] = $settings[$name]['format'];
259
    return $form;
260
  }
261
262
  public static function render($value) {
263
    return array(
264
      'content' => array('#markup' => check_plain($value['value'])),
265
      '#attributes' => array('class' => array('rules-parameter-text-formatted')),
266
    );
267
  }
268
}
269
270
271
272
/**
273
 * UI for decimal data.
274
 */
275
class RulesDataUIDecimal extends RulesDataUIText {
276
277
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
278
    $form = parent::inputForm($name, $info, $settings, $element);
279
    if (empty($info['options list'])) {
280
      $form[$name]['#type'] = 'textfield';
281
    }
282
    $form[$name]['#element_validate'][] = 'rules_ui_element_decimal_validate';
283
    $form[$name]['#rows'] = 1;
284
    return $form;
285
  }
286
}
287
288
/**
289
 * UI for integers.
290
 */
291
class RulesDataUIInteger extends RulesDataUIText {
292
293
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
294
    $form = parent::inputForm($name, $info, $settings, $element);
295
    if (empty($info['options list'])) {
296
      $form[$name]['#type'] = 'textfield';
297
    }
298
    $form[$name]['#element_validate'][] = 'rules_ui_element_integer_validate';
299
    return $form;
300
  }
301
}
302
303 76e2e7c3 Assos Assos
/**
304
 * UI for IP addresses.
305
 */
306
class RulesDataUIIPAddress extends RulesDataUIText {
307
308
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
309
    $form = parent::inputForm($name, $info, $settings, $element);
310
    if (empty($info['options list'])) {
311
      $form[$name]['#type'] = 'textfield';
312
      $form[$name]['#description'] = t('If not provided, the IP address of the current user will be used.');
313
    }
314
    $form[$name]['#element_validate'][] = 'rules_ui_element_ip_address_validate';
315
    $form[$name]['#rows'] = 1;
316
    return $form;
317
  }
318
}
319
320 85ad3d82 Assos Assos
/**
321
 * UI for boolean data.
322
 */
323
class RulesDataUIBoolean extends RulesDataUI implements RulesDataDirectInputFormInterface {
324
325
  public static function getDefaultMode() {
326
    return 'input';
327
  }
328
329
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
330
    $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
331
    // Note: Due to the checkbox even optional parameter always receive a value.
332
    $form[$name] = array(
333 384fc62a Assos Assos
      '#type' => 'radios',
334 85ad3d82 Assos Assos
      '#default_value' => $settings[$name],
335 384fc62a Assos Assos
      '#options' => array(
336
        TRUE => t('@label: True.', array('@label' => $info['label'])),
337
        FALSE => t('@label: False.', array('@label' => $info['label'])),
338
      ),
339 85ad3d82 Assos Assos
    );
340
    return $form;
341
  }
342
343
  public static function render($value) {
344
    return array(
345
      'content' => array('#markup' => !empty($value) ? t('true') : t('false')),
346
      '#attributes' => array('class' => array('rules-parameter-boolean')),
347
    );
348
  }
349
}
350
351
/**
352
 * UI for dates.
353
 */
354
class RulesDataUIDate extends RulesDataUIText {
355
356
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
357
    $settings += array($name => isset($info['default value']) ? $info['default value'] : (empty($info['optional']) ? gmdate('Y-m-d H:i:s', time()) : NULL));
358
359
    // Convert any configured timestamp into a readable format.
360
    if (is_numeric($settings[$name])) {
361
      $settings[$name] = gmdate('Y-m-d H:i:s', $settings[$name]);
362
    }
363
    $form = parent::inputForm($name, $info, $settings, $element);
364
    $form[$name]['#type'] = 'textfield';
365
    $form[$name]['#element_validate'][] = 'rules_ui_element_date_validate';
366
    // Note that the date input evaluator takes care for parsing dates using
367
    // strtotime() into a timestamp, which is the internal date format.
368
    $form[$name]['#description'] = t('The date in GMT. You may enter a fixed time (like %format) or any other values in GMT known by the PHP !strtotime function (like "+1 day"). Relative dates like "+1 day" or "now" relate to the evaluation time.',
369
      array('%format' => gmdate('Y-m-d H:i:s', time() + 86400),
370
            '!strtotime' => l('strtotime()', 'http://php.net/strtotime')));
371
372
    //TODO: Leverage the jquery datepicker+timepicker once a module providing
373
    //the timpeicker is available.
374
    return $form;
375
  }
376
377
  public static function render($value) {
378
    $value = is_numeric($value) ? format_date($value, 'short') : check_plain($value);
379
    return array(
380
      'content' => array('#markup' => $value),
381
      '#attributes' => array('class' => array('rules-parameter-date')),
382
    );
383
  }
384
}
385
386
/**
387
 * UI for duration type parameter.
388
 */
389
class RulesDataUIDuration extends RulesDataUIText {
390
391
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
392
    $form = parent::inputForm($name, $info, $settings, $element);
393
    $form[$name]['#type'] = 'rules_duration';
394
    $form[$name]['#after_build'][] = 'rules_ui_element_duration_after_build';
395
    return $form;
396
  }
397
398
  public static function render($value) {
399
    $value = is_numeric($value) ? format_interval($value) : check_plain($value);
400
    return array(
401
      'content' => array('#markup' => $value),
402
      '#attributes' => array('class' => array('rules-parameter-duration')),
403
    );
404
  }
405
}
406
407
/**
408
 * UI for the URI type parameter.
409
 */
410
class RulesDataUIURI extends RulesDataUIText {
411
412
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
413
    $form = parent::inputForm($name, $info, $settings, $element);
414
    $form[$name]['#rows'] = 1;
415
    $form[$name]['#description'] = t('You may enter relative URLs like %url as well as absolute URLs like %absolute-url.', array('%url' => 'user/login?destination=node', '%absolute-url' => 'http://drupal.org'));
416
    return $form;
417
  }
418
}
419
420
/**
421
 * UI for lists of textual data.
422
 */
423
class RulesDataUIListText extends RulesDataUIText {
424
425
  public static function getDefaultMode() {
426
    return 'input';
427
  }
428
429
  /**
430
   * @todo This does not work for inputting textual values including "\n".
431
   */
432
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
433
    $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
434
    $form = parent::inputForm($name, $info, $settings, $element);
435
436
    if ($form[$name]['#type'] == 'textarea') {
437
      // Fix up the value to be an array during after build.
438
      $form[$name]['#delimiter'] = "\n";
439
      $form[$name]['#after_build'][] = 'rules_ui_list_textarea_after_build';
440
      $form[$name]['#pre_render'][] = 'rules_ui_list_textarea_pre_render';
441
      $form[$name]['#default_value'] = !empty($settings[$name]) ? implode("\n", $settings[$name]) : NULL;
442
      $form[$name]['#description'] = t('A list of values, one on each line.');
443
    }
444
    else {
445
      $form[$name]['#multiple'] = TRUE;
446
    }
447
    return $form;
448
  }
449
450
  public static function render($value) {
451
    return array(
452
      'content' => array('#markup' => check_plain(implode(', ', $value))),
453
      '#attributes' => array('class' => array('rules-parameter-list')),
454
    );
455
  }
456
}
457
458
/**
459
 * UI for lists of integers.
460
 */
461
class RulesDataUIListInteger extends RulesDataUIListText {
462
463
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
464
    $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
465
    $form = parent::inputForm($name, $info, $settings, $element);
466
467
    if ($form[$name]['#type'] == 'textarea') {
468
      $form[$name]['#description'] = t('A list of integers, separated by commas. E.g. enter "1, 2, 3".');
469
      $form[$name]['#delimiter'] = ',';
470
      $form[$name]['#default_value'] = !empty($settings[$name]) ? implode(", ", $settings[$name]) : NULL;
471
      $form[$name]['#element_validate'][] = 'rules_ui_element_integer_list_validate';
472
      $form[$name]['#rows'] = 1;
473
    }
474
    return $form;
475
  }
476
}
477
478
/**
479
 * UI for lists of tokens.
480
 */
481
class RulesDataUIListToken extends RulesDataUIListInteger {
482
483
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
484
    $form = parent::inputForm($name, $info, $settings, $element);
485
486
    if ($form[$name]['#type'] == 'textarea') {
487
      $form[$name]['#description'] = t('A list of text tokens, separated by commas. E.g. enter "one, two, three".');
488
      $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
489
    }
490
    return $form;
491
  }
492
}
493
494
/**
495
 * UI for entity-based data types.
496
 */
497
class RulesDataUIEntity extends RulesDataUIText {
498
499
  public static function getDefaultMode() {
500
    return 'selector';
501
  }
502
503
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
504
    $form = parent::inputForm($name, $info, $settings, $element);
505
    if (empty($info['options list'])) {
506
      $form[$name]['#type'] = 'textfield';
507
508
      $entity_info = entity_get_info($info['type']);
509
      if (empty($entity_info['entity keys']['name'])) {
510
        $form[$name]['#element_validate'][] = 'rules_ui_element_integer_validate';
511
      }
512
      $form[$name]['#title'] = t('@entity identifier', array('@entity' => $entity_info['label']));
513
      $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
514
      $form[$name]['#description'] = t('Specify an identifier of a @entity.', array('@entity' => $entity_label));
515
    }
516
    return $form;
517
  }
518
}
519
520
/**
521
 * UI for exportable entity-based data types.
522
 */
523
class RulesDataUIEntityExportable extends RulesDataUIEntity {
524
525
  public static function getDefaultMode() {
526
    return 'input';
527
  }
528
}
529
530
/**
531
 * Data UI variant displaying a select list of available bundle entities.
532
 *
533
 * This is used for "bundle entities" implemented via the 'bundle of' feature
534
 * of entity.module.
535
 */
536
class RulesDataUIBundleEntity extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
537
538
  public static function getDefaultMode() {
539
    return 'input';
540
  }
541
542
  /**
543
   * Implements RulesDataInputOptionsListInterface::optionsList().
544
   */
545
  public static function optionsList(RulesPlugin $element, $name) {
546
    list($data_type, $parameter_info) = RulesDataUI::getTypeInfo($element, $name);
547
    $bundles = array();
548
    $entity_info = entity_get_info();
549
    $bundle_of_type = $entity_info[$data_type]['bundle of'];
550
    if (isset($entity_info[$bundle_of_type]['bundles'])) {
551
      foreach ($entity_info[$bundle_of_type]['bundles'] as $bundle_name => $bundle_info) {
552
        $bundles[$bundle_name] = $bundle_info['label'];
553
      }
554
    }
555
    return $bundles;
556
  }
557
}
558
559
/**
560
 * UI for taxonomy vocabularies.
561
 *
562
 * @see RulesTaxonomyVocabularyWrapper
563
 */
564
class RulesDataUITaxonomyVocabulary extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
565
566
  public static function getDefaultMode() {
567
    return 'input';
568
  }
569
570
  /**
571
   * Implements RulesDataInputOptionsListInterface::optionsList().
572
   */
573
  public static function optionsList(RulesPlugin $element, $name) {
574
    $options = array();
575
    foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocab) {
576
      $options[$machine_name] = $vocab->name;
577
    }
578
    return $options;
579
  }
580
}
581
582
/**
583
 * UI for lists of entity-based data types.
584
 */
585
class RulesDataUIListEntity extends RulesDataUIListInteger {
586
587
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
588
    $form = parent::inputForm($name, $info, $settings, $element);
589
    if (empty($info['options list'])) {
590
591
      $entity_info = entity_get_info(entity_property_list_extract_type($info['type']));
592
      if (!empty($entity_info['entity keys']['name'])) {
593
        $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
594
      }
595
      $form[$name]['#title'] = t('@entity identifiers', array('@entity' => $entity_info['label']));
596
      $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
597
      $form[$name]['#description'] = t('Specify a comma-separated list of identifiers of @entity entities.', array('@entity' => $entity_label));
598
    }
599
    return $form;
600
  }
601
}