| | 721 | /** |
| | 722 | * Build a list from an array. |
| | 723 | * |
| | 724 | * Given an array, this method builds a simple unordered or ordered list |
| | 725 | * with an id. |
| | 726 | * Only a (simple) array is required which will generate an |
| | 727 | * unordered list; optionally id, class, type of list and an indent level |
| | 728 | * can be specified. For type 'menu' an unordered list is generated but |
| | 729 | * with an id in group 'menu' instead of 'ul' or 'ol': this enables the |
| | 730 | * list being styled as a menu. |
| | 731 | * |
| | 732 | * @todo _ make maximum indent configurable |
| | 733 | * - support for nested lists (same type) |
| | 734 | * - support for definition lists |
| | 735 | * |
| | 736 | * @author {@link http://wikkawiki.org/JavaWoman JavaWoman} |
| | 737 | * @copyright Copyright © 2005, Marjolein Katsma |
| | 738 | * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License |
| | 739 | * |
| | 740 | * @access public |
| | 741 | * @uses makeId() |
| | 742 | * |
| | 743 | * @param array $array required: flat array with content items |
| | 744 | * @param mixed $id optional: id for the list, will be treated with makeId() |
| | 745 | * or FALSE which suppresses id (for multiple-use elements) |
| | 746 | * @param string $class optional: class for (extra) styling |
| | 747 | * @param string $type optional: type of list to generate; default: ul |
| | 748 | * @param integer $indent optional: indent level |
| | 749 | * @return string generated list |
| | 750 | */ |
| | 751 | function makeList($array,$id='',$class='',$type='ul',$indent=0) |
| | 752 | { |
| | 753 | static $validate = TRUE; # need to validate input |
| | 754 | if (!defined('MAKELIST_MAX_INDENT')) define('MAKELIST_MAX_INDENT',20); # @@@ make max configurable) |
| | 755 | // definition |
| | 756 | $aTypes = array('ul','ol','menu'); # @@@ add dl later (menu generates ul) |
| | 757 | // validate/treat input |
| | 758 | if ($validate) |
| | 759 | { |
| | 760 | if (!is_array($array)) |
| | 761 | { |
| | 762 | return '<p class="error">Could not generate tag: array required</p>'."\n"; # @@@ i18n |
| | 763 | } |
| | 764 | $type = (!in_array($type,$aTypes)) ? 'ul' : $type; |
| | 765 | $class = trim(strip_tags($class)); # minimum protection for user-supplied input |
| | 766 | $indent = min(MAKELIST_MAX_INDENT,abs((int)$indent)); # positive integer no larger than MAX_INDENT |
| | 767 | $validate = FALSE; # validation done: no need to repeat for recursion |
| | 768 | } |
| | 769 | // build tag |
| | 770 | $tag = ('menu' == $type) ? 'ul' : $type; |
| | 771 | $ind = str_repeat("\t",$indent); |
| | 772 | $attrId = (FALSE !== $id) ? ' id="'.$id = $this->makeId($type,$id).'"' : ''; |
| | 773 | $attrClass = ('' != $class) ? ' class="'.$class.'"' : ''; |
| | 774 | $out = $ind.'<'.$tag.$attrClass.$attrId.">\n"; |
| | 775 | foreach ($array as $item) |
| | 776 | { |
| | 777 | $out .= $ind.' <li>'; |
| | 778 | # @@@ add recursion for nested lists |
| | 779 | if (!is_array($item)) |
| | 780 | { |
| | 781 | $out .= $item; |
| | 782 | } |
| | 783 | else |
| | 784 | { |
| | 785 | $out .= 'nested list not yet supported'; # @@@ i18n; |
| | 786 | } |
| | 787 | $out .= "</li>\n"; |
| | 788 | } |
| | 789 | $out .= $ind.'</'.$tag.">\n"; |
| | 790 | // return result |
| | 791 | return $out; |
| | 792 | } |
| | 793 | |
| | 794 | |