Completed
Push — master ( 8e922e...3329c8 )
by Alexey
05:08
created

Tree::showLi()   D

Complexity

Conditions 9
Paths 32

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 34
rs 4.909
cc 9
eloc 25
nc 32
nop 4
1
<?php
2
/**
3
 * Tree
4
 *
5
 * @author Alexey Krupskiy <[email protected]>
6
 * @link http://inji.ru/
7
 * @copyright 2015 Alexey Krupskiy
8
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
9
 */
10
11
namespace Ui;
12
13
class Tree extends \Object
14
{
15
    public static function ul($objectRoot, $maxDeep = 0, $hrefFunc = null)
16
    {
17
        $count = 0;
18
        if (!$hrefFunc) {
19
            $hrefFunc = function($object) {
20
                return "<a href='#'> {$object->name()}</a>";
21
            };
22
        }
23
        ?>
24
        <ul class="treeview" data-col='tree_path'>
25
          <?php
26
          if (is_string($objectRoot)) {
27
              $items = $objectRoot::getList(['where' => ['parent_id', 0]]);
28
          } else {
29
              $class = get_class($objectRoot);
30
              $items = $class::getList(['where' => ['parent_id', $objectRoot->pk()]]);
31
          }
32
          $count += count($items);
33
          foreach ($items as $objectChild) {
34
              $count+=static::showLi($objectChild, 1, $maxDeep, $hrefFunc);
35
          }
36
          ?>
37
        </ul>
38
        <?php
39
        return $count;
40
    }
41
42
    public static function showLi($object, $deep = 1, $maxDeep = 0, $hrefFunc = null)
43
    {
44
        $count = 0;
45
        $isset = false;
46
        $class = get_class($object);
47
        $item = $hrefFunc ? $hrefFunc($object) : "<a href='#'> {$object->name()}</a> ";
48
        $attributes = [];
49
        
50
        if (is_array($item)) {
51
            $attributes = $item['attributes'];
52
            $item = $item['text'];
53
        }
54
        if (!isset($attributes['id'])) {
55
            $attributes['id'] = str_replace('\\', '_', get_class($object)) . "-{$object->pk()}";
56
        }
57
        if (!$maxDeep || $deep < $maxDeep) {
58
            $items = $class::getList(['where' => ['parent_id', $object->pk()]]);
59
            $count += count($items);
60
            foreach ($items as $objectChild) {
61
                if (!$isset) {
62
                    $isset = true;
63
                    echo \Html::el('li', $attributes, $item, true);
64
                    echo '<ul>';
65
                }
66
                $count+=static::showLi($objectChild, $deep + 1, $maxDeep, $hrefFunc);
67
            }
68
        }
69
        if ($isset) {
70
            echo '</ul></li>';
71
        } else {
72
            echo \Html::el('li', $attributes, $item);
73
        }
74
        return $count;
75
    }
76
77
}
78