Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_field_math.inc @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_field_math.
6
 */
7

    
8
/**
9
 * Render a mathematical expression as a numeric value
10
 *
11
 * Definition terms:
12
 * - float: If true this field contains a decimal value. If unset this field
13
 *   will be assumed to be integer.
14
 *
15
 * @ingroup views_field_handlers
16
 */
17
class views_handler_field_math extends views_handler_field_numeric {
18

    
19
  /**
20
   * {@inheritdoc}
21
   */
22
  public function option_definition() {
23
    $options = parent::option_definition();
24
    $options['expression'] = array('default' => '');
25

    
26
    return $options;
27
  }
28

    
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function options_form(&$form, &$form_state) {
33
    $form['expression'] = array(
34
      '#type' => 'textarea',
35
      '#title' => t('Expression'),
36
      '#description' => t("Enter mathematical expressions such as 2 + 2 or sqrt(5). You may assign variables and create mathematical functions and evaluate them. Use the ; to separate these. For example: f(x) = x + 2; f(2). The result of the previous row's mathematical expression can be accessed by using the [expression] token itself."),
37
      '#default_value' => $this->options['expression'],
38
    );
39

    
40
    // Create a place for the help.
41
    $form['expression_help'] = array();
42
    parent::options_form($form, $form_state);
43

    
44
    // Then move the existing help.
45
    $form['expression_help'] = $form['alter']['help'];
46
    unset($form['expression_help']['#dependency']);
47
    unset($form['alter']['help']);
48
  }
49

    
50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function render($values) {
54
    ctools_include('math-expr');
55
    $tokens = array_map('floatval', $this->get_render_tokens(array()));
56
    $value = strtr($this->options['expression'], $tokens);
57
    $expressions = explode(';', $value);
58
    $math = new ctools_math_expr;
59
    foreach ($expressions as $expression) {
60
      if ($expression !== '') {
61
        $value = $math->evaluate($expression);
62
      }
63
    }
64

    
65
    // The rest is directly from views_handler_field_numeric but because it
66
    // does not allow the value to be passed in, it is copied.
67
    if (!empty($this->options['set_precision'])) {
68
      $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
69
    }
70
    else {
71
      $remainder = abs($value) - intval(abs($value));
72
      $value = $value > 0 ? floor($value) : ceil($value);
73
      $value = number_format($value, 0, '', $this->options['separator']);
74
      if ($remainder) {
75
        // The substr may not be locale safe.
76
        $value .= $this->options['decimal'] . substr($remainder, 2);
77
      }
78
    }
79

    
80
    // Check to see if hiding should happen before adding prefix and suffix.
81
    if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
82
      return '';
83
    }
84

    
85
    // Should we format as a plural.
86
    if (!empty($this->options['format_plural']) && ($value != 0 || !$this->options['empty_zero'])) {
87
      $value = format_plural($value, $this->options['format_plural_singular'], $this->options['format_plural_plural']);
88
    }
89

    
90
    return $this->sanitize_value($this->options['prefix'] . $value . $this->options['suffix']);
91
  }
92

    
93
  /**
94
   * {@inheritdoc}
95
   */
96
  public function query() {
97
  }
98

    
99
}