webform_handler_numeric_data.inc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @file
  4. * Definition of handlers for using numeric submission data.
  5. */
  6. /**
  7. * Extended version of the numeric field handler specialized for Webform values.
  8. *
  9. * @ingroup views_field_handlers
  10. */
  11. class webform_handler_field_numeric_data extends views_handler_field_numeric {
  12. public $formula = NULL;
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function construct() {
  17. parent::construct();
  18. $this->formula = TRUE;
  19. }
  20. /**
  21. * Get the formula for this argument.
  22. *
  23. * $this->ensure_my_table() MUST have been called prior to this.
  24. */
  25. public function get_formula() {
  26. return ("(0.0 + $this->table_alias.$this->real_field)");
  27. }
  28. /**
  29. * Called to add the field to a query.
  30. */
  31. public function query() {
  32. $this->ensure_my_table();
  33. // Add the field.
  34. $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
  35. $this->field_alias = $this->query->add_field(NULL, $this->get_formula(), $this->table_alias . '_' . $this->field, $params);
  36. $this->add_additional_fields();
  37. }
  38. /**
  39. * Shortcut to get a handler's raw field value.
  40. *
  41. * This should be overridden for handlers with formulae or other
  42. * non-standard fields. Because this takes an argument, fields
  43. * overriding this can just call return parent::get_field($formula)
  44. */
  45. public function get_field($field = NULL) {
  46. return parent::get_field($this->get_formula());
  47. }
  48. }
  49. /**
  50. * Numeric filter handler that works with Webform numeric submission data.
  51. *
  52. * @ingroup views_filter_handlers
  53. */
  54. class webform_handler_filter_numeric_data extends views_handler_filter_numeric {
  55. /**
  56. * Get the formula for this argument.
  57. *
  58. * $this->ensure_my_table() MUST have been called prior to this.
  59. */
  60. public function get_formula() {
  61. return ("(0.0 + $this->table_alias.$this->real_field)");
  62. }
  63. /**
  64. * Called to add the filter to a query.
  65. */
  66. public function query() {
  67. $this->ensure_my_table();
  68. $info = $this->operators();
  69. if (!empty($info[$this->operator]['method'])) {
  70. $this->{$info[$this->operator]['method']}($this->get_formula());
  71. }
  72. }
  73. /**
  74. * Adds a simple operator condition to the query.
  75. */
  76. public function op_simple($field) {
  77. static $sequence = 1;
  78. $param = ":value" . $sequence++;
  79. $this->query->add_where_expression($this->options['group'],
  80. $field . $this->operator . $param,
  81. array($param => $this->value['value']));
  82. }
  83. /**
  84. * Adds a between or not-between condition to the query.
  85. */
  86. public function op_between($field) {
  87. static $sequence = 1;
  88. $min = ":min" . $sequence;
  89. $max = ":max" . $sequence++;
  90. if ($this->operator == 'between') {
  91. $this->query->add_where_expression($this->options['group'],
  92. "($min <= $field AND $field <= $max)",
  93. array($min => $this->value['min'], $max => $this->value['max']));
  94. }
  95. else {
  96. $this->query->add_where_expression($this->options['group'],
  97. "($min > $field OR $field > $max)",
  98. array($min => $this->value['min'], $max => $this->value['max']));
  99. }
  100. }
  101. /**
  102. * Adds an empty or not-empty condition to the query.
  103. */
  104. public function op_empty($field) {
  105. if ($this->operator == 'empty') {
  106. $operator = "IS NULL";
  107. }
  108. else {
  109. $operator = "IS NOT NULL";
  110. }
  111. $this->query->add_where_expression($this->options['group'],
  112. "$field $operator");
  113. }
  114. /**
  115. * Adds a regular expression condition to the query.
  116. */
  117. public function op_regex($field) {
  118. static $sequence = 1;
  119. $param = ":expression" . $sequence++;
  120. $this->query->add_where_expression($this->options['group'],
  121. "$field RLIKE $param",
  122. array($param => $this->value['value']));
  123. }
  124. }
  125. /**
  126. * Sort handler that works with Webform numeric submission data.
  127. *
  128. * @ingroup views_sort_handlers
  129. */
  130. class webform_handler_sort_numeric_data extends views_handler_sort {
  131. /**
  132. * Get the formula for this sort.
  133. *
  134. * $this->ensure_my_table() MUST have been called prior to this.
  135. */
  136. public function get_formula() {
  137. return ("(0.0 + $this->table_alias.$this->real_field)");
  138. }
  139. /**
  140. * Called to add the sort to a query.
  141. */
  142. public function query() {
  143. $this->ensure_my_table();
  144. // Add the field.
  145. $alias = $this->query->add_field(NULL, $this->get_formula(), $this->table_alias . '_' . $this->field . '_sort');
  146. // Add the sort for the field using only the alias.
  147. $this->query->add_orderby(NULL, NULL, $this->options['order'], $alias);
  148. }
  149. }