Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Performs basic mathematical calculations on the imported value.
6
 */
7

    
8
$plugin = array(
9
  'form' => 'feeds_tamper_math_form',
10
  'validate' => 'feeds_tamper_math_validate',
11
  'callback' => 'feeds_tamper_math_callback',
12
  'name' => 'Mathematical operation',
13
  'category' => 'Number',
14
  'multi' => 'loop',
15
);
16

    
17
function feeds_tamper_math_form($importer, $element_key, $settings) {
18
  $form = array();
19

    
20
  $form['operation'] = array(
21
    '#type' => 'select',
22
    '#title' => t('Operation'),
23
    '#description' => t('The operation to apply to the imported value.'),
24
    '#required' => TRUE,
25
    '#options' => array(
26
      'addition' =>  '+',
27
      'subtraction' =>  '-',
28
      'multiplication' =>  '*',
29
      'division' =>  '/',
30
    ),
31
    '#default_value' => isset($settings['operation']) ? $settings['operation'] : '',
32
  );
33

    
34
  $form['flip'] = array(
35
    '#type' => 'checkbox',
36
    '#title' => t('Flip'),
37
    '#description' => t('Normally, the feed item will be processed like feed-value / setting-value. This option switches the order so that it is setting-value / feed-value.'),
38
    '#states' => array(
39
      'visible' => array(
40
        ':input[name="settings[operation]"]' => array(
41
          array('value' => 'subtraction'),
42
          array('value' => 'division'),
43
        ),
44
      ),
45
    ),
46
    '#default_value' => isset($settings['flip']) ? $settings['flip'] : FALSE,
47
  );
48

    
49
  $form['value'] = array(
50
    '#type' => 'textfield',
51
    '#title' => t('Value'),
52
    '#required' => TRUE,
53
    '#description' => t('A numerical value.'),
54
    '#default_value' => isset($settings['value']) ? $settings['value'] : '',
55
  );
56

    
57
  $form['log'] = array(
58
    '#type' => 'checkbox',
59
    '#default_value' => !empty($settings['log']),
60
    '#title' => t('Log'),
61
    '#description' => t('Log to the Feed log and print a message when an invalid value is found.'),
62
  );
63

    
64
  return $form;
65
}
66

    
67
function feeds_tamper_math_validate(&$settings) {
68
  if (!is_numeric($settings['value'])) {
69
    form_set_error('settings][value', t('The value must be numeric.'));
70
  }
71
  elseif ($settings['operation'] === 'division' && empty($settings['flip']) && $settings['value'] == 0) {
72
    form_set_error('settings][value', t('Cannot divide by zero.'));
73
  }
74
}
75

    
76
function feeds_tamper_math_callback($result, $item_key, $element_key, &$field, $settings, $source) {
77
  if ($field === TRUE || $field === FALSE || $field === NULL) {
78
    $field = (int) $field;
79
  }
80

    
81
  if (!is_numeric($field)) {
82

    
83
    if (!empty($settings['log'])) {
84
      $source->log('feeds_tamper:math', 'Math plugin failed because @key was not numeric. Value: @field', array('@key' => $element_key, '@field' => $field));
85
      drupal_set_message(t('Math plugin failed because @key was not numeric. Value: @field', array('@key' => $element_key, '@field' => $field)));
86
    }
87

    
88
    return;
89
  }
90

    
91
  if (!empty($settings['flip'])) {
92

    
93
    switch ($settings['operation']) {
94
      case 'subtraction':
95
        $field = $settings['value'] - $field;
96
        return;
97

    
98
      case 'division':
99
        // Avoid divide by zero.
100
        if (empty($field)) {
101
          if (!empty($settings['log'])) {
102
            $source->log('feeds_tamper:math', 'Math plugin failed because @key was zero.', array('@key' => $element_key));
103
            drupal_set_message(t('Math plugin failed because @key was zero.', array('@key' => $element_key)));
104
          }
105

    
106
          return;
107
        }
108

    
109
        $field = $settings['value'] / $field;
110
        return;
111
    }
112
  }
113

    
114
  switch ($settings['operation']) {
115
    case 'addition':
116
      $field = $field + $settings['value'];
117
      return;
118

    
119
    case 'subtraction':
120
      $field = $field - $settings['value'];
121
      return;
122

    
123
    case 'multiplication':
124
      $field = $field * $settings['value'];
125
      return;
126

    
127
    case 'division':
128
      $field = $field / $settings['value'];
129
      return;
130
  }
131
}