Projet

Général

Profil

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

root / drupal7 / sites / all / modules / feeds_tamper / plugins / convert_boolean.inc @ 7b9e8704

1
<?php
2

    
3
/**
4
 * @file
5
 * Convert text value to boolean value.
6
 */
7

    
8
$plugin = array(
9
  'form' => 'feeds_tamper_convert_boolean_form',
10
  'callback' => 'feeds_tamper_convert_boolean_callback',
11
  'validate' => 'feeds_tamper_convert_boolean_validate',
12
  'name' => 'Convert to boolean',
13
  'multi' => 'loop',
14
  'category' => 'Text',
15
);
16

    
17
function feeds_tamper_convert_boolean_form($importer, $element_key, $settings) {
18
  $form = array();
19
  $form['true_value'] = array(
20
    '#type' => 'textfield',
21
    '#title' => t('Truth value'),
22
    '#default_value' => isset($settings['true_value']) ? $settings['true_value'] : 'true',
23
  );
24
  $form['false_value'] = array(
25
    '#type' => 'textfield',
26
    '#title' => t('False value'),
27
    '#default_value' => isset($settings['false_value']) ? $settings['false_value'] : 'false',
28
  );
29
  $form['match_case'] = array(
30
    '#type' => 'checkbox',
31
    '#title' => t('Match case'),
32
    '#default_value' => isset($settings['match_case']) ? $settings['match_case'] : FALSE,
33
  );
34
  $form['no_match'] = array(
35
    '#type' => 'radios',
36
    '#title' => t('If no match'),
37
    '#default_value' => isset($settings['no_match']) ? $settings['no_match'] : 'false',
38
    '#options' => array('true' => ('True'), 'false' => t('False'), 'null' => t('Null'), 'pass' => t('Do not modify'), 'other' => t('Other')),
39
    '#description' => t('The value to set if the true and false values do not match.'),
40
  );
41
  $form['other_text'] = array(
42
    '#type' => 'textfield',
43
    '#title' => t('Other text'),
44
    '#default_value' => isset($settings['other_text']) ? $settings['other_text'] : '',
45
    '#states' => array(
46
      'visible' => array(
47
        'input[name="settings[no_match]"]' => array('value' => 'other'),
48
      ),
49
    ),
50
  );
51
  return $form;
52
}
53

    
54
function feeds_tamper_convert_boolean_validate(&$settings) {
55
  if (!$settings['match_case']) {
56
    $settings['false_value'] = drupal_strtolower($settings['false_value']);
57
    $settings['true_value'] = drupal_strtolower($settings['true_value']);
58
  }
59
  switch ($settings['no_match']) {
60
    case 'true':
61
      $settings['no_match_value'] = TRUE;
62
      break;
63
    case 'false':
64
      $settings['no_match_value'] = FALSE;
65
      break;
66
    case 'null':
67
      $settings['no_match_value'] = NULL;
68
      break;
69
    case 'other':
70
      $settings['no_match_value'] = $settings['other_text'];
71
      break;
72
  }
73
}
74

    
75
function feeds_tamper_convert_boolean_callback($result, $item_key, $element_key, &$field, $settings, $source) {
76
  // Copy field value in case 'pass' is set.
77
  $match_field = $field;
78
  if (!$settings['match_case']) {
79
    $match_field = drupal_strtolower($match_field);
80
  }
81
  if ($match_field == $settings['true_value']) {
82
    $field = 1;
83
    return;
84
  }
85
  if ($match_field == $settings['false_value']) {
86
    $field = 0;
87
    return;
88
  }
89
  if ($settings['no_match'] == 'pass') {
90
    return;
91
  }
92
  $field = $settings['no_match_value'];
93
}