1 | <?php |
---|
2 | // $Id: views_handler_field_boolean.inc,v 1.3.2.1 2010/03/25 01:14:22 merlinofchaos Exp $ |
---|
3 | |
---|
4 | /** |
---|
5 | * A handler to provide proper displays for booleans. |
---|
6 | * |
---|
7 | * Allows for display of true/false, yes/no, on/off. |
---|
8 | * |
---|
9 | * Definition terms: |
---|
10 | * - output formats: An array where the first entry is displayed on boolean false |
---|
11 | * and the second is displayed on boolean true. An example for sticky is: |
---|
12 | * @code |
---|
13 | * 'output formats' => array( |
---|
14 | * 'sticky' => array('', t('Sticky')), |
---|
15 | * ), |
---|
16 | * @endcode |
---|
17 | * |
---|
18 | * @ingroup views_field_handlers |
---|
19 | */ |
---|
20 | class views_handler_field_boolean extends views_handler_field { |
---|
21 | function option_definition() { |
---|
22 | $options = parent::option_definition(); |
---|
23 | $options['type'] = array('default' => 'yes-no'); |
---|
24 | $options['not'] = array('definition bool' => 'reverse'); |
---|
25 | |
---|
26 | return $options; |
---|
27 | } |
---|
28 | |
---|
29 | function init(&$view, $options) { |
---|
30 | parent::init($view, $options); |
---|
31 | |
---|
32 | $default_formats = array( |
---|
33 | 'yes-no' => array(t('Yes'), t('No')), |
---|
34 | 'true-false' => array(t('True'), t('False')), |
---|
35 | 'on-off' => array(t('On'), t('Off')), |
---|
36 | ); |
---|
37 | $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array(); |
---|
38 | $this->formats = array_merge($default_formats, $output_formats); |
---|
39 | } |
---|
40 | |
---|
41 | function options_form(&$form, &$form_state) { |
---|
42 | parent::options_form($form, $form_state); |
---|
43 | foreach ($this->formats as $key => $item) { |
---|
44 | $options[$key] = implode('/', $item); |
---|
45 | } |
---|
46 | |
---|
47 | $form['type'] = array( |
---|
48 | '#type' => 'select', |
---|
49 | '#title' => t('Output format'), |
---|
50 | '#options' => $options, |
---|
51 | '#default_value' => $this->options['type'], |
---|
52 | ); |
---|
53 | $form['not'] = array( |
---|
54 | '#type' => 'checkbox', |
---|
55 | '#title' => t('Reverse'), |
---|
56 | '#description' => t('If checked, true will be displayed as false.'), |
---|
57 | '#default_value' => $this->options['not'], |
---|
58 | ); |
---|
59 | } |
---|
60 | |
---|
61 | function render($values) { |
---|
62 | $value = $values->{$this->field_alias}; |
---|
63 | if (!empty($this->options['not'])) { |
---|
64 | $value = !$value; |
---|
65 | } |
---|
66 | |
---|
67 | if (isset($this->formats[$this->options['type']])) { |
---|
68 | return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1]; |
---|
69 | } |
---|
70 | else { |
---|
71 | return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1]; |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|