Habari\Format::term_tree PHP Метод

term_tree() публичный статический Метод

Turns Terms or an array of terms from a hierarchical vocabulary into a ordered HTML list with list items for each term.
public static term_tree ( mixed $terms, string $tree_name, array $config = [] ) : string
$terms mixed An array of Term objects or a Terms object.
$tree_name string The name of the tree, used for unique node id's
$config array an array of values to use to configure the output of this function
Результат string The transformed vocabulary.
    public static function term_tree($terms, $tree_name, $config = array())
    {
        $defaults = array('treestart' => '<ol %s>', 'treeattr' => array('class' => 'tree', 'id' => Utils::slugify('tree_' . $tree_name)), 'treeend' => '</ol>', 'liststart' => '<ol %s>', 'listattr' => array(), 'listend' => '</ol>', 'itemstart' => '<li %s>', 'itemattr' => array('class' => 'treeitem'), 'itemend' => '</li>', 'wrapper' => '<div>%s</div>', 'linkcallback' => null, 'itemcallback' => null, 'listcallback' => null);
        $config = array_merge($defaults, $config);
        $out = sprintf($config['treestart'], Utils::html_attr($config['treeattr']));
        $stack = array();
        $tree_name = Utils::slugify($tree_name);
        if (!$terms instanceof Terms) {
            $terms = new Terms($terms);
        }
        foreach ($terms as $term) {
            if (count($stack)) {
                if ($term->mptt_left - end($stack)->mptt_left == 1) {
                    if (isset($config['listcallback'])) {
                        $config = call_user_func($config['listcallback'], $term, $config);
                    }
                    $out .= sprintf($config['liststart'], Utils::html_attr($config['listattr']));
                }
                while (count($stack) && $term->mptt_left > end($stack)->mptt_right) {
                    $out .= $config['listend'] . $config['itemend'] . "\n";
                    array_pop($stack);
                }
            }
            $config['itemattr']['id'] = $tree_name . '_' . $term->id;
            if (isset($config['itemcallback'])) {
                $config = call_user_func($config['itemcallback'], $term, $config);
            }
            $out .= sprintf($config['itemstart'], Utils::html_attr($config['itemattr']));
            if (isset($config['linkcallback'])) {
                $display = call_user_func($config['linkcallback'], $term, $config);
            } else {
                $display = $term->term_display;
            }
            $out .= sprintf($config['wrapper'], $display);
            if ($term->mptt_right - $term->mptt_left > 1) {
                $stack[] = $term;
            } else {
                $out .= $config['itemend'] . "\n";
            }
        }
        while (count($stack)) {
            $out .= $config['listend'] . $config['itemend'] . "\n";
            array_pop($stack);
        }
        $out .= $config['treeend'];
        return $out;
    }

Usage Example

Пример #1
0
<?php

if (!defined('HABARI_PATH')) {
    die('No direct access');
}
?>
<div <?php 
echo $_attributes;
?>
><input type="hidden" name="<?php 
echo $_name;
?>
" value=""><?php 
echo \Habari\Format::term_tree($terms, $_name, $_settings);
?>
</div>