ManyToOneHelper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace Drupal\views;
  3. use Drupal\Core\Database\Query\Condition;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\views\Plugin\views\HandlerBase;
  6. /**
  7. * This many to one helper object is used on both arguments and filters.
  8. *
  9. * @todo This requires extensive documentation on how this class is to
  10. * be used. For now, look at the arguments and filters that use it. Lots
  11. * of stuff is just pass-through but there are definitely some interesting
  12. * areas where they interact.
  13. *
  14. * Any handler that uses this can have the following possibly additional
  15. * definition terms:
  16. * - numeric: If true, treat this field as numeric, using %d instead of %s in
  17. * queries.
  18. */
  19. class ManyToOneHelper {
  20. public function __construct($handler) {
  21. $this->handler = $handler;
  22. }
  23. public static function defineOptions(&$options) {
  24. $options['reduce_duplicates'] = ['default' => FALSE];
  25. }
  26. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  27. $form['reduce_duplicates'] = [
  28. '#type' => 'checkbox',
  29. '#title' => t('Reduce duplicates'),
  30. '#description' => t("This filter can cause items that have more than one of the selected options to appear as duplicate results. If this filter causes duplicate results to occur, this checkbox can reduce those duplicates; however, the more terms it has to search for, the less performant the query will be, so use this with caution. Shouldn't be set on single-value fields, as it may cause values to disappear from display, if used on an incompatible field."),
  31. '#default_value' => !empty($this->handler->options['reduce_duplicates']),
  32. '#weight' => 4,
  33. ];
  34. }
  35. /**
  36. * Sometimes the handler might want us to use some kind of formula, so give
  37. * it that option. If it wants us to do this, it must set $helper->formula = TRUE
  38. * and implement handler->getFormula();
  39. */
  40. public function getField() {
  41. if (!empty($this->formula)) {
  42. return $this->handler->getFormula();
  43. }
  44. else {
  45. return $this->handler->tableAlias . '.' . $this->handler->realField;
  46. }
  47. }
  48. /**
  49. * Add a table to the query.
  50. *
  51. * This is an advanced concept; not only does it add a new instance of the table,
  52. * but it follows the relationship path all the way down to the relationship
  53. * link point and adds *that* as a new relationship and then adds the table to
  54. * the relationship, if necessary.
  55. */
  56. public function addTable($join = NULL, $alias = NULL) {
  57. // This is used for lookups in the many_to_one table.
  58. $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
  59. if (empty($join)) {
  60. $join = $this->getJoin();
  61. }
  62. // See if there's a chain between us and the base relationship. If so, we need
  63. // to create a new relationship to use.
  64. $relationship = $this->handler->relationship;
  65. // Determine the primary table to seek
  66. if (empty($this->handler->query->relationships[$relationship])) {
  67. $base_table = $this->handler->view->storage->get('base_table');
  68. }
  69. else {
  70. $base_table = $this->handler->query->relationships[$relationship]['base'];
  71. }
  72. // Cycle through the joins. This isn't as error-safe as the normal
  73. // ensurePath logic. Perhaps it should be.
  74. $r_join = clone $join;
  75. while ($r_join->leftTable != $base_table) {
  76. $r_join = HandlerBase::getTableJoin($r_join->leftTable, $base_table);
  77. }
  78. // If we found that there are tables in between, add the relationship.
  79. if ($r_join->table != $join->table) {
  80. $relationship = $this->handler->query->addRelationship($this->handler->table . '_' . $r_join->table, $r_join, $r_join->table, $this->handler->relationship);
  81. }
  82. // And now add our table, using the new relationship if one was used.
  83. $alias = $this->handler->query->addTable($this->handler->table, $relationship, $join, $alias);
  84. // Store what values are used by this table chain so that other chains can
  85. // automatically discard those values.
  86. if (empty($this->handler->view->many_to_one_tables[$field])) {
  87. $this->handler->view->many_to_one_tables[$field] = $this->handler->value;
  88. }
  89. else {
  90. $this->handler->view->many_to_one_tables[$field] = array_merge($this->handler->view->many_to_one_tables[$field], $this->handler->value);
  91. }
  92. return $alias;
  93. }
  94. public function getJoin() {
  95. return $this->handler->getJoin();
  96. }
  97. /**
  98. * Provide the proper join for summary queries. This is important in part because
  99. * it will cooperate with other arguments if possible.
  100. */
  101. public function summaryJoin() {
  102. $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
  103. $join = $this->getJoin();
  104. // shortcuts
  105. $options = $this->handler->options;
  106. $view = $this->handler->view;
  107. $query = $this->handler->query;
  108. if (!empty($options['require_value'])) {
  109. $join->type = 'INNER';
  110. }
  111. if (empty($options['add_table']) || empty($view->many_to_one_tables[$field])) {
  112. return $query->ensureTable($this->handler->table, $this->handler->relationship, $join);
  113. }
  114. else {
  115. if (!empty($view->many_to_one_tables[$field])) {
  116. foreach ($view->many_to_one_tables[$field] as $value) {
  117. $join->extra = [
  118. [
  119. 'field' => $this->handler->realField,
  120. 'operator' => '!=',
  121. 'value' => $value,
  122. 'numeric' => !empty($this->definition['numeric']),
  123. ],
  124. ];
  125. }
  126. }
  127. return $this->addTable($join);
  128. }
  129. }
  130. /**
  131. * Override ensureMyTable so we can control how this joins in.
  132. * The operator actually has influence over joining.
  133. */
  134. public function ensureMyTable() {
  135. if (!isset($this->handler->tableAlias)) {
  136. // Case 1: Operator is an 'or' and we're not reducing duplicates.
  137. // We hence get the absolute simplest:
  138. $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
  139. if ($this->handler->operator == 'or' && empty($this->handler->options['reduce_duplicates'])) {
  140. if (empty($this->handler->options['add_table']) && empty($this->handler->view->many_to_one_tables[$field])) {
  141. // query optimization, INNER joins are slightly faster, so use them
  142. // when we know we can.
  143. $join = $this->getJoin();
  144. if (isset($join)) {
  145. $join->type = 'INNER';
  146. }
  147. $this->handler->tableAlias = $this->handler->query->ensureTable($this->handler->table, $this->handler->relationship, $join);
  148. $this->handler->view->many_to_one_tables[$field] = $this->handler->value;
  149. }
  150. else {
  151. $join = $this->getJoin();
  152. $join->type = 'LEFT';
  153. if (!empty($this->handler->view->many_to_one_tables[$field])) {
  154. foreach ($this->handler->view->many_to_one_tables[$field] as $value) {
  155. $join->extra = [
  156. [
  157. 'field' => $this->handler->realField,
  158. 'operator' => '!=',
  159. 'value' => $value,
  160. 'numeric' => !empty($this->handler->definition['numeric']),
  161. ],
  162. ];
  163. }
  164. }
  165. $this->handler->tableAlias = $this->addTable($join);
  166. }
  167. return $this->handler->tableAlias;
  168. }
  169. // Case 2: it's an 'and' or an 'or'.
  170. // We do one join per selected value.
  171. if ($this->handler->operator != 'not') {
  172. // Clone the join for each table:
  173. $this->handler->tableAliases = [];
  174. foreach ($this->handler->value as $value) {
  175. $join = $this->getJoin();
  176. if ($this->handler->operator == 'and') {
  177. $join->type = 'INNER';
  178. }
  179. $join->extra = [
  180. [
  181. 'field' => $this->handler->realField,
  182. 'value' => $value,
  183. 'numeric' => !empty($this->handler->definition['numeric']),
  184. ],
  185. ];
  186. // The table alias needs to be unique to this value across the
  187. // multiple times the filter or argument is called by the view.
  188. if (!isset($this->handler->view->many_to_one_aliases[$field][$value])) {
  189. if (!isset($this->handler->view->many_to_one_count[$this->handler->table])) {
  190. $this->handler->view->many_to_one_count[$this->handler->table] = 0;
  191. }
  192. $this->handler->view->many_to_one_aliases[$field][$value] = $this->handler->table . '_value_' . ($this->handler->view->many_to_one_count[$this->handler->table]++);
  193. }
  194. $this->handler->tableAliases[$value] = $this->addTable($join, $this->handler->view->many_to_one_aliases[$field][$value]);
  195. // Set tableAlias to the first of these.
  196. if (empty($this->handler->tableAlias)) {
  197. $this->handler->tableAlias = $this->handler->tableAliases[$value];
  198. }
  199. }
  200. }
  201. // Case 3: it's a 'not'.
  202. // We just do one join. We'll add a where clause during
  203. // the query phase to ensure that $table.$field IS NULL.
  204. else {
  205. $join = $this->getJoin();
  206. $join->type = 'LEFT';
  207. $join->extra = [];
  208. $join->extraOperator = 'OR';
  209. foreach ($this->handler->value as $value) {
  210. $join->extra[] = [
  211. 'field' => $this->handler->realField,
  212. 'value' => $value,
  213. 'numeric' => !empty($this->handler->definition['numeric']),
  214. ];
  215. }
  216. $this->handler->tableAlias = $this->addTable($join);
  217. }
  218. }
  219. return $this->handler->tableAlias;
  220. }
  221. /**
  222. * Provides a unique placeholders for handlers.
  223. */
  224. protected function placeholder() {
  225. return $this->handler->query->placeholder($this->handler->options['table'] . '_' . $this->handler->options['field']);
  226. }
  227. public function addFilter() {
  228. if (empty($this->handler->value)) {
  229. return;
  230. }
  231. $this->handler->ensureMyTable();
  232. // Shorten some variables:
  233. $field = $this->getField();
  234. $options = $this->handler->options;
  235. $operator = $this->handler->operator;
  236. $formula = !empty($this->formula);
  237. $value = $this->handler->value;
  238. if (empty($options['group'])) {
  239. $options['group'] = 0;
  240. }
  241. // If $add_condition is set to FALSE, a single expression is enough. If it
  242. // is set to TRUE, conditions will be added.
  243. $add_condition = TRUE;
  244. if ($operator == 'not') {
  245. $value = NULL;
  246. $operator = 'IS NULL';
  247. $add_condition = FALSE;
  248. }
  249. elseif ($operator == 'or' && empty($options['reduce_duplicates'])) {
  250. if (count($value) > 1) {
  251. $operator = 'IN';
  252. }
  253. else {
  254. $value = is_array($value) ? array_pop($value) : $value;
  255. $operator = '=';
  256. }
  257. $add_condition = FALSE;
  258. }
  259. if (!$add_condition) {
  260. if ($formula) {
  261. $placeholder = $this->placeholder();
  262. if ($operator == 'IN') {
  263. $operator = "$operator IN($placeholder)";
  264. }
  265. else {
  266. $operator = "$operator $placeholder";
  267. }
  268. $placeholders = [
  269. $placeholder => $value,
  270. ];
  271. $this->handler->query->addWhereExpression($options['group'], "$field $operator", $placeholders);
  272. }
  273. else {
  274. $placeholder = $this->placeholder();
  275. if (count($this->handler->value) > 1) {
  276. $placeholder .= '[]';
  277. if ($operator == 'IS NULL') {
  278. $this->handler->query->addWhereExpression(0, "$field $operator");
  279. }
  280. else {
  281. $this->handler->query->addWhereExpression(0, "$field $operator($placeholder)", [$placeholder => $value]);
  282. }
  283. }
  284. else {
  285. if ($operator == 'IS NULL') {
  286. $this->handler->query->addWhereExpression(0, "$field $operator");
  287. }
  288. else {
  289. $this->handler->query->addWhereExpression(0, "$field $operator $placeholder", [$placeholder => $value]);
  290. }
  291. }
  292. }
  293. }
  294. if ($add_condition) {
  295. $field = $this->handler->realField;
  296. $clause = $operator == 'or' ? new Condition('OR') : new Condition('AND');
  297. foreach ($this->handler->tableAliases as $value => $alias) {
  298. $clause->condition("$alias.$field", $value);
  299. }
  300. // implode on either AND or OR.
  301. $this->handler->query->addWhere($options['group'], $clause);
  302. }
  303. }
  304. }