Commit e2085055 authored by dawehner's avatar dawehner Committed by tim.plunkett

Issue #1641904 by dawehner: Convert views_join to PSR-0 files.

parent 98f0fddf
......@@ -5,6 +5,8 @@
* Views' relationship handlers.
*/
use Drupal\views\Join;
/**
* @defgroup views_relationship_handlers Views relationship handlers
* @{
......@@ -129,7 +131,7 @@ function query() {
$join = new $def['join_handler'];
}
else {
$join = new views_join();
$join = new Join();
}
$join->definition = $def;
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\views\View;
use Drupal\views\JoinSubquery;
/**
* Relationship handler that allows a groupwise maximum of the linked in table.
......@@ -368,7 +369,7 @@ function query() {
$join = new $def['join_handler'];
}
else {
$join = new views_join_subquery();
$join = new JoinSubquery();
}
$join->definition = $def;
......
......@@ -5,6 +5,8 @@
* Definition of views_handler_sort_menu_hierarchy.
*/
use Drupal\views\Join;
/**
* Sort in menu hierarchy order.
*
......@@ -38,7 +40,7 @@ function query() {
$max_depth = isset($this->definition['max depth']) ? $this->definition['max depth'] : MENU_MAX_DEPTH;
for ($i = 1; $i <= $max_depth; ++$i) {
if ($this->options['sort_within_level']) {
$join = new views_join();
$join = new Join();
$join->construct('menu_links', $this->table_alias, $this->field . $i, 'mlid');
$menu_links = $this->query->add_table('menu_links', NULL, $join);
$this->query->add_orderby($menu_links, 'weight', $this->options['order']);
......
......@@ -132,7 +132,7 @@ <h3>'join': Linking your table to existing base tables</h3>
Quite a few more fields are available in this definition:
<dl>
<dt>handler</dt>
<dd>The name of the handler object to use. Defaults to 'views_join'. You may create custom join handlers that may or may not use any of the data below, as they see fit.</dd>
<dd>The name of the handler object to use. Defaults to 'Drupal\views\Join'. You may create custom join handlers that may or may not use any of the data below, as they see fit.</dd>
<dt>table</dt>
<dd>Table to join. This is optional, and should only be used if the table being referenced is an alias.</dd>
<dt>field</dt>
......
This diff is collapsed.
<?php
/**
* @file
* Definition of Drupal\views\Join
*/
namespace Drupal\views;
/**
* @defgroup views_join_handlers Views join handlers
* @{
* Handlers to tell Views how to join tables together.
*
* Here is how you do complex joins:
*
* @code
* class JoinComplex extends Join {
* // PHP 4 doesn't call constructors of the base class automatically from a
* // constructor of a derived class. It is your responsibility to propagate
* // the call to constructors upstream where appropriate.
* function construct($table, $left_table, $left_field, $field, $extra = array(), $type = 'LEFT') {
* parent::construct($table, $left_table, $left_field, $field, $extra, $type);
* }
*
* function build_join($select_query, $table, $view_query) {
* $this->extra = 'foo.bar = baz.boing';
* parent::build_join($select_query, $table, $view_query);
* }
* }
* @endcode
*/
/**
* A function class to represent a join and create the SQL necessary
* to implement the join.
*
* This is the Delegation pattern. If we had PHP5 exclusively, we would
* declare this an interface.
*
* Extensions of this class can be used to create more interesting joins.
*
* join definition
* - table: table to join (right table)
* - field: field to join on (right field)
* - left_table: The table we join to
* - left_field: The field we join to
* - type: either LEFT (default) or INNER
* - extra: An array of extra conditions on the join. Each condition is
* either a string that's directly added, or an array of items:
* - - table: If not set, current table; if NULL, no table. If you specify a
* table in cached definition, Views will try to load from an existing
* alias. If you use realtime joins, it works better.
* - - field: Field or formula
* in formulas we can reference the right table by using %alias
* @see SelectQueryInterface::addJoin()
* - - operator: defaults to =
* - - value: Must be set. If an array, operator will be defaulted to IN.
* - - numeric: If true, the value will not be surrounded in quotes.
* - - extra type: How all the extras will be combined. Either AND or OR. Defaults to AND.
*/
class Join {
var $table = NULL;
var $left_table = NULL;
var $left_field = NULL;
var $field = NULL;
var $extra = NULL;
var $type = NULL;
var $definition = array();
/**
* Construct the Drupal\views\Join object.
*/
function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT') {
$this->extra_type = 'AND';
if (!empty($table)) {
$this->table = $table;
$this->left_table = $left_table;
$this->left_field = $left_field;
$this->field = $field;
$this->extra = $extra;
$this->type = strtoupper($type);
}
elseif (!empty($this->definition)) {
// if no arguments, construct from definition.
// These four must exist or it will throw notices.
$this->table = $this->definition['table'];
$this->left_table = $this->definition['left_table'];
$this->left_field = $this->definition['left_field'];
$this->field = $this->definition['field'];
if (!empty($this->definition['extra'])) {
$this->extra = $this->definition['extra'];
}
if (!empty($this->definition['extra type'])) {
$this->extra_type = strtoupper($this->definition['extra type']);
}
$this->type = !empty($this->definition['type']) ? strtoupper($this->definition['type']) : 'LEFT';
}
}
/**
* Build the SQL for the join this object represents.
*
* When possible, try to use table alias instead of table names.
*
* @param $select_query
* An implementation of SelectQueryInterface.
* @param $table
* The base table to join.
* @param $view_query
* The source query, implementation of views_plugin_query.
*/
function build_join($select_query, $table, $view_query) {
if (empty($this->definition['table formula'])) {
$right_table = $this->table;
}
else {
$right_table = $this->definition['table formula'];
}
if ($this->left_table) {
$left = $view_query->get_table_info($this->left_table);
$left_field = "$left[alias].$this->left_field";
}
else {
// This can be used if left_field is a formula or something. It should be used only *very* rarely.
$left_field = $this->left_field;
}
$condition = "$left_field = $table[alias].$this->field";
$arguments = array();
// Tack on the extra.
if (isset($this->extra)) {
if (is_array($this->extra)) {
$extras = array();
foreach ($this->extra as $info) {
$extra = '';
// Figure out the table name. Remember, only use aliases provided
// if at all possible.
$join_table = '';
if (!array_key_exists('table', $info)) {
$join_table = $table['alias'] . '.';
}
elseif (isset($info['table'])) {
// If we're aware of a table alias for this table, use the table
// alias instead of the table name.
if (isset($left) && $left['table'] == $info['table']) {
$join_table = $left['alias'] . '.';
}
else {
$join_table = $info['table'] . '.';
}
}
// Convert a single-valued array of values to the single-value case,
// and transform from IN() notation to = notation
if (is_array($info['value']) && count($info['value']) == 1) {
if (empty($info['operator'])) {
$operator = '=';
}
else {
$operator = $info['operator'] == 'NOT IN' ? '!=' : '=';
}
$info['value'] = array_shift($info['value']);
}
if (is_array($info['value'])) {
// With an array of values, we need multiple placeholders and the
// 'IN' operator is implicit.
foreach ($info['value'] as $value) {
$placeholder_i = ':views_join_condition_' . $select_query->nextPlaceholder();
$arguments[$placeholder_i] = $value;
}
$operator = !empty($info['operator']) ? $info['operator'] : 'IN';
$placeholder = '( ' . implode(', ', array_keys($arguments)) . ' )';
}
else {
// With a single value, the '=' operator is implicit.
$operator = !empty($info['operator']) ? $info['operator'] : '=';
$placeholder = ':views_join_condition_' . $select_query->nextPlaceholder();
$arguments[$placeholder] = $info['value'];
}
$extras[] = "$join_table$info[field] $operator $placeholder";
}
if ($extras) {
if (count($extras) == 1) {
$condition .= ' AND ' . array_shift($extras);
}
else {
$condition .= ' AND (' . implode(' ' . $this->extra_type . ' ', $extras) . ')';
}
}
}
elseif ($this->extra && is_string($this->extra)) {
$condition .= " AND ($this->extra)";
}
}
$select_query->addJoin($this->type, $right_table, $table['alias'], $condition, $arguments);
}
}
/**
* @}
*/
<?php
/**
* @file
* Definition of Drupal\views\JoinSubquery.
*/
namespace Drupal\views;
use Drupal\views\Join;
/**
* Join handler for relationships that join with a subquery as the left field.
* eg:
* LEFT JOIN node node_term_data ON ([YOUR SUBQUERY HERE]) = node_term_data.nid
*
* join definition
* same as Join class above, except:
* - left_query: The subquery to use in the left side of the join clause.
*/
class JoinSubquery extends Join {
function construct($table = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $extra = array(), $type = 'LEFT') {
parent::construct($table, $left_table, $left_field, $field, $extra, $type);
$this->left_query = $this->definition['left_query'];
}
/**
* Build the SQL for the join this object represents.
*
* @param $select_query
* An implementation of SelectQueryInterface.
* @param $table
* The base table to join.
* @param $view_query
* The source query, implementation of views_plugin_query.
* @return
*
*/
function build_join($select_query, $table, $view_query) {
if (empty($this->definition['table formula'])) {
$right_table = "{" . $this->table . "}";
}
else {
$right_table = $this->definition['table formula'];
}
// Add our join condition, using a subquery on the left instead of a field.
$condition = "($this->left_query) = $table[alias].$this->field";
$arguments = array();
// Tack on the extra.
// This is just copied verbatim from the parent class, which itself has a bug: http://drupal.org/node/1118100
if (isset($this->extra)) {
if (is_array($this->extra)) {
$extras = array();
foreach ($this->extra as $info) {
$extra = '';
// Figure out the table name. Remember, only use aliases provided
// if at all possible.
$join_table = '';
if (!array_key_exists('table', $info)) {
$join_table = $table['alias'] . '.';
}
elseif (isset($info['table'])) {
$join_table = $info['table'] . '.';
}
$placeholder = ':views_join_condition_' . $select_query->nextPlaceholder();
if (is_array($info['value'])) {
$operator = !empty($info['operator']) ? $info['operator'] : 'IN';
// Transform from IN() notation to = notation if just one value.
if (count($info['value']) == 1) {
$info['value'] = array_shift($info['value']);
$operator = $operator == 'NOT IN' ? '!=' : '=';
}
}
else {
$operator = !empty($info['operator']) ? $info['operator'] : '=';
}
$extras[] = "$join_table$info[field] $operator $placeholder";
$arguments[$placeholder] = $info['value'];
}
if ($extras) {
if (count($extras) == 1) {
$condition .= ' AND ' . array_shift($extras);
}
else {
$condition .= ' AND (' . implode(' ' . $this->extra_type . ' ', $extras) . ')';
}
}
}
elseif ($this->extra && is_string($this->extra)) {
$condition .= " AND ($this->extra)";
}
}
$select_query->addJoin($this->type, $right_table, $table['alias'], $condition, $arguments);
}
}
......@@ -5,6 +5,8 @@
* Definition of views_handler_field_ncs_last_comment_name.
*/
use Drupal\views\Join;
/**
* Field handler to present the name of the last comment poster.
*
......@@ -16,7 +18,7 @@ function query() {
// have to join in a specially related user table.
$this->ensure_my_table();
// join 'users' to this table via vid
$join = new views_join();
$join = new Join();
$join->construct('users', $this->table_alias, 'last_comment_uid', 'uid');
$join->extra = array(array('field' => 'uid', 'operator' => '!=', 'value' => '0'));
......
......@@ -5,6 +5,8 @@
* Definition of views_handler_sort_ncs_last_comment_name.
*/
use Drupal\views\Join;
/**
* Sort handler to sort by last comment name which might be in 2 different
* fields.
......@@ -14,7 +16,7 @@
class views_handler_sort_ncs_last_comment_name extends views_handler_sort {
function query() {
$this->ensure_my_table();
$join = new views_join();
$join = new Join();
$join->construct('users', $this->table_alias, 'last_comment_uid', 'uid');
// @todo this might be safer if we had an ensure_relationship rather than guessing
......
......@@ -5,6 +5,8 @@
* Definition of views_handler_relationship_entity_reverse.
*/
use Drupal\views\Join;
/**
* A relationship handlers which reverse entity references.
*
......@@ -45,7 +47,7 @@ function query() {
$first_join = new $this->definition['join_handler'];
}
else {
$first_join = new views_join();
$first_join = new Join();
}
$first_join->definition = $first;
$first_join->construct();
......@@ -70,7 +72,7 @@ function query() {
$second_join = new $this->definition['join_handler'];
}
else {
$second_join = new views_join();
$second_join = new Join();
}
$second_join->definition = $second;
$second_join->construct();
......
......@@ -36,7 +36,7 @@ function node_views_data() {
// this explains how the 'node' table (named in the line above)
// links toward the node_revision table.
'node_revision' => array(
'handler' => 'views_join', // this is actually optional
'handler' => 'Drupal\views\Join', // this is actually optional
'left_table' => 'node_revision', // Because this is a direct link it could be left out.
'left_field' => 'nid',
'field' => 'nid',
......
......@@ -5,6 +5,8 @@
* Definition of views_handler_argument_search.
*/
use Drupal\views\Join;
/**
* Argument that accepts query keys for search.
*
......@@ -52,7 +54,7 @@ function query($group_by = FALSE) {
$search_condition = db_and();
// Create a new join to relate the 'search_total' table to our current 'search_index' table.
$join = new views_join;
$join = new Join();
$join->construct('search_total', $search_index, 'word', 'word');
$search_total = $this->query->add_relationship('search_total', $join, $search_index);
......
......@@ -5,6 +5,8 @@
* Contains a search filter handler.
*/
use Drupal\views\Join;
/**
* Field handler to provide simple renderer that allows linking to a node.
*
......@@ -127,7 +129,7 @@ function query() {
$search_condition = db_and();
// Create a new join to relate the 'serach_total' table to our current 'search_index' table.
$join = new views_join;
$join = new Join();
$join->construct('search_total', $search_index, 'word', 'word');
$search_total = $this->query->add_relationship('search_total', $join, $search_index);
......
......@@ -5,6 +5,8 @@
* Definition of views_handler_relationship_node_term_data.
*/
use Drupal\views\Join;
/**
* Relationship handler to return the taxonomy terms of nodes.
*
......@@ -81,7 +83,7 @@ function query() {
$def['table formula'] = $query;
}
$join = new views_join();
$join = new Join();
$join->definition = $def;
$join->construct();
......
......@@ -5,6 +5,8 @@
* Definition of views_handler_relationship_translation.
*/
use Drupal\views\Join;
/**
* Handles relationships for content translation sets and provides multiple
* options.
......@@ -88,7 +90,7 @@ function query() {
$join = new $def['join_handler'];
}
else {
$join = new views_join();
$join = new Join();
}
$join->definition = $def;
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Database\Database;
use Drupal\views\Join;
/**
* Object used to create a SELECT query.
......@@ -316,8 +317,8 @@ function options_submit(&$form, &$form_state) {
* @param $alias
* What this relationship will be called, and is also the alias
* for the table.
* @param views_join $join
* A views_join object (or derived object) to join the alias in.
* @param Drupal\views\Join $join
* A Join object (or derived object) to join the alias in.
* @param $base
* The name of the 'base' table this relationship represents; this
* tells the join search which path to attempt to use when finding
......@@ -328,7 +329,7 @@ function options_submit(&$form, &$form_state) {
* might have a relationship to an 'album' node, which might
* have a relationship to an 'artist' node.
*/
function add_relationship($alias, $join, $base, $link_point = NULL) {
function add_relationship($alias, Join $join, $base, $link_point = NULL) {
if (empty($link_point)) {
$link_point = $this->base_table;
}
......@@ -388,7 +389,7 @@ function add_relationship($alias, $join, $base, $link_point = NULL) {
* tables exist and are properly aliased. If set to NULL the path to
* the primary table will be ensured. If the path cannot be made, the
* table will NOT be added.
* @param views_join $join
* @param Drupal\views\Join $join
* In some join configurations this table may actually join back through
* a different method; this is most likely to be used when tracing
* a hierarchy path. (node->parent->parent2->parent3). This parameter
......@@ -402,7 +403,7 @@ function add_relationship($alias, $join, $base, $link_point = NULL) {
* adding parts to the query. Or FALSE if the table was not able to be
* added.
*/
function add_table($table, $relationship = NULL, $join = NULL, $alias = NULL) {
function add_table($table, $relationship = NULL, Join $join = NULL, $alias = NULL) {
if (!$this->ensure_path($table, $relationship, $join)) {
return FALSE;
}
......@@ -427,7 +428,7 @@ function add_table($table, $relationship = NULL, $join = NULL, $alias = NULL) {
* @param $relationship
* The primary table alias this table is related to. If not set, the
* primary table will be used.
* @param views_join $join
* @param Drupal\views\Join $join
* In some join configurations this table may actually join back through
* a different method; this is most likely to be used when tracing
* a hierarchy path. (node->parent->parent2->parent3). This parameter
......@@ -441,7 +442,7 @@ function add_table($table, $relationship = NULL, $join = NULL, $alias = NULL) {
* adding parts to the query. Or FALSE if the table was not able to be
* added.
*/
function queue_table($table, $relationship = NULL, $join = NULL, $alias = NULL) {
function queue_table($table, $relationship = NULL, Join $join = NULL, $alias = NULL) {
// If the alias is set, make sure it doesn't already exist.
if (isset($this->table_queue[$alias])) {
return $alias;
......@@ -539,14 +540,14 @@ function mark_table($table, $relationship, $alias) {
* The relationship to ensure the table links to. Each relationship will
* get a unique instance of the table being added. If not specified,
* will be the primary table.
* @param views_join $join
* A views_join object (or derived object) to join the alias in.
* @param Drupal\views\Join $join
* A Join object (or derived object) to join the alias in.
*
* @return
* The alias used to refer to this specific table, or NULL if the table
* cannot be ensured.
*/
function ensure_table($table, $relationship = NULL, $join = NULL) {
function ensure_table($table, $relationship = NULL, Join $join = NULL) {
// ensure a relationship
if (empty($relationship)) {
$relationship = $this->base_table;
......@@ -727,8 +728,8 @@ function adjust_join($join, $relationship) {
* @param $base_table
* The path we're following to get this join.
*
* @return views_join
* A views_join object or child object, if one exists.
* @return Drupal\views\Join
* A Join object or child object, if one exists.
*/
function get_join_data($table, $base_table) {
// Check to see if we're linking to a known alias. If so, get the real
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment