html.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: html.php 8220 2009-07-04 22:04:40Z davidpersson $ */
00003 /**
00004  * Html Helper class file.
00005  *
00006  * Simplifies the construction of HTML elements.
00007  *
00008  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00009  * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00010  *
00011  * Licensed under The MIT License
00012  * Redistributions of files must retain the above copyright notice.
00013  *
00014  * @filesource
00015  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00016  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00017  * @package       cake
00018  * @subpackage    cake.cake.libs.view.helpers
00019  * @since         CakePHP(tm) v 0.9.1
00020  * @version       $Revision: 8220 $
00021  * @modifiedby    $LastChangedBy: davidpersson $
00022  * @lastmodified  $Date: 2009-07-04 18:04:40 -0400 (Sat, 04 Jul 2009) $
00023  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00024  */
00025 /**
00026  * Html Helper class for easy use of HTML widgets.
00027  *
00028  * HtmlHelper encloses all methods needed while working with HTML pages.
00029  *
00030  * @package       cake
00031  * @subpackage    cake.cake.libs.view.helpers
00032  */
00033 class HtmlHelper extends AppHelper {
00034 /*************************************************************************
00035  * Public variables
00036  *************************************************************************/
00037 /**#@+
00038  * @access public
00039  */
00040 /**
00041  * html tags used by this helper.
00042  *
00043  * @var array
00044  */
00045     var $tags = array(
00046         'meta' => '<meta%s/>',
00047         'metalink' => '<link href="%s"%s/>',
00048         'link' => '<a href="%s"%s>%s</a>',
00049         'mailto' => '<a href="mailto:%s" %s>%s</a>',
00050         'form' => '<form %s>',
00051         'formend' => '</form>',
00052         'input' => '<input name="%s" %s/>',
00053         'textarea' => '<textarea name="%s" %s>%s</textarea>',
00054         'hidden' => '<input type="hidden" name="%s" %s/>',
00055         'checkbox' => '<input type="checkbox" name="%s" %s/>',
00056         'checkboxmultiple' => '<input type="checkbox" name="%s[]"%s />',
00057         'radio' => '<input type="radio" name="%s" id="%s" %s />%s',
00058         'selectstart' => '<select name="%s"%s>',
00059         'selectmultiplestart' => '<select name="%s[]"%s>',
00060         'selectempty' => '<option value=""%s>&nbsp;</option>',
00061         'selectoption' => '<option value="%s"%s>%s</option>',
00062         'selectend' => '</select>',
00063         'optiongroup' => '<optgroup label="%s"%s>',
00064         'optiongroupend' => '</optgroup>',
00065         'checkboxmultiplestart' => '',
00066         'checkboxmultipleend' => '',
00067         'password' => '<input type="password" name="%s" %s/>',
00068         'file' => '<input type="file" name="%s" %s/>',
00069         'file_no_model' => '<input type="file" name="%s" %s/>',
00070         'submit' => '<input type="submit" %s/>',
00071         'submitimage' => '<input type="image" src="%s" %s/>',
00072         'button' => '<input type="%s" %s/>',
00073         'image' => '<img src="%s" %s/>',
00074         'tableheader' => '<th%s>%s</th>',
00075         'tableheaderrow' => '<tr%s>%s</tr>',
00076         'tablecell' => '<td%s>%s</td>',
00077         'tablerow' => '<tr%s>%s</tr>',
00078         'block' => '<div%s>%s</div>',
00079         'blockstart' => '<div%s>',
00080         'blockend' => '</div>',
00081         'tag' => '<%s%s>%s</%s>',
00082         'tagstart' => '<%s%s>',
00083         'tagend' => '</%s>',
00084         'para' => '<p%s>%s</p>',
00085         'parastart' => '<p%s>',
00086         'label' => '<label for="%s"%s>%s</label>',
00087         'fieldset' => '<fieldset%s>%s</fieldset>',
00088         'fieldsetstart' => '<fieldset><legend>%s</legend>',
00089         'fieldsetend' => '</fieldset>',
00090         'legend' => '<legend>%s</legend>',
00091         'css' => '<link rel="%s" type="text/css" href="%s" %s/>',
00092         'style' => '<style type="text/css"%s>%s</style>',
00093         'charset' => '<meta http-equiv="Content-Type" content="text/html; charset=%s" />',
00094         'ul' => '<ul%s>%s</ul>',
00095         'ol' => '<ol%s>%s</ol>',
00096         'li' => '<li%s>%s</li>',
00097         'error' => '<div%s>%s</div>'
00098     );
00099 /**
00100  * Base URL
00101  *
00102  * @var string
00103  */
00104     var $base = null;
00105 /**
00106  * URL to current action.
00107  *
00108  * @var string
00109  */
00110     var $here = null;
00111 /**
00112  * Parameter array.
00113  *
00114  * @var array
00115  */
00116     var $params = array();
00117 /**
00118  * Current action.
00119  *
00120  * @var string
00121  */
00122     var $action = null;
00123 /**
00124  * Enter description here...
00125  *
00126  * @var array
00127  */
00128     var $data = null;
00129 /**#@-*/
00130 /*************************************************************************
00131  * Private variables
00132  *************************************************************************/
00133 /**#@+
00134  * @access private
00135  */
00136 /**
00137  * Breadcrumbs.
00138  *
00139  * @var array
00140  * @access private
00141  */
00142     var $_crumbs = array();
00143 /**
00144  * Document type definitions
00145  *
00146  * @var array
00147  * @access private
00148  */
00149     var $__docTypes = array(
00150         'html4-strict'  => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
00151         'html4-trans'  => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
00152         'html4-frame'  => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
00153         'xhtml-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
00154         'xhtml-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
00155         'xhtml-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
00156         'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
00157     );
00158 /**
00159  * Adds a link to the breadcrumbs array.
00160  *
00161  * @param string $name Text for link
00162  * @param string $link URL for link (if empty it won't be a link)
00163  * @param mixed $options Link attributes e.g. array('id'=>'selected')
00164  */
00165     function addCrumb($name, $link = null, $options = null) {
00166         $this->_crumbs[] = array($name, $link, $options);
00167     }
00168 /**
00169  * Returns a doctype string.
00170  *
00171  * Possible doctypes:
00172  *   + html4-strict:  HTML4 Strict.
00173  *   + html4-trans:  HTML4 Transitional.
00174  *   + html4-frame:  HTML4 Frameset.
00175  *   + xhtml-strict: XHTML1 Strict.
00176  *   + xhtml-trans: XHTML1 Transitional.
00177  *   + xhtml-frame: XHTML1 Frameset.
00178  *   + xhtml11: XHTML1.1.
00179  *
00180  * @param  string $type Doctype to use.
00181  * @return string Doctype.
00182  */
00183     function docType($type = 'xhtml-strict') {
00184         if (isset($this->__docTypes[$type])) {
00185             return $this->output($this->__docTypes[$type]);
00186         }
00187         return null;
00188     }
00189 /**
00190  * Creates a link to an external resource and handles basic meta tags
00191  *
00192  * @param  string  $type The title of the external resource
00193  * @param  mixed   $url   The address of the external resource or string for content attribute
00194  * @param  array   $attributes Other attributes for the generated tag. If the type attribute is html, rss, atom, or icon, the mime-type is returned.
00195  * @param  boolean $inline If set to false, the generated tag appears in the head tag of the layout.
00196  * @return string
00197  */
00198     function meta($type, $url = null, $attributes = array(), $inline = true) {
00199         if (!is_array($type)) {
00200             $types = array(
00201                 'rss'   => array('type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => $type, 'link' => $url),
00202                 'atom'  => array('type' => 'application/atom+xml', 'title' => $type, 'link' => $url),
00203                 'icon'  => array('type' => 'image/x-icon', 'rel' => 'icon', 'link' => $url),
00204                 'keywords' => array('name' => 'keywords', 'content' => $url),
00205                 'description' => array('name' => 'description', 'content' => $url),
00206             );
00207 
00208             if ($type === 'icon' && $url === null) {
00209                 $types['icon']['link'] = $this->webroot('favicon.ico');
00210             }
00211 
00212             if (isset($types[$type])) {
00213                 $type = $types[$type];
00214             } elseif (!isset($attributes['type']) && $url !== null) {
00215                 if (is_array($url) && isset($url['ext'])) {
00216                     $type = $types[$url['ext']];
00217                 } else {
00218                     $type = $types['rss'];
00219                 }
00220             } elseif (isset($attributes['type']) && isset($types[$attributes['type']])) {
00221                 $type = $types[$attributes['type']];
00222                 unset($attributes['type']);
00223             } else {
00224                 $type = array();
00225             }
00226         } elseif ($url !== null) {
00227             $inline = $url;
00228         }
00229         $attributes = array_merge($type, $attributes);
00230         $out = null;
00231 
00232         if (isset($attributes['link'])) {
00233             if (isset($attributes['rel']) && $attributes['rel'] === 'icon') {
00234                 $out = sprintf($this->tags['metalink'], $attributes['link'], $this->_parseAttributes($attributes, array('link'), ' ', ' '));
00235                 $attributes['rel'] = 'shortcut icon';
00236             } else {
00237                 $attributes['link'] = $this->url($attributes['link'], true);
00238             }
00239             $out .= sprintf($this->tags['metalink'], $attributes['link'], $this->_parseAttributes($attributes, array('link'), ' ', ' '));
00240         } else {
00241             $out = sprintf($this->tags['meta'], $this->_parseAttributes($attributes, array('type')));
00242         }
00243 
00244         if ($inline) {
00245             return $this->output($out);
00246         } else {
00247             $view =& ClassRegistry::getObject('view');
00248             $view->addScript($out);
00249         }
00250     }
00251 /**
00252  * Returns a charset META-tag.
00253  *
00254  * @param  string  $charset The character set to be used in the meta tag. Example: "utf-8".
00255  * @return string A meta tag containing the specified character set.
00256  */
00257     function charset($charset = null) {
00258         if (empty($charset)) {
00259             $charset = strtolower(Configure::read('App.encoding'));
00260         }
00261         return $this->output(sprintf($this->tags['charset'], (!empty($charset) ? $charset : 'utf-8')));
00262     }
00263 /**
00264  * Creates an HTML link.
00265  *
00266  * If $url starts with "http://" this is treated as an external link. Else,
00267  * it is treated as a path to controller/action and parsed with the
00268  * HtmlHelper::url() method.
00269  *
00270  * If the $url is empty, $title is used instead.
00271  *
00272  * @param  string  $title The content to be wrapped by <a> tags.
00273  * @param  mixed   $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
00274  * @param  array   $htmlAttributes Array of HTML attributes.
00275  * @param  string  $confirmMessage JavaScript confirmation message.
00276  * @param  boolean $escapeTitle Whether or not $title should be HTML escaped.
00277  * @return string   An <a /> element.
00278  */
00279     function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true) {
00280         if ($url !== null) {
00281             $url = $this->url($url);
00282         } else {
00283             $url = $this->url($title);
00284             $title = $url;
00285             $escapeTitle = false;
00286         }
00287 
00288         if (isset($htmlAttributes['escape']) && $escapeTitle == true) {
00289             $escapeTitle = $htmlAttributes['escape'];
00290         }
00291 
00292         if ($escapeTitle === true) {
00293             $title = h($title);
00294         } elseif (is_string($escapeTitle)) {
00295             $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
00296         }
00297 
00298         if (!empty($htmlAttributes['confirm'])) {
00299             $confirmMessage = $htmlAttributes['confirm'];
00300             unset($htmlAttributes['confirm']);
00301         }
00302         if ($confirmMessage) {
00303             $confirmMessage = str_replace("'", "\'", $confirmMessage);
00304             $confirmMessage = str_replace('"', '\"', $confirmMessage);
00305             $htmlAttributes['onclick'] = "return confirm('{$confirmMessage}');";
00306         } elseif (isset($htmlAttributes['default']) && $htmlAttributes['default'] == false) {
00307             if (isset($htmlAttributes['onclick'])) {
00308                 $htmlAttributes['onclick'] .= ' event.returnValue = false; return false;';
00309             } else {
00310                 $htmlAttributes['onclick'] = 'event.returnValue = false; return false;';
00311             }
00312             unset($htmlAttributes['default']);
00313         }
00314         return $this->output(sprintf($this->tags['link'], $url, $this->_parseAttributes($htmlAttributes), $title));
00315     }
00316 /**
00317  * Creates a link element for CSS stylesheets.
00318  *
00319  * @param mixed $path The name of a CSS style sheet or an array containing names of
00320  *   CSS stylesheets. If `$path` is prefixed with '/', the path will be relative to the webroot
00321  *   of your application. Otherwise, the path will be relative to your CSS path, usually webroot/css.
00322  * @param string $rel Rel attribute. Defaults to "stylesheet". If equal to 'import' the stylesheet will be imported.
00323  * @param array $htmlAttributes Array of HTML attributes.
00324  * @param boolean $inline If set to false, the generated tag appears in the head tag of the layout.
00325  * @return string CSS <link /> or <style /> tag, depending on the type of link.
00326  */
00327     function css($path, $rel = null, $htmlAttributes = array(), $inline = true) {
00328         if (is_array($path)) {
00329             $out = '';
00330             foreach ($path as $i) {
00331                 $out .= "\n\t" . $this->css($i, $rel, $htmlAttributes, $inline);
00332             }
00333             if ($inline)  {
00334                 return $out . "\n";
00335             }
00336             return;
00337         }
00338 
00339         if (strpos($path, '://') !== false) {
00340             $url = $path;
00341         } else {
00342             if ($path[0] !== '/') {
00343                 $path = CSS_URL . $path;
00344             }
00345 
00346             if (strpos($path, '?') === false) {
00347                 if (strpos($path, '.css') === false) {
00348                     $path .= '.css';
00349                 }
00350             }
00351 
00352             $path = $this->webroot($path);
00353 
00354             $url = $path;
00355             if (strpos($path, '?') === false && ((Configure::read('Asset.timestamp') === true && Configure::read() > 0) || Configure::read('Asset.timestamp') === 'force')) {
00356                 $url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $path));
00357             }
00358 
00359             if (Configure::read('Asset.filter.css')) {
00360                 $url = str_replace(CSS_URL, 'ccss/', $url);
00361             }
00362         }
00363 
00364         if ($rel == 'import') {
00365             $out = sprintf($this->tags['style'], $this->_parseAttributes($htmlAttributes, null, '', ' '), '@import url(' . $url . ');');
00366         } else {
00367             if ($rel == null) {
00368                 $rel = 'stylesheet';
00369             }
00370             $out = sprintf($this->tags['css'], $rel, $url, $this->_parseAttributes($htmlAttributes, null, '', ' '));
00371         }
00372         $out = $this->output($out);
00373 
00374         if ($inline) {
00375             return $out;
00376         } else {
00377             $view =& ClassRegistry::getObject('view');
00378             $view->addScript($out);
00379         }
00380     }
00381 /**
00382  * Builds CSS style data from an array of CSS properties
00383  *
00384  * @param array $data Style data array
00385  * @param boolean $inline Whether or not the style block should be displayed inline
00386  * @return string CSS styling data
00387  */
00388     function style($data, $inline = true) {
00389         if (!is_array($data)) {
00390             return $data;
00391         }
00392         $out = array();
00393         foreach ($data as $key=> $value) {
00394             $out[] = $key.':'.$value.';';
00395         }
00396         if ($inline) {
00397             return join(' ', $out);
00398         }
00399         return join("\n", $out);
00400     }
00401 /**
00402  * Returns the breadcrumb trail as a sequence of &raquo;-separated links.
00403  *
00404  * @param  string  $separator Text to separate crumbs.
00405  * @param  string  $startText This will be the first crumb, if false it defaults to first crumb in array
00406  * @return string
00407  */
00408     function getCrumbs($separator = '&raquo;', $startText = false) {
00409         if (count($this->_crumbs)) {
00410             $out = array();
00411             if ($startText) {
00412                 $out[] = $this->link($startText, '/');
00413             }
00414 
00415             foreach ($this->_crumbs as $crumb) {
00416                 if (!empty($crumb[1])) {
00417                     $out[] = $this->link($crumb[0], $crumb[1], $crumb[2]);
00418                 } else {
00419                     $out[] = $crumb[0];
00420                 }
00421             }
00422             return $this->output(join($separator, $out));
00423         } else {
00424             return null;
00425         }
00426     }
00427 /**
00428  * Creates a formatted IMG element.
00429  *
00430  * @param string $path Path to the image file, relative to the app/webroot/img/ directory.
00431  * @param array $options Array of HTML attributes.
00432  * @return string
00433  */
00434     function image($path, $options = array()) {
00435         if (is_array($path)) {
00436             $path = $this->url($path);
00437         } elseif ($path[0] === '/') {
00438             $path = $this->webroot($path);
00439         } elseif (strpos($path, '://') === false) {
00440             $path = $this->webroot(IMAGES_URL . $path);
00441 
00442             if ((Configure::read('Asset.timestamp') == true && Configure::read() > 0) || Configure::read('Asset.timestamp') === 'force') {
00443                 $path .= '?' . @filemtime(str_replace('/', DS, WWW_ROOT . $path));
00444             }
00445         }
00446 
00447         if (!isset($options['alt'])) {
00448             $options['alt'] = '';
00449         }
00450 
00451         $url = false;
00452         if (!empty($options['url'])) {
00453             $url = $options['url'];
00454             unset($options['url']);
00455         }
00456 
00457         $image = sprintf($this->tags['image'], $path, $this->_parseAttributes($options, null, '', ' '));
00458 
00459         if ($url) {
00460             return $this->output(sprintf($this->tags['link'], $this->url($url), null, $image));
00461         }
00462 
00463         return $this->output($image);
00464     }
00465 /**
00466  * Returns a row of formatted and named TABLE headers.
00467  *
00468  * @param array $names      Array of tablenames.
00469  * @param array $trOptions  HTML options for TR elements.
00470  * @param array $thOptions  HTML options for TH elements.
00471  * @return string
00472  */
00473     function tableHeaders($names, $trOptions = null, $thOptions = null) {
00474         $out = array();
00475         foreach ($names as $arg) {
00476             $out[] = sprintf($this->tags['tableheader'], $this->_parseAttributes($thOptions), $arg);
00477         }
00478         $data = sprintf($this->tags['tablerow'], $this->_parseAttributes($trOptions), join(' ', $out));
00479         return $this->output($data);
00480     }
00481 /**
00482  * Returns a formatted string of table rows (TR's with TD's in them).
00483  *
00484  * @param array $data       Array of table data
00485  * @param array $oddTrOptions HTML options for odd TR elements if true useCount is used
00486  * @param array $evenTrOptions HTML options for even TR elements
00487  * @param bool $useCount adds class "column-$i"
00488  * @param bool $continueOddEven If false, will use a non-static $count variable, so that the odd/even count is reset to zero just for that call
00489  * @return string   Formatted HTML
00490  */
00491     function tableCells($data, $oddTrOptions = null, $evenTrOptions = null, $useCount = false, $continueOddEven = true) {
00492         if (empty($data[0]) || !is_array($data[0])) {
00493             $data = array($data);
00494         }
00495 
00496         if ($oddTrOptions === true) {
00497             $useCount = true;
00498             $oddTrOptions = null;
00499         }
00500 
00501         if ($evenTrOptions === false) {
00502             $continueOddEven = false;
00503             $evenTrOptions = null;
00504         }
00505 
00506         if ($continueOddEven) {
00507             static $count = 0;
00508         } else {
00509             $count = 0;
00510         }
00511 
00512         foreach ($data as $line) {
00513             $count++;
00514             $cellsOut = array();
00515             $i = 0;
00516             foreach ($line as $cell) {
00517                 $cellOptions = array();
00518 
00519                 if (is_array($cell)) {
00520                     $cellOptions = $cell[1];
00521                     $cell = $cell[0];
00522                 } elseif ($useCount) {
00523                     $cellOptions['class'] = 'column-' . ++$i;
00524                 }
00525                 $cellsOut[] = sprintf($this->tags['tablecell'], $this->_parseAttributes($cellOptions), $cell);
00526             }
00527             $options = $this->_parseAttributes($count % 2 ? $oddTrOptions : $evenTrOptions);
00528             $out[] = sprintf($this->tags['tablerow'], $options, join(' ', $cellsOut));
00529         }
00530         return $this->output(join("\n", $out));
00531     }
00532 /**
00533  * Returns a formatted block tag, i.e DIV, SPAN, P.
00534  *
00535  * @param string $name Tag name.
00536  * @param string $text String content that will appear inside the div element.
00537  *   If null, only a start tag will be printed
00538  * @param array $attributes Additional HTML attributes of the DIV tag
00539  * @param boolean $escape If true, $text will be HTML-escaped
00540  * @return string The formatted tag element
00541  */
00542     function tag($name, $text = null, $attributes = array(), $escape = false) {
00543         if ($escape) {
00544             $text = h($text);
00545         }
00546         if (!is_array($attributes)) {
00547             $attributes = array('class' => $attributes);
00548         }
00549         if ($text === null) {
00550             $tag = 'tagstart';
00551         } else {
00552             $tag = 'tag';
00553         }
00554         return $this->output(sprintf($this->tags[$tag], $name, $this->_parseAttributes($attributes, null, ' ', ''), $text, $name));
00555     }
00556 /**
00557  * Returns a formatted DIV tag for HTML FORMs.
00558  *
00559  * @param string $class CSS class name of the div element.
00560  * @param string $text String content that will appear inside the div element.
00561  *   If null, only a start tag will be printed
00562  * @param array $attributes Additional HTML attributes of the DIV tag
00563  * @param boolean $escape If true, $text will be HTML-escaped
00564  * @return string The formatted DIV element
00565  */
00566     function div($class = null, $text = null, $attributes = array(), $escape = false) {
00567         if ($class != null && !empty($class)) {
00568             $attributes['class'] = $class;
00569         }
00570         return $this->tag('div', $text, $attributes, $escape);
00571     }
00572 /**
00573  * Returns a formatted P tag.
00574  *
00575  * @param string $class CSS class name of the p element.
00576  * @param string $text String content that will appear inside the p element.
00577  * @param array $attributes Additional HTML attributes of the P tag
00578  * @param boolean $escape If true, $text will be HTML-escaped
00579  * @return string The formatted P element
00580  */
00581     function para($class, $text, $attributes = array(), $escape = false) {
00582         if ($escape) {
00583             $text = h($text);
00584         }
00585         if ($class != null && !empty($class)) {
00586             $attributes['class'] = $class;
00587         }
00588         if ($text === null) {
00589             $tag = 'parastart';
00590         } else {
00591             $tag = 'para';
00592         }
00593         return $this->output(sprintf($this->tags[$tag], $this->_parseAttributes($attributes, null, ' ', ''), $text));
00594     }
00595 /**
00596  * Build a nested list (UL/OL) out of an associative array.
00597  *
00598  * @param array $list Set of elements to list
00599  * @param array $attributes Additional HTML attributes of the list (ol/ul) tag or if ul/ol use that as tag
00600  * @param array $itemAttributes Additional HTML attributes of the list item (LI) tag
00601  * @param string $tag Type of list tag to use (ol/ul)
00602  * @return string The nested list
00603  * @access public
00604  */
00605     function nestedList($list, $attributes = array(), $itemAttributes = array(), $tag = 'ul') {
00606         if (is_string($attributes)) {
00607             $tag = $attributes;
00608             $attributes = array();
00609         }
00610         $items = $this->__nestedListItem($list, $attributes, $itemAttributes, $tag);
00611         return sprintf($this->tags[$tag], $this->_parseAttributes($attributes, null, ' ', ''), $items);
00612     }
00613 /**
00614  * Internal function to build a nested list (UL/OL) out of an associative array.
00615  *
00616  * @param array $list Set of elements to list
00617  * @param array $attributes Additional HTML attributes of the list (ol/ul) tag
00618  * @param array $itemAttributes Additional HTML attributes of the list item (LI) tag
00619  * @param string $tag Type of list tag to use (ol/ul)
00620  * @return string The nested list element
00621  * @access private
00622  * @see nestedList()
00623  */
00624     function __nestedListItem($items, $attributes, $itemAttributes, $tag) {
00625         $out = '';
00626 
00627         $index = 1;
00628         foreach ($items as $key => $item) {
00629             if (is_array($item)) {
00630                 $item = $key . $this->nestedList($item, $attributes, $itemAttributes, $tag);
00631             }
00632             if (isset($itemAttributes['even']) && $index % 2 == 0) {
00633                 $itemAttributes['class'] = $itemAttributes['even'];
00634             } else if (isset($itemAttributes['odd']) && $index % 2 != 0) {
00635                 $itemAttributes['class'] = $itemAttributes['odd'];
00636             }
00637             $out .= sprintf($this->tags['li'], $this->_parseAttributes(array_diff_key($itemAttributes, array_flip(array('even', 'odd'))), null, ' ', ''), $item);
00638             $index++;
00639         }
00640         return $out;
00641     }
00642 }
00643 ?>

Generated on Sun Nov 22 00:30:54 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7