tripal_views_handler_field_boolean.inc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. class tripal_views_handler_field_boolean extends tripal_views_handler_field {
  3. function option_definition() {
  4. $options = parent::option_definition();
  5. $options['type'] = array('default' => 'yes-no');
  6. $options['type_custom_true'] = array('default' => '', 'translatable' => TRUE);
  7. $options['type_custom_false'] = array('default' => '', 'translatable' => TRUE);
  8. $options['not'] = array('definition bool' => 'reverse');
  9. return $options;
  10. }
  11. function init(&$view, &$options) {
  12. parent::init($view, $options);
  13. $default_formats = array(
  14. 'yes-no' => array(t('Yes'), t('No')),
  15. 'true-false' => array(t('True'), t('False')),
  16. 'on-off' => array(t('On'), t('Off')),
  17. 'enabled-disabled' => array(t('Enabled'), t('Disabled')),
  18. 'boolean' => array(1, 0),
  19. 'unicode-yes-no' => array('✔', '✖'),
  20. );
  21. $output_formats = isset($this->definition['output formats']) ? $this->definition['output formats'] : array();
  22. $custom_format = array('custom' => array(t('Custom')));
  23. $this->formats = array_merge($default_formats, $output_formats, $custom_format);
  24. }
  25. function options_form(&$form, &$form_state) {
  26. foreach ($this->formats as $key => $item) {
  27. $options[$key] = implode('/', $item);
  28. }
  29. $form['type'] = array(
  30. '#type' => 'select',
  31. '#title' => t('Output format'),
  32. '#options' => $options,
  33. '#default_value' => $this->options['type'],
  34. );
  35. $form['type_custom_true'] = array(
  36. '#type' => 'textfield',
  37. '#title' => t('Custom output for TRUE'),
  38. '#default_value' => $this->options['type_custom_true'],
  39. '#states' => array(
  40. 'visible' => array(
  41. 'select[name="options[type]"]' => array('value' => 'custom'),
  42. ),
  43. ),
  44. );
  45. $form['type_custom_false'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Custom output for FALSE'),
  48. '#default_value' => $this->options['type_custom_false'],
  49. '#states' => array(
  50. 'visible' => array(
  51. 'select[name="options[type]"]' => array('value' => 'custom'),
  52. ),
  53. ),
  54. );
  55. $form['not'] = array(
  56. '#type' => 'checkbox',
  57. '#title' => t('Reverse'),
  58. '#description' => t('If checked, true will be displayed as false.'),
  59. '#default_value' => $this->options['not'],
  60. );
  61. parent::options_form($form, $form_state);
  62. }
  63. function render($values) {
  64. $value = $this->get_value($values);
  65. if (!empty($this->options['not'])) {
  66. $value = !$value;
  67. }
  68. if ($this->options['type'] == 'custom') {
  69. return $value ? filter_xss_admin($this->options['type_custom_true']) : filter_xss_admin($this->options['type_custom_false']);
  70. }
  71. else if (isset($this->formats[$this->options['type']])) {
  72. return $value ? $this->formats[$this->options['type']][0] : $this->formats[$this->options['type']][1];
  73. }
  74. else {
  75. return $value ? $this->formats['yes-no'][0] : $this->formats['yes-no'][1];
  76. }
  77. }
  78. }