[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/includes/ -> tabs.inc (source)

   1  <?php
   2  /**
   3   * @file
   4   *
   5   * Classes and theme functions for rendering javascript UI tabs.
   6   */
   7  
   8  /**
   9   * Contain a set of tabs as well as the ability to render them.
  10   *
  11   * There are three 'areas' of a tabset.
  12   * - title: The clickable link to display the tab area. These are always visible.
  13   * - body: The actual HTML body of the tab. Only one body is visible at a time.
  14   * - extra: An optional decorative area around the tabs.
  15   */
  16  class views_tabset {
  17    var $tabs = array();
  18    var $extra = '';
  19    var $selected = NULL;
  20  
  21    /**
  22     * Add a tab to the tabset.
  23     *
  24     * @param $name
  25     *   The name of the tab; this is the internal identifier and must be
  26     *   unique within the tabset.
  27     * @param $title
  28     *   If given, this will be the visible title of the tab. This can also
  29     *   be set via $tabset->set(). This will be the link to click on to
  30     *   view the tab.
  31     * @param $body
  32     *   If given, this is the body of the tab itself. It will display
  33     *   when the tab title is clicked on.
  34     */
  35    function add($name, $title = '', $body = '') {
  36      if (is_object($name) && is_subclass_of($name, 'views_tab')) {
  37        $this->add_tab($name);
  38      }
  39      elseif (is_array($name)) {
  40        foreach ($name as $real_tab) {
  41          $this->add($real_tab);
  42        }
  43      }
  44      else {
  45        $this->add_tab(new views_tab($name, $title, $body));
  46      }
  47    }
  48  
  49    /**
  50     * Add a fully realized tab object to the tabset.
  51     *
  52     * @param $tab
  53     *   A fully populated views_tab object.
  54     */
  55    function add_tab($tab) {
  56      $this->tabs[$tab->name] = $tab;
  57    }
  58  
  59    /**
  60     * Set the values of a tab.
  61     *
  62     * @param $name
  63     *   The unique identifier of the tab to set.
  64     * @param $title
  65     *   The title of the tab; this will be clickable.
  66     * @param $body
  67     *   The HTML body of the tab.
  68     */
  69    function set($name, $title, $body = NULL) {
  70      if (empty($this->tabs[$name])) {
  71        return $this->add($name, $title, $body);
  72      }
  73      $this->tabs[$name]->title = $title;
  74      if (isset($body)) {
  75        $this->tabs[$name]->body = $body;
  76      }
  77    }
  78  
  79    /**
  80     * Set the body of a tab.
  81     */
  82    function set_body($name, $body) {
  83      if (empty($this->tabs[$name])) {
  84        return $this->add($name, '', $body);
  85      }
  86      $this->tabs[$name]->body = $body;
  87    }
  88  
  89    /**
  90     * Add text to the 'extra' region of the tabset.
  91     */
  92    function add_extra($text) {
  93      $this->extra .= $text;
  94    }
  95  
  96    /**
  97     * Remove a tab.
  98     *
  99     * @param $tab
 100     *   May be the name of the tab or a views_tab object.
 101     */
 102    function remove($tab) {
 103      if (is_string($tab)) {
 104        unset($this->tabs[$tab]);
 105      }
 106      else {
 107        unset($this->tabs[$tab->name]);
 108      }
 109    }
 110  
 111    /**
 112     * Control which tab will be selected when it is rendered.
 113     */
 114    function set_selected($name) {
 115      $this->selected = $name;
 116    }
 117  
 118    /**
 119     * Output the HTML for the tabs.
 120     *
 121     * @return
 122     *   HTML representation of the tabs.
 123     */
 124    function render() {
 125      views_add_js('tabs');
 126      views_add_css('views-tabs');
 127  
 128      if (empty($this->selected)) {
 129        $keys = array_keys($this->tabs);
 130        $this->selected = array_shift($keys);
 131      }
 132  
 133      drupal_alter('views_tabset', $this);
 134      return theme('views_tabset', $this->tabs, $this->extra, $this->selected);
 135    }
 136  }
 137  
 138  /**
 139   * An object to represent an individual tab within a tabset.
 140   */
 141  class views_tab {
 142    var $title;
 143    var $body;
 144    var $name;
 145  
 146    /**
 147     * Construct a new tab.
 148     */
 149    function views_tab($name, $title, $body = NULL) {
 150      $this->name = $name;
 151      $this->title = $title;
 152      $this->body = $body;
 153    }
 154  
 155    /**
 156     * Generate HTML output for a tab.
 157     */
 158    function render() {
 159      return theme('views_tab', $this->body);
 160    }
 161  }
 162  
 163  /**
 164   * Render a tabset.
 165   *
 166   * @todo Turn this into a template.
 167   */
 168  function theme_views_tabset($tabs, $extra = NULL, $selected = NULL) {
 169    $link_output = "<div class=\"views-tabs\"><ul id=\"views-tabset\">\n";
 170    $tab_output = "<div class=\"views-tab-area\">\n";
 171  
 172    foreach ($tabs as $name => $tab) {
 173      $link_output .= '<li' . ($name == $selected ? ' class="active"': '') . '><a href="#views-tab-' . $tab->name . '" id="views-tab-title-' . $tab->name . '">' . check_plain($tab->title) . '</a></li>' . "\n";
 174      $tab_output .= '<div id="views-tab-' . $tab->name . '" class="views-tab">' . $tab->render() . "</div>\n";
 175    }
 176    $link_output .= "</ul>\n";
 177  
 178    if ($extra) {
 179      $link_output .= "<div class=\"extra\">$extra</div>\n";
 180    }
 181  
 182    $link_output .= "</div>\n";
 183    $tab_output .= "</div>\n";
 184    return '<div class="views-tabset clear-block">' . $link_output . $tab_output . '</div>';
 185  }
 186  
 187  /**
 188   * Theme a simple tab.
 189   */
 190  function theme_views_tab($body) {
 191    return $body;
 192  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7