chado_views_handler_field_math.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. class chado_views_handler_field_math extends views_handler_field_math {
  3. /**
  4. * Defines the defaults for the options form
  5. */
  6. function option_definition() {
  7. $options = parent::option_definition();
  8. $options['type'] = array('default' => 'separator');
  9. $options['separator'] = array('default' => ', ');
  10. return $options;
  11. }
  12. /**
  13. * Defines the options form (form available to admin when they add a field to a view)
  14. */
  15. function options_form(&$form, &$form_state) {
  16. parent::options_form($form, $form_state);
  17. $form['type'] = array(
  18. '#type' => 'radios',
  19. '#title' => t('Display type'),
  20. '#options' => array(
  21. 'ul' => t('Unordered list'),
  22. 'ol' => t('Ordered list'),
  23. 'separator' => t('Simple separator'),
  24. ),
  25. '#default_value' => $this->options['type'],
  26. );
  27. $form['separator'] = array(
  28. '#type' => 'textfield',
  29. '#title' => t('Separator'),
  30. '#default_value' => $this->options['separator'],
  31. '#process' => array('views_process_dependency'),
  32. '#dependency' => array('radio:options[type]' => array('separator')),
  33. );
  34. }
  35. /**
  36. * Determines whether the current field is aggregated or not
  37. * Note: The parent::query() takes care of adding the field to the query, etc.
  38. */
  39. function query () {
  40. parent::query();
  41. $table = $this->query->get_table_info($this->table);
  42. if (preg_match('/aggregator/',$table['join']->definition['handler'])) {
  43. $this->aggregated = TRUE;
  44. } else {
  45. $this->aggregated = FALSE;
  46. }
  47. }
  48. /**
  49. * Render the field.
  50. *
  51. * Note: Checks to see if we have an array or simple field. If we have an array, then
  52. * split it up and render each part using the parent render functionality.
  53. *
  54. * @param $values
  55. * The values retrieved from the database.
  56. */
  57. function render($values) {
  58. // If it's aggregated (an array), then render each part
  59. // using the parent render functionality
  60. if ($this->aggregated) {
  61. $items = array();
  62. $parts = $this->split_array_agg_results($values->{$this->field_alias});
  63. foreach ($parts as $p) {
  64. $v[ $this->field_alias ] = $p;
  65. $val = (object) $v;
  66. $items[] = parent::render($val);
  67. unset($v, $val);
  68. }
  69. if ($this->options['type'] == 'separator') {
  70. return implode(check_plain($this->options['separator']), $items);
  71. }
  72. else {
  73. return theme('item_list', $items, NULL, $this->options['type']);
  74. }
  75. // Otherwise it is not aggragated
  76. // Just render like the default handler would
  77. } else {
  78. return parent::render($values);
  79. }
  80. }
  81. /**
  82. * Splits an SQL array of results in a single field
  83. * into a php array
  84. *
  85. * @param $field
  86. * An SQL array (ie: {"",^(.*)$,646,tripal_analysis_blast} )
  87. * @return
  88. * A PHP version of the SQL array (ie: array('','^(.*)$','646','tripal_analysis_blast') )
  89. */
  90. function split_array_agg_results($field) {
  91. if(preg_match('/^{(.*)}$/',$field, $matches)) {
  92. return str_getcsv($matches[1]);
  93. } else {
  94. return array();
  95. }
  96. }
  97. }