Projet

Général

Profil

Paste
Télécharger (19,6 ko) Statistiques
| Branche: | Révision:

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

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 boolean data.
305
 */
306
class RulesDataUIBoolean extends RulesDataUI implements RulesDataDirectInputFormInterface {
307

    
308
  public static function getDefaultMode() {
309
    return 'input';
310
  }
311

    
312
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
313
    $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
314
    // Note: Due to the checkbox even optional parameter always receive a value.
315
    $form[$name] = array(
316
      '#type' => 'checkbox',
317
      '#title' => check_plain($info['label']),
318
      '#default_value' => $settings[$name],
319
    );
320
    return $form;
321
  }
322

    
323
  public static function render($value) {
324
    return array(
325
      'content' => array('#markup' => !empty($value) ? t('true') : t('false')),
326
      '#attributes' => array('class' => array('rules-parameter-boolean')),
327
    );
328
  }
329
}
330

    
331
/**
332
 * UI for dates.
333
 */
334
class RulesDataUIDate extends RulesDataUIText {
335

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

    
339
    // Convert any configured timestamp into a readable format.
340
    if (is_numeric($settings[$name])) {
341
      $settings[$name] = gmdate('Y-m-d H:i:s', $settings[$name]);
342
    }
343
    $form = parent::inputForm($name, $info, $settings, $element);
344
    $form[$name]['#type'] = 'textfield';
345
    $form[$name]['#element_validate'][] = 'rules_ui_element_date_validate';
346
    // Note that the date input evaluator takes care for parsing dates using
347
    // strtotime() into a timestamp, which is the internal date format.
348
    $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.',
349
      array('%format' => gmdate('Y-m-d H:i:s', time() + 86400),
350
            '!strtotime' => l('strtotime()', 'http://php.net/strtotime')));
351

    
352
    //TODO: Leverage the jquery datepicker+timepicker once a module providing
353
    //the timpeicker is available.
354
    return $form;
355
  }
356

    
357
  public static function render($value) {
358
    $value = is_numeric($value) ? format_date($value, 'short') : check_plain($value);
359
    return array(
360
      'content' => array('#markup' => $value),
361
      '#attributes' => array('class' => array('rules-parameter-date')),
362
    );
363
  }
364
}
365

    
366
/**
367
 * UI for duration type parameter.
368
 */
369
class RulesDataUIDuration extends RulesDataUIText {
370

    
371
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
372
    $form = parent::inputForm($name, $info, $settings, $element);
373
    $form[$name]['#type'] = 'rules_duration';
374
    $form[$name]['#after_build'][] = 'rules_ui_element_duration_after_build';
375
    return $form;
376
  }
377

    
378
  public static function render($value) {
379
    $value = is_numeric($value) ? format_interval($value) : check_plain($value);
380
    return array(
381
      'content' => array('#markup' => $value),
382
      '#attributes' => array('class' => array('rules-parameter-duration')),
383
    );
384
  }
385
}
386

    
387
/**
388
 * UI for the URI type parameter.
389
 */
390
class RulesDataUIURI extends RulesDataUIText {
391

    
392
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
393
    $form = parent::inputForm($name, $info, $settings, $element);
394
    $form[$name]['#rows'] = 1;
395
    $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'));
396
    return $form;
397
  }
398
}
399

    
400
/**
401
 * UI for lists of textual data.
402
 */
403
class RulesDataUIListText extends RulesDataUIText {
404

    
405
  public static function getDefaultMode() {
406
    return 'input';
407
  }
408

    
409
  /**
410
   * @todo This does not work for inputting textual values including "\n".
411
   */
412
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
413
    $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
414
    $form = parent::inputForm($name, $info, $settings, $element);
415

    
416
    if ($form[$name]['#type'] == 'textarea') {
417
      // Fix up the value to be an array during after build.
418
      $form[$name]['#delimiter'] = "\n";
419
      $form[$name]['#after_build'][] = 'rules_ui_list_textarea_after_build';
420
      $form[$name]['#pre_render'][] = 'rules_ui_list_textarea_pre_render';
421
      $form[$name]['#default_value'] = !empty($settings[$name]) ? implode("\n", $settings[$name]) : NULL;
422
      $form[$name]['#description'] = t('A list of values, one on each line.');
423
    }
424
    else {
425
      $form[$name]['#multiple'] = TRUE;
426
    }
427
    return $form;
428
  }
429

    
430
  public static function render($value) {
431
    return array(
432
      'content' => array('#markup' => check_plain(implode(', ', $value))),
433
      '#attributes' => array('class' => array('rules-parameter-list')),
434
    );
435
  }
436
}
437

    
438
/**
439
 * UI for lists of integers.
440
 */
