views_handler_argument_numeric.inc

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

Contains the numeric argument handler.

File

handlers/views_handler_argument_numeric.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the numeric argument handler.
  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. function option_definition() {
  14. $options = parent::option_definition();
  15. $options['break_phrase'] = array('default' => FALSE);
  16. $options['not'] = array('default' => FALSE);
  17. return $options;
  18. }
  19. function options_form(&$form, &$form_state) {
  20. parent::options_form($form, $form_state);
  21. // allow + for or, , for and
  22. $form['break_phrase'] = array(
  23. '#type' => 'checkbox',
  24. '#title' => t('Allow multiple terms per argument.'),
  25. '#description' => t('If selected, users can enter multiple arguments in the form of 1+2+3 or 1,2,3.'),
  26. '#default_value' => !empty($this->options['break_phrase']),
  27. );
  28. $form['not'] = array(
  29. '#type' => 'checkbox',
  30. '#title' => t('Exclude the argument'),
  31. '#description' => t('If selected, the numbers entered in the argument will be excluded rather than limiting the view.'),
  32. '#default_value' => !empty($this->options['not']),
  33. );
  34. }
  35. function title() {
  36. if (!$this->argument) {
  37. return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
  38. }
  39. if (!empty($this->options['break_phrase'])) {
  40. views_break_phrase($this->argument, $this);
  41. }
  42. else {
  43. $this->value = array($this->argument);
  44. $this->operator = 'or';
  45. }
  46. if (empty($this->value)) {
  47. return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
  48. }
  49. if ($this->value === array(-1)) {
  50. return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
  51. }
  52. return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
  53. }
  54. /**
  55. * Override for specific title lookups.
  56. */
  57. function title_query() {
  58. return $this->value;
  59. }
  60. function query() {
  61. $this->ensure_my_table();
  62. if (!empty($this->options['break_phrase'])) {
  63. views_break_phrase($this->argument, $this);
  64. }
  65. else {
  66. $this->value = array($this->argument);
  67. }
  68. $null_check = empty($this->options['not']) ? '' : " OR $this->table_alias.$this->real_field IS NULL";
  69. if (count($this->value) > 1) {
  70. $operator = empty($this->options['not']) ? 'IN' : 'NOT IN';
  71. $placeholders = implode(', ', array_fill(0, sizeof($this->value), '%d'));
  72. $this->query->add_where(0, "$this->table_alias.$this->real_field $operator ($placeholders) $null_check", $this->value);
  73. }
  74. else {
  75. $operator = empty($this->options['not']) ? '=' : '!=';
  76. $this->query->add_where(0, "$this->table_alias.$this->real_field $operator %d $null_check", $this->argument);
  77. }
  78. }
  79. }