You are here

class views_plugin_style_lineage_nested in Taxonomy Lineage 7

Same name and namespace in other branches
  1. 6 views_plugin_style_lineage_nested.inc \views_plugin_style_lineage_nested

Implements views_plugin_style.

Hierarchy

Expanded class hierarchy of views_plugin_style_lineage_nested

1 string reference to 'views_plugin_style_lineage_nested'
lineage_views_plugins in ./lineage.views.inc
Implements hook_views_plugins().

File

plugins/views_plugin_style_lineage_nested.inc, line 12
Views style plugin that allows a view to be rendered as a nested list, based on Lineage's term hierarchy.

View source
class views_plugin_style_lineage_nested extends views_plugin_style_list {
  function respond_to_filter($filter) {
    $allowed = array(
      'views_handler_filter_term_node_tid',
      'views_handler_filter_term_node_tid_depth',
      'views_handler_argument_term_node_tid',
      'views_handler_argument_term_node_tid_depth',
    );
    if (in_array($filter, $allowed)) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
  function options_form(&$form, &$form_state) {

    // Assemble a list of Taxonomy Lineage fields.
    $nesting_options = array(
      '' => '[None]',
    );
    foreach ($this->display->handler
      ->get_handlers('field') as $field => $handler) {

      // Only include lineage fields.
      if ($handler->definition['handler'] == 'lineage_handler_field') {
        if ($label = $handler
          ->label()) {
          $nesting_options[$field] = $label;
        }
        else {
          $nesting_options[$field] = $handler
            ->ui_name();
        }
      }
    }

    // Assemble a list of Taxonomy filters and arguments
    $filter_options = array(
      '' => '[None]',
    );
    foreach ($this->display->handler
      ->get_handlers('filter') as $filter => $handler) {
      if ($this
        ->respond_to_filter($handler->definition['handler'])) {
        $filter_options['Filters']['f_' . $filter] = $handler
          ->ui_name();
      }
    }
    foreach ($this->display->handler
      ->get_handlers('argument') as $arg => $handler) {
      if ($this
        ->respond_to_filter($handler->definition['handler'])) {
        $filter_options['Arguments']['a_' . $arg] = $handler
          ->ui_name();
      }
    }
    $options = parent::options_form($form, $form_state);
    if (sizeof($nesting_options) > 1) {

      // one option is [None]
      $form['nesting'] = array(
        '#type' => 'select',
        '#title' => t('Nesting Field'),
        '#options' => $nesting_options,
        '#default_value' => $this->options['nesting'],
        '#description' => t('Select the Lineage field that will control the nesting.'),
      );
      $h_options = array(
        0 => t('default'),
      );
      for ($i = 1; $i <= 6; $i++) {
        $h_options[$i] = "<h{$i}>";
      }
      $default_header = $this->options['start_depth'] ? $this->options['start_depth'] : 0;
      $form['start_depth'] = array(
        '#type' => 'select',
        '#title' => 'Top-level header',
        '#options' => $h_options,
        '#default_value' => $default_header,
        '#description' => t('Header tag to use for the top-level term in the nesting.  If %default is selected, the plugin will choose the header level based on the properties of your view.', array(
          '%default' => $h_options[0],
        )),
      );
      if (sizeof($filter_options) > 1) {

        // one option is [None]
        $form['filter'] = array(
          '#type' => 'select',
          '#title' => 'Set top level with filter or argument',
          '#options' => $filter_options,
          '#default_value' => $this->options['filter'],
          '#description' => t("If selected, the nesting will begin with the term set by this argument or filter, and headers for parent terms will be hidden.  (Filters and arguments are listed in the same order as in your view.)  This feature is experimental."),
        );
      }
    }
  }
  function render() {
    if ($this
      ->uses_row_plugin() && empty($this->row_plugin)) {
      vpr('views_plugin_style_default: Missing row plugin');
      return;
    }
    $output = '';

    // First group the rows according to the grouping field, if specified.
    $sets = $this
      ->render_grouping($this->view->result, $this->options['grouping']);
    foreach ($sets as $title => $records) {
      if ($this
        ->uses_row_plugin()) {
        $rendered = array();
        foreach ($records as $row_index => $row) {
          $this->view->row_index = $row_index;
          $rendered[$row_index] = $this->row_plugin
            ->render($row);
        }
      }

      // If the user specified a filter for the nesting, remove other levels.
      $term_name = FALSE;
      if ($this->options['filter']) {

        // Option is either a filter or an argument, marked by f and a.
        // If it's an argument
        if ($arg_name = strstr($this->options['filter'], "a_")) {
          $arg_name = substr($arg_name, 2);
          $arg = $this->display->handler->handlers['argument'][$arg_name];
          $term = $arg->argument;
        }
        elseif ($filter_name = strstr($this->options['filter'], "f_")) {
          $filter_name = substr($filter_name, 2);
          $filter = $this->display->handler->handlers['filter'][$filter_name];
          $terms = $filter->value;
          $term = array_pop($terms);
        }
        if (is_numeric($term)) {
          $term_obj = taxonomy_get_term($term);
          $term_name = $term_obj->name;
        }
        else {
          $term_name = $term;
        }
      }

      // Now, nest each grouping by the nesting field.
      $nested_set = $this
        ->render_nesting($records, $rendered, $this->options['nesting'], $term_name);

      // Determine the starting depth of the header.
      // If the user has defined a start depth, use that.
      if ($this->options['start_depth']) {
        $depth = $this->options['start_depth'];
      }
      else {
        $depth = LINEAGE_START_DEPTH;
      }

      // @todo Provide theming.
      $output .= $this
        ->nested_list($nested_set, $title, $this->options['type'], $depth);
    }
    unset($this->view->row_index);
    return $output;
  }
  function render_nesting($records, $rendered, $nesting_field = '', $term_name = FALSE) {
    $nested_sets = array();
    if (isset($this->view->field[$nesting_field])) {
      $field_alias = $this->view->field[$nesting_field]->field_alias;
    }
    if ($field_alias) {
      foreach ($records as $index => $row) {
        $nesting = '';

        // @todo Avoid using eval().
        $lineage = $row->{$field_alias};

        // If a filter term was passed, remove the lineage before the term.
        if ($term_name) {
          $lineage = strstr($lineage, $term_name);
          if ($lineage) {
            $lineage = substr($lineage, strlen($term_name) + 1);

            // +1 for \n
          }
        }

        // Strip weights from the lineage, if any.
        if ($lineage) {
          $lineage = explode("\n", $lineage);
          foreach ($lineage as $key => $value) {
            if ($value) {
              $lineage[$key] = lineage_strip_weight($value);
            }
          }
          $lineage = implode("\n", $lineage);
        }

        // Add the row to the array.
        $eval = "\$nested_sets" . "['" . str_replace("\n", "']['", addslashes($lineage) . $index) . "']" . " = \$rendered[\$index];";
        eval($eval);
      }
    }
    else {

      // Create a single group with an empty grouping field.
      $nested_sets[''] = $records;
    }
    return $nested_sets;
  }
  function header($header, $depth) {

    // Use a header tag if possible; else use a class indicating header depth.
    if ($depth <= 6) {
      return "<h{$depth}>" . stripslashes($header) . "</h{$depth}>\n";
    }
    else {
      return "<span class=\"h{$depth}\">{$header}</span>\n";
    }
  }
  function nested_list($rows, $header, $type, $depth = 1) {
    $output = "";
    if (!empty($header)) {
      $output .= $this
        ->header($header, $depth);
      $depth++;
    }
    $output .= "<{$type}>\n";
    foreach ($rows as $key => $row) {
      $output .= "<li>\n";

      // @todo Add classes.
      // If the next child is an array of rows, recurse.
      if (is_array($row) || is_object($row)) {

        // @todo Allow type to vary per list? (And how would we store that?)
        $output .= $this
          ->nested_list($row, $key, $type, $depth);
      }
      else {
        $output .= $row;
      }
      $output .= "</li>\n";
    }
    $output .= "</{$type}>\n";
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::options_submit public function Handle any special handling on the validate form. 9
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin_style::$row_plugin public property The row plugin, if it's initialized and the style itself supports it.
views_plugin_style::$row_tokens public property Store all available tokens row rows.
views_plugin_style::build_sort public function Called by the view builder to see if this style handler wants to interfere with the sorts. If so it should build; if it returns any non-TRUE value, normal sorting will NOT be added to the query. 1
views_plugin_style::build_sort_post public function Called by the view builder to let the style build a second set of sorts that will come after any other sorts in the view. 1
views_plugin_style::destroy public function Destructor. Overrides views_object::destroy
views_plugin_style::even_empty public function Should the output of the style plugin be rendered even if it's empty. 1
views_plugin_style::get_field public function Get a rendered field.
views_plugin_style::get_field_value public function Get the raw field value.
views_plugin_style::get_row_class public function Return the token replaced row class for the specified row.
views_plugin_style::init public function Initialize a style plugin.
views_plugin_style::options_validate public function Validate the options form. Overrides views_plugin::options_validate
views_plugin_style::pre_render public function Allow the style to do stuff before each row is rendered.
views_plugin_style::query public function Add anything to the query that we might need to. Overrides views_plugin::query 2
views_plugin_style::render_fields public function Render all of the fields for a given style and store them on the object.
views_plugin_style::render_grouping public function Group records as needed for rendering.
views_plugin_style::render_grouping_sets public function Render the grouping sets.
views_plugin_style::tokenize_value public function Take a value and apply token replacement logic to it.
views_plugin_style::uses_fields public function Return TRUE if this style also uses fields.
views_plugin_style::uses_row_class public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_row_plugin public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_tokens public function Return TRUE if this style uses tokens.
views_plugin_style::validate public function Validate that the plugin is correct and can be saved. Overrides views_plugin::validate
views_plugin_style_lineage_nested::header function
views_plugin_style_lineage_nested::nested_list function
views_plugin_style_lineage_nested::options_form function Render the given style. Overrides views_plugin_style_list::options_form
views_plugin_style_lineage_nested::render function Render the display in this style. Overrides views_plugin_style::render
views_plugin_style_lineage_nested::render_nesting function
views_plugin_style_lineage_nested::respond_to_filter function
views_plugin_style_list::option_definition public function Set default options. Overrides views_plugin_style::option_definition