You are here

menu_node_views_relationship_double.inc in Menu Node Views 7.2

File

includes/menu_node_views_relationship_double.inc
View source
<?php

/**
 * Relationship handler to create a double jointure, ie through 2 tables:
 *  from the base table to the final table through the relation table
 * 
 * Definition items:
 * TODO: i just realized that i'm mistaken: the base table is the final table.... TO BE REVIEWED...
 * - ...
 * - ...
 * - ...
 * - ...
 * - ...
 * - ...
 * - ...
 * - label: The default label to provide for this relationship, which is
 *   shown in parentheses next to any field/sort/filter/argument that uses
 *   the relationship.
 */
class menu_node_views_relationship_double extends views_handler_relationship {

  /**
   * Init handler to let relationships live on tables other than
   * the table they operate on.
   * TODO: check necessity and validity
   */
  function init(&$view, &$options) {
    parent::init($view, $options);
    if (isset($this->definition['relationship table'])) {
      $this->table = $this->definition['relationship table'];
    }
    if (isset($this->definition['relationship field'])) {

      // Set both real_field and field so custom handler
      // can rely on the old field value.
      $this->real_field = $this->field = $this->definition['relationship field'];
    }
  }

  /**
   * Get this field's label.
   * TODO: check necessity and validity
   */
  function label() {
    if (!isset($this->options['label'])) {
      return $this
        ->ui_name();
    }
    return $this->options['label'];
  }

  /**
   * Default options form that provides the label widget that all fields
   * should have.
   * TODO: check necessity and validity
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Identifier'),
      '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
      '#description' => t('Edit the administrative label displayed when referencing this relationship from filters, etc.'),
      '#required' => TRUE,
    );
    $form['required'] = array(
      '#type' => 'checkbox',
      '#title' => t('Require this relationship'),
      '#description' => t('Enable to hide items that do not contain this relationship'),
      '#default_value' => !empty($this->options['required']),
    );
  }

  /**
   * Called to implement a relationship in a query.
   */
  function query() {

    // Figure out what base table this relationship brings to the party.
    $this
      ->ensure_my_table();
    $middle_table = $this->query
      ->add_table($this->definition['middle_table'], $this->relationship);
    $def['table'] = $this->definition['right_table'];
    $def['left_table'] = $middle_table;
    $def['left_field'] = $this->definition['middle_field_2'];
    $def['field'] = $this->definition['right_field_2'];
    $def['type'] = empty($this->options['required']) ? 'LEFT' : 'INNER';
    $join = new views_join();
    $join->definition = $def;
    $join
      ->construct();
    $join->adjusted = TRUE;

    // use a short alias for this:
    $alias = $def['table'] . '_' . $this->table;
    $this->alias = $this->query
      ->add_relationship($alias, $join, $this->definition['right_table'], $this->relationship);
  }

  /**
   * You can't groupby a relationship.
   */
  function use_group_by() {
    return FALSE;
  }

}

Classes

Namesort descending Description
menu_node_views_relationship_double Relationship handler to create a double jointure, ie through 2 tables: from the base table to the final table through the relation table