- <?php
- * @file
- * Defines the various handler objects to help build and display views.
- */
-
- * Base handler, from which all the other handlers are derived.
- * It creates a common interface to create consistency amongst
- * handlers and data.
- *
- * This class would be abstract in PHP5, but PHP4 doesn't understand that.
- *
- * Definition terms:
- * - table: The actual table this uses; only specify if different from
- * the table this is attached to.
- * - real field: The actual field this uses; only specify if different from
- * the field this item is attached to.
- * - group: A text string representing the 'group' this item is attached to,
- * for display in the UI. Examples: "Node", "Taxonomy", "Comment",
- * "User", etc. This may be inherited from the parent definition or
- * the 'table' definition.
- * - title: The title for this handler in the UI. This may be inherited from
- * the parent definition or the 'table' definition.
- * - help: A more informative string to give to the user to explain what this
- * field/handler is or does.
- * - access callback: If this field should have access control, this could
- * be a function to use. 'user_access' is a common
- * function to use here. If not specified, no access
- * control is provided.
- * - access arguments: An array of arguments for the access callback.
- */
- class views_handler extends views_object {
-
- * The top object of a view.
- *
- * @var view
- */
- var $view = NULL;
-
-
- * Where the $query object will reside:
- *
- * @var views_plugin_query_default
- */
- var $query = NULL;
-
-
- * The type of the handler, for example filter/footer/field.
- */
- var $handler_type = NULL;
-
-
- * The Views name of the table on which this handler effects.
- */
- public $table;
-
-
- * The alias of the table of this handler which is used in the query.
- */
- public $table_alias;
-
-
- * The named field for this handler.
- */
- var $field;
-
-
- * The actual field in the database table, maybe different
- * on other kind of query plugins/special handlers.
- */
- var $real_field;
-
-
- * The relationship used for this field.
- */
- var $relationship = NULL;
-
-
- * init the handler with necessary data.
- * @param $view
- * The $view object this handler is attached to.
- * @param $options
- * The item from the database; the actual contents of this will vary
- * based upon the type of handler.
- */
- function init(&$view, &$options) {
- $this->view = &$view;
- $display_id = $this->view->current_display;
-
-
-
-
-
- if (isset($this->actual_table)) {
- $options['table'] = $this->actual_table;
- }
-
- if (isset($this->actual_field)) {
- $options['field'] = $this->actual_field;
- }
-
- $types = views_object_types();
- $plural = $this->handler_type;
- if (isset($types[$this->handler_type]['plural'])) {
- $plural = $types[$this->handler_type]['plural'];
- }
- if ($this->view->display_handler->is_defaulted($plural)) {
- $display_id = 'default';
- }
-
- $this->localization_keys = array(
- $display_id,
- $this->handler_type,
- $options['table'],
- $options['id']
- );
-
- $this->unpack_options($this->options, $options);
-
-
- if (isset($options['table'])) {
- $this->table = $options['table'];
- }
-
- if (isset($this->definition['real field'])) {
- $this->real_field = $this->definition['real field'];
- }
-
- if (isset($this->definition['field'])) {
- $this->real_field = $this->definition['field'];
- }
-
- if (isset($options['field'])) {
- $this->field = $options['field'];
- if (!isset($this->real_field)) {
- $this->real_field = $options['field'];
- }
- }
-
- $this->query = &$view->query;
- }
-
- function option_definition() {
- $options = parent::option_definition();
-
- $options['id'] = array('default' => '');
- $options['table'] = array('default' => '');
- $options['field'] = array('default' => '');
- $options['relationship'] = array('default' => 'none');
- $options['group_type'] = array('default' => 'group');
- $options['ui_name'] = array('default' => '');
-
- return $options;
- }
-
-
- * Return a string representing this handler's name in the UI.
- */
- function ui_name($short = FALSE) {
- if (!empty($this->options['ui_name'])) {
- $title = check_plain($this->options['ui_name']);
- return $title;
- }
- $title = ($short && isset($this->definition['title short'])) ? $this->definition['title short'] : $this->definition['title'];
- return t('!group: !title', array('!group' => $this->definition['group'], '!title' => $title));
- }
-
-
- * Shortcut to get a handler's raw field value.
- *
- * This should be overridden for handlers with formulae or other
- * non-standard fields. Because this takes an argument, fields
- * overriding this can just call return parent::get_field($formula)
- */
- function get_field($field = NULL) {
- if (!isset($field)) {
- if (!empty($this->formula)) {
- $field = $this->get_formula();
- }
- else {
- $field = $this->table_alias . '.' . $this->real_field;
- }
- }
-
-
- if ($this->view->display_handler->use_group_by()) {
- $this->view->init_query();
- if ($this->query) {
- $info = $this->query->get_aggregation_info();
- if (!empty($info[$this->options['group_type']]['method']) && function_exists($info[$this->options['group_type']]['method'])) {
- return $info[$this->options['group_type']]['method']($this->options['group_type'], $field);
- }
- }
- }
-
- return $field;
- }
-
-
- * Sanitize the value for output.
- *
- * @param $value
- * The value being rendered.
- * @param $type
- * The type of sanitization needed. If not provided, check_plain() is used.
- *
- * @return string
- * Returns the safe value.
- */
- function sanitize_value($value, $type = NULL) {
- switch ($type) {
- case 'xss':
- $value = filter_xss($value);
- break;
- case 'xss_admin':
- $value = filter_xss_admin($value);
- break;
- case 'url':
- $value = check_url($value);
- break;
- default:
- $value = check_plain($value);
- break;
- }
- return $value;
- }
-
-
- * Transform a string by a certain method.
- *
- * @param $string
- * The input you want to transform.
- * @param $option
- * How do you want to transform it, possible values:
- * - upper: Uppercase the string.
- * - lower: lowercase the string.
- * - ucfirst: Make the first char uppercase.
- * - ucwords: Make each word in the string uppercase.
- *
- * @return string
- * The transformed string.
- */
- function case_transform($string, $option) {
- global $multibyte;
-
- switch ($option) {
- default:
- return $string;
- case 'upper':
- return backdrop_strtoupper($string);
- case 'lower':
- return backdrop_strtolower($string);
- case 'ucfirst':
- return backdrop_strtoupper(backdrop_substr($string, 0, 1)) . backdrop_substr($string, 1);
- case 'ucwords':
- if ($multibyte == UNICODE_MULTIBYTE) {
- return mb_convert_case($string, MB_CASE_TITLE);
- }
- else {
- return ucwords($string);
- }
- }
- }
-
-
- * Validate the options form.
- */
- function options_validate(&$form, &$form_state) { }
-
-
- * Build the options form.
- */
- function options_form(&$form, &$form_state) {
-
-
-
-
- $form['#pre_render'][] = 'views_ui_pre_render_add_fieldset_markup';
-
- $form['ui_name'] = array(
- '#type' => 'textfield',
- '#title' => t('Administrative title'),
- '#description' => t('This title will be displayed on the views edit page instead of the default one. This might be useful if you have the same item twice.'),
- '#default_value' => $this->options['ui_name'],
- '#fieldset' => 'more',
- );
-
-
-
- $form['more'] = array(
- '#type' => 'fieldset',
- '#title' => t('More'),
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#weight' => 150,
- );
-
- backdrop_alter('views_handler_options', $this->options, $view);
- }
-
-
- * Perform any necessary changes to the form values prior to storage.
- * There is no need for this function to actually store the data.
- */
- function options_submit(&$form, &$form_state) { }
-
-
- * Provides the handler some groupby.
- */
- function use_group_by() {
- return TRUE;
- }
-
- * Provide a form for aggregation settings.
- */
- function groupby_form(&$form, &$form_state) {
- $view = &$form_state['view'];
- $display_id = $form_state['display_id'];
- $types = views_object_types();
- $type = $form_state['type'];
- $id = $form_state['id'];
-
- $form['#title'] = check_plain($view->display[$display_id]->display_title) . ': ';
- $form['#title'] .= t('Configure aggregation settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $this->ui_name()));
-
- $form['#section'] = $display_id . '-' . $type . '-' . $id;
-
- $view->init_query();
- $info = $view->query->get_aggregation_info();
- foreach ($info as $id => $aggregate) {
- $group_types[$id] = $aggregate['title'];
- }
-
- $form['group_type'] = array(
- '#type' => 'select',
- '#title' => t('Aggregation type'),
- '#default_value' => $this->options['group_type'],
- '#description' => t('Select the aggregation function to use on this field.'),
- '#options' => $group_types,
- );
- }
-
-
- * Perform any necessary changes to the form values prior to storage.
- * There is no need for this function to actually store the data.
- */
- function groupby_form_submit(&$form, &$form_state) {
- $item =& $form_state['handler']->options;
-
- $item['group_type'] = $form_state['values']['options']['group_type'];
- }
-
-
- * If a handler has 'extra options' it will get a little settings widget and
- * another form called extra_options.
- */
- function has_extra_options() { return FALSE; }
-
-
- * Provide defaults for the handler.
- */
- function extra_options(&$option) { }
-
-
- * Provide a form for setting options.
- */
- function extra_options_form(&$form, &$form_state) { }
-
-
- * Validate the options form.
- */
- function extra_options_validate($form, &$form_state) { }
-
-
- * Perform any necessary changes to the form values prior to storage.
- * There is no need for this function to actually store the data.
- */
- function extra_options_submit($form, &$form_state) { }
-
-
- * Determine if a handler can be exposed.
- */
- function can_expose() { return FALSE; }
-
-
- * Set new exposed option defaults when exposed setting is flipped
- * on.
- */
- function expose_options() { }
-
-
- * Get information about the exposed form for the form renderer.
- */
- function exposed_info() { }
-
-
- * Render our chunk of the exposed handler form when selecting
- */
- function exposed_form(&$form, &$form_state) { }
-
-
- * Validate the exposed handler form
- */
- function exposed_validate(&$form, &$form_state) { }
-
-
- * Submit the exposed handler form
- */
- function exposed_submit(&$form, &$form_state) { }
-
-
- * Form for exposed handler options.
- */
- function expose_form(&$form, &$form_state) { }
-
-
- * Validate the options form.
- */
- function expose_validate($form, &$form_state) { }
-
-
- * Perform any necessary changes to the form exposes prior to storage.
- * There is no need for this function to actually store the data.
- */
- function expose_submit($form, &$form_state) { }
-
-
- * Shortcut to display the expose/hide button.
- */
- function show_expose_button(&$form, &$form_state) { }
-
-
- * Shortcut to display the exposed options form.
- */
- function show_expose_form(&$form, &$form_state) {
- if (empty($this->options['exposed'])) {
- return;
- }
-
- $this->expose_form($form, $form_state);
-
-
-
-
-
- if (!empty($form_state['force_expose_options'])) {
- foreach (element_children($form['expose']) as $id) {
- if (isset($form['expose'][$id]['#default_value']) && !isset($form['expose'][$id]['#value'])) {
- $form['expose'][$id]['#value'] = $form['expose'][$id]['#default_value'];
- }
- }
- }
- }
-
-
- * Check whether current user has access to this handler.
- *
- * @return boolean
- */
- function access() {
- if (isset($this->definition['access callback']) && function_exists($this->definition['access callback'])) {
- if (isset($this->definition['access arguments']) && is_array($this->definition['access arguments'])) {
- return call_user_func_array($this->definition['access callback'], $this->definition['access arguments']);
- }
- return $this->definition['access callback']();
- }
-
- return TRUE;
- }
-
-
- * Run before the view is built.
- *
- * This gives all the handlers some time to set up before any handler has
- * been fully run.
- */
- function pre_query() { }
-
-
- * Run after the view is executed, before the result is cached.
- *
- * This gives all the handlers some time to modify values. This is primarily
- * used so that handlers that pull up secondary data can put it in the
- * $values so that the raw data can be utilized externally.
- */
- function post_execute(&$values) { }
-
-
- * Provides a unique placeholders for handlers.
- */
- function placeholder() {
- return $this->query->placeholder($this->options['table'] . '_' . $this->options['field']);
- }
-
-
- * Called just prior to query(), this lets a handler set up any relationship
- * it needs.
- */
- function set_relationship() {
-
- $this->relationship = NULL;
-
-
- if (empty($this->options['relationship']) || $this->options['relationship'] == 'none') {
- return;
- }
-
- $relationship = $this->options['relationship'];
-
-
- if (empty($this->view->relationship[$relationship])) {
- return;
- }
-
-
-
- if (empty($this->view->relationship[$relationship]->alias)) {
- return;
- }
-
-
- $this->relationship = $this->view->relationship[$relationship]->alias;
- }
-
-
- * Ensure the main table for this handler is in the query. This is used
- * a lot.
- */
- function ensure_my_table() {
- if (!isset($this->table_alias)) {
- if (!method_exists($this->query, 'ensure_table')) {
- watchdog('views', t('Ensure my table called but query has no ensure_table method.'));
- return;
- }
- $this->table_alias = $this->query->ensure_table($this->table, $this->relationship);
- }
- return $this->table_alias;
- }
-
-
- * Provide text for the administrative summary
- */
- function admin_summary() { return NULL; }
-
-
- * Determine if the argument needs a style plugin.
- *
- * @return TRUE/FALSE
- */
- function needs_style_plugin() { return FALSE; }
-
-
- * Determine if this item is 'exposed', meaning it provides form elements
- * to let users modify the view.
- *
- * @return TRUE/FALSE
- */
- function is_exposed() {
- return !empty($this->options['exposed']);
- }
-
-
- * Returns TRUE if the exposed filter works like a grouped filter.
- */
- function is_a_group() { return FALSE; }
-
-
- * Define if the exposed input has to be submitted multiple times.
- * This is TRUE when exposed filters grouped are using checkboxes as
- * widgets.
- */
- function multiple_exposed_input() { return FALSE; }
-
-
- * Take input from exposed handlers and assign to this handler, if necessary.
- */
- function accept_exposed_input($input) { return TRUE; }
-
-
- * If set to remember exposed input in the session, store it there.
- */
- function store_exposed_input($input, $status) { return TRUE; }
-
-
- * Get the join object that should be used for this handler.
- *
- * This method isn't used a great deal, but it's very handy for getting the
- * join if it is necessary to make some changes to it, such as adding an
- * 'extra'.
- */
- function get_join() {
-
-
- if (empty($this->query->relationships[$this->relationship])) {
- $base_table = $this->query->base_table;
- }
- else {
- $base_table = $this->query->relationships[$this->relationship]['base'];
- }
-
- $join = views_get_table_join($this->table, $base_table);
- if ($join) {
- return clone $join;
- }
- }
-
-
- * Validates the handler against the complete View.
- *
- * This is called when the complete View is being validated. For validating
- * the handler options form use options_validate().
- *
- * @see views_handler::options_validate()
- *
- * @return
- * Empty array if the handler is valid; an array of error strings if it is not.
- */
- function validate() { return array(); }
-
-
- * Determine if the handler is considered 'broken', meaning it's a
- * a placeholder used when a handler can't be found.
- */
- function broken() { }
- }
-
- * This many to one helper object is used on both arguments and filters.
- *
- * @todo This requires extensive documentation on how this class is to
- * be used. For now, look at the arguments and filters that use it. Lots
- * of stuff is just pass-through but there are definitely some interesting
- * areas where they interact.
- *
- * Any handler that uses this can have the following possibly additional
- * definition terms:
- * - numeric: If true, treat this field as numeric, using %d instead of %s in
- * queries.
- *
- */
- class views_many_to_one_helper {
-
- * Contains possible existing placeholders used by the query.
- *
- * @var array
- */
- public $placeholders = array();
-
- function __construct(&$handler) {
- $this->handler = &$handler;
- }
-
- static function option_definition(&$options) {
- $options['reduce_duplicates'] = array('default' => FALSE, 'bool' => TRUE);
- }
-
- function options_form(&$form, &$form_state) {
- $form['reduce_duplicates'] = array(
- '#type' => 'checkbox',
- '#title' => t('Reduce duplicates'),
- '#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.'),
- '#default_value' => !empty($this->handler->options['reduce_duplicates']),
- '#weight' => 4,
- );
- }
-
-
- * Sometimes the handler might want us to use some kind of formula, so give
- * it that option. If it wants us to do this, it must set $helper->formula = TRUE
- * and implement handler->get_formula();
- */
- function get_field() {
- if (!empty($this->formula)) {
- return $this->handler->get_formula();
- }
- else {
- return $this->handler->table_alias . '.' . $this->handler->real_field;
- }
- }
-
-
- * Add a table to the query.
- *
- * This is an advanced concept; not only does it add a new instance of the table,
- * but it follows the relationship path all the way down to the relationship
- * link point and adds *that* as a new relationship and then adds the table to
- * the relationship, if necessary.
- */
- function add_table($join = NULL, $alias = NULL) {
-
- $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
-
- if (empty($join)) {
- $join = $this->get_join();
- }
-
-
-
- $relationship = $this->handler->relationship;
-
-
- if (empty($this->handler->query->relationships[$relationship])) {
- $base_table = $this->handler->query->base_table;
- }
- else {
- $base_table = $this->handler->query->relationships[$relationship]['base'];
- }
-
-
-
- $r_join = clone $join;
- while (!empty($r_join) && $r_join->left_table != $base_table) {
- $r_join = views_get_table_join($r_join->left_table, $base_table);
- }
-
- if ($r_join->table != $join->table) {
- $relationship = $this->handler->query->add_relationship($this->handler->table . '_' . $r_join->table, $r_join, $r_join->table, $this->handler->relationship);
- }
-
-
- $alias = $this->handler->query->add_table($this->handler->table, $relationship, $join, $alias);
-
-
-
- if (empty($this->handler->view->many_to_one_tables[$field])) {
- $this->handler->view->many_to_one_tables[$field] = $this->handler->value;
- }
- else {
- $this->handler->view->many_to_one_tables[$field] = array_merge($this->handler->view->many_to_one_tables[$field], $this->handler->value);
- }
-
- return $alias;
- }
-
- function get_join() {
- return $this->handler->get_join();
- }
-
-
- * Provide the proper join for summary queries. This is important in part because
- * it will cooperate with other arguments if possible.
- */
- function summary_join() {
- $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
- $join = $this->get_join();
-
-
- $options = $this->handler->options;
- $view = &$this->handler->view;
- $query = &$this->handler->query;
-
- if (!empty($options['require_value'])) {
- $join->type = 'INNER';
- }
-
- if (empty($options['add_table']) || empty($view->many_to_one_tables[$field])) {
- return $query->ensure_table($this->handler->table, $this->handler->relationship, $join);
- }
- else {
- if (!empty($view->many_to_one_tables[$field])) {
- foreach ($view->many_to_one_tables[$field] as $value) {
- $join->extra = array(
- array(
- 'field' => $this->handler->real_field,
- 'operator' => '!=',
- 'value' => $value,
- 'numeric' => !empty($this->definition['numeric']),
- ),
- );
- }
- }
- return $this->add_table($join);
- }
- }
-
-
- * Override ensure_my_table so we can control how this joins in.
- * The operator actually has influence over joining.
- */
- function ensure_my_table() {
- if (!isset($this->handler->table_alias)) {
-
-
- $field = $this->handler->relationship . '_' . $this->handler->table . '.' . $this->handler->field;
- if ($this->handler->operator == 'or' && empty($this->handler->options['reduce_duplicates'])) {
- if (empty($this->handler->options['add_table']) && empty($this->handler->view->many_to_one_tables[$field])) {
-
-
- $join = $this->get_join();
- if (isset($join)) {
- $join->type = 'INNER';
- }
- $this->handler->table_alias = $this->handler->query->ensure_table($this->handler->table, $this->handler->relationship, $join);
- $this->handler->view->many_to_one_tables[$field] = $this->handler->value;
- }
- else {
- $join = $this->get_join();
- $join->type = 'LEFT';
- if (!empty($this->handler->view->many_to_one_tables[$field])) {
- foreach ($this->handler->view->many_to_one_tables[$field] as $value) {
- $join->extra = array(
- array(
- 'field' => $this->handler->real_field,
- 'operator' => '!=',
- 'value' => $value,
- 'numeric' => !empty($this->handler->definition['numeric']),
- ),
- );
- }
- }
-
- $this->handler->table_alias = $this->add_table($join);
- }
-
- return $this->handler->table_alias;
- }
-
-
-
- $this->handler->table_aliases = array();
- $values = $this->handler->operator === 'not' ? array($this->handler->value) : $this->handler->value;
- foreach ($values as $value) {
- $join = $this->get_join();
- if ($this->handler->operator == 'and') {
- $join->type = 'INNER';
- }
- if (empty($join->extra)) {
- $join->extra = array();
- }
- $join->extra[] = array(
- 'field' => $this->handler->real_field,
- 'value' => $value,
- 'numeric' => !empty($this->handler->definition['numeric']),
- );
- if (($this->handler->is_a_group() && is_array($value)) || $this->handler->operator === 'not') {
- $value = serialize($value);
- }
-
-
- if (!isset($this->handler->view->many_to_one_aliases[$field][$value])) {
- if (!isset($this->handler->view->many_to_one_count[$this->handler->table])) {
- $this->handler->view->many_to_one_count[$this->handler->table] = 0;
- }
- $this->handler->view->many_to_one_aliases[$field][$value] = $this->handler->table . '_value_' . ($this->handler->view->many_to_one_count[$this->handler->table]++);
- $alias = $this->handler->table_aliases[$value] = $this->add_table($join,
- $this->handler->view->many_to_one_aliases[$field][$value]);
-
- if (empty($this->handler->table_alias)) {
- $this->handler->table_alias = $alias;
- }
- }
- else {
- $this->handler->table_aliases[$value] = $this->handler->view->many_to_one_aliases[$field][$value];
- }
- }
- }
- return $this->handler->table_alias;
- }
-
-
- * Provides a unique placeholders for handlers.
- */
- function placeholder() {
- return $this->handler->query->placeholder($this->handler->options['table'] . '_' . $this->handler->options['field']);
- }
-
- function add_filter() {
- if (empty($this->handler->value)) {
- return;
- }
- $this->handler->ensure_my_table();
-
-
- $field = $this->get_field();
- $options = $this->handler->options;
- $operator = $this->handler->operator;
- $formula = !empty($this->formula);
- $value = $this->handler->value;
- if (empty($options['group'])) {
- $options['group'] = 0;
- }
-
-
-
- $add_condition = TRUE;
- if ($operator == 'or' && empty($options['reduce_duplicates'])) {
- if (count($value) > 1) {
- $operator = 'IN';
- }
- else {
- $value = is_array($value) ? array_pop($value) : $value;
- if (is_array($value) && count($value) > 1) {
- $operator = 'IN';
- }
- else {
- $operator = '=';
- }
- }
- $add_condition = FALSE;
- }
- elseif ($operator == 'not') {
- $value = NULL;
- $operator = 'IS NULL';
- $add_condition = FALSE;
- }
-
- if (!$add_condition) {
- if ($formula) {
- $placeholder = $this->placeholder();
- if ($operator == 'IN') {
- $operator = "$operator IN($placeholder)";
- }
- else {
- $operator = "$operator $placeholder";
- }
- $placeholders = array(
- $placeholder => $value,
- ) + $this->placeholders;
- $this->handler->query->add_where_expression($options['group'], "$field $operator", $placeholders);
- }
- else {
- $this->handler->query->add_where($options['group'], $field, $value, $operator);
- }
- }
-
- if ($add_condition) {
- $field = $this->handler->real_field;
- $clause = $operator == 'or' ? db_or() : db_and();
- foreach ($this->handler->table_aliases as $value => $alias) {
- if ($operator == 'not') {
- $value = NULL;
- }
- $clause->condition("$alias.$field", $value);
- }
-
-
- $this->handler->query->add_where($options['group'], $clause);
- }
- }
- }
-
- * @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 = NULL, $left_table = NULL, $left_field = NULL, $field = NULL, $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)) {
-
-
- $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 {
-
-
- $left_field = $this->left_field;
- }
-
- $condition = "$left_field = $table[alias].$this->field";
- $arguments = array();
-
-
- if (isset($this->extra)) {
-
-
- if (!is_array($this->extra)) {
- $this->extra = array($this->extra);
- }
-
- $extras = array();
- foreach ($this->extra as $info) {
- if (is_array($info)) {
- $extra = '';
-
-
- $join_table = '';
- if (!array_key_exists('table', $info)) {
- $join_table = $table['alias'] . '.';
- }
- elseif (isset($info['table'])) {
-
-
- if (isset($left) && $left['table'] == $info['table']) {
- $join_table = $left['alias'] . '.';
- }
- else {
- $join_table = $info['table'] . '.';
- }
- }
-
-
-
- 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'])) {
- $value_placeholders = array();
-
-
-
- foreach ($info['value'] as $value) {
- $placeholder_i = $view_query->placeholder('views_join_condition_');
- $value_placeholders[] = $placeholder_i;
- $arguments[$placeholder_i] = $value;
- }
-
- $operator = !empty($info['operator']) ? $info['operator'] : 'IN';
- $placeholder = '( ' . implode(', ', $value_placeholders) . ' )';
- }
- else {
-
- $operator = !empty($info['operator']) ? $info['operator'] : '=';
- $placeholder = $view_query->placeholder('views_join_condition_');
- $arguments[$placeholder] = $info['value'];
- }
- $extras[] = "$join_table$info[field] $operator $placeholder";
- }
- elseif (is_string($info)) {
- $extras[] = $info;
- }
- }
-
- if ($extras) {
- if (count($extras) == 1) {
- $condition .= ' AND (' . array_shift($extras) . ')';
- }
- else {
- $condition .= ' AND (' . implode(' ' . $this->extra_type . ' ', $extras) . ')';
- }
- }
- }
-
- $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
- *
- */
- public 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'];
- }
-
-
- $condition = "($this->left_query) = $table[alias].$this->field";
- $arguments = array();
-
-
-
-
-
- if (isset($this->extra)) {
-
-
- if (!is_array($this->extra)) {
- $this->extra = array($this->extra);
- }
-
- $extras = array();
- foreach ($this->extra as $info) {
- if (is_array($info)) {
- $extra = '';
-
-
- $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';
-
- 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'];
- }
- elseif (is_string($info)) {
- $extras[] = $info;
- }
- }
-
- if ($extras) {
- if (count($extras) == 1) {
- $condition .= ' AND (' . array_shift($extras) . ')';
- }
- else {
- $condition .= ' AND (' . implode(' ' . $this->extra_type . ' ', $extras) . ')';
- }
- }
- }
-
- $select_query->addJoin($this->type, $right_table, $table['alias'], $condition, $arguments);
- }
- }
-
- * @}
- */