Projet

Général

Profil

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

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

1
<?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
/**
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
/**
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
      '#type' => 'checkbox',
334
      '#title' => check_plain($info['label']),
335
      '#default_value' => $settings[$name],
336
    );
337
    return $form;
338
  }
339

    
340
  public static function render($value) {
341
    return array(
342
      'content' => array('#markup' => !empty($value) ? t('true') : t('false')),
343
      '#attributes' => array('class' => array('rules-parameter-boolean')),
344
    );
345
  }
346
}
347

    
348
/**
349
 * UI for dates.
350
 */
351
class RulesDataUIDate extends RulesDataUIText {
352

    
353
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
354
    $settings += array($name => isset($info['default value']) ? $info['default value'] : (empty($info['optional']) ? gmdate('Y-m-d H:i:s', time()) : NULL));
355

    
356
    // Convert any configured timestamp into a readable format.
357
    if (is_numeric($settings[$name])) {
358
      $settings[$name] = gmdate('Y-m-d H:i:s', $settings[$name]);
359
    }
360
    $form = parent::inputForm($name, $info, $settings, $element);
361
    $form[$name]['#type'] = 'textfield';
362
    $form[$name]['#element_validate'][] = 'rules_ui_element_date_validate';
363
    // Note that the date input evaluator takes care for parsing dates using
364
    // strtotime() into a timestamp, which is the internal date format.
365
    $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.',
366
      array('%format' => gmdate('Y-m-d H:i:s', time() + 86400),
367
            '!strtotime' => l('strtotime()', 'http://php.net/strtotime')));
368

    
369
    //TODO: Leverage the jquery datepicker+timepicker once a module providing
370
    //the timpeicker is available.
371
    return $form;
372
  }
373

    
374
  public static function render($value) {
375
    $value = is_numeric($value) ? format_date($value, 'short') : check_plain($value);
376
    return array(
377
      'content' => array('#markup' => $value),
378
      '#attributes' => array('class' => array('rules-parameter-date')),
379
    );
380
  }
381
}
382

    
383
/**
384
 * UI for duration type parameter.
385
 */
386
class RulesDataUIDuration extends RulesDataUIText {
387

    
388
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
389
    $form = parent::inputForm($name, $info, $settings, $element);
390
    $form[$name]['#type'] = 'rules_duration';
391
    $form[$name]['#after_build'][] = 'rules_ui_element_duration_after_build';
392
    return $form;
393
  }
394

    
395
  public static function render($value) {
396
    $value = is_numeric($value) ? format_interval($value) : check_plain($value);
397
    return array(
398
      'content' => array('#markup' => $value),
399
      '#attributes' => array('class' => array('rules-parameter-duration')),
400
    );
401
  }
402
}
403

    
404
/**
405
 * UI for the URI type parameter.
406
 */
407
class RulesDataUIURI extends RulesDataUIText {
408

    
409
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
410
    $form = parent::inputForm($name, $info, $settings, $element);
411
    $form[$name]['#rows'] = 1;
412
    $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'));
413
    return $form;
414
  }
415
}
416

    
417
/**
418
 * UI for lists of textual data.
419
 */
420
class RulesDataUIListText extends RulesDataUIText {
421

    
422
  public static function getDefaultMode() {
423
    return 'input';
424
  }
425

    
426
  /**
427
   * @todo This does not work for inputting textual values including "\n".
428
   */
429
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
430
    $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
431
    $form = parent::inputForm($name, $info, $settings, $element);
432

    
433
    if ($form[$name]['#type'] == 'textarea') {
434
      // Fix up the value to be an array during after build.
435
      $form[$name]['#delimiter'] = "\n";
436
      $form[$name]['#after_build'][] = 'rules_ui_list_textarea_after_build';
437
      $form[$name]['#pre_render'][] = 'rules_ui_list_textarea_pre_render';
438
      $form[$name]['#default_value'] = !empty($settings[$name]) ? implode("\n", $settings[$name]) : NULL;
439
      $form[$name]['#description'] = t('A list of values, one on each line.');
440
    }
441
    else {
442
      $form[$name]['#multiple'] = TRUE;
443
    }
444
    return $form;
445
  }
446

    
447
  public static function render($value) {
448
    return array(
449
      'content' => array('#markup' => check_plain(implode(', ', $value))),
450
      '#attributes' => array('class' => array('rules-parameter-list')),
451
    );
452
  }
453
}
454

    
455
/**
456
 * UI for lists of integers.
457
 */
