views_handler_argument_numeric.inc 3.6 KB

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