441
class RulesDataUIListInteger extends RulesDataUIListText {
442

    
443
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
444
    $settings += array($name => isset($info['default value']) ? $info['default value'] : NULL);
445
    $form = parent::inputForm($name, $info, $settings, $element);
446

    
447
    if ($form[$name]['#type'] == 'textarea') {
448
      $form[$name]['#description'] = t('A list of integers, separated by commas. E.g. enter "1, 2, 3".');
449
      $form[$name]['#delimiter'] = ',';
450
      $form[$name]['#default_value'] = !empty($settings[$name]) ? implode(", ", $settings[$name]) : NULL;
451
      $form[$name]['#element_validate'][] = 'rules_ui_element_integer_list_validate';
452
      $form[$name]['#rows'] = 1;
453
    }
454
    return $form;
455
  }
456
}
457

    
458
/**
459
 * UI for lists of tokens.
460
 */
461
class RulesDataUIListToken extends RulesDataUIListInteger {
462

    
463
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
464
    $form = parent::inputForm($name, $info, $settings, $element);
465

    
466
    if ($form[$name]['#type'] == 'textarea') {
467
      $form[$name]['#description'] = t('A list of text tokens, separated by commas. E.g. enter "one, two, three".');
468
      $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
469
    }
470
    return $form;
471
  }
472
}
473

    
474
/**
475
 * UI for entity-based data types.
476
 */
477
class RulesDataUIEntity extends RulesDataUIText {
478

    
479
  public static function getDefaultMode() {
480
    return 'selector';
481
  }
482

    
483
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
484
    $form = parent::inputForm($name, $info, $settings, $element);
485
    if (empty($info['options list'])) {
486
      $form[$name]['#type'] = 'textfield';
487

    
488
      $entity_info = entity_get_info($info['type']);
489
      if (empty($entity_info['entity keys']['name'])) {
490
        $form[$name]['#element_validate'][] = 'rules_ui_element_integer_validate';
491
      }
492
      $form[$name]['#title'] = t('@entity identifier', array('@entity' => $entity_info['label']));
493
      $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
494
      $form[$name]['#description'] = t('Specify an identifier of a @entity.', array('@entity' => $entity_label));
495
    }
496
    return $form;
497
  }
498
}
499

    
500
/**
501
 * UI for exportable entity-based data types.
502
 */
503
class RulesDataUIEntityExportable extends RulesDataUIEntity {
504

    
505
  public static function getDefaultMode() {
506
    return 'input';
507
  }
508
}
509

    
510
/**
511
 * Data UI variant displaying a select list of available bundle entities.
512
 *
513
 * This is used for "bundle entities" implemented via the 'bundle of' feature
514
 * of entity.module.
515
 */
516
class RulesDataUIBundleEntity extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
517

    
518
  public static function getDefaultMode() {
519
    return 'input';
520
  }
521

    
522
  /**
523
   * Implements RulesDataInputOptionsListInterface::optionsList().
524
   */
525
  public static function optionsList(RulesPlugin $element, $name) {
526
    list($data_type, $parameter_info) = RulesDataUI::getTypeInfo($element, $name);
527
    $bundles = array();
528
    $entity_info = entity_get_info();
529
    $bundle_of_type = $entity_info[$data_type]['bundle of'];
530
    if (isset($entity_info[$bundle_of_type]['bundles'])) {
531
      foreach ($entity_info[$bundle_of_type]['bundles'] as $bundle_name => $bundle_info) {
532
        $bundles[$bundle_name] = $bundle_info['label'];
533
      }
534
    }
535
    return $bundles;
536
  }
537
}
538

    
539
/**
540
 * UI for taxonomy vocabularies.
541
 *
542
 * @see RulesTaxonomyVocabularyWrapper
543
 */
544
class RulesDataUITaxonomyVocabulary extends RulesDataUIEntity implements RulesDataInputOptionsListInterface {
545

    
546
  public static function getDefaultMode() {
547
    return 'input';
548
  }
549

    
550
  /**
551
   * Implements RulesDataInputOptionsListInterface::optionsList().
552
   */
553
  public static function optionsList(RulesPlugin $element, $name) {
554
    $options = array();
555
    foreach (taxonomy_vocabulary_get_names() as $machine_name => $vocab) {
556
      $options[$machine_name] = $vocab->name;
557
    }
558
    return $options;
559
  }
560
}
561

    
562
/**
563
 * UI for lists of entity-based data types.
564
 */
565
class RulesDataUIListEntity extends RulesDataUIListInteger {
566

    
567
  public static function inputForm($name, $info, $settings, RulesPlugin $element) {
568
    $form = parent::inputForm($name, $info, $settings, $element);
569
    if (empty($info['options list'])) {
570

    
571
      $entity_info = entity_get_info(entity_property_list_extract_type($info['type']));
572
      if (!empty($entity_info['entity keys']['name'])) {
573
        $form[$name]['#element_validate'] = array('rules_ui_element_token_list_validate');
574
      }
575
      $form[$name]['#title'] = t('@entity identifiers', array('@entity' => $entity_info['label']));
576
      $entity_label = strtolower($entity_info['label'][0]) . substr($entity_info['label'], 1);
577
      $form[$name]['#description'] = t('Specify a comma-separated list of identifiers of @entity entities.', array('@entity' => $entity_label));
578
    }
579
    return $form;
580
  }
581
}