458
class RulesDataUIListInteger extends RulesDataUIListText {
459

    
460
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
461
    $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
462
    $form = parent::inputForm($name, $info, $settings, $element);
463

    
464
    if ($form[$name]['#type'] == 'textarea') {
465
      $form[$name]['#description'] = t('A list of integers, separated by commas. E.g. enter "1, 2, 3".');
466
      $form[$name]['#delimiter'] = ',';
467
      $form[$name]['#default_value'] = !empty($settings[$name]) ? implode(", ", $settings[$name]) : NULL;
468
      $form[$name]['#element_validate'][] = 'rules_ui_element_integer_list_validate';
469
      $form[$name]['#rows'] = 1;
470
    }
471
    return $form;
472
  }
473
}
474

    
475
/**
476
 * UI for lists of tokens.
477
 */
478
class RulesDataUIListToken extends RulesDataUIListInteger {
479

    
480
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
481
    $form = parent::inputForm($name, $info, $settings, $element);
482

    
483
    if ($form[$name]['#type'] == 'textarea') {
484
      $form[$name]['#description'] = t('A list of text tokens, separated by commas. E.g. enter "one, two, three".');
485
      $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
486
    }
487
    return $form;
488
  }
489
}
490

    
491
/**
492
 * UI for entity-based data types.
493
 */
494
class RulesDataUIEntity extends RulesDataUIText {
495

    
496
  public static function getDefaultMode() {
497
    return 'selector';
498
  }
499

    
500
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
501
    $form = parent::inputForm($name, $info, $settings, $element);
502
    if (empty($info['options list'])) {
503
      $form[$name]['#type'] = 'textfield';
504

    
505
      $entity_info = entity_get_info($info['type']);
506
      if (empty($entity_info['entity keys']['name'])) {
507
        $form[$name]['#element_validate'][] = 'rules_ui_element_integer_validate';
508
      }
509
      $form[$name]['#title'] = t('@entity identifier', array('@entity' => $entity_info['label']));
510
      $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
511
      $form[$name]['#description'] = t('Specify an identifier of a @entity.', array('@entity' => $entity_label));
512
    }
513
    return $form;
514
  }
515
}
516

    
517
/**
518
 * UI for exportable entity-based data types.
519
 */
520
class RulesDataUIEntityExportable extends RulesDataUIEntity {
521

    
522
  public static function getDefaultMode() {
523
    return 'input';
524
  }
525
}
526

    
527
/**
528
 * Data UI variant displaying a select list of available bundle entities.
529
 *
530
 * This is used for "bundle entities" implemented via the 'bundle of' feature
531
 * of entity.module.
532
 */
533
class RulesDataUIBundleEntity extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
534

    
535
  public static function getDefaultMode() {
536
    return 'input';
537
  }
538

    
539
  /**
540
   * Implements RulesDataInputOptionsListInterface::optionsList().
541
   */
542
  public static function optionsList(RulesPlugin $element, $name) {
543
    list($data_type, $parameter_info) = RulesDataUI::getTypeInfo($element, $name);
544
    $bundles = array();
545
    $entity_info = entity_get_info();
546
    $bundle_of_type = $entity_info[$data_type]['bundle of'];
547
    if (isset($entity_info[$bundle_of_type]['bundles'])) {
548
      foreach ($entity_info[$bundle_of_type]['bundles'] as $bundle_name => $bundle_info) {
549
        $bundles[$bundle_name] = $bundle_info['label'];
550
      }
551
    }
552
    return $bundles;
553
  }
554
}
555

    
556
/**
557
 * UI for taxonomy vocabularies.
558
 *
559
 * @see RulesTaxonomyVocabularyWrapper
560
 */
561
class RulesDataUITaxonomyVocabulary extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
562

    
563
  public static function getDefaultMode() {
564
    return 'input';
565
  }
566

    
567
  /**
568
   * Implements RulesDataInputOptionsListInterface::optionsList().
569
   */
570
  public static function optionsList(RulesPlugin $element, $name) {
571
    $options = array();
572
    foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocab) {
573
      $options[$machine_name] = $vocab->name;
574
    }
575
    return $options;
576
  }
577
}
578

    
579
/**
580
 * UI for lists of entity-based data types.
581
 */
582
class RulesDataUIListEntity extends RulesDataUIListInteger {
583

    
584
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
585
    $form = parent::inputForm($name, $info, $settings, $element);
586
    if (empty($info['options list'])) {
587

    
588
      $entity_info = entity_get_info(entity_property_list_extract_type($info['type']));
589
      if (!empty($entity_info['entity keys']['name'])) {
590
        $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
591
      }
592
      $form[$name]['#title'] = t('@entity identifiers', array('@entity' => $entity_info['label']));
593
      $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
594
      $form[$name]['#description'] = t('Specify a comma-separated list of identifiers of @entity entities.', array('@entity' => $entity_label));
595
    }
596
    return $form;
597
  }
598
}