chado_views_handler_field.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. class chado_views_handler_field extends views_handler_field {
  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. * Splits the aggregated values up for use in rendering
  50. */
  51. function pre_render (&$values) {
  52. if ($this->aggregated) {
  53. foreach($values as $k => $v) {
  54. $values[$k]->{$this->field_alias} = $this->split_array_agg_results($v->{$this->field_alias});
  55. }
  56. }
  57. }
  58. /**
  59. * Render the field.
  60. *
  61. * Note: Checks to see if we have an array or simple field. If we have an array, then
  62. * split it up and render each part using the parent render functionality.
  63. *
  64. * @param $values
  65. * The values retrieved from the database.
  66. */
  67. function render($values) {
  68. // If it's aggregated (an array), then render each part
  69. // using the parent render functionality
  70. if ($this->aggregated) {
  71. $items = array();
  72. $parts = $values->{$this->field_alias};
  73. foreach ($parts as $p) {
  74. $v[ $this->field_alias ] = $p;
  75. $val = (object) $v;
  76. $items[] = parent::render($val);
  77. unset($v, $val);
  78. }
  79. if ($this->options['type'] == 'separator') {
  80. return implode(check_plain($this->options['separator']), $items);
  81. }
  82. else {
  83. return theme('item_list', $items, NULL, $this->options['type']);
  84. }
  85. // Otherwise it is not aggragated
  86. // Just render like the default handler would
  87. } else {
  88. return parent::render($values);
  89. }
  90. }
  91. /**
  92. * Splits an SQL array of results in a single field
  93. * into a php array
  94. *
  95. * @param $field
  96. * An SQL array (ie: {"",^(.*)$,646,tripal_analysis_blast} )
  97. * @return
  98. * A PHP version of the SQL array (ie: array('','^(.*)$','646','tripal_analysis_blast') )
  99. */
  100. function split_array_agg_results($field) {
  101. if(preg_match('/^{(.*)}$/',$field, $matches)) {
  102. return str_getcsv($matches[1]);
  103. } else {
  104. return array();
  105. }
  106. }
  107. }