<?php
/**
* Hierarchy - Output lists hierarchicaly
*
* This class can be used to output lists hierarchicaly, like a
* directory. It uses CSS to create the layout that can be
* configured with other names (see contants section).
* If browser do not suport CSS, a simple HTML list is returned
* hierarchicaly, without styles.
*
* This class do not control which item of list will be shown opened
* or closed, but you can extends this class to provide this.
* An example is made with a simple function to expose a strategy
* of how to do this.
*
* @author Rubens Takiguti Ribeiro
* @date 2008-07-16
* @version 1.0.1 2008-12-19
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3 (LICENSE.TXT)
* @copyright Copyright (C) 2008 Rubens Takiguti Ribeiro
*/
class hierarchy {
/// # CONSTANTS
/**
* CSS class names
*/
const CSS_HIERARCHY = 'hierarchy';
const CSS_LEFT_LINE = 'h_left';
const CSS_LEFT_BOTTOM_LINE = 'h_left_bottom';
const CSS_VALUE = 'h_value';
const CSS_CLEAR = 'h_clear';
const CSS_BUTTON_OPEN = 'bt_open';
/// # METHODS
/**
* This class is not instantiable (use static methods only)
*/
private function __construct() {}
/**
* Prints data hierarchicaly.
* You should pass an array indexed by the content of items and
* points to a boolean or an array value that indicates a sub-list.
*
* @param array[string => (bool || type)] $list List of data
* @return string List disposed hierarchicaly
*/
static public function print_list($list) {
return self::print_list_recursion($list, true);
}
/**
* Recursion of print_list method.
*
* @param array[string => (bool || type)] $list List of data
* @param bool $first_call True if called by print_list, false oderwise
*/
static private function print_list_recursion($list, $first_call = false) {
$return = '';
if (is_array($list)) {
$length = count($list);
$arr_contents = array_keys($list);
$arr_sublist = array_values($list);
unset($list);
$class = $first_call ? ' class="'.self::CSS_HIERARCHY.'"' : '';
$return = "<ul{$class}>\n";
for ($i = 0; $i < $length; $i++) {
$content = &$arr_contents[$i];
$sublist = &$arr_sublist[$i];
$class = ($i < ($length - 1)) ? ' class="'.self::CSS_LEFT_LINE.'"' : '';
$return .= '<li>';
$return .= '<span class="'.self::CSS_LEFT_BOTTOM_LINE.'"> </span>';
$return .= '<span'.$class.'>';
$return .= '<span class="'.self::CSS_VALUE.'">';
$return .= $content;
if (is_array($sublist)) {
$return .= self::print_list_recursion($sublist);
}
$return .= '</span>';
$return .= '</span>';
$return .= "</li>\n";
}
$return .= "</ul>\n";
}
if ($first_call) {
$return .= "<br class=\"".self::CSS_CLEAR."\" />\n";
}
return $return;
}
}//class