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

Definition of views_handler_argument_many_to_one.

File

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