views_handler_relationship.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_relationship.
  5. */
  6. /**
  7. * @defgroup views_relationship_handlers Views relationship handlers
  8. * @{
  9. * Handlers to tell Views how to create alternate relationships.
  10. */
  11. /**
  12. * Relationship handler, allows a new version of the primary table to be linked.
  13. *
  14. * The base relationship handler can only handle a single join. Some
  15. * relationships are more complex and might require chains of joins; for those,
  16. * you must use a custom relationship handler.
  17. *
  18. * Definition items:
  19. * - base: The new base table this relationship will be adding. This does not
  20. * have to be a declared base table, but if there are no tables that
  21. * utilize this base table, it won't be very effective.
  22. * - base field: The field to use in the relationship; if left out this will be
  23. * assumed to be the primary field.
  24. * - relationship table: The actual table this relationship operates against.
  25. * This is analogous to using a 'table' override.
  26. * - relationship field: The actual field this relationship operates against.
  27. * This is analogous to using a 'real field' override.
  28. * - label: The default label to provide for this relationship, which is
  29. * shown in parentheses next to any field/sort/filter/argument that uses
  30. * the relationship.
  31. *
  32. * @ingroup views_relationship_handlers
  33. */
  34. class views_handler_relationship extends views_handler {
  35. /**
  36. * Let relationships live on tables other than the table they operate on.
  37. */
  38. public function init(&$view, &$options) {
  39. parent::init($view, $options);
  40. if (isset($this->definition['relationship table'])) {
  41. $this->table = $this->definition['relationship table'];
  42. }
  43. if (isset($this->definition['relationship field'])) {
  44. // Set both real_field and field so custom handler can rely on the old
  45. // field value.
  46. $this->real_field = $this->field = $this->definition['relationship field'];
  47. }
  48. }
  49. /**
  50. * Get this field's label.
  51. */
  52. public function label() {
  53. if (!isset($this->options['label'])) {
  54. return $this->ui_name();
  55. }
  56. return $this->options['label'];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function option_definition() {
  62. $options = parent::option_definition();
  63. // Relationships definitions should define a default label, but if they
  64. // aren't get another default value.
  65. if (!empty($this->definition['label'])) {
  66. $label = $this->definition['label'];
  67. }
  68. else {
  69. $label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
  70. }
  71. $options['label'] = array('default' => $label, 'translatable' => TRUE);
  72. $options['required'] = array('default' => FALSE, 'bool' => TRUE);
  73. return $options;
  74. }
  75. /**
  76. * Provide the label widget that all fields should have.
  77. */
  78. public function options_form(&$form, &$form_state) {
  79. parent::options_form($form, $form_state);
  80. $form['label'] = array(
  81. '#type' => 'textfield',
  82. '#title' => t('Identifier'),
  83. '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
  84. '#description' => t('Edit the administrative label displayed when referencing this relationship from filters, etc.'),
  85. '#required' => TRUE,
  86. );
  87. $form['required'] = array(
  88. '#type' => 'checkbox',
  89. '#title' => t('Require this relationship'),
  90. '#description' => t('Enable to hide items that do not contain this relationship'),
  91. '#default_value' => !empty($this->options['required']),
  92. );
  93. }
  94. /**
  95. * Called to implement a relationship in a query.
  96. */
  97. public function query() {
  98. // Figure out what base table this relationship brings to the party.
  99. $table_data = views_fetch_data($this->definition['base']);
  100. $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
  101. $this->ensure_my_table();
  102. $def = $this->definition;
  103. $def['table'] = $this->definition['base'];
  104. $def['field'] = $base_field;
  105. $def['left_table'] = $this->table_alias;
  106. $def['left_field'] = $this->real_field;
  107. if (!empty($this->options['required'])) {
  108. $def['type'] = 'INNER';
  109. }
  110. if (!empty($this->definition['extra'])) {
  111. $def['extra'] = $this->definition['extra'];
  112. }
  113. if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
  114. $join = new $def['join_handler']();
  115. }
  116. else {
  117. $join = new views_join();
  118. }
  119. $join->definition = $def;
  120. $join->options = $this->options;
  121. $join->construct();
  122. $join->adjusted = TRUE;
  123. // Use a short alias for this.
  124. $alias = $def['table'] . '_' . $this->table;
  125. $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
  126. // Add access tags if the base table provide it.
  127. if (empty($this->query->options['disable_sql_rewrite']) && isset($table_data['table']['base']['access query tag'])) {
  128. $access_tag = $table_data['table']['base']['access query tag'];
  129. $this->query->add_tag($access_tag);
  130. }
  131. }
  132. /**
  133. * You can't groupby a relationship.
  134. */
  135. public function use_group_by() {
  136. return FALSE;
  137. }
  138. }
  139. /**
  140. * A special handler to take the place of missing or broken handlers.
  141. *
  142. * @ingroup views_relationship_handlers
  143. */
  144. class views_handler_relationship_broken extends views_handler_relationship {
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function ui_name($short = FALSE) {
  149. return t('Broken/missing handler');
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function ensure_my_table() {
  155. // No table to ensure!
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function query() {
  161. // No query to run.
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function options_form(&$form, &$form_state) {
  167. $form['markup'] = array(
  168. '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
  169. );
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function broken() {
  175. return TRUE;
  176. }
  177. }
  178. /**
  179. * @}
  180. */