views_handler_argument_numeric.inc

  1. 3.x handlers/views_handler_argument_numeric.inc
  2. 2.x handlers/views_handler_argument_numeric.inc

Definition of views_handler_argument_numeric.

File

handlers/views_handler_argument_numeric.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_argument_numeric.
  5. */
  6. /**
  7. * Basic argument handler for arguments that are numeric. Incorporates
  8. * break_phrase.
  9. *
  10. * @ingroup views_argument_handlers
  11. */
  12. class views_handler_argument_numeric extends views_handler_argument {
  13. /**
  14. * The operator used for the query: or|and.
  15. * @var string
  16. */
  17. var $operator;
  18. /**
  19. * The actual value which is used for querying.
  20. * @var array
  21. */
  22. var $value;
  23. function option_definition() {
  24. $options = parent::option_definition();
  25. $options['break_phrase'] = array('default' => FALSE, 'bool' => TRUE);
  26. $options['not'] = array('default' => FALSE, 'bool' => TRUE);
  27. return $options;
  28. }
  29. function options_form(&$form, &$form_state) {
  30. parent::options_form($form, $form_state);
  31. // allow + for or, , for and
  32. $form['break_phrase'] = array(
  33. '#type' => 'checkbox',
  34. '#title' => t('Allow multiple values'),
  35. '#description' => t('If selected, users can enter multiple values in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
  36. '#default_value' => !empty($this->options['break_phrase']),
  37. '#fieldset' => 'more',
  38. );
  39. $form['not'] = array(
  40. '#type' => 'checkbox',
  41. '#title' => t('Exclude'),
  42. '#description' => t('If selected, the numbers entered for the filter will be excluded rather than limiting the view.'),
  43. '#default_value' => !empty($this->options['not']),
  44. '#fieldset' => 'more',
  45. );
  46. }
  47. function title() {
  48. if (!$this->argument) {
  49. return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
  50. }
  51. if (!empty($this->options['break_phrase'])) {
  52. views_break_phrase($this->argument, $this);
  53. }
  54. else {
  55. $this->value = array($this->argument);
  56. $this->operator = 'or';
  57. }
  58. if (empty($this->value)) {
  59. return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
  60. }
  61. if ($this->value === array(-1)) {
  62. return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
  63. }
  64. return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
  65. }
  66. /**
  67. * Override for specific title lookups.
  68. * @return array
  69. * Returns all titles, if it's just one title it's an array with one entry.
  70. */
  71. function title_query() {
  72. return $this->value;
  73. }
  74. function query($group_by = FALSE) {
  75. $this->ensure_my_table();
  76. if (!empty($this->options['break_phrase'])) {
  77. views_break_phrase($this->argument, $this);
  78. }
  79. else {
  80. $this->value = array($this->argument);
  81. }
  82. $placeholder = $this->placeholder();
  83. $null_check = empty($this->options['not']) ? '' : "OR $this->table_alias.$this->real_field IS NULL";
  84. if (count($this->value) > 1) {
  85. $operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
  86. $this->query->add_where_expression(0, "$this->table_alias.$this->real_field $operator($placeholder) $null_check", array($placeholder => $this->value));
  87. }
  88. else {
  89. $operator = empty($this->options['not']) ? '=' : '!=';
  90. $this->query->add_where_expression(0, "$this->table_alias.$this->real_field $operator $placeholder $null_check", array($placeholder => $this->argument));
  91. }
  92. }
  93. function get_sort_name() {
  94. return t('Numerical', array(), array('context' => 'Sort order'));
  95. }
  96. }