views_handler_argument_many_to_one.inc

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

File

handlers/views_handler_argument_many_to_one.inc
View source
  1. <?php
  2. /**
  3. * An argument handler for use in fields that have a many to one relationship
  4. * with the table(s) to the left. This adds a bunch of options that are
  5. * reasonably common with this type of relationship.
  6. * Definition terms:
  7. * - numeric: If true, the field will be considered numeric. Probably should
  8. * always be set TRUE as views_handler_argument_string has many to one
  9. * capabilities.
  10. * - zero is null: If true, a 0 will be handled as empty, so for example
  11. * a default argument can be provided or a summary can be shown.
  12. *
  13. * @ingroup views_argument_handlers
  14. */
  15. class views_handler_argument_many_to_one extends views_handler_argument {
  16. function init(&$view, &$options) {
  17. parent::init($view, $options);
  18. $this->helper = new views_many_to_one_helper($this);
  19. // Ensure defaults for these, during summaries and stuff:
  20. $this->operator = 'or';
  21. $this->value = array();
  22. }
  23. function option_definition() {
  24. $options = parent::option_definition();
  25. if (!empty($this->definition['numeric'])) {
  26. $options['break_phrase'] = array('default' => FALSE);
  27. }
  28. $options['add_table'] = array('default' => FALSE);
  29. $options['require_value'] = array('default' => FALSE);
  30. views_many_to_one_helper::option_definition($options);
  31. return $options;
  32. }
  33. function options_form(&$form, &$form_state) {
  34. parent::options_form($form, $form_state);
  35. // allow + for or, , for and
  36. if (!empty($this->definition['numeric'])) {
  37. $form['break_phrase'] = array(
  38. '#type' => 'checkbox',
  39. '#title' => t('Allow multiple terms per argument.'),
  40. '#description' => t('If selected, users can enter multiple arguments in the form of 1+2+3 (for OR) or 1,2,3 (for AND).'),
  41. '#default_value' => !empty($this->options['break_phrase']),
  42. );
  43. }
  44. $form['add_table'] = array(
  45. '#type' => 'checkbox',
  46. '#title' => t('Allow multiple arguments to work together.'),
  47. '#description' => t('If selected, multiple instances of this argument can work together, as though multiple terms were supplied to the same argument. This setting is not compatible with the "Reduce duplicates" setting.'),
  48. '#default_value' => !empty($this->options['add_table']),
  49. );
  50. $form['require_value'] = array(
  51. '#type' => 'checkbox',
  52. '#title' => t('Do not display items with no value in summary'),
  53. '#default_value' => !empty($this->options['require_value']),
  54. );
  55. $this->helper->options_form($form, $form_state);
  56. }
  57. /**
  58. * Override ensure_my_table so we can control how this joins in.
  59. * The operator actually has influence over joining.
  60. */
  61. function ensure_my_table() {
  62. $this->helper->ensure_my_table();
  63. }
  64. function query() {
  65. $empty = FALSE;
  66. if (isset($this->definition['zero is null']) && $this->definition['zero is null']) {
  67. if (empty($this->argument)) {
  68. $empty = TRUE;
  69. }
  70. }
  71. else {
  72. if (!isset($this->argument)) {
  73. $empty = TRUE;
  74. }
  75. }
  76. if ($empty) {
  77. parent::ensure_my_table();
  78. $this->query->add_where(0, "$this->table_alias.$this->real_field IS NULL");
  79. return;
  80. }
  81. if (!empty($this->options['break_phrase'])) {
  82. views_break_phrase($this->argument, $this);
  83. }
  84. else {
  85. $this->value = array($this->argument);
  86. $this->operator = 'or';
  87. }
  88. $this->helper->add_filter();
  89. }
  90. function title() {
  91. if (!$this->argument) {
  92. return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
  93. }
  94. if (!empty($this->options['break_phrase'])) {
  95. views_break_phrase($this->argument, $this);
  96. }
  97. else {
  98. $this->value = array($this->argument);
  99. $this->operator = 'or';
  100. }
  101. // @todo -- both of these should check definition for alternate keywords.
  102. if (empty($this->value)) {
  103. return !empty($this->definition['empty field name']) ? $this->definition['empty field name'] : t('Uncategorized');
  104. }
  105. if ($this->value === array(-1)) {
  106. return !empty($this->definition['invalid input']) ? $this->definition['invalid input'] : t('Invalid input');
  107. }
  108. return implode($this->operator == 'or' ? ' + ' : ', ', $this->title_query());
  109. }
  110. function summary_query() {
  111. $field = $this->table . '.' . $this->field;
  112. $join = $this->get_join();
  113. if (!empty($this->options['require_value'])) {
  114. $join->type = 'INNER';
  115. }
  116. if (empty($this->options['add_table']) || empty($this->view->many_to_one_tables[$field])) {
  117. $this->table_alias = $this->query->ensure_table($this->table, $this->relationship, $join);
  118. }
  119. else {
  120. $this->table_alias = $this->helper->summary_join();
  121. }
  122. // Add the field.
  123. $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
  124. $this->summary_name_field();
  125. return $this->summary_basics();
  126. }
  127. function summary_argument($data) {
  128. $value = $data->{$this->base_alias};
  129. if (empty($value)) {
  130. $value = 0;
  131. }
  132. return $value;
  133. }
  134. /**
  135. * Override for specific title lookups.
  136. */
  137. function title_query() {
  138. return $this->value;
  139. }
  140. }