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>
......
......@@ -7,6 +7,7 @@
use Drupal\Core\Database\Database;
use Drupal\views\View;
use Drupal\views\Join;
/**
* Instantiate and construct a new handler
......@@ -101,7 +102,7 @@ function views_get_table_join($table, $base_table) {
$handler = new $h['handler'];
}
else {
$handler = new views_join();
$handler = new Join();
}
// Fill in some easy defaults
......@@ -1396,297 +1397,3 @@ function views_date_sql_extract($extract_type, $field, $field_type = 'int', $set
/**
* @}
*/
/**
* @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 views_join_complex extends views_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 views_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 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);
}
}
/**
* 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 views_join class above, except:
* - left_query: The subquery to use in the left side of the join clause.
*/
class views_join_subquery extends views_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);
}
}
/**
* @}
*/
<?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);