views_handler_argument_many_to_one.inc 5.7 KB